Rob Lanphier avatar Rob Lanphier committed cdd66ab

New schema functions: set_title, generate_schema_from_data, get_order_map, get_json_type
Laying groundwork for csvfile

Comments (0)

Files changed (4)

jsonwidget/jsonbase.py

             raise JsonBaseError("self.children has invalid type %s" %
                                 type(self.children).__name__)
 
+    def get_data(self):
+        return self.data
 
+
+def get_json_type(data):
+    """Given an arbitrary piece of data, return the type as represented in 
+       json schemas."""
+    if(isinstance(data, basestring)):
+        return 'str'
+    elif(isinstance(data, bool)):
+        return 'bool'
+    elif(isinstance(data, int)):
+        return 'int'
+    elif(isinstance(data, float)):
+        return 'number'
+    elif(isinstance(data, dict)):
+        return 'map'
+    elif(isinstance(data, list)):
+        return 'seq'
+    elif(data is None):
+        return 'none'
+    else:
+        raise JsonNodeError("unknown type: %s" % type(data).__name__)
+
+

jsonwidget/jsonnode.py

 
     def get_type(self):
         """Get type string as defined by the schema language"""
-
-        if(isinstance(self.data, basestring)):
-            return 'str'
-        elif(isinstance(self.data, bool)):
-            return 'bool'
-        elif(isinstance(self.data, int)):
-            return 'int'
-        elif(isinstance(self.data, float)):
-            return 'number'
-        elif(isinstance(self.data, dict)):
-            return 'map'
-        elif(isinstance(self.data, list)):
-            return 'seq'
-        elif(self.data is None):
-            return 'none'
-        else:
-            raise JsonNodeError("unknown type: %s" % type(self.data).__name__)
+        return get_json_type(self.data)
 
     def get_key(self):
         return self.key
     def set_key(self, key):
         self.key = key
 
-    def get_data(self):
-        return self.data
-
     def set_data(self, data):
         """Set raw data"""
 

jsonwidget/schema.py

             else:
                 return str(self.key)
 
+    def set_title(self, title):
+        self.data['title'] = title
+
     def get_type(self):
         return self.data['type']
 
     def _get_key_order(self):
         return self.ordermap['children']['mapping']['keys']
 
+    def get_order_map(self):
+        return self.ordermap
+
     def get_children(self):
         """
         Get a list of children, possibly ordered.  Note that even though the
         else:
             retval = None
         return retval
+
+
+def generate_schema_from_data(jsondata):
+    schema = {}
+    ordermap = {}
+
+    schema['type'] = get_json_type(jsondata)
+
+    if schema['type'] == 'map':
+        schema['mapping'] = {}
+        ordermap['keys'] = ['type', 'mapping']
+        ordermap['children'] = {"type": {}, "mapping": {}}
+        ordermap['children']['mapping'] = {"keys": [], "children": {}}
+        for name in jsondata:
+            schemaobj = generate_schema_from_data(jsondata[name])
+            schema['mapping'][name] = schemaobj.get_data()
+            ordermap['children']['mapping']['keys'].append(name)
+            ordermap['children']['mapping']['children'][name] = schemaobj.get_order_map()
+    elif schema['type'] == 'seq':
+        schemaobj = generate_schema_from_data(jsondata[0])
+        schema['sequence'] = [schemaobj.get_data()]
+        ordermap['keys'] = ['type', 'sequence']
+        ordermap['children'] = {"type": {}, "sequence": {}}
+        ordermap['children']['sequence'] = {"keys": [0], "children": {}}
+        ordermap['children']['sequence']['children'][0] = schemaobj.get_order_map()
+
+    return SchemaNode(data=schema, ordermap=ordermap)
+

jsonwidget/termedit.py

     """
     This is an editor for in-memory data instead of a file
     """
-    def __init__(self, jsondata=None, schemafile=None, 
-                 program_name="JsonWidget", monochrome=True):
-        self.json = JsonNode(data=jsondata, schemafile=schemafile)
+    def __init__(self, jsondata=None, schemafile=None, schemadata=None,
+                 schemanode=None, program_name="JsonWidget", monochrome=True):
+        self.json = JsonNode(data=jsondata, schemafile=schemafile, 
+                             schemadata=schemadata, schemanode=schemanode)
         self.schema = self.json.get_schema_node()
         self.listbox = JsonFrame(self.json)
         PinotFileEditor.__init__(self, program_name=program_name, 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.