-class Error(Exception):
- def __init__(self, msg):
- Exception.__init__(self)
- self.msg = msg
-
-def getMember(json, name, validTypes, description, default=None):
- if name in json:
- member = json[name]
- if type(member) not in validTypes:
- raise Error("%s: type mismatch for '%s' member"
- % (description, name))
- return member
- return default
-
-def mustGetMember(json, name, expectedType, description):
- member = getMember(json, name, expectedType, description)
- if member == None:
- raise Error("%s: missing '%s' member" % (description, name))
- return member
-
-class DbSchema:
- def __init__(self, name, comment, tables, idlPrefix, idlHeader):
- self.name = name
- self.comment = comment
- self.tables = tables
- self.idlPrefix = idlPrefix
- self.idlHeader = idlHeader
-
- @staticmethod
- def fromJson(json):
- name = mustGetMember(json, 'name', [unicode], 'database')
- comment = getMember(json, 'comment', [unicode], 'database')
- tablesJson = mustGetMember(json, 'tables', [dict], 'database')
- tables = {}
- for name, tableJson in tablesJson.iteritems():
- tables[name] = TableSchema.fromJson(tableJson, "%s table" % name)
- idlPrefix = mustGetMember(json, 'idlPrefix', [unicode], 'database')
- idlHeader = mustGetMember(json, 'idlHeader', [unicode], 'database')
- return DbSchema(name, comment, tables, idlPrefix, idlHeader)
-
- def toJson(self):
- d = {"name": self.name,
- "tables": {}}
- for name, table in self.tables.iteritems():
- d["tables"][name] = table.toJson()
- if self.comment != None:
- d["comment"] = self.comment
- return d
-
-class TableSchema:
- def __init__(self, comment, columns):
- self.comment = comment
- self.columns = columns
-
- @staticmethod
- def fromJson(json, description):
- comment = getMember(json, 'comment', [unicode], description)
- columnsJson = mustGetMember(json, 'columns', [dict], description)
- columns = {}
- for name, json in columnsJson.iteritems():
- columns[name] = ColumnSchema.fromJson(
- json, "column %s in %s" % (name, description))
- return TableSchema(comment, columns)
-
- def toJson(self):
- d = {"columns": {}}
- for name, column in self.columns.iteritems():
- d["columns"][name] = column.toJson()
- if self.comment != None:
- d["comment"] = self.comment
- return d
-
-class ColumnSchema:
- def __init__(self, comment, type, persistent):
- self.comment = comment
- self.type = type
- self.persistent = persistent
-
- @staticmethod
- def fromJson(json, description):
- comment = getMember(json, 'comment', [unicode], description)
- type = Type.fromJson(mustGetMember(json, 'type', [dict, unicode],
- description),
- 'type of %s' % description)
- ephemeral = getMember(json, 'ephemeral', [bool], description)
- persistent = ephemeral != True
- return ColumnSchema(comment, type, persistent)
-
- def toJson(self):
- d = {"type": self.type.toJson()}
- if self.persistent == False:
- d["ephemeral"] = True
- if self.comment != None:
- d["comment"] = self.comment
- return d
-
-class Type:
- def __init__(self, key, keyRefTable=None, value=None, valueRefTable=None,
- min=1, max=1):
- self.key = key
- self.keyRefTable = keyRefTable
- self.value = value
- self.valueRefTable = valueRefTable
- self.min = min
- self.max = max
-
- @staticmethod
- def fromJson(json, description):
- if type(json) == unicode:
- return Type(json)
- else:
- key = mustGetMember(json, 'key', [unicode], description)
- keyRefTable = getMember(json, 'keyRefTable', [unicode], description)
- value = getMember(json, 'value', [unicode], description)
- valueRefTable = getMember(json, 'valueRefTable', [unicode], description)
- min = getMember(json, 'min', [int], description, 1)
- max = getMember(json, 'max', [int, unicode], description, 1)
- return Type(key, keyRefTable, value, valueRefTable, min, max)
-
- def toJson(self):
- if self.value == None and self.min == 1 and self.max == 1:
- return self.key
- else:
- d = {"key": self.key}
- if self.value != None:
- d["value"] = self.value
- if self.min != 1:
- d["min"] = self.min
- if self.max != 1:
- d["max"] = self.max
- return d
-