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 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_decoding(self, prp, decoding, type): """Emits code corresponding to a class property decoding. :param prp: Ontology class property definition. :param decoding: Ontology class property decoding definition. :param type: Ontology class property type definition. :type prp: pyesdoc_mp.ontology.property.Property :type decoding: pyesdoc_mp.ontology.decoding.Decoding :type type: pyesdoc_mp.ontology.type.Type """ def get_decoding_function(): # ... simple/enum types - return type functional name # (is directly mapped to a convertor function). if prp.type.is_simple or prp.type.is_enum: return '\'{0}\''.format(get_type_functional_name(prp.type)) # ... complex classes - return class functional name. elif prp.type.is_class: type_name = prp.type.name if type is None else type return get_class_decoder_function_name(type_name) tmpl = '{0}(\'{1}\', {2}, {3}, \'{4}\'),' return tmpl.format( emit_line_return() + emit_indent(2), prp.name, prp.is_iterative, get_decoding_function(), '' if decoding is None else decoding)
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 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_class_property_constants(self, ctx): """Emits set of class property constants. :param ctx: Generation context information. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext """ code = '' tmpl = '{0}self.{1} = {2}("{3}"){4}' # Assign constants. for cnt in ctx.cls.constants: prp = ctx.cls.get_property(cnt[0]) if prp is not None: if code == '': code += emit_line_return(1) code += tmpl.format(emit_indent(2), cnt[0], get_type_functional_name(prp.type), cnt[1], emit_line_return()) return code
def emit_class_properties(self, ctx): """Emits set of class properties. :param ctx: Generation context information. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext """ code = '' prp_ctor = '' tmpl = '{0}{1}{2}# type = {3}{4}' # Initialise property fields. for prp in ctx.cls.properties: prp_ctor = get_property_ctor(prp) if code == '': code += emit_line_return(1) code += tmpl.format(emit_indent(2), prp_ctor, ''.ljust(45 - len(prp_ctor)), get_type_doc_name(prp.type), emit_line_return()) return code
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
def get_imports(): imports = '' is_first = True tmpl = "from {0}.v{1}.serialization.{2} import {3}" for e in ctx.ontology.entities: 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(e.package, 'decoder'), get_class_decoder_function_name(e)) is_first = False return imports
def emit_class_imports(self, ctx, imports): """Emits code corresponding to a set of python class imports. :param ctx: Generation context information. :param imports: Imports being parsed. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext :type imports: list """ code = '' tmpl = 'from py{0}.{1} import {2}' for package, type in imports: pkg_path = get_package_path(ctx.ontology, 'types', package) type_name = get_class_name(type) type_import_name = get_class_import_name(type) code += tmpl.format(pkg_path, type_import_name, type_name) code += emit_line_return() return code
def emit_imports_for_sub_package(self, ctx, pkg=None): """Emits code corresponding to a set of sub package imports. :param ctx: Generation context information. :type ctx: pyesdoc_mp.generators.generator.GeneratorContext """ code = '' pkg = pkg if pkg is not None else ctx.pkg for cls in pkg.classes: code += 'from py{0}.v{1}.types.{2}.{3} import {4}{5}'.format( get_ontology_name(ctx.ontology), get_ontology_version(ctx.ontology), get_package_name(pkg), get_class_import_name(cls), get_class_name(cls), emit_line_return()) return code