Esempio n. 1
0
File: enum.py Progetto: susamn/lance
    def __init__(self, document, folder=None):
        self.fqcn = document.get("fqcn")
        if self.fqcn:
            self.class_name = class_name_from_package(self.fqcn)
            self.package = package_name_from_package(self.fqcn)
        else:
            raise ValueError("A class must have a fqcn")
        self.imports = set()
        self.attributes = []
        self.methods = []
        self.generate_folder = folder
        self.enums = []

        enums = document.get("values")
        if not enums or not type(enums) == list or len(enums) == 0:
            raise ValueError(
                f"Please provide enum values to the Enum {self.class_name}")
        self.enums.extend(enums)

        # Process class level annotations
        annotations = document.get("annotations")
        self.annotations = parse_annotations(annotations)
        if self.annotations and len(self.annotations) > 0:
            for a in self.annotations:
                if a and a.get_imports() and len(a.get_imports()) > 0:
                    self.imports.update(a.get_imports())
Esempio n. 2
0
    def __init__(self, document, folder=None):
        self.fqcn = document.get("fqcn")
        if self.fqcn:
            self.class_name = class_name_from_package(self.fqcn)
            self.package = package_name_from_package(self.fqcn)
        else:
            raise ValueError("A class must have a fqcn")
        self.imports = set()
        self.attributes = []
        self.methods = []
        self.generate_folder = folder

        # Process class level implementations
        extends = document.get("extends")
        self.extends = None
        if extends and not type(extends) == list:
            raise ValueError(
                f"Please provide implementation data properly for interface {self.fqcn}"
            )
        if extends:
            if len(extends) > 0:
                all_extended_interfaces = []
                for e in extends:
                    extends_fqcn_class_name = e.get("fqcn")
                    extends_generic_types = e.get("generic_types")
                    extends_fqcn, imports = handle_generic_types(
                        extends_fqcn_class_name, extends_generic_types)

                    all_extended_interfaces.append(extends_fqcn)
                    self.imports.update(imports)
                self.extends = ", ".join(all_extended_interfaces)

        # Process class level annotations
        annotations = document.get("annotations")
        self.annotations = parse_annotations(annotations)
        if self.annotations and len(self.annotations) > 0:
            for a in self.annotations:
                if a and a.get_imports() and len(a.get_imports()) > 0:
                    self.imports.update(a.get_imports())

        # Attributes
        attributes = document.get("attributes")
        if attributes:
            for a_doc in attributes:
                attribute = Attribute(a_doc)
                attribute.make_final()
                attribute.make_static()
                attribute.make_public()
                self.add_attribute(attribute)

        # Methods
        methods = document.get("methods")
        if methods:
            for m_doc in methods:
                method = Method(m_doc)
                method.make_abstract()
                self.add_method(method)
Esempio n. 3
0
    def __init__(self, document):

        self.annotations = []
        self.imports = []
        self.methods = []
        self.initialized_form = None

        self.name = document.get("name")
        self.mode = document.get("mode") or MODE_PRIVATE
        attrib_type = document.get("type")
        self.attribute_accessors_generate = document.get("accessors")
        self.annotation_level = document.get("annotation_level") or ANNOTATION_LEVEL_ATTRIBUTE
        initialized_form = document.get("initialized_form")
        self.is_final = document.get("is_final") or False
        self.is_static = document.get("is_static") or False

        if not self.name or not attrib_type:
            raise ValueError("The attribute name and type must be provided")

        # Type information fetching
        self.typ, imports = obtain_type(self.name, attrib_type)

        if not self.typ:
            raise ValueError(f"The return type of method {self.name} could not be obtained")

        if imports and len(imports) > 0:
            self.imports.extend(imports)

        method_annotations_info = document.get("annotations")
        self.annotations = parse_annotations(method_annotations_info)

        if self.annotations and len(self.annotations) > 0:
            for a in self.annotations:
                if a and a.get_imports() and len(a.get_imports()) > 0:
                    self.imports.extend(a.get_imports())

        if self.attribute_accessors_generate:
            attribute_getter_method = getter(self.name, attrib_type)
            attribute_setter_method = setter(self.name, attrib_type)
            self.methods.append(attribute_getter_method)
            self.methods.append(attribute_setter_method)
            self.imports.extend(attribute_getter_method.get_imports())
            self.imports.extend(attribute_setter_method.get_imports())

        # Add initialization data
        if initialized_form:
            i_form = initialized_form.get("form")
            i_imports = initialized_form.get("imports")
            if i_form:
                self.initialized_form = i_form
            if i_imports and len(i_imports) > 0:
                self.imports.extend(i_imports)
Esempio n. 4
0
def obtain_type(name, doc):
    __get_type_checked(name, doc)

    # Type information
    type_of = doc.get("of")
    object_fqcn = doc.get("fqcn")
    annotations_info = doc.get("annotations")
    generic_types = doc.get("generic_types")

    annotations = None
    if annotations_info:
        annotations = parse_annotations(annotations_info)

    if not type_of:
        raise ValueError(
            "Check attribute section, it is not properly structured")

    typ, imports = process_type(type_of,
                                fqcn=object_fqcn,
                                annotations=annotations,
                                generic_types=generic_types)
    if not typ:
        raise ValueError("The attribute type could not be parsed")
    return typ, imports
