Пример #1
0
def writeClassDiagram(diagram):
    stream = STARTUML
    stream += STYLECLASS
    stream += TITLE.format(title=diagram.title)

    for obj in diagram.objects:
        attributes = diagram.get_attrs(obj.node)
        methods = diagram.get_methods(obj.node)

        if attributes or methods:
            template = INTERFACEOPEN if is_interface(obj.node) else CLASSOPEN
            stream += template.format(name=obj.title)

            for attr in sorted(attributes):
                attrDesc = getAttrDesc(attr)
                stream += CLASSATTR.format(name=attrDesc)

            for method in sorted(methods, key=lambda m: m.name):
                methodDesc = getAttrDesc(method.name)
                stream += CLASSMETHOD.format(
                    name=methodDesc, args=method.args.format_args()
                )

            stream += CLOSE
        else:
            template = INTERFACE if is_interface(obj.node) else CLASS
            stream += template.format(name=obj.title)

    stream += EMPTY

    for relation_type, relationsships in diagram.relationships.items():
        for rel in relationsships:
            stream += relationship2plantuml[rel.type].format(
                parent=rel.to_object.title, child=rel.from_object.title
            )

    stream += "\n" + ENDUML

    classesFile = classes.format(package=diagram.title)
    with open(classesFile, "w") as f:
        f.write(stream)
    return classesFile
Пример #2
0
 def extract_relationships(self):
     """extract relation ships between nodes in the diagram
     """
     for obj in self.classes():
         node = obj.node
         obj.attrs = self.get_attrs(node)
         obj.methods = self.get_methods(node)
         # shape
         if is_interface(node):
             obj.shape = 'interface'
         else:
             obj.shape = 'class'
         # inheritance link
         for par_node in node.ancestors(recurs=False):
             try:
                 par_obj = self.object_from_node(par_node)
                 self.add_relationship(obj, par_obj, 'specialization')
             except KeyError:
                 continue
         # implements link
         for impl_node in node.implements:
             try:
                 impl_obj = self.object_from_node(impl_node)
                 self.add_relationship(obj, impl_obj, 'implements')
             except KeyError:
                 continue
         # associations link
         for name, values in list(node.instance_attrs_type.items()) + \
                             list(node.locals_type.items()):
             for value in values:
                 if value is astroid.YES:
                     continue
                 if isinstance(value, astroid.Instance):
                     value = value._proxied
                 try:
                     ass_obj = self.object_from_node(value)
                     self.add_relationship(ass_obj, obj, 'association',
                                           name)
                 except KeyError:
                     continue
Пример #3
0
 def extract_relationships(self):
     """extract relation ships between nodes in the diagram
     """
     for obj in self.classes():
         node = obj.node
         obj.attrs = self.get_attrs(node)
         obj.methods = self.get_methods(node)
         # shape
         if is_interface(node):
             obj.shape = "interface"
         else:
             obj.shape = "class"
         # inheritance link
         for par_node in node.ancestors(recurs=False):
             try:
                 par_obj = self.object_from_node(par_node)
                 self.add_relationship(obj, par_obj, "specialization")
             except KeyError:
                 continue
         # implements link
         for impl_node in node.implements:
             try:
                 impl_obj = self.object_from_node(impl_node)
                 self.add_relationship(obj, impl_obj, "implements")
             except KeyError:
                 continue
         # associations link
         for name, values in list(node.instance_attrs_type.items()) + list(
             node.locals_type.items()
         ):
             for value in values:
                 if value is astroid.Uninferable:
                     continue
                 if isinstance(value, astroid.Instance):
                     value = value._proxied
                 try:
                     associated_obj = self.object_from_node(value)
                     self.add_relationship(associated_obj, obj, "association", name)
                 except KeyError:
                     continue
