OSDN Git Service

refactoring Formatter classes
authort.moriyama <t.moriyama@users.sourceforge.jp>
Sun, 1 Sep 2013 15:41:53 +0000 (00:41 +0900)
committert.moriyama <t.moriyama@users.sourceforge.jp>
Sun, 1 Sep 2013 15:41:53 +0000 (00:41 +0900)
linkpair/formatter/baseformatter.py
linkpair/formatter/grapheasy.py
linkpair/formatter/graphviz_tb.py
linkpair/formatter/springy.py
linkpair/formatter/springy_raphael.py
linkpair/linkpair.py
linkpair/port.py

index 72d3371..63b38cd 100755 (executable)
@@ -18,12 +18,19 @@ class BaseFormatter(object):
     PATCH_STYLE = ''
     NOT_CONNECTED_STYLE = ''
 
-    DEFAULT_FORMAT = '[ %s ] %s -- { label: "%s   %s"; } [ %s ] %s'
-    OS_ROUTING_FORMAT = '[ %s ] %s -- { label: "%s %s"; } [ %s ] %s'
-    NAMESPACE_FORMAT = '[ %s ] %s --> { label: %s   %s; } [ %s ] %'
-    PATCH_FORMAT = '[ %s ] %s <==> { label: %s  :  %s; } [ %s ] %s'
-    VETH_FORMAT = '[ %s ] %s <--> { label: %s  \|  %s; } [ %s ] %s'
-    VLAN_DIST_FORMAT = '[ %s ] %s <--> { label: %s\(%s\); } [ %s ] %s'
+    DEFAULT_FORMAT = 0
+    OS_ROUTING_FORMAT = 1
+    NAMESPACE_FORMAT = 2
+    PATCH_FORMAT = 3
+    VETH_FORMAT = 4
+    VLAN_DIST_FORMAT = 5
+
+    DEFAULT_FORMAT_DESC = '[ %s ] %s -- { label: "%s   %s"; } [ %s ] %s'
+    OS_ROUTING_FORMAT_DESC = '[ %s ] %s -- { label: "%s %s"; } [ %s ] %s'
+    NAMESPACE_FORMAT_DESC = '[ %s ] %s --> { label: %s   %s; } [ %s ] %'
+    PATCH_FORMAT_DESC = '[ %s ] %s <==> { label: %s  :  %s; } [ %s ] %s'
+    VETH_FORMAT_DESC = '[ %s ] %s <--> { label: %s  \|  %s; } [ %s ] %s'
+    VLAN_DIST_FORMAT_DESC = '[ %s ] %s <--> { label: %s\(%s\); } [ %s ] %s'
 
     HEADER_FORMAT = ""
     FOOTER_FORMAT = ""
@@ -70,7 +77,7 @@ class BaseFormatter(object):
             port2_label = port2_label + " vnid=" + \
                 linkpair.port2.metadata["vxlan_vnid"]
 