Esempio n. 5
0
    def __init__(self, document, folder=None):
        self.fqcn = document.get("fqcn")
        if self.fqcn:
            self.class_name = class_name_from_package(self.fqcn)
            self.package = package_name_from_package(self.fqcn)
        else:
            raise ValueError("A class must have a fqcn")
        self.imports = set()
        self.attributes = []
        self.methods = []
        self.constructors = []
        self.generate_folder = folder

        # Process inheritance
        extends = document.get("extends")
        self.extends = None
        if extends:
            try:
                extends_class_name = extends.get("fqcn")
                extends_generic_types = extends.get("generic_types")
                if not extends_class_name:
                    raise ValueError("The extends classname is not provided")
                extends_fqcn, imports = handle_generic_types(extends_class_name, extends_generic_types)
                self.extends = extends_fqcn
                self.imports.update(imports)
            except ValueError:
                raise ValueError("Please provide inheritance class and its fqcn")

        # Process class level implementations
        implements = document.get("implements")
        self.implements = None
        if implements and not type(implements) == list:
            raise ValueError("Please provide implementation data properly")
        if implements:
            if len(implements) > 0:
                all_implementation_classes = []
                for i in implements:
                    implements_fqcn_class_name = i.get("fqcn")
                    implements_generic_types = i.get("generic_types")
                    implements_fqcn, imports = handle_generic_types(implements_fqcn_class_name,
                                                                    implements_generic_types)

                    all_implementation_classes.append(implements_fqcn)
                    self.imports.update(imports)
                self.implements = ", ".join(all_implementation_classes)

        # Process class level annotations
        annotations = document.get("annotations")
        self.annotations = parse_annotations(annotations)
        if self.annotations and len(self.annotations) > 0:
            for a in self.annotations:
                if a and a.get_imports() and len(a.get_imports()) > 0:
                    self.imports.update(a.get_imports())

        # Attributes
        attributes = document.get("attributes")
        if attributes:
            for a_doc in attributes:
                if a_doc:
                    attribute = Attribute(a_doc)
                    self.add_attribute(attribute)
                else:
                    print(f"Ignoring attribute in class {self.class_name}")

        # Constructor
        constructors_info = document.get("constructors")
        if constructors_info and len(constructors_info) > 0:
            for cons in constructors_info:
                constructor = Constructor(self.class_name, cons, self.attributes)
                self.constructors.append(constructor)
                imports = constructor.get_imports()
                if imports and len(imports) > 0:
                    self.imports.update(imports)

        # Methods
        methods = document.get("methods")
        if methods:
            for m_doc in methods:
                method = Method(m_doc)
                self.add_method(method)
Esempio n. 6
0
    def __init__(self, document):
        self.inputs = []
        self.named_inputs = {}
        self.body = None
        self.isAbstract = False
        self.isStatic = document.get("is_static") or False
        self.imports = []

        self.name = document.get("name")
        self.mode = document.get("mode") or MODE_PRIVATE
        return_type = document.get("type")
        method_body = document.get("body")

        if not self.name or not return_type:
            raise ValueError(
                f'The method argument must have method name and type')

        # Type check of the method
        self.return_type, method_imports = obtain_type(self.name, return_type)

        if not self.return_type:
            raise ValueError(
                f"The return type of method {self.name} could not be obtained")

        if method_imports and len(method_imports) > 0:
            self.imports.extend(method_imports)

        inputs = document.get("inputs")
        if inputs and len(inputs) > 0:
            for i in inputs:
                input_type_information = i.get("type")
                input_type_name = i.get("name")
                if not input_type_information:
                    raise ValueError(
                        f"The input type of method {self.name} could not be found"
                    )
                method_input_type, method_input_imports = obtain_type(
                    self.name, input_type_information)
                if not method_input_type:
                    raise ValueError(
                        f"The input type of method {self.name} could not be parsed"
                    )
                # Add method input type
                if input_type_name:
                    self.named_inputs[input_type_name] = method_input_type
                else:
                    self.inputs.append(method_input_type)
                if len(method_input_imports) > 0:
                    self.imports.extend(method_input_imports)

        method_annotations_info = document.get("annotations")
        self.annotations = parse_annotations(method_annotations_info)

        if self.annotations and len(self.annotations) > 0:
            for a in self.annotations:
                if a and a.get_imports() and len(a.get_imports()) > 0:
                    self.imports.extend(a.get_imports())

        if method_body:
            m_body_form = method_body.get("form")
            m_body_imports = method_body.get("imports")
            if m_body_form:
                self.body = m_body_form

                if m_body_imports and len(m_body_imports) > 0:
                    self.imports.extend(m_body_imports)