11import os
2+ import re
23import json
34import yaml
45from lightbeam import util
@@ -12,6 +13,7 @@ def __init__(self, lightbeam=None):
1213 self .earthmover_file = "earthmover.yml"
1314
1415 def create (self ):
16+ self .lightbeam .api .load_swagger_docs ()
1517 os .makedirs (self .template_folder , exist_ok = True )
1618 earthmover_yaml = {}
1719 # check if file exists!
@@ -31,6 +33,12 @@ def create(self):
3133# This is an earthmover.yml file, generated with `lightbeam create`, for creating Ed-Fi JSON payloads
3234# using earthmover. See https://github.com/edanalytics/earthmover for documentation.
3335
36+ config:
37+ macros: >
38+ {% macro descriptor_namespace() -%}
39+ uri://ed-fi.org
40+ {%- endmacro %}
41+
3442# Define your source data here:
3543sources:
3644 # Example:
@@ -57,59 +65,41 @@ def create_em_destination_node(self, endpoint):
5765 template: { self .template_folder } { endpoint } .jsont
5866 extension: jsonl
5967 linearize: True"""
68+
69+ def upper_repl (self , match ):
70+ value = match .group (1 )
71+ return "/" + value [0 ].upper () + value [1 :] + "Descriptor#"
72+
6073
6174 def create_jsont (self , endpoint ):
6275 template_file = f"{ self .template_folder } { endpoint } .jsont"
6376 # check if file exists!
6477 if os .path .isfile (template_file ):
6578 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
66102 # write out json template
67103 self .logger .info (f"creating file `{ template_file } `..." )
68104 with open (template_file , 'w+' ) as file :
69- # TODO: implement a function in `lightbeam/api.py` that constructs a "sample" payload for the endpoint
70- # Example:
71- # {
72- # "property_bool": true,
73- # "property_int": 1,
74- # "property_float": 1.0,
75- # "property_string": "string",
76- # "property_date": "date",
77- # "property_string_optional": "string",
78- # "property_descriptor": "uri://ed-fi.org/SomeDescriptor#SomeValue",
79- # "property_object": {
80- # "property_object_1": "string",
81- # "property_object_2": "string"
82- # },
83- # "property_array": [
84- # {
85- # "property_array_1": "string",
86- # "property_array_2": "string"
87- # }
88- # ]
89- # }
90- # TODO: turn the "sample" payload into a Jinja template
91- # Example:
92- # {
93- # "property_bool": {{property_bool}},
94- # "property_int": {{property_int}},
95- # "property_float": {{property_float}},
96- # "property_string": "{{property_string}}",
97- # "property_date": "{{property_date}}",
98- # {% if property_string_optional %}
99- # "property_string_optional": "{{property_string_optional}}",
100- # {% endif %}
101- # "property_descriptor": "uri://ed-fi.org/SomeDescriptor#{{property_descriptor}}",
102- # "property_object": {
103- # "property_object_1": "{{property_object_1}}",
104- # "property_object_2": "{{property_object_2}}"
105- # },
106- # "property_array": [
107- # {% for item in property_array %}
108- # {
109- # "property_array_1": "{{item.property_array_1}}",
110- # "property_array_2": "{{item.property_array_2}}"
111- # } {% if not loop.last %},{% endif %}
112- # {% endfor %}
113- # ]
114- # }
115- file .write ("coming soon..." ) # (for now)
105+ file .write (content )
0 commit comments