OSDN Git Service

fix JsonExporter
authort.moriyama <t.moriyama@users.sourceforge.jp>
Sun, 14 Jul 2013 18:53:39 +0000 (03:53 +0900)
committert.moriyama <t.moriyama@users.sourceforge.jp>
Sun, 14 Jul 2013 18:53:39 +0000 (03:53 +0900)
linkpair/exporter/json.py
linkpair/linkobject.py
linkpair/linkpair.py

index 5ab6bbf..dcde498 100755 (executable)
@@ -2,8 +2,6 @@
 # -*- coding: utf-8 -*-
 #
 
-__version__ = '1.1'
-
 import sys
 import os
 import re
@@ -16,18 +14,12 @@ class JsonExporter(object):
         self._linkpairs = linkpairs
 
     def export(self):
-        json_strings = ""
+        json_strings = "{\n"
+        linkpair_index = 0
         for linkpair in self._linkpairs:
-            json_strings = json_strings + linkpair.to_json() + "\n"
-        json_strings = json_strings.rstrip(",")
+            linkpair_json = "\"lp_" + str(linkpair_index) + "\":"
+            json_strings = json_strings + linkpair_json + linkpair.to_json() + ",\n"
+            linkpair_index += 1
+        json_strings = json_strings.rstrip(",\n") + "\n}"
         return json_strings
-
-#         try:
-#             json_file = open(db_name, 'w')
-#             json_file.write("{\"linkpairs\": [")
-#             json_file.write(json_strings)
-#             json_file.write("]\n}")
-#             json_file.close()
-#         except:
-#             print "error:", sys.exc_info()[0]
-#             raise
+        
\ No newline at end of file
index 7cf1b26..fa2a590 100755 (executable)
@@ -2,8 +2,6 @@
 # -*- coding: utf-8 -*-
 #
 
-__version__ = '1.1'
-
 
 class LinkObject(object):
     DEFAULT_TYPE = 1
@@ -49,7 +47,22 @@ class LinkObject(object):
         return self.label
 
     def __repr__(self):
-        return "[\"" + self.label + "\", \"" + repr(self.type) + "\", [" + repr(self.metadata) + "]]"
+        return "{\"label\":\"" + self.label + \
+            "\", \"type\":\"" + repr(self.type) + \
+            "\", \"meta\":" + self._blank_dict(self.metadata) + "}"
+
+    def _blank_dict(self, dict_object):
+        if len(dict_object) > 0:
+            return self._dict_to_json(dict_object)
+        else:
+            return "\"\""
 
+    def _dict_to_json(self, dict_object):
+        json_strings = "{"
+        for dict_key in dict_object.keys():
+            json_strings += "\"" + dict_key + "\":\"" + dict_object[dict_key] + "\","
+        json_strings = json_strings.rstrip(",") + "}"
+        return json_strings
+        
     def __add__(self, label):
         return self.label + label
index 8f79306..bbd1494 100755 (executable)
@@ -2,8 +2,6 @@
 # -*- coding: utf-8 -*-
 #
 
-__version__ = '1.1'
-
 import base64
 from device import Device
 from port import Port
@@ -62,9 +60,15 @@ 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 to_json(self):
-        return "[ \"device1\": " + repr(self.device1) + ", " + \
-               "  \"device2\": " + repr(self.device2) + ", " + \
-               "  \"port1\": " + repr(self.port1) + ", " + \
-               "  \"port2\": " + repr(self.port2) + ", " + \
-               "  \"format\": \"" + repr(self.format_name) + "\" ]"
+        return "{ \"device1\":" + repr(self.device1) + ", " + \
+               "  \"device2\":" + repr(self.device2) + ", " + \
+               "  \"port1\":" + repr(self.port1) + ", " + \
+               "  \"port2\":" + repr(self.port2) + ", " + \
+               "  \"format\":" + self._blank_format(self.format_name) + "}"