def test_multiple_schemas(self): s1 = { "required": [ "a", "c" ], "type": "object", "properties": { "a": { "type": "string" }, "c": { "items": [ { "type": "integer" } ], "type": "array" } } } s2 = { "required": [ "c" ], "type": "object", "properties": { "a": { "type": "string" }, "c": { "items": [ { "type": "integer" } ], "type": "array" } } } s = Schema() s.add_schema(s1) s.add_schema(s2) self.assertEqual(s.to_dict(), s2)
def test_multitype_merge(self): s = Schema().add_object([1, "2", None, False]) self.assertEqual(s.to_dict(), { "type": "array", "items": [{ "type": ["boolean", "integer", "null", "string"]}] })
def test_three_deep(self): s = Schema().add_object( {"matryoshka": { "design": { "principle": "FTW!" } }}) self.assertEqual( s.to_dict(), { "required": ["matryoshka"], "type": "object", "properties": { "matryoshka": { "required": ["design"], "type": "object", "properties": { "design": { "required": ["principle"], "type": "object", "properties": { "principle": { "type": "string" } } } } } } })
def test_object_in_array(self): s = Schema().add_object([{ "name": "Sir Lancelot of Camelot", "quest": "to seek the Holy Grail", "favorite colour": "blue" }, { "name": "Sir Robin of Camelot", "quest": "to seek the Holy Grail", "capitol of Assyria": None }]) self.assertEqual( s.to_dict(), { "items": [{ "required": ["name", "quest"], "type": "object", "properties": { "quest": { "type": "string" }, "name": { "type": "string" }, "favorite colour": { "type": "string" }, "capitol of Assyria": { "type": "null" } } }], "type": "array" })
def test_object_in_array(self): s = Schema().add_object([ {"name": "Sir Lancelot of Camelot", "quest": "to seek the Holy Grail", "favorite colour": "blue"}, {"name": "Sir Robin of Camelot", "quest": "to seek the Holy Grail", "capitol of Assyria": None}]) self.assertEqual(s.to_dict(), { "items": [ { "required": [ "name", "quest" ], "type": "object", "properties": { "quest": { "type": "string" }, "name": { "type": "string" }, "favorite colour": { "type": "string" }, "capitol of Assyria": { "type": "null" } } } ], "type": "array" })
def test_monotype(self): s = Schema().add_object(["spam", "spam", "spam", "egg", "spam"]) self.assertEqual(s.to_dict(), { "type": "array", "items": [{ "type": "string" }] })
def test_multitype_merge(self): s = Schema().add_object([1, "2", None, False]) self.assertEqual( s.to_dict(), { "type": "array", "items": [{ "type": ["boolean", "integer", "null", "string"] }] })
def test_multitype_sep(self): s = Schema(merge_arrays=False).add_object([1, "2", None, False]) self.assertEqual(s.to_dict(), { "type": "array", "items": [ {"type": "integer"}, {"type": "string"}, {"type": "null"}, {"type": "boolean"}] })
def test_multi_type(self): s = Schema() s.add_object('string') s.add_object(1.1) s.add_object(True) s.add_object(None) self.assertEqual(s.to_dict(), {'type': ['boolean', 'null', 'number', 'string']})
def test_array_reduce(self): s = Schema().add_object([["surprise"], ["fear", "surprise"], ["fear", "surprise", "ruthless efficiency"], ["fear", "surprise", "ruthless efficiency", "an almost fanatical devotion to the Pope"]]) self.assertEqual(s.to_dict(), { "type": "array", "items": [{ "type": "array", "items": [{"type": "string"}]}] })
def test_basic_object(self): s = Schema().add_object({ "Red Windsor": "Normally, but today the van broke down.", "Stilton": "Sorry.", "Gruyere": False}) self.assertEqual(s.to_dict(), { "required": ["Gruyere", "Red Windsor", "Stilton"], "type": "object", "properties": { "Red Windsor": {"type": "string"}, "Gruyere": {"type": "boolean"}, "Stilton": {"type": "string"}} })
def test_multi_type(self): s = Schema() s.add_object("string") s.add_object(1.1) s.add_object(True) s.add_object(None) self.assertEqual(s.to_dict(), {"type": ["boolean", "null", "number", "string"]})
def test_three_deep(self): s = Schema().add_object( {"matryoshka": {"design": {"principle": "FTW!"}}}) self.assertEqual(s.to_dict(), { "required": ["matryoshka"], "type": "object", "properties": {"matryoshka": { "required": ["design"], "type": "object", "properties": {"design": { "required": ["principle"], "type": "object", "properties": {"principle": {"type": "string"}}}} }} })
def test_multi_type(self): s = Schema() s.add_object('string') s.add_object(1.1) s.add_object(True) s.add_object(None) self.assertSchema(s.to_dict(), {'type': ['boolean', 'null', 'number', 'string']})
def test_multitype_sep(self): s = Schema(merge_arrays=False).add_object([1, "2", None, False]) self.assertEqual( s.to_dict(), { "type": "array", "items": [{ "type": "integer" }, { "type": "string" }, { "type": "null" }, { "type": "boolean" }] })
def test_array_reduce(self): s = Schema().add_object([["surprise"], ["fear", "surprise"], ["fear", "surprise", "ruthless efficiency"], [ "fear", "surprise", "ruthless efficiency", "an almost fanatical devotion to the Pope" ]]) self.assertEqual( s.to_dict(), { "type": "array", "items": [{ "type": "array", "items": [{ "type": "string" }] }] })
def test_array_in_object(self): s = Schema().add_object({"a": "b", "c": [1, 2, 3]}) self.assertEqual( s.to_dict(), { "required": ["a", "c"], "type": "object", "properties": { "a": { "type": "string" }, "c": { "items": [{ "type": "integer" }], "type": "array" } } })
def test_array_in_object(self): s = Schema().add_object({"a": "b", "c": [1, 2, 3]}) self.assertEqual(s.to_dict(), { "required": [ "a", "c" ], "type": "object", "properties": { "a": { "type": "string" }, "c": { "items": [ { "type": "integer" } ], "type": "array" } } })
def test_basic_object(self): s = Schema().add_object({ "Red Windsor": "Normally, but today the van broke down.", "Stilton": "Sorry.", "Gruyere": False }) self.assertEqual( s.to_dict(), { "required": ["Gruyere", "Red Windsor", "Stilton"], "type": "object", "properties": { "Red Windsor": { "type": "string" }, "Gruyere": { "type": "boolean" }, "Stilton": { "type": "string" } } })
class SchemaTestCase(unittest.TestCase): def setUp(self): self._schema = Schema() self._objects = [] self._schemas = [] def set_schema_options(self, **options): self._schema = Schema(**options) def add_object(self, obj): self._schema.add_object(obj) self._objects.append(obj) def add_schema(self, schema): self._schema.add_schema(schema) self._schemas.append(schema) def assertObjectValidates(self, obj): jsonschema.Draft4Validator(self._schema.to_dict()).validate(obj) def assertObjectDoesNotValidate(self, obj): with self.assertRaises(jsonschema.exceptions.ValidationError): jsonschema.Draft4Validator(self._schema.to_dict()).validate(obj) def assertResult(self, expected): self.assertEqual(self._schema.to_dict(), expected) self.assertUserContract() def assertUserContract(self): self._assertSchemaIsValid() self._assertComponentObjectsValidate() def _assertSchemaIsValid(self): jsonschema.Draft4Validator.check_schema(self._schema.to_dict()) def _assertComponentObjectsValidate(self): compiled_schema = self._schema.to_dict() for obj in self._objects: jsonschema.Draft4Validator(compiled_schema).validate(obj)
def test_boolean(self): s = Schema().add_object(True) self.assertEqual(s.to_dict(), {"type": "boolean"})
def __init__(self, filename=None): self.__yaml = "" self.__json = "" self.__genson = Schema() if filename: self.open(filename)
def test_single_type_unicode(self): schema = {u'type': u'string'} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_preserves_existing_keys(self): schema = {'type': 'number', 'value': 5} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_empty(self): s = Schema().add_object([]) self.assertEqual(s.to_dict(), {"type": "array", "items": []})
def test_preserves_existing_keys(self): schema = {'type': 'number', 'value': 5} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_empty_object(self): s = Schema().add_object({}) self.assertEqual(s.to_dict(), {"type": "object", "properties": {}})
def test_single_type(self): s = Schema() s.add_object('bacon') s.add_object('egg') s.add_object('spam') self.assertSchema(s.to_dict(), {'type': 'string'})
import sys import json from genson import Schema if __name__ == "__main__": fctypeMap = {} with open("packetLog.txt", encoding="utf-8") as log: for line in log: # Strip off the timestamp and filename line = line[38:] packet = json.loads(line) if (packet["FCType"] == "MANAGELIST" or packet["FCType"] == "TAGS" or packet["FCType"] == "ROOMDATA"): # These packet types are too diverse # or overly unique and generate schemas # that are not particularly useful continue schema = fctypeMap.setdefault(packet["FCType"], Schema()) sMessage = packet.setdefault("sMessage", None) schema.add_object(sMessage) full_schema = "{" for k, v in fctypeMap.items(): full_schema += '"{}": {},'.format(k, v.to_json()) full_schema = full_schema[:-1] full_schema += "}" with open("packetLogSchema.json", "w") as output: output.write(json.dumps(json.loads(full_schema), sort_keys=True, indent=4))
def test_number(self): s = Schema().add_object(1.1) self.assertEqual(s.to_dict(), {"type": "number"})
def test_monotype(self): s = Schema().add_object(["spam", "spam", "spam", "egg", "spam"]) self.assertEqual(s.to_dict(), {"type": "array", "items": [{"type": "string"}]})
def test_recurse_deprecation_warning(self): with self.assertWarns(DeprecationWarning): builder = Schema() builder.add_object('Go away or I shall taunt you a second time!') builder.to_dict(recurse=True)
def set_schema_options(self, **options): self._schema = Schema(**options)
def test_redundant_integer_type(self): s = Schema() s.add_object(1) s.add_object(1.1) self.assertSchema(s.to_dict(), {'type': 'number'})
def setUp(self): self._schema = Schema() self._objects = [] self._schemas = []
def test_multi_type(self): schema = {'type': ['boolean', 'null', 'number', 'string']} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_single_type(self): schema = {'type': 'string'} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_no_schema(self): schema = {} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_single_type(self): schema = {'type': 'string'} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_multi_type(self): schema = {'type': ['boolean', 'null', 'number', 'string']} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_to_dict_pending_deprecation_warning(self): with self.assertWarns(PendingDeprecationWarning): builder = Schema() with self.assertWarns(PendingDeprecationWarning): builder.add_object('I fart in your general direction!') builder.to_dict()
def _new_schema(self, value): return Schema().add_object(value).to_dict()
class Yaml2JsonSchema: schemas = list() stack = list() cur = str() def __init__(self, filename=None): self.__yaml = "" self.__json = "" self.__genson = Schema() if filename: self.open(filename) def is_valid(self, value): try: validate(value, Json.loads(self.yaml_to_jsonschema())) sys.stdout.write("OK\n") except ValidationError as ve: sys.stderr.write("Validation Error\n") sys.stderr.write(str(ve) + "\n") return False return True def to_jsonschema(self): return self.__genson.to_json(indent=2) def to_jsonloads(self): return Json.loads(self.json) def yaml_to_jsonschema(self): return self.ex_yaml(self.yaml.split('\n')) @property def json(self): return self.__json @property def yaml(self): return self.__yaml @yaml.setter def yaml(self, yaml): self.__yaml = yaml json = Yaml.load(self.yaml) self.__json = Json.dumps(json, indent=2) self.__genson.add_object(json) def open(self, filename): with open(filename, 'r') as f: data = f.read() self.yaml = data def ex_yaml(self, v): schema_list = Yaml2JsonSchema.yaml_extention_parser(v) for s in schema_list: self.__genson.add_schema(s) return self.to_jsonschema() @staticmethod def make_json_schema(v, value, buf=None): if buf is None: buf = dict() if not v: buf.update({"properties": value}) return buf name = v.pop(0) buf.update({"properties": {name: dict()}}) return Yaml2JsonSchema.make_json_schema( v, value, buf["properties"][name]) @staticmethod def yaml_extention_parser(v, depth=0): # 데이터 아님 while True: if not v: return Yaml2JsonSchema.schemas d = v.pop(0) if -1 == d.find(":"): continue break # 하위 아이템 존재 if ":" == d[-1]: len_current = len(d) - len(d.lstrip()) # 하위 아이템 if depth <= len_current: depth = len_current name = d[:-1].strip() Yaml2JsonSchema.stack.append(name) # 상위로 돌아감 elif depth > len_current: depth = len_current Yaml2JsonSchema.stack.pop() # 같은 레벨 else: pass # 확장 코드 elif -1 < d.find("#! "): info = d.strip()[3:] name = info[:info.find(":")] value = info[info.find(":") + 2:] if value.isdigit(): value = int(value) if value.isdecimal() else float(value) value = {Yaml2JsonSchema.cur: {name: value}} buf = dict() cs = copy.deepcopy(Yaml2JsonSchema.stack) temp = buf Yaml2JsonSchema.make_json_schema(cs, value, temp) Yaml2JsonSchema.schemas.append(buf) else: d = d.strip() name = d[:d.find(":")] Yaml2JsonSchema.cur = name return Yaml2JsonSchema.yaml_extention_parser(v, depth)
def test_single_type(self): s = Schema() s.add_object("bacon") s.add_object("egg") s.add_object("spam") self.assertEqual(s.to_dict(), {"type": "string"})
def test_null(self): s = Schema().add_object(None) self.assertEqual(s.to_dict(), {"type": "null"})
def test_redundant_integer_type(self): s = Schema() s.add_object(1) s.add_object(1.1) self.assertEqual(s.to_dict(), {"type": "number"})
def test_no_schema(self): schema = {} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_integer(self): s = Schema().add_object(1) self.assertEqual(s.to_dict(), {"type": "integer"})
def test_single_type_unicode(self): schema = {u'type': u'string'} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def assertGenSchema(self, instance, options, expected): actual = Schema(**options).add_object(instance).to_dict() self.assertSchema(actual, expected) self.assertObjectValid(instance, actual) return actual