-        formatted_lp = linkpair.format % (
+        formatted_lp = cls.get_format_desc(linkpair.format) % (
             device1_label,
             cls.get_device_style(linkpair.device1.device_type),
             port1_label,
@@ -97,20 +104,20 @@ class BaseFormatter(object):
         return footer
 
     @classmethod
-    def get_device_style(cls, type=Device.DEFAULT_TYPE):
-        if type == Device.DEFAULT_TYPE:
+    def get_device_style(cls, device_type=Device.DEFAULT_TYPE):
+        if device_type == Device.DEFAULT_TYPE:
             return cls.DEFAULT_STYLE
-        elif type == Device.VM_TYPE:
+        elif device_type == Device.VM_TYPE:
             return cls.VM_STYLE
-        elif type == Device.BR_TYPE:
+        elif device_type == Device.BR_TYPE:
             return cls.BR_STYLE
-        elif type == Device.OS_ROUTE_TYPE:
+        elif device_type == Device.OS_ROUTE_TYPE:
             return cls.OS_ROUTE_STYLE
-        elif type == Device.PHYNET_TYPE:
+        elif device_type == Device.PHYNET_TYPE:
             return cls.PHYNET_STYLE
-        elif type == Device.NAMESPACE_TYPE:
+        elif device_type == Device.NAMESPACE_TYPE:
             return cls.NAMESPACE_STYLE
-        elif type == Device.NOT_CONNECTED_TYPE:
+        elif device_type == Device.NOT_CONNECTED_TYPE:
             return cls.NOT_CONNECTED_STYLE
         else:
             return cls.DEFAULT_STYLE
@@ -133,6 +140,23 @@ class BaseFormatter(object):
         devices = sorted(set(devices), key=devices.index)
         return devices
 
+    @classmethod
+    def get_format_desc(cls, format=Device.DEFAULT_TYPE):
+        if format == cls.DEFAULT_FORMAT:
+            return cls.DEFAULT_FORMAT_DESC
+        elif format == cls.OS_ROUTING_FORMAT:
+            return cls.OS_ROUTING_FORMAT_DESC
+        elif format == cls.NAMESPACE_FORMAT:
+            return cls.NAMESPACE_FORMAT_DESC
+        elif format == cls.PATCH_FORMAT:
+            return cls.PATCH_FORMAT_DESC
+        elif format == cls.VETH_FORMAT:
+            return cls.VETH_FORMAT_DESC
+        elif format == cls.VLAN_DIST_FORMAT:
+            return cls.VLAN_DIST_FORMAT_DESC
+        else:
+            return cls.DEFAULT_FORMAT_DESC
+
     @apply
     def linkpairs():
         def get(self):
index ed86fec..4dc2f15 100755 (executable)
@@ -21,12 +21,12 @@ class GraphEasyFormatter(BaseFormatter):
     PATCH_STYLE = '{ fill: #ddddff; border: dotted; bordercolor: black; }'
     NOT_CONNECTED_STYLE = '{ fill: #ffdddd; border: dotted; bordercolor: black; }'
 
-    DEFAULT_FORMAT = '[ %s ] %s -- { label: "%s   %s"; labelcolor: #663333; } [ %s ] %s'
-    OS_ROUTING_FORMAT = '[ %s ] %s -- { label: "%s   %s"; labelcolor: #663333; } [ %s ] %s'
-    NAMESPACE_FORMAT = '[ %s ] %s --> { label: "%s   %s"; labelcolor: #663333; } [ %s ] %s'
-    PATCH_FORMAT = '[ %s ] %s <==> { label: "%s : %s"; labelcolor: #333366; } [ %s ] %s'
-    VETH_FORMAT = '[ %s ] %s <--> { label: "%s \| %s"; labelcolor: #336633; } [ %s ] %s'
-    VLAN_DIST_FORMAT = '[ %s ] %s --> { label: "%s\(%s\)"; labelcolor: #336633; } [ %s ] %s'
+    DEFAULT_FORMAT_DESC = '[ %s ] %s -- { label: "%s   %s"; labelcolor: #663333; } [ %s ] %s'
+    OS_ROUTING_FORMAT_DESC = '[ %s ] %s -- { label: "%s   %s"; labelcolor: #663333; } [ %s ] %s'
+    NAMESPACE_FORMAT_DESC = '[ %s ] %s --> { label: "%s   %s"; labelcolor: #663333; } [ %s ] %s'
+    PATCH_FORMAT_DESC = '[ %s ] %s <==> { label: "%s : %s"; labelcolor: #333366; } [ %s ] %s'
+    VETH_FORMAT_DESC = '[ %s ] %s <--> { label: "%s \| %s"; labelcolor: #336633; } [ %s ] %s'
+    VLAN_DIST_FORMAT_DESC = '[ %s ] %s --> { label: "%s\(%s\)"; labelcolor: #336633; } [ %s ] %s'
 
     HEADER = ""
     FOOTER = ""
index 86a77d9..076c27d 100755 (executable)
@@ -21,12 +21,12 @@ class GraphvizTBFormatter(BaseFormatter):
     PATCH_STYLE = 'fillcolor="#ddddff", peripheries=2'
     NOT_CONNECTED_STYLE = 'fillcolor="#ffdddd", style="filled,dotted"'
 
-    DEFAULT_FORMAT = '"%s" -> "%s" [ color="#000000", fontcolor="#663333", label = "%s     %s" ]'
-    OS_ROUTING_FORMAT = '"%s" -> "%s" [ color="#000000", fontcolor="#663333", label = "%s     %s", style="dotted" ]'
-    NAMESPACE_FORMAT = '"%s" -> "%s" [ color="#000000", fontcolor="#663333", label = "%s     %s" ]'
-    PATCH_FORMAT = '"%s" -> "%s" [ dir=both, arrowhead=box, arrowtail=box, color="#000000:#000000", fontcolor="#333366", label = "%s : %s" ]'
-    VETH_FORMAT = '"%s" -> "%s" [ dir=both, arrowhead=obox, arrowtail=obox, color="#000000", fontcolor="#336633", label="%s \| %s" ]'
-    VLAN_DIST_FORMAT = '"%s" -> "%s" [ dir=forward, arrowhead=crow, arrowtail=none, color="#660000", fontcolor="#336633", label="%s\(%s\)", style="dotted" ]'
+    DEFAULT_FORMAT_DESC = '"%s" -> "%s" [ color="#000000", fontcolor="#663333", label = "%s     %s" ]'
+    OS_ROUTING_FORMAT_DESC = '"%s" -> "%s" [ color="#000000", fontcolor="#663333", label = "%s     %s", style="dotted" ]'
+    NAMESPACE_FORMAT_DESC = '"%s" -> "%s" [ color="#000000", fontcolor="#663333", label = "%s     %s" ]'
+    PATCH_FORMAT_DESC = '"%s" -> "%s" [ dir=both, arrowhead=box, arrowtail=box, color="#000000:#000000", fontcolor="#333366", label = "%s : %s" ]'
+    VETH_FORMAT_DESC = '"%s" -> "%s" [ dir=both, arrowhead=obox, arrowtail=obox, color="#000000", fontcolor="#336633", label="%s \| %s" ]'
+    VLAN_DIST_FORMAT_DESC = '"%s" -> "%s" [ dir=forward, arrowhead=crow, arrowtail=none, color="#660000", fontcolor="#336633", label="%s\(%s\)", style="dotted" ]'
 
     # ADDNODE_STYLE = '"%s" [ fillcolor="#ffffff", style="filled,dotted" ]'
     ADDNODE_STYLE = '"%s" [ %s ]'
@@ -166,7 +166,7 @@ node [
             port2_label = port2_label + " vnid=" + \
                 linkpair.port2.metadata["vxlan_vnid"]
 
-        formatted_lp = linkpair.format % (
+        formatted_lp = cls.get_format_desc(linkpair.format) % (
             device1_label,
             device2_label,
             port1_label,
index 1605c3c..8ede84f 100755 (executable)
@@ -21,12 +21,12 @@ class SpringyFormatter(BaseFormatter):
     PATCH_STYLE = "color: '#DDDDFF', directional: true"
     NOT_CONNECTED_STYLE = "color: '#FFDDDD', directional: true"
 
-    DEFAULT_FORMAT = "['%s', '%s', {%s, label: '%s %s'}]"
-    OS_ROUTING_FORMAT = "['%s', '%s', {%s, label: '%s %s'}]"
-    NAMESPACE_FORMAT = "['%s', '%s', {%s, label: '%s %s'}]"
-    PATCH_FORMAT = "['%s', '%s', {%s, label: '%s : %s'}]"
-    VETH_FORMAT = "['%s', '%s', {%s, label: '%s \| %s'}]"
-    VLAN_DIST_FORMAT = "['%s', '%s', {%s, label: '%s\(%s\)'}]"
+    DEFAULT_FORMAT_DESC = "['%s', '%s', {%s, label: '%s %s'}]"
+    OS_ROUTING_FORMAT_DESC = "['%s', '%s', {%s, label: '%s %s'}]"
+    NAMESPACE_FORMAT_DESC = "['%s', '%s', {%s, label: '%s %s'}]"
+    PATCH_FORMAT_DESC = "['%s', '%s', {%s, label: '%s : %s'}]"
+    VETH_FORMAT_DESC = "['%s', '%s', {%s, label: '%s \| %s'}]"
+    VLAN_DIST_FORMAT_DESC = "['%s', '%s', {%s, label: '%s\(%s\)'}]"
 
     ADDNODE_STYLE = "graph.addNodes('%s');"
     ADDEDGE_START = "graph.addEdges("
@@ -108,7 +108,7 @@ jQuery(function(){
             port2_label = port2_label + " vnid=" + \
                 linkpair.port2.metadata["vxlan_vnid"]
 
-        formatted_lp = linkpair.format % (
+        formatted_lp = cls.get_format_desc(linkpair.format) % (
             device1_label,
             device2_label,
             cls.get_device_style(linkpair.device1.device_type),
index b1fcfbd..04bf2ec 100755 (executable)
@@ -21,12 +21,12 @@ class SpringyRaphaelFormatter(BaseFormatter):
     PATCH_STYLE = "color: '#DDDDFF', directional: true"
     NOT_CONNECTED_STYLE = "color: '#FFDDDD', directional: true"
 
-    DEFAULT_FORMAT = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
-    OS_ROUTING_FORMAT = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
-    NAMESPACE_FORMAT = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
-    PATCH_FORMAT = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
-    VETH_FORMAT = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
-    VLAN_DIST_FORMAT = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
+    DEFAULT_FORMAT_DESC = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
+    OS_ROUTING_FORMAT_DESC = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
+    NAMESPACE_FORMAT_DESC = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
+    PATCH_FORMAT_DESC = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
+    VETH_FORMAT_DESC = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
+    VLAN_DIST_FORMAT_DESC = "graph.newEdge(%s, %s, {color: '#00A0B0'});"
 
     ADDNODE_STYLE = "var %s = graph.newNode({label: '%s'});"
     ADDEDGE_START = "graph.addEdges("
@@ -284,7 +284,7 @@ jQuery(function(){
     @classmethod
     def format_linkpair(cls, linkpair):
         formatted_lp = ""
-        formatted_lp = linkpair.format % (
+        formatted_lp = cls.get_format_desc(linkpair.format) % (
             str(linkpair.device1).replace(" ", "_").replace("-", "_"),
             str(linkpair.device2).replace(" ", "_").replace("-", "_"))
         return formatted_lp
index f537460..e715702 100755 (executable)
@@ -60,11 +60,8 @@ class LinkPair(object):
         return "[" + str(self.device1) + "]:" + str(self.port1) + \
                " -- " + str(self.port2) + ":[" + str(self.device2) + "]"
 
-    def _blank_format(self, format_name):
-        if len(format_name) > 0:
-            repr(format_name)
-        else:
-            return "\"\""
+    def _get_format_as_string(self, format):
+        return "\"" + str(self.format) + "\""
     
     def get_linkpair_linkid(self):
         return str(self.device1).replace(" ", "_") + "__" + \
@@ -77,4 +74,4 @@ class LinkPair(object):
                "\"device2\":" + repr(self.device2) + "," + \
                "\"port1\":" + repr(self.port1) + "," + \
                "\"port2\":" + repr(self.port2) + "," + \
-               "\"format\":" + self._blank_format(self.format_name) + "}"
+               "\"format\":" + self._get_format_as_string(self.format) + "}"
index 264a370..2c9a8dc 100755 (executable)
@@ -12,7 +12,6 @@ class Port(LinkObject):
         self.label = label
         self.port_type = port_type
         self.metadata = metadata
-        
 
     def __repr__(self):
         return "{\"label\":\"" + self.label + \