Пример #4
0
 def extract_relationships(self):
     """extract relation ships between nodes in the diagram
     """
     for obj in self.classes():
         node = obj.node
         obj.attrs = self.get_attrs(node)
         obj.methods = self.get_methods(node)
         # shape
         if is_interface(node):
             obj.shape = 'interface'
         else:
             obj.shape = 'class'
         # inheritance link
         for par_node in node.ancestors(recurs=False):
             try:
                 par_obj = self.object_from_node(par_node)
                 self.add_relationship(obj, par_obj, 'specialization')
             except KeyError:
                 continue
         # implements link
         for impl_node in node.implements:
             try:
                 impl_obj = self.object_from_node(impl_node)
                 self.add_relationship(obj, impl_obj, 'implements')
             except KeyError:
                 continue
         # associations link
         for name, values in node.instance_attrs_type.items() + \
                             node.locals_type.items():
             for value in values:
                 if value is astng.YES:
                     continue
                 if isinstance( value, astng.Instance):
                     value = value._proxied
                 try:
                     ass_obj = self.object_from_node(value)
                     self.add_relationship(ass_obj, obj, 'association', name)
                 except KeyError:
                     continue
Пример #5
0
 def extract_relationships(self) -> None:
     """Extract relationships between nodes in the diagram."""
     for obj in self.classes():
         node = obj.node
         obj.attrs = self.get_attrs(node)
         obj.methods = self.get_methods(node)
         # shape
         if is_interface(node):
             obj.shape = "interface"
         else:
             obj.shape = "class"
         # inheritance link
         for par_node in node.ancestors(recurs=False):
             try:
                 par_obj = self.object_from_node(par_node)
                 self.add_relationship(obj, par_obj, "specialization")
             except KeyError:
                 continue
         # implements link
         for impl_node in node.implements:
             try:
                 impl_obj = self.object_from_node(impl_node)
                 self.add_relationship(obj, impl_obj, "implements")
             except KeyError:
                 continue
         # associations link
         for name, values in list(node.instance_attrs_type.items()) + list(
             node.locals_type.items()
         ):
             for value in values:
                 if value is astroid.Uninferable:
                     continue
                 if isinstance(value, astroid.Instance):
                     value = value._proxied
                 try:
                     associated_obj = self.object_from_node(value)
                     self.add_relationship(associated_obj, obj, "association", name)
                 except KeyError:
                     continue
Пример #6
0
def writeClassDiagram(diagram):
    stream = STARTUML
    stream += STYLECLASS

    stream += TITLE.format(title=diagram.title)

    # modules = []

    for obj in diagram.objects:
        # objModule = obj.node.root()
        # if objModule not in modules:
        #     modules.append(objModule)
        #     pprint (objModule.locals)

        attributes = diagram.get_attrs(obj.node)
        methods = diagram.get_methods(obj.node)
        if attributes or methods:
            template = INTERFACEOPEN if is_interface(obj.node) else CLASSOPEN
            stream += template.format(name=obj.title)

            # magics = getMagicAttrs()
            publics = getPublicAttrs(attributes)
            protecteds = getProtectedAttrs(attributes)
            privates = getPrivateAttrs(attributes)

            # if publics:
            #     stream += ".. public ..\n"
            for attr in sorted(publics):
                stream += getAttrDesc(attr) + "\n"

            # if protecteds:
            #     stream += ".. protected ..\n"
            for attr in sorted(protecteds):
                stream += getAttrDesc(attr) + "\n"

            # if privates:
            #     stream += ".. private ..\n"
            for attr in sorted(privates):
                stream += getAttrDesc(attr) + "\n"

            # for attr in sorted(attributes):
            #     shortAttr = attr.split(":")[0].strip()
            #     prefixedAttr = getFieldTypePrefix(shortAttr) + shortAttr
            #     stream += CLASSATTR.format(name=prefixedAttr)

            # stream += "==\n"

            for method in sorted(methods, key=lambda m: m.name):
                prefixedMethod = getFieldTypePrefix(method.name) + method.name
                stream += CLASSMETHOD.format(name=prefixedMethod,
                                             args=method.args.format_args())

            stream += CLOSE
        else:
            template = INTERFACE if is_interface(obj.node) else CLASS
            stream += template.format(name=obj.title)

    stream += EMPTY

    for relation_type, relationsships in diagram.relationships.items():
        for rel in relationsships:
            stream += relationship2plantuml[rel.type].format(
                parent=rel.to_object.title, child=rel.from_object.title)
    stream += "\n" + ENDUML

    with open("classes.txt", "w") as f:
        f.write(stream)