OSDN Git Service

refactoring Formatter classes
[ti2/ti2.git] / linkpair / formatter / baseformatter.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):