Skip to content

Instantly share code, notes, and snippets.

@AlexMorgan3817
Last active January 28, 2021 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexMorgan3817/003c3b51131c2f5899a573af9ed978da to your computer and use it in GitHub Desktop.
Save AlexMorgan3817/003c3b51131c2f5899a573af9ed978da to your computer and use it in GitHub Desktop.
This Script use .yml to create multiply samples of same file, but with changed parts
# This Script use .yml to create multiply samples of same file, but with changed parts
# Example:
#
# Hello.txt
# Hello, {{:helloString}}!
# .yml
# Hello.txt:
# Hello2.txt:
# helloString: "World"
# Hello3.txt:
# helloString: "Bro"
# Hello2.txt
# Hello, World!
# Hello3.txt
# Hello, Bro!
import sys
from os import system as sh
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
class ErrorCode:
Code = 0
Name = "None"
def __init__(self, code, name):
self.Code = code
self.Name = name
def __str__(self):
return f"ERROR {self.Code}: {self.Name}."
ErrorStack = [
ErrorCode(-1, "Arguments not found"),
ErrorCode(-2, "File not found")
]
def RaiseError(code):
for i in ErrorStack:
if(isinstance(i, ErrorCode) and i.Code == code):
print(i)
return exit(i.Code)
class Helpers:
def ReadFile(fName):
try:
file = open(fName, 'r')
Data = file.read()
file.close()
return Data
except IOError:
RaiseError(-2)
def CreateFile(fName, content):
file = open(fName, 'w')
Data = file.write(content)
file.close()
return Data
def ParseOptions(options):
pass
def EditFilesBy(yml):
for fileName in yml:
template = Helpers.ReadFile(fileName)
for sample in yml[fileName]:
sample_content = template
for variable in yml[fileName][sample]:
sample_content = sample_content.replace("{{:" + variable + "}}", yml[fileName][sample][variable])
Helpers.CreateFile(sample, sample_content)
if(__name__ == '__main__'):
arglist = sys.argv
if(len(arglist) < 2):
RaiseError(-1)
yml_file = arglist[1]
options = ParseOptions(arglist[2:])
yml = load(Helpers.ReadFile(yml_file), Loader=Loader)
EditFilesBy(yml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment