|
| 1 | +import os |
| 2 | +import re |
| 3 | +import json |
| 4 | +import yaml |
| 5 | +from lightbeam import util |
| 6 | + |
| 7 | +class Creator: |
| 8 | + |
| 9 | + def __init__(self, lightbeam=None): |
| 10 | + self.lightbeam = lightbeam |
| 11 | + self.logger = self.lightbeam.logger |
| 12 | + self.template_folder = "templates/" |
| 13 | + self.earthmover_file = "earthmover.yml" |
| 14 | + |
| 15 | + def create(self): |
| 16 | + self.lightbeam.api.load_swagger_docs() |
| 17 | + os.makedirs(self.template_folder, exist_ok=True) |
| 18 | + earthmover_yaml = {} |
| 19 | + # check if file exists! |
| 20 | + if os.path.isfile(self.earthmover_file): |
| 21 | + with open(self.earthmover_file) as file: |
| 22 | + earthmover_yaml = yaml.safe_load(file) |
| 23 | + for endpoint in self.lightbeam.endpoints: |
| 24 | + if endpoint in (earthmover_yaml.get("destinations", {}) or {}).keys(): |
| 25 | + self.logger.critical(f"The file `{self.earthmover_file}` already exists in the current directory and contains `$destinations.{endpoint}`; to re-create it, please first manually remove it (and `{self.template_folder}{endpoint}.jsont`).") |
| 26 | + self.create_jsont(endpoint) |
| 27 | + # write out earthmover_yaml |
| 28 | + if not os.path.isfile(self.earthmover_file): |
| 29 | + self.logger.info(f"creating file `{self.earthmover_file}`...") |
| 30 | + with open(self.earthmover_file, 'w+') as file: |
| 31 | + file.write("""version: 2.0 |
| 32 | +
|
| 33 | +# This is an earthmover.yml file, generated with `lightbeam create`, for creating Ed-Fi JSON payloads |
| 34 | +# using earthmover. See https://github.com/edanalytics/earthmover for documentation. |
| 35 | +
|
| 36 | +config: |
| 37 | + macros: > |
| 38 | + {% macro descriptor_namespace() -%} |
| 39 | + uri://ed-fi.org |
| 40 | + {%- endmacro %} |
| 41 | +
|
| 42 | +# Define your source data here: |
| 43 | +sources: |
| 44 | + # Example: |
| 45 | + # mysource: |
| 46 | + # file: path/to/myfile.csv |
| 47 | + # ... |
| 48 | +
|
| 49 | +# (If needed, define your data transformations here:) |
| 50 | +# transformations: |
| 51 | +
|
| 52 | +destinations:""") |
| 53 | + for endpoint in self.lightbeam.endpoints: |
| 54 | + file.write(self.create_em_destination_node(endpoint)) |
| 55 | + else: |
| 56 | + self.logger.info(f"appending to file `{self.earthmover_file}`...") |
| 57 | + with open(self.earthmover_file, 'a+') as file: |
| 58 | + for endpoint in self.lightbeam.endpoints: |
| 59 | + file.write(self.create_em_destination_node(endpoint)) |
| 60 | + |
| 61 | + def create_em_destination_node(self, endpoint): |
| 62 | + return f""" |
| 63 | + {endpoint}: |
| 64 | + source: $transformations.{endpoint} |
| 65 | + template: {self.template_folder}{endpoint}.jsont |
| 66 | + extension: jsonl |
| 67 | + linearize: True""" |
| 68 | + |
| 69 | + def upper_repl(self, match): |
| 70 | + value = match.group(1) |
| 71 | + return "/" + value[0].upper() + value[1:] + "Descriptor#" |
| 72 | + |
| 73 | + |
| 74 | + def create_jsont(self, endpoint): |
| 75 | + template_file = f"{self.template_folder}{endpoint}.jsont" |
| 76 | + # check if file exists! |
| 77 | + if os.path.isfile(template_file): |
| 78 | + self.logger.critical(f"The file `{template_file}` already exists in the current directory; to re-create it, please first manually delete it.") |
| 79 | + # generate base JSON structure: |
| 80 | + content = self.lightbeam.api.get_params_for_endpoint(endpoint, type='all') |
| 81 | + # pretty-print it: |
| 82 | + content = json.dumps(content, indent=2) |
| 83 | + # annotate required/optional properties: |
| 84 | + content = content.replace('"[required]', '{# (required) #} "') |
| 85 | + content = content.replace('"[optional]', '{# (optional) #} "') |
| 86 | + # appropriate quoting based on property data type: |
| 87 | + content = re.sub('"\[string\](.*)Descriptor"', r'"{{descriptor_namespace()}}/\1Descriptor#{{\1Descriptor}}"', content) |
| 88 | + content = re.sub('/(.*)_(.*)Descriptor#', r'/\2Descriptor#', content) |
| 89 | + content = re.sub(r'/(.*)Descriptor#', self.upper_repl, content) |
| 90 | + content = re.sub('"\[string\](.*)"', r'"{{\1}}"', content) |
| 91 | + content = re.sub('"\[(integer|boolean)\](.*)"', r'{{\2}}', content) |
| 92 | + # for loops over arrays: |
| 93 | + content = re.sub('"(.*)": \[', r'"\1": [ {% for item in \1 %}', content) |
| 94 | + content = re.sub('\]', r'{% endfor %} ]', content) |
| 95 | + content = re.sub('{{(.*)-(.*)}}', r'{{item.\2}}', content) |
| 96 | + # add info header message: |
| 97 | + content = """{# |
| 98 | + This is an earthmover JSON template file, generated with `lightbeam create`, for creating Ed-Fi JSON `"""+endpoint+"""` |
| 99 | + payloads using earthmover. See https://github.com/edanalytics/earthmover for documentation. |
| 100 | +#} |
| 101 | +""" + content |
| 102 | + # write out json template |
| 103 | + self.logger.info(f"creating file `{template_file}`...") |
| 104 | + with open(template_file, 'w+') as file: |
| 105 | + file.write(content) |
0 commit comments