Commits

Anonymous committed 52150a4

add dot format

Comments (0)

Files changed (1)

     ]
 
     return '\n'.join(result)
+
+
+def dot(desc):
+    """Generate dot file
+
+    :param desc: result of sadisplay.describe function
+
+    Return string
+    """
+
+    classes, relations, inherits = desc
+
+    CLASS_BODY = "\n\t\tlabel=\"{%(name)s|%(cols)s|%(props)s|%(methods)s\l}\""
+    CLASS_TEMPLATE = "\t%(name)s [" + CLASS_BODY + "\n\t]\n"
+
+    COLUMN_TEMPLATE = "\t%(type)s \t\t%(name)s\l"
+
+    PROPERTY_TEMPLATE = "\t%(name)s\l"
+
+    METHOD_TEMPLATE = "\t%(name)s()\l"
+
+    EDGE_INHERIT = "\tedge [\n\t\tarrowhead = empty\n\t]"
+    INHERIT_TEMPLATE = "\t%(child)s -> %(parent)s \n"
+
+    EDGE_REL = "\tedge [\n\t\tarrowhead = ediamond\n\t\tarrowtail = open\n\t]"
+    RELATION_TEMPLATE = "\t%(from)s -> %(to)s [taillabel = \"%(by)s\"]"
+
+    result = ["""digraph G {
+\tfontname = "Bitstream Vera Sans"
+\tfontsize = 8
+
+\tnode [
+\t\tfontname = "Bitstream Vera Sans"
+\t\tfontsize = 8
+\t\tshape = "record"
+\t]
+
+\tedge [
+\t\tfontname = "Bitstream Vera Sans"
+\t\tfontsize = 8
+\t]"""]
+
+    for cls in classes:
+        renderd = CLASS_TEMPLATE % {
+                'name': cls['name'],
+                'cols': ' '.join([
+                    COLUMN_TEMPLATE % {'type': c[0], 'name': c[1]}
+                        for c in cls['cols']
+                ]),
+                'props': ' '.join([
+                    PROPERTY_TEMPLATE % {'name': p}
+                        for p in cls['props']
+                ]),
+                'methods': ' '.join([
+                    METHOD_TEMPLATE % {'name': m}
+                        for m in cls['methods']
+                ]),
+            }
+
+        result.append(renderd)
+
+    result += [EDGE_INHERIT]
+    for item in inherits:
+        result.append(INHERIT_TEMPLATE % item)
+
+    result += [EDGE_REL]
+    for item in relations:
+        result.append(RELATION_TEMPLATE % item)
+
+    result += [
+        '}'
+    ]
+
+    return '\n'.join(result)