def emit_class(self, ctx): """Emits code corresponding to a python class. :param ctx: Generation context information. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext """ # Set helper vars. class_imports = self.emit_class_imports(ctx, ctx.cls.imports) class_circular_imports = self.emit_class_imports(ctx, ctx.cls.circular_imports) class_properties = self.emit_class_properties(ctx) class_property_constants = self.emit_class_property_constants(ctx) class_representations = self.emit_class_representations(ctx) # Open template. if ctx.cls.is_abstract: code = get_template(ctx, _TEMPLATE_CLASS_ABSTRACT) else: code = get_template(ctx, _TEMPLATE_CLASS_CONCRETE) # Generate code. code = code.replace('{package-name}', get_package_name(ctx.pkg)) code = code.replace('{class-name}', get_class_name(ctx.cls)) code = code.replace('{base-class-name}', get_class_base_name(ctx.cls.base)) code = code.replace('{class-doc-string}', ctx.cls.doc_string) code = code.replace('{class_constants}', class_property_constants) code = code.replace('{class-imports}', class_imports) code = code.replace('{class-circular-imports}', class_circular_imports) code = code.replace('{class-properties}', class_properties) code = code.replace('{class-representations}', class_representations) return code
def emit_class_representation_as_dict(self, ctx): """Emits code corresponding to a dictionary representation of the class. :param ctx: Generation context information. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext """ # Set dict ctor. dict_ctor = '' if ctx.cls.base is not None: dict_ctor = 'super({class-name}, self).as_dict()' # Set dict items. dict_items = '' for prp in ctx.cls.properties: dict_items += "{0}append(d, '{1}', {2}, {3}, {4}, {5})".format( emit_line_return() + emit_indent(2), get_property_name(prp), get_property_field_name(prp), prp.is_iterative, prp.type.is_simple, prp.type.is_enum) # Open template. code = get_template(ctx, _TEMPLATE_CLASS_REPRESENTATIONS) # Generate code. code = code.replace('{dict-ctor}', dict_ctor) code = code.replace('{dict-items}', dict_items) code = code.replace('{class-name}', get_class_name(ctx.cls)) return code
def emit_root_init_file(self, ctx): """Emits package initialisation file. :param ctx: Generation context information. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext """ def get_imports(): imports = '' is_first = True tmpl = "from py{0}.v{1}.validation.{2} import {3}" for cls in ctx.ontology.classes: if not is_first: imports += emit_line_return() imports += tmpl.format( get_ontology_name(ctx.ontology), get_ontology_version(ctx.ontology), get_package_module_name(cls.package, 'validator'), get_class_validator_function_name(cls)) is_first = False return imports code = get_template(ctx, _TEMPLATE_PACKAGE) code = code.replace('{module-imports}', get_imports()) return code
def get_functions(): fns = '' for cls in ctx.pkg.classes: fn = get_template(ctx, _TEMPLATE_VALIDATOR_FUNCTION) fn = fn.replace('{class-name}', get_class_name(cls)) fn = fn.replace('{class-function-name}', get_class_functional_name(cls)) fn = fn.replace('{class-doc-name}', get_class_doc_string_name(cls)) fn += emit_line_return(3) fns += fn return fns
def on_package_parse(self, ctx): """Event handler for the package parse event. :param ctx: Generation context information. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext """ def get_imports(): imports = [] def append_import(imp): if imp not in imports: imports.append(imp) # Set package class type imports. imp = 'from py{0}.v{1}.types.{2} import *'.format( get_ontology_name(ctx.ontology), get_ontology_version(ctx.ontology), get_package_name(ctx.pkg)) append_import(imp) # Set type decoding imports. for type in [t for t in ctx.pkg.external_types if t.is_class]: imp = 'from py{0}.v{1}.validation.{2} import *'.format( get_ontology_name(ctx.ontology), get_ontology_version(ctx.ontology), get_package_module_name(type.name_of_package, 'validator')) append_import(imp) if len(imports) > 0: return reduce(add, map(lambda i : i + emit_line_return(), sorted(imports))) else: return '' def get_functions(): fns = '' for cls in ctx.pkg.classes: fn = get_template(ctx, _TEMPLATE_VALIDATOR_FUNCTION) fn = fn.replace('{class-name}', get_class_name(cls)) fn = fn.replace('{class-function-name}', get_class_functional_name(cls)) fn = fn.replace('{class-doc-name}', get_class_doc_string_name(cls)) fn += emit_line_return(3) fns += fn return fns code = get_template(ctx, _TEMPLATE_VALIDATOR_MODULE) code = code.replace('{module-imports}', get_imports()) code = code.replace('{validation-functions}', get_functions()) code = code.replace('{package-name}', get_package_name(ctx.pkg)) dir = self.output_dir file = get_package_module_file_name(ctx.pkg, 'validator') return (code, dir, file)
def get_functions(): fns = '' for cls in ctx.pkg.classes: dcs = self.get_decodings(cls) fn = get_template(ctx, _TEMPLATE_DECODER_FUNCTION) fn = fn.replace('{class-name}', get_class_name(cls)) fn = fn.replace('{class-function-name}', get_class_functional_name(cls)) fn = fn.replace('{class-doc-name}', get_class_doc_string_name(cls)) fn = fn.replace('{class-decodings}', dcs) fn += emit_line_return(3) fns += fn return fns
def emit_enum(self, ctx): """Emits code corresponding to a python class. :param ctx: Generation context information. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext """ code = get_template(ctx, _TEMPLATE_ENUM) code = code.replace('{enum-name}', get_enum_name(ctx.enum)) code = code.replace('{enum-doc-string}', ctx.enum.doc_string) return code
def emit_package_init_file(self, ctx, template, package_imports_fn): """Emits a package initialisation file. :param ctx: Generation context information. :param template: Code template. :param package_imports_fn: Package imports generator callback. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext :type template: str :type package_imports_fn: function """ code = get_template(ctx, template) if ctx.pkg is not None: code = code.replace('{package-name}', get_package_name(ctx.pkg)) code = code.replace('{module-imports}', package_imports_fn()) return code
def get_code(template, include_version): return (get_template(ctx, template), \ get_ontology_directory(ctx, include_version=include_version), \ get_package_init_file_name())