def _load_template(j2_env, template_name): assert isinstance(j2_env, jinja2.Environment) _LOGGER.debug("Begin loading template %s", template_name) try: j2_template = j2_env.get_template(template_name) except Exception as error: raise my_exceptions.CodeGenError( "Couldn't load jinja2 template {} from env path {}".format( template_name, PySrcRenderer.TEMPLATES_PATH), error) else: _LOGGER.debug("Finished loading template %s", template_name) return j2_template
def __init__(self): _LOGGER.debug("Load textx metamodel for MOF parsing") try: metamodel = textx.metamodel.metamodel_from_file( MOFParser.META_MODEL_PATH, classes=MOFParser.META_MODEL_CLASSES) except Exception as error: raise my_exceptions.CodeGenError( "Fatal error loading textx metamodel for MOF parsing", error) else: _LOGGER.debug("Successfully loaded text metamodel from %s : %s", MOFParser.META_MODEL_PATH, metamodel) self._meta_model = metamodel
def _write_python_file(py_src_text, module_name, parent_path): """Write a Python source file based on the text, the module name of the file and the path""" assert py_src_text is not None assert module_name assert os.path.exists(parent_path) output_path = os.path.join(parent_path, "{base_name}.py".format(base_name=module_name)) _LOGGER.info("Begin writing Python source file %s", output_path) try: with open(output_path, 'w') as output_file: output_file.write(py_src_text) except Exception as error: raise my_exceptions.CodeGenError( "Fatal error writing Python source file {}".format(output_path), error) else: _LOGGER.info("Finished writing Python source file %s", output_path)
def render_sub_module(self, dell_version, mof_class): """Render a Python sub-module based on the mof class object""" _LOGGER.info("Begin rendering sub-module based on mof class %s", mof_class.name) try: # pylint: disable=no-member rendered_mof = self._sub_module_template.render(dell_version=dell_version, mof_class=mof_class) _LOGGER.debug("Successfully rendered with jinja template") except Exception as error: raise my_exceptions.CodeGenError( "Fatal error rendering mof class {} to python sub-module".format( mof_class.name), error) else: _LOGGER.info("Finished rendering sub-module based on mof class %s", mof_class.name) return rendered_mof
def render_parent_module(self, dell_version, module_contents): """Render the Python parent-module based on dell version and module contents""" _LOGGER.info("Begin rendering parent module for dell version %s", dell_version) try: module_all = sorted(module_contents.keys()) # pylint: disable=no-member rendered_text = self._parent_module_template.render( dell_version=dell_version, module_all=module_all, module_contents=module_contents) _LOGGER.debug("Successfully rendered with jinja template") except Exception as error: raise my_exceptions.CodeGenError( "Fatal error rendering parent module for dell version {}".format(dell_version), error) else: _LOGGER.info("Finished rendering parent module for dell version %s", dell_version) return rendered_text
def __init__(self): _LOGGER.debug("Begin setting up jinja2 environment") try: j2_loader = jinja2.FileSystemLoader(PySrcRenderer.TEMPLATES_PATH) j2_env = jinja2.Environment(loader=j2_loader, trim_blocks=True) except Exception as error: raise my_exceptions.CodeGenError( "Couldn't setup jinja environment using path {}".format( PySrcRenderer.TEMPLATES_PATH), error) else: _LOGGER.debug("Finished setting up jinja2 environment") _LOGGER.debug("Begin loading templates") self._parent_module_template = PySrcRenderer._load_template( j2_env, PySrcRenderer.PARENT_MODULE_TEMPLATE_NAME) self._sub_module_template = PySrcRenderer._load_template( j2_env, PySrcRenderer.SUB_MODULE_TEMPLATE_NAME) _LOGGER.debug("Finished loading templates")
def translate(self, dell_version): """Translates MOF to Python""" assert dell_version _LOGGER.info("Begin translating mof to python code for dell version %s", dell_version) try: parent_module_path = self._make_parent_module_path(dell_version) parent_module_contents = {} # iterate across the available mof files and produce python sub-modules for mof_file_entry in my_mof.MOFParser.find_avail_mof_files(dell_version): # first parse the mof file into a mof class object try: mof_class = self._parser.parser_mof_file(mof_file_entry) except my_exceptions.SkipMOFFile as e: _LOGGER.warning(str(e)) continue # now render it into python source code text and write it out py_sub_module_src_text = self._renderer.render_sub_module(dell_version, mof_class) py_sub_module_name = mof_file_entry.base_name.replace(".mof", "") MOFTranslator._write_python_file( py_sub_module_src_text, py_sub_module_name, parent_module_path) # update the manifest of the module contents if mof_class.attributes: py_class_name = "{class_name}Factory".format(class_name=mof_class.name) else: py_class_name = mof_class.name parent_module_contents[mof_class.name] = [py_class_name] # after writing out all the sub-modules, write out the parent's __init__.py py_parent_module_src_text = self._renderer.render_parent_module( dell_version, parent_module_contents) MOFTranslator._write_python_file( py_parent_module_src_text, '__init__', parent_module_path) except my_exceptions.CodeGenException as e: raise e except Exception as error: # pylint: disable=broad-except raise my_exceptions.CodeGenError( "Fatal error while trying to translate mof to python for dell " "version: {}".format(dell_version), error) else: _LOGGER.info("Finished translating mof to python for dell version %s", dell_version)
def parser_mof_file(self, mof_file_entry): assert isinstance(mof_file_entry, MOFParser.MOFFileEntry) _LOGGER.info("Begin parsing MOF file: %s", mof_file_entry) try: mof_class = self._meta_model.model_from_file(mof_file_entry.path) _LOGGER.debug("successfully parsed mof file") except Exception as error: raise my_exceptions.CodeGenError( "Fatal error while parsing MOF file {}".format( mof_file_entry.path), error) # now check if it has members if not mof_class.members: raise my_exceptions.SkipMOFFile( "MOF class in MOF file {} has no members, so skipping it". format(mof_file_entry.path)) _LOGGER.info("Finished parsing MOF file: %s", mof_file_entry) return mof_class