Skip to content

Instantly share code, notes, and snippets.

@Anthonyhawkins
Last active May 19, 2022 07:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anthonyhawkins/be687d8d1c0bc0da45a10d066ec1358b to your computer and use it in GitHub Desktop.
Save Anthonyhawkins/be687d8d1c0bc0da45a10d066ec1358b to your computer and use it in GitHub Desktop.
import inspect
import os
import sys
import yaml
from yaml.parser import ParserError
def env(env_variable):
var = os.getenv(env_variable)
return var if var else ""
def prune_empty(items):
return list(filter(None, items))
class Sources(object):
def __init__(self, sources):
self.source_map = {}
for source_label, file_path in sources.items():
self.source_map[source_label] = {
"data": None,
"file_path": file_path
}
try:
with open(r'{f}'.format(f=file_path)) as file:
try:
self.source_map[source_label]["data"] = yaml.load(file, Loader=yaml.FullLoader)
except ParserError:
print("Error - {f} is Not valid YAML.".format(f=file_path))
except FileNotFoundError:
print("Error - {f} No Such File.".format(f=file_path))
def grab(self, source_label, path):
if source_label not in self.source_map:
print("Error - No Source with label {l} exists".format(l=source_label))
return None
data = self.source_map[source_label]["data"]
file_path = self.source_map[source_label]["file_path"]
dict_path = ''
for part in path.split('.'):
dict_path += "[" + part + "]" if part.isdigit() else "['" + part + "']"
try:
return eval("data" + dict_path)
except SyntaxError:
print("Error - {p} is invalid syntax. Evaluated to {d}".format(p=path, d=dict_path))
sys.exit(1)
except KeyError:
print("Error - {p} does not exist in file: {f}".format(p=path, f=file_path))
sys.exit(1)
except IndexError:
print("Error - {p} index our of range in file: {f}".format(p=path, f=file_path))
sys.exit(1)
class Files(object):
def __init__(self, files):
self.file_map = {}
for file_label, file_path in files.items():
self.file_map[file_label] = {
"text": None,
"file_path": file_path
}
try:
with open(r'{f}'.format(f=file_path)) as file:
self.file_map[file_label]["text"] = file.read()
except FileNotFoundError:
print("Error - {f} No Such File.".format(f=file_path))
def grab(self, file_label):
if file_label not in self.file_map:
print("Error - No File with label {l} exists".format(l=file_label))
return None
return self.file_map[file_label]["text"]
class Include(object):
@staticmethod
def when(expression, if_block, else_block={}):
return if_block if expression else else_block
def generate(config):
frame = inspect.stack()[1]
module = inspect.getmodule(frame[0])
slash = '\\' if os.name == "nt" else "/"
filename = module.__file__.split(slash)[-1].replace(".py", "")
yaml.SafeDumper.org_represent_str = yaml.SafeDumper.represent_str
def multi_str(dumper, data):
if '\n' in data:
return dumper.represent_scalar(
'tag:yaml.org,2002:str', data, style='|')
return dumper.org_represent_str(data)
yaml.add_representer(str, multi_str, Dumper=yaml.SafeDumper)
with open(filename + ".yml", 'w') as file:
yaml.SafeDumper.ignore_aliases = lambda *args: True
yaml.safe_dump(config, file, sort_keys=False, default_flow_style=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment