Last active
January 28, 2021 12:45
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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