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 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