From 8b19fd669e4bd1de7fcba350cd673133891cf939 Mon Sep 17 00:00:00 2001 From: iripiri Date: Wed, 15 Jun 2022 15:08:31 +0000 Subject: [PATCH] clean up code --- json_ld/langPack.py | 85 ++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 56 deletions(-) diff --git a/json_ld/langPack.py b/json_ld/langPack.py index d1e36e8..6639b85 100644 --- a/json_ld/langPack.py +++ b/json_ld/langPack.py @@ -1,56 +1,51 @@ import os import chevron import logging -import glob -import sys logger = logging.getLogger(__name__) -# This makes sure we have somewhere to write the classes, and -# creates a couple of files the python implementation needs. + +# This makes sure we have somewhere to write the generated files def setup(version_path): if not os.path.exists(version_path): os.makedirs(version_path) - _create_init(version_path) - _create_base(version_path) + def location(version): - return "cimpy." + version + ".Base"; + return "cimpy." + version + ".Base" + base = { "base_class": "Base", "class_location": location } -template_files=[ { "filename": "json_ld_template.mustache", "ext": ".py" } ] +template_files = [{"filename": "json_ld_template.mustache", "ext": ".json"}] + def get_class_location(class_name, class_map, version): - # Check if the current class has a parent class - if class_map[class_name].superClass(): - if class_map[class_name].superClass() in class_map: - return 'cimpy.' + version + "." + class_map[class_name].superClass() - elif class_map[class_name].superClass() == 'Base' or class_map[class_name].superClass() == None: - return location(version) - else: - return location(version) + pass -partials = {} -# called by chevron, text contains the label {{dataType}}, which is evaluated by the renderer (see class template) +# called by chevron, text contains the label {{dataType}}, which is evaluated +# by the renderer (see class template) def _set_default(text, render): result = render(text) - # the field {{dataType}} either contains the multiplicity of an attribute if it is a reference or otherwise the - # datatype of the attribute. If no datatype is set and there is also no multiplicity entry for an attribute, the - # default value is set to None. The multiplicity is set for all attributes, but the datatype is only set for basic - # data types. If the data type entry for an attribute is missing, the attribute contains a reference and therefore - # the default value is either None or [] depending on the mutliplicity. See also write_python_files + # the field {{dataType}} either contains the multiplicity of an attribute + # if it is a reference or otherwise the datatype of the attribute. If no + # datatype is set and there is also no multiplicity entry for an attribute, + # the default value is set to None. The multiplicity is set for all + # attributes, but the datatype is only set for basic data types. + # If the data type entry for an attribute is missing, the attribute + # contains a reference and therefore the default value is either None or [] + # depending on the mutliplicity. See also write_python_files if result in ['M:1', 'M:0..1', 'M:1..1', '']: return 'None' elif result in ['M:0..n', 'M:1..n'] or 'M:' in result: return '"list"' result = result.split('#')[1] - if result in ['integer', 'Integer', 'Seconds' ]: + if result in ['integer', 'Integer', 'Seconds']: return '0' elif result in ['String', 'DateTime', 'Date']: return "''" @@ -60,54 +55,32 @@ def _set_default(text, render): # everything else should be a float return '0.0' + def set_enum_classes(new_enum_classes): return + def set_float_classes(new_float_classes): return + def run_template(version_path, class_details): for template_info in template_files: - class_file = os.path.join(version_path, class_details['class_name'] + template_info["ext"]) + class_file = os.path.join(version_path, class_details['class_name'] + + template_info["ext"]) if not os.path.exists(class_file): with open(class_file, 'w') as file: - template_path = os.path.join(os.getcwd(), 'json_ld/templates', template_info["filename"]) + template_path = os.path.join(os.getcwd(), 'json_ld/templates', + template_info["filename"]) class_details['setDefault'] = _set_default with open(template_path) as f: args = { 'data': class_details, - 'template': f, - 'partials_dict': partials - } + 'template': f + } output = chevron.render(**args) file.write(output) -def _create_init(path): - init_file = path + "/__init__.py" - with open(init_file, 'w'): - pass - -# creates the Base class file, all classes inherit from this class -def _create_base(path): - base_path = path + "/Base.py" - base = ['from enum import Enum\n\n', '\n', 'class Base():\n', ' """\n', ' Base Class for CIM\n', - ' """\n\n', - ' cgmesProfile = Enum("cgmesProfile", {"EQ": 0, "SSH": 1, "TP": 2, "SV": 3, "DY": 4, "GL": 5, "DL": 6, "TP_BD": 7, "EQ_BD": 8})', - '\n\n', ' def __init__(self, *args, **kw_args):\n', ' pass\n', - '\n', ' def printxml(self, dict={}):\n', ' return dict\n'] - - with open(base_path, 'w') as f: - for line in base: - f.write(line) - - def resolve_headers(path): - filenames = glob.glob(path + "/*.py") - include_names = [] - for filename in filenames: - include_names.append(os.path.splitext(os.path.basename(filename))[0]) - with open(path + "/__init__.py", "w") as header_file: - for include_name in include_names: - header_file.write("from " + "." + include_name + " import " + include_name + " as " + include_name + "\n") - header_file.close() + pass