python: Accept multiple forms of strings and lists when parsing JSON.
[sliver-openvswitch.git] / python / ovs / db / schema.py
index 9c453df..1c474a8 100644 (file)
@@ -19,6 +19,12 @@ from ovs.db import error
 import ovs.db.parser
 from ovs.db import types
 
+def _check_id(name, json):
+    if name.startswith('_'):
+        raise error.Error('names beginning with "_" are reserved', json)
+    elif not ovs.db.parser.is_identifier(name):
+        raise error.Error("name must be a valid id", json)
+
 class DbSchema(object):
     """Schema for an OVSDB database."""
 
@@ -58,23 +64,19 @@ class DbSchema(object):
     def from_json(json):
         parser = ovs.db.parser.Parser(json, "database schema")
         name = parser.get("name", ['id'])
-        version = parser.get_optional("version", [unicode])
-        parser.get_optional("cksum", [unicode])
+        version = parser.get_optional("version", [str, unicode])
+        parser.get_optional("cksum", [str, unicode])
         tablesJson = parser.get("tables", [dict])
         parser.finish()
 
         if (version is not None and
             not re.match('[0-9]+\.[0-9]+\.[0-9]+$', version)):
-            raise error.Error("schema version \"%s\" not in format x.y.z"
+            raise error.Error('schema version "%s" not in format x.y.z'
                               % version)
 
         tables = {}
         for tableName, tableJson in tablesJson.iteritems():
-            if tableName.startswith('_'):
-                raise error.Error("names beginning with \"_\" are reserved",
-                                  json)
-            elif not ovs.db.parser.is_identifier(tableName):
-                raise error.Error("name must be a valid id", json)
+            _check_id(tableName, json)
             tables[tableName] = TableSchema.from_json(tableJson, tableName)
 
         return DbSchema(name, version, tables)
@@ -123,8 +125,8 @@ class IdlSchema(DbSchema):
     @staticmethod
     def from_json(json):
         parser = ovs.db.parser.Parser(json, "IDL schema")
-        idlPrefix = parser.get("idlPrefix", [unicode])
-        idlHeader = parser.get("idlHeader", [unicode])
+        idlPrefix = parser.get("idlPrefix", [str, unicode])
+        idlHeader = parser.get("idlHeader", [str, unicode])
 
         subjson = dict(json)
         del subjson["idlPrefix"]
@@ -165,7 +167,7 @@ class TableSchema(object):
     @staticmethod
     def from_json(json, name):
         parser = ovs.db.parser.Parser(json, "table schema for table %s" % name)
-        columnsJson = parser.get("columns", [dict])
+        columns_json = parser.get("columns", [dict])
         mutable = parser.get_optional("mutable", [bool], True)
         max_rows = parser.get_optional("maxRows", [int])
         is_root = parser.get_optional("isRoot", [bool], False)
@@ -177,18 +179,14 @@ class TableSchema(object):
         elif max_rows <= 0:
             raise error.Error("maxRows must be at least 1", json)
 
-        if not columnsJson:
+        if not columns_json:
             raise error.Error("table must have at least one column", json)
 
         columns = {}
-        for columnName, columnJson in columnsJson.iteritems():
-            if columnName.startswith('_'):
-                raise error.Error("names beginning with \"_\" are reserved",
-                                  json)
-            elif not ovs.db.parser.is_identifier(columnName):
-                raise error.Error("name must be a valid id", json)
-            columns[columnName] = ColumnSchema.from_json(columnJson,
-                                                         columnName)
+        for column_name, column_json in columns_json.iteritems():
+            _check_id(column_name, json)
+            columns[column_name] = ColumnSchema.from_json(column_json,
+                                                          column_name)
 
         indexes = []
         for index_json in indexes_json:
@@ -239,11 +237,11 @@ class TableSchema(object):
         return json
 
 class ColumnSchema(object):
-    def __init__(self, name, mutable, persistent, type):
+    def __init__(self, name, mutable, persistent, type_):
         self.name = name
         self.mutable = mutable
         self.persistent = persistent
-        self.type = type
+        self.type = type_
         self.unique = False
 
     @staticmethod
@@ -251,10 +249,10 @@ class ColumnSchema(object):
         parser = ovs.db.parser.Parser(json, "schema for column %s" % name)
         mutable = parser.get_optional("mutable", [bool], True)
         ephemeral = parser.get_optional("ephemeral", [bool], False)
-        type = types.Type.from_json(parser.get("type", [dict, unicode]))
+        type_ = types.Type.from_json(parser.get("type", [dict, str, unicode]))
         parser.finish()
 
-        return ColumnSchema(name, mutable, not ephemeral, type)
+        return ColumnSchema(name, mutable, not ephemeral, type_)
 
     def to_json(self):
         json = {"type": self.type.to_json()}