Commits

Rob Lanphier committed 3b4a5f5

Shuffling imports around to make things a tiny bit cleaner. Still needs work

Comments (0)

Files changed (2)

jsonwidget/__init__.py

 """
 Library for generating a user interface from a JSON schema.
 
-This library allows an application developer to provide a curses-based user 
-interface for an application using not much more than a JSON schema.  The 
-JSON schema language is described here:
-http://robla.net/jsonwidget/jsonschema/
+jsonwidget is a general-purpose JSON validation and manipulation library.  This 
+library provides the following functions for applications:
 
-However, most people will find it much simpler to use a tool to generate a 
-schema for tweaking.  The tool available here will generate a schema from an
-example piece of JSON, and allow editing the resulting file:
-http://robla.net/jsonwidget/example.php?sample=byexample&user=normal
+*  Validation of JSON-compatible data against a schema
+*  Automatic generation of a schema from JSON-compatible data
+*  Construction of a curses-based tree data editing user interface from a schema
+*  Construction of a Javascript-based web tree data editing user interface 
+   from a schema
+*  Simple WSGI server to serve the web user interface, and validate and store 
+   result of editing with the web user interface
 
-This library currently only operates on a subset of JSON files, but a near-term
-goal is to allow editing of any arbitrary JSON file.
+Though jsonwidget is optimized for working with JSON, it is useful for 
+providing editing capability for any JSON-compatible data structure.
+
 
 Usage:
     import jsonwidget
 Website: http://robla.net/jsonwidget
 """
 
-import jsonwidget.termedit
-import os
+__version__ = "0.1.7"
+
+__author__ = 'Rob Lanphier <robla@robla.net>'
+
+# using __all__ for doc purposes only
+__all__ = ['TreeFileDatatype', 'TreeFileWrapper']
+
+from termedit import JsonFileEditor, JsonPinotFile, TreeFileDatatype, \
+    TreeFileWrapper
+from schema import generate_schema_from_data
+
 
 from jsontypes import schemaformat
 
-__version__ = "0.1.7"
+from jsonnode import JsonNodeError
 
-def run_editor(jsonfile, schemafile=None, schemaobj=None, 
-               program_name="jsonwidget " + __version__):
-    """ 
-    Run a simple editor with a given jsonfile and corresponding schema file.
-    """
-    
-    form = jsonwidget.termedit.JsonFileEditor(jsonfile=jsonfile, 
-                                              schemafile=schemafile,
-                                              schemaobj=schemaobj,
-                                              program_name=program_name)
-    if schemafile is None:
-        form.set_startup_notification(
-            'Using schema derived from json file.  Use "--schema" at startup to provide custom schema')
-    form.run()
+from commands import jsonedit, upgrade_schema, run_editor, find_system_schema,\
+    generate_schema
 
 
-def find_system_schema(schemaname):
-    """
-    Resolve schemaname to a full path.  This allows referencing
-    one of the bundled schemas without spelling out the full path.
-    """
-    # TODO: implement schemapath config variable.
-    try:
-        import pkg_resources
-    except ImportError:
-        filename = os.path.join("jsonwidget", "schema", schemaname)
-    else:
-        filename = os.path.join("schema", schemaname)
-        filename = pkg_resources.resource_filename("jsonwidget", 
-            filename)
-    return filename
 
-def generate_schema(filename=None, data=None, jsonstring=None, 
-                    version=schemaformat.version):
-    """
-    Generate a schema from a JSON example.
-    """
-    import json
-    import jsonwidget.jsonorder
-    if filename is not None:
-        with open(filename, 'r') as f:
-            jsonbuffer = f.read()
-        jsondata = json.loads(jsonbuffer)
-        jsonordermap = \
-            jsonwidget.jsonorder.JsonOrderMap(jsonbuffer).get_order_map()
-        return jsonwidget.schema.generate_schema_from_data(jsondata, 
-            jsonordermap=jsonordermap, version=version)
-    else:
-        raise RuntimeError("only filename-based generation is supported")
+

jsonwidget/commands.py

 #
 # Copyright (c) 2010, Rob Lanphier
 # All rights reserved.
-# Licensed under BSD-style license.  See LICENSE.txt for details.
+# Licensed under 3-clause BSD license.  See LICENSE.txt for details.
 
 import optparse
+import os
 import sys
 import jsonwidget
 
 from jsonwidget.jsonnode import JsonNodeError
+from jsontypes import schemaformat
 
 def jsonedit():
     '''urwid-based JSON editor'''
     usage = "usage: %prog [options] arg"
     parser = optparse.OptionParser(usage)
-    defschema = jsonwidget.find_system_schema("datatype-example-schema.json")
+    defschema = find_system_schema("datatype-example-schema.json")
  
     parser.add_option("-s", "--schema", dest="schema",
                       default=None,
     schemanode.convert(jsonwidget.jsontypes.schemaformat_v2)
     print schemanode.dumps()
 
+
+def run_editor(jsonfile, schemafile=None, schemaobj=None, 
+               program_name="jsonwidget " + jsonwidget.__version__):
+    """ 
+    Run a simple editor with a given jsonfile and corresponding schema file.
+    """
+    
+    form = jsonwidget.termedit.JsonFileEditor(jsonfile=jsonfile, 
+                                              schemafile=schemafile,
+                                              schemaobj=schemaobj,
+                                              program_name=program_name)
+    if schemafile is None:
+        form.set_startup_notification(
+            'Using schema derived from json file.  Use "--schema" at startup to provide custom schema')
+    form.run()
+
+
+def find_system_schema(schemaname):
+    """
+    Resolve schemaname to a full path.  This allows referencing
+    one of the bundled schemas without spelling out the full path.
+    """
+    # TODO: implement schemapath config variable.
+    try:
+        import pkg_resources
+    except ImportError:
+        filename = os.path.join("jsonwidget", "schema", schemaname)
+    else:
+        filename = os.path.join("schema", schemaname)
+        filename = pkg_resources.resource_filename("jsonwidget", 
+            filename)
+    return filename
+
+
+def generate_schema(filename=None, data=None, jsonstring=None, 
+                    version=schemaformat.version):
+    """
+    Generate a schema from a JSON example.
+    """
+    import json
+    import jsonwidget.jsonorder
+    if filename is not None:
+        with open(filename, 'r') as f:
+            jsonbuffer = f.read()
+        jsondata = json.loads(jsonbuffer)
+        jsonordermap = \
+            jsonwidget.jsonorder.JsonOrderMap(jsonbuffer).get_order_map()
+        return jsonwidget.schema.generate_schema_from_data(jsondata, 
+            jsonordermap=jsonordermap, version=version)
+    else:
+        raise RuntimeError("only filename-based generation is supported")
+
 if __name__ == "__main__":
     jsonedit()