Commits

Rob Lanphier committed 053fabd

Loading default schema from package resources

  • Participants
  • Parent commits 837bf88

Comments (0)

Files changed (11)

File jsonwidget/__init__.py

 """
 
 import jsonwidget.termedit
+import os
+
 
 def run_editor(jsonfile, schemafile=None, program_name="jsonwidget 0.1"):
     """ 
     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
+

File jsonwidget/commands.py

     '''urwid-based JSON editor'''
     usage = "usage: %prog [options] arg"
     parser = optparse.OptionParser(usage)
+    defschema = jsonwidget.find_system_schema("datatype-example-schema.json")
+ 
     parser.add_option("-s", "--schema", dest="schema",
-                      default="schema/datatype-example-schema.json",
+                      default=defschema,
                       help="use this schema to build the form")
     parser.add_option("--tracebacks", dest="tracebacks",
                       default=False,
         addressbook = args[0]
     else:
         addressbook = None
+
+    defschema = jsonwidget.find_system_schema("addressbookschema.json")
     jsonwidget.run_editor(addressbook, 
-                          schemafile="schema/addressbookschema.json",
+                          schemafile=defschema,
                           program_name="jsonaddress 0.1")
 
 

File jsonwidget/schema/addressbookschema.json

+{
+  "type":"seq",
+  "title":"Address Book",
+  "sequence":
+  [
+    {
+      "type":"map",
+      "title":"Address Book Entry",
+      "mapping":
+      {
+        "firstName":
+        {
+          "type":"str",
+          "title":"First Name",
+          "required":true
+        },
+        "lastName":
+        {
+          "type":"str",
+          "title":"Last Name",
+          "required":true
+        },
+        "organizations":
+        {
+          "type":"seq",
+          "title":"Affiliations",
+          "sequence":
+          [
+            {
+              "type":"map",
+              "required":true,
+              "mapping":
+              {
+                "name":
+                {
+                  "type":"str",
+                  "required":true,
+                  "title":"Organization Name"
+                },
+                "title":
+                {
+                  "type":"str",
+                  "title":"Title/Role"
+                },
+                "department":
+                {
+                  "type":"str",
+                  "title":"Department"
+                },
+                "type":
+                {
+                  "type":"str",
+                  "enum":
+                  [
+                    "job",
+                    "school",
+                    "other"
+                  ],
+                  "title":"Type"
+                }
+              },
+              "title":"Organization"
+            }
+          ]
+        },
+        "addresses":
+        {
+          "type":"seq",
+          "title":"Addresses",
+          "sequence":
+          [
+            {
+              "type":"map",
+              "title":"Address",
+              "required":true,
+              "mapping":
+              {
+                "streetAddress":
+                {
+                  "type":"str",
+                  "title":"Street Address",
+                  "required":true
+                },
+                "locality":
+                {
+                  "type":"str",
+                  "title":"City (locality)",
+                  "required":true
+                },
+                "region":
+                {
+                  "type":"str",
+                  "title":"State/Provence/Region",
+                  "required":true
+                },
+                "postalCode":
+                {
+                  "type":"str",
+                  "title":"Postal (Zip) Code",
+                  "required":true
+                },
+                "country":
+                {
+                  "type":"str",
+                  "title":"Country",
+                  "required":true
+                }
+              }
+            }
+          ]
+        },
+        "phoneNumbers":
+        {
+          "type":"seq",
+          "title":"Phone number(s)",
+          "sequence":
+          [
+            {
+              "type":"map",
+              "title":"Phone number",
+              "required":true,
+              "mapping":
+              {
+                "workhomeother":
+                {
+                  "type":"str",
+                  "required":true,
+                  "enum":
+                  [
+                    "Work",
+                    "Home",
+                    "Mobile",
+                    "Fax",
+                    "Other"
+                  ],
+                  "title":"Work/Home/Other"
+                },
+                "Number":
+                {
+                  "type":"str",
+                  "required":true
+                },
+                "comment":
+                {
+                  "type":"str",
+                  "title":"Comment"
+                }
+              }
+            }
+          ]
+        },
+        "emails":
+        {
+          "type":"seq",
+          "title":"Email addresses",
+          "sequence":
+          [
+            {
+              "type":"map",
+              "title":"Email",
+              "required":true,
+              "mapping":
+              {
+                "type":
+                {
+                  "type":"str",
+                  "required":true,
+                  "title":"Work/Home/Other",
+                  "enum":
+                  [
+                    "work",
+                    "home",
+                    "other"
+                  ]
+                },
+                "value":
+                {
+                  "type":"str",
+                  "required":true,
+                  "title":"Address"
+                },
+                "primary":
+                {
+                  "type":"bool",
+                  "required":true,
+                  "title":"Primary?"
+                }
+              }
+            }
+          ]
+        },
+        "ims":
+        {
+          "type":"seq",
+          "title":"IM Accounts",
+          "sequence":
+          [
+            {
+              "type":"map",
+              "title":"Account",
+              "required":true,
+              "mapping":
+              {
+                "type":
+                {
+                  "type":"str",
+                  "required":true,
+                  "title":"Service",
+                  "enum":
+                  [
+                    "aim",
+                    "gtalk",
+                    "icq",
+                    "xmpp",
+                    "msn",
+                    "skype",
+                    "qq",
+                    "yahoo"
+                  ],
+                  "desc_enum":
+                  {
+                    "aim":"AIM (AOL)",
+                    "gtalk":"GTalk (Google)",
+                    "icq":"ICQ",
+                    "xmpp":"XMPP/Jabber",
+                    "msn":"MSN (Microsoft)",
+                    "skype":"Skype",
+                    "qq":"QQ (Tencent)",
+                    "yahoo":"Yahoo!"
+                  }
+                },
+                "value":
+                {
+                  "type":"str",
+                  "required":true,
+                  "title":"Account ID"
+                }
+              }
+            }
+          ]
+        }
+      }
+    }
+  ]
+}

File jsonwidget/schema/datatype-example-schema.json

+{
+  "type":"seq",
+  "title":"Datatype example schema (datatype-example-schema.json)\n  Not terribly useful for anything other than demonstration purposes.  Edit one\n  of the existing schemas for your purpose, or generate one using the online\n  tool at http://robla.net/2005/jsonwidget\nData",
+  "desc":"Example with all datatypes in one place",
+  "sequence":
+  [
+    {
+      "type":"map",
+      "title":"Item",
+      "mapping":
+      {
+        "map_example":
+        {
+          "type":"map",
+          "title":"Map (map)",
+          "mapping":
+          {
+            "field1":
+            {
+              "type":"str"
+            },
+            "field2":
+            {
+              "type":"str"
+            }
+          }
+        },
+        "seq_example":
+        {
+          "type":"seq",
+          "title":"Sequence (seq)",
+          "sequence":
+          [
+            {
+              "title":"Item",
+              "type":"str"
+            }
+          ]
+        },
+        "num_example":
+        {
+          "title":"Number (num)",
+          "type":"number"
+        },
+        "int_example":
+        {
+          "title":"Integer (int)",
+          "type":"int"
+        },
+        "str_example":
+        {
+          "title":"String (str)",
+          "type":"str"
+        },
+        "bool_example":
+        {
+          "title":"Boolean (bool)",
+          "type":"bool"
+        },
+        "str_enum_example":
+        {
+          "title":"String Enumeration (str + enum)",
+          "type":"str",
+          "enum":
+          [
+            "value1",
+            "value2",
+            "value3"
+          ]
+        }
+      }
+    }
+  ]
+}

File jsonwidget/schema/pasterschema.json

+{
+  "type":"map",
+  "mapping":
+  {
+    "project_name":
+    {
+      "type":"str",
+      "title":"Project name"
+    },
+    "version":
+    {
+      "type":"str",
+      "title":"Version"
+    },
+    "description":
+    {
+      "type":"str",
+      "title":"Description (one liner)"
+    },
+    "long_description":
+    {
+      "type":"str",
+      "title":"Long description (multiline reStructuredText)"
+    },
+    "keywords":
+    {
+      "type":"str",
+      "title":"Keywords (space separated)"
+    },
+    "author":
+    {
+      "type":"str",
+      "title":"Author"
+    },
+    "author_email":
+    {
+      "type":"str",
+      "title":"Author email"
+    },
+    "url":
+    {
+      "type":"str",
+      "title":"URL to project page"
+    },
+    "license_name":
+    {
+      "type":"str",
+      "title":"License name"
+    },
+    "zip_safe":
+    {
+      "type":"bool",
+      "title":"Zip safe?"
+    }
+  }
+}

File jsonwidget/schema/simpleaddr-schema.json

+{
+  "type":"map",
+  "title":"Address Book Entry",
+  "mapping":
+  {
+    "firstName":
+    {
+      "type":"str",
+      "title":"First Name",
+      "required":true
+    },
+    "lastName":
+    {
+      "type":"str",
+      "title":"Last Name",
+      "required":true
+    },
+    "streetAddress":
+    {
+      "type":"str",
+      "title":"Street Address",
+      "required":true
+    },
+    "city":
+    {
+      "type":"str",
+      "title":"City",
+      "required":true
+    },
+    "state":
+    {
+      "type":"str",
+      "title":"State",
+      "required":true
+    },
+    "postalCode":
+    {
+      "type":"str",
+      "title":"Postal (Zip) Code",
+      "required":true
+    },
+    "phoneNumbers":
+    {
+      "type":"seq",
+      "title":"Phone numbers",
+      "required":true,
+      "sequence":
+      [
+        {
+          "type":"str",
+          "required":true,
+          "title":"Phone number"
+        }
+      ]
+    }
+  }
+}

File schema/addressbookschema.json

-{
-  "type":"seq",
-  "title":"Address Book",
-  "sequence":
-  [
-    {
-      "type":"map",
-      "title":"Address Book Entry",
-      "mapping":
-      {
-        "firstName":
-        {
-          "type":"str",
-          "title":"First Name",
-          "required":true
-        },
-        "lastName":
-        {
-          "type":"str",
-          "title":"Last Name",
-          "required":true
-        },
-        "organizations":
-        {
-          "type":"seq",
-          "title":"Affiliations",
-          "sequence":
-          [
-            {
-              "type":"map",
-              "required":true,
-              "mapping":
-              {
-                "name":
-                {
-                  "type":"str",
-                  "required":true,
-                  "title":"Organization Name"
-                },
-                "title":
-                {
-                  "type":"str",
-                  "title":"Title/Role"
-                },
-                "department":
-                {
-                  "type":"str",
-                  "title":"Department"
-                },
-                "type":
-                {
-                  "type":"str",
-                  "enum":
-                  [
-                    "job",
-                    "school",
-                    "other"
-                  ],
-                  "title":"Type"
-                }
-              },
-              "title":"Organization"
-            }
-          ]
-        },
-        "addresses":
-        {
-          "type":"seq",
-          "title":"Addresses",
-          "sequence":
-          [
-            {
-              "type":"map",
-              "title":"Address",
-              "required":true,
-              "mapping":
-              {
-                "streetAddress":
-                {
-                  "type":"str",
-                  "title":"Street Address",
-                  "required":true
-                },
-                "locality":
-                {
-                  "type":"str",
-                  "title":"City (locality)",
-                  "required":true
-                },
-                "region":
-                {
-                  "type":"str",
-                  "title":"State/Provence/Region",
-                  "required":true
-                },
-                "postalCode":
-                {
-                  "type":"str",
-                  "title":"Postal (Zip) Code",
-                  "required":true
-                },
-                "country":
-                {
-                  "type":"str",
-                  "title":"Country",
-                  "required":true
-                }
-              }
-            }
-          ]
-        },
-        "phoneNumbers":
-        {
-          "type":"seq",
-          "title":"Phone number(s)",
-          "sequence":
-          [
-            {
-              "type":"map",
-              "title":"Phone number",
-              "required":true,
-              "mapping":
-              {
-                "workhomeother":
-                {
-                  "type":"str",
-                  "required":true,
-                  "enum":
-                  [
-                    "Work",
-                    "Home",
-                    "Mobile",
-                    "Fax",
-                    "Other"
-                  ],
-                  "title":"Work/Home/Other"
-                },
-                "Number":
-                {
-                  "type":"str",
-                  "required":true
-                },
-                "comment":
-                {
-                  "type":"str",
-                  "title":"Comment"
-                }
-              }
-            }
-          ]
-        },
-        "emails":
-        {
-          "type":"seq",
-          "title":"Email addresses",
-          "sequence":
-          [
-            {
-              "type":"map",
-              "title":"Email",
-              "required":true,
-              "mapping":
-              {
-                "type":
-                {
-                  "type":"str",
-                  "required":true,
-                  "title":"Work/Home/Other",
-                  "enum":
-                  [
-                    "work",
-                    "home",
-                    "other"
-                  ]
-                },
-                "value":
-                {
-                  "type":"str",
-                  "required":true,
-                  "title":"Address"
-                },
-                "primary":
-                {
-                  "type":"bool",
-                  "required":true,
-                  "title":"Primary?"
-                }
-              }
-            }
-          ]
-        },
-        "ims":
-        {
-          "type":"seq",
-          "title":"IM Accounts",
-          "sequence":
-          [
-            {
-              "type":"map",
-              "title":"Account",
-              "required":true,
-              "mapping":
-              {
-                "type":
-                {
-                  "type":"str",
-                  "required":true,
-                  "title":"Service",
-                  "enum":
-                  [
-                    "aim",
-                    "gtalk",
-                    "icq",
-                    "xmpp",
-                    "msn",
-                    "skype",
-                    "qq",
-                    "yahoo"
-                  ],
-                  "desc_enum":
-                  {
-                    "aim":"AIM (AOL)",
-                    "gtalk":"GTalk (Google)",
-                    "icq":"ICQ",
-                    "xmpp":"XMPP/Jabber",
-                    "msn":"MSN (Microsoft)",
-                    "skype":"Skype",
-                    "qq":"QQ (Tencent)",
-                    "yahoo":"Yahoo!"
-                  }
-                },
-                "value":
-                {
-                  "type":"str",
-                  "required":true,
-                  "title":"Account ID"
-                }
-              }
-            }
-          ]
-        }
-      }
-    }
-  ]
-}

File schema/datatype-example-schema.json

-{
-  "type":"seq",
-  "title":"Datatype example schema (datatype-example-schema.json)\n  Not terribly useful for anything other than demonstration purposes.  Edit one\n  of the existing schemas for your purpose, or generate one using the online\n  tool at http://robla.net/2005/jsonwidget\nData",
-  "desc":"Example with all datatypes in one place",
-  "sequence":
-  [
-    {
-      "type":"map",
-      "title":"Item",
-      "mapping":
-      {
-        "map_example":
-        {
-          "type":"map",
-          "title":"Map (map)",
-          "mapping":
-          {
-            "field1":
-            {
-              "type":"str"
-            },
-            "field2":
-            {
-              "type":"str"
-            }
-          }
-        },
-        "seq_example":
-        {
-          "type":"seq",
-          "title":"Sequence (seq)",
-          "sequence":
-          [
-            {
-              "title":"Item",
-              "type":"str"
-            }
-          ]
-        },
-        "num_example":
-        {
-          "title":"Number (num)",
-          "type":"number"
-        },
-        "int_example":
-        {
-          "title":"Integer (int)",
-          "type":"int"
-        },
-        "str_example":
-        {
-          "title":"String (str)",
-          "type":"str"
-        },
-        "bool_example":
-        {
-          "title":"Boolean (bool)",
-          "type":"bool"
-        },
-        "str_enum_example":
-        {
-          "title":"String Enumeration (str + enum)",
-          "type":"str",
-          "enum":
-          [
-            "value1",
-            "value2",
-            "value3"
-          ]
-        }
-      }
-    }
-  ]
-}

File schema/pasterschema.json

-{
-  "type":"map",
-  "mapping":
-  {
-    "project_name":
-    {
-      "type":"str",
-      "title":"Project name"
-    },
-    "version":
-    {
-      "type":"str",
-      "title":"Version"
-    },
-    "description":
-    {
-      "type":"str",
-      "title":"Description (one liner)"
-    },
-    "long_description":
-    {
-      "type":"str",
-      "title":"Long description (multiline reStructuredText)"
-    },
-    "keywords":
-    {
-      "type":"str",
-      "title":"Keywords (space separated)"
-    },
-    "author":
-    {
-      "type":"str",
-      "title":"Author"
-    },
-    "author_email":
-    {
-      "type":"str",
-      "title":"Author email"
-    },
-    "url":
-    {
-      "type":"str",
-      "title":"URL to project page"
-    },
-    "license_name":
-    {
-      "type":"str",
-      "title":"License name"
-    },
-    "zip_safe":
-    {
-      "type":"bool",
-      "title":"Zip safe?"
-    }
-  }
-}

File schema/simpleaddr-schema.json

-{
-  "type":"map",
-  "title":"Address Book Entry",
-  "mapping":
-  {
-    "firstName":
-    {
-      "type":"str",
-      "title":"First Name",
-      "required":true
-    },
-    "lastName":
-    {
-      "type":"str",
-      "title":"Last Name",
-      "required":true
-    },
-    "streetAddress":
-    {
-      "type":"str",
-      "title":"Street Address",
-      "required":true
-    },
-    "city":
-    {
-      "type":"str",
-      "title":"City",
-      "required":true
-    },
-    "state":
-    {
-      "type":"str",
-      "title":"State",
-      "required":true
-    },
-    "postalCode":
-    {
-      "type":"str",
-      "title":"Postal (Zip) Code",
-      "required":true
-    },
-    "phoneNumbers":
-    {
-      "type":"seq",
-      "title":"Phone numbers",
-      "required":true,
-      "sequence":
-      [
-        {
-          "type":"str",
-          "required":true,
-          "title":"Phone number"
-        }
-      ]
-    }
-  }
-}
       author_email='robla@robla.net',
       url='http://robla.net/jsonwidget',
       license='BSD',
-      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+      packages=['jsonwidget'],
       include_package_data=True,
       zip_safe=False,
       install_requires=["urwid",
                          'jsonaddress = jsonwidget.commands:jsonaddress'
                         ]
                    },
+      package_data={'schema':['schema/*.json']},
       )