Update event schema parsing.

The templating system now parses event schemas into a form which
can be easily dumped into multiple tables (for nested object types)
This commit is contained in:
Kegan Dougal 2015-05-21 09:46:14 +01:00
parent 717ad190ab
commit 0899e0b772
5 changed files with 72 additions and 51 deletions

View file

@ -14,6 +14,7 @@
},
"answer": {
"type": "object",
"title": "Answer",
"description": "The session description object",
"properties": {
"type": {

View file

@ -15,6 +15,7 @@
},
"offer": {
"type": "object",
"title": "Offer",
"description": "The session description object",
"properties": {
"type": {

View file

@ -18,12 +18,14 @@
"users_default": { "type": "number" },
"events": {
"type": "object",
"title": "Event power levels",
"additionalProperties": {
"type": "number"
}
},
"users": {
"type": "object",
"title": "User power levels",
"additionalProperties": {
"type": "number"
}

View file

@ -27,36 +27,58 @@ def _load_schemas():
path = "../event-schemas/schema/v1"
schemata = {}
def format_for_obj(obj):
obj_type = "<%s>" % obj.get("type")
if obj_type == "<object>":
if obj.get("properties"):
format = {}
for key in obj.get("properties"):
format[key] = format_for_obj(obj.get("properties")[key])
return format
elif obj.get("additionalProperties"):
return {
"<string>": (
"<%s>" % obj.get("additionalProperties").get("type")
def get_content_fields(obj, enforce_title=False):
# Algorithm:
# f.e. property => add field info (if field is object then recurse)
if obj.get("type") != "object":
raise Exception(
"get_content_fields: Object %s isn't an object." % obj
)
if enforce_title and not obj.get("title"):
raise Exception(
"get_content_fields: Nested object %s doesn't have a title." % obj
)
required_keys = obj.get("required")
if not required_keys:
required_keys = []
fields = {
"title": obj.get("title"),
"rows": []
}
elif obj_type == "<array>" and obj.get("items"):
return [
format_for_obj(obj.get("items"))
]
tables = [fields]
enum_text = ""
# add on enum info
enum = obj.get("enum")
if enum:
if len(enum) > 1:
obj_type = "<enum>"
enum_text = " (" + "|".join(enum) + ")"
props = obj["properties"]
for key_name in props:
value_type = None
required = key_name in required_keys
desc = props[key_name].get("description")
if props[key_name]["type"] == "object":
if props[key_name].get("additionalProperties"):
# not "really" an object, just a KV store
value_type = (
"{string: %s}" %
props[key_name]["additionalProperties"]["type"]
)
else:
obj_type = enum[0]
nested_object = get_content_fields(
props[key_name],
enforce_title=True
)
value_type = nested_object[0]["title"]
tables += nested_object
else:
value_type = props[key_name]["type"]
return obj_type + enum_text
fields["rows"].append({
"key": key_name,
"type": value_type,
"required": required,
"desc": desc
})
return tables
for filename in os.listdir(path):
if not filename.startswith("m."):
@ -67,10 +89,17 @@ def _load_schemas():
schema = {
"typeof": None,
"type": None,
"summary": None,
"title": None,
"desc": None,
"json_format": None,
"required_keys": None
"content_fields": [
# {
# title: "<title> key"
# rows: [
# { key: <key_name>, type: <string>,
# desc: <desc>, required: <bool> }
# ]
# }
]
}
# add typeof
@ -87,18 +116,14 @@ def _load_schemas():
schema["type"] = prop(json_schema, "properties/type/enum")[0]
# add summary and desc
schema["summary"] = json_schema.get("title")
schema["title"] = json_schema.get("title")
schema["desc"] = json_schema.get("description")
# add json_format
content_props = prop(json_schema, "properties/content")
if content_props:
schema["json_format"] = format_for_obj(content_props)
# add required_keys
schema["required_keys"] = prop(
json_schema, "properties/content/required"
# walk the object for field info
schema["content_fields"] = get_content_fields(
prop(json_schema, "properties/content")
)
schemata[filename] = schema
return schemata

View file

@ -1,16 +1,8 @@
``{{event.type}}``
Summary:
{{event.summary | wrap(76) | indent(4)}}
Type:
{{event.typeof}}
Description:
{{event.desc | wrap(76) | indent(4)}}
Required Keys:
``{{event.required_keys | jsonify}}``
{{(4 + event.type | length) * '-'}}
{{event.desc}}
JSON Format::
{{event.json_format | jsonify(4, 4)}}
{{event.content_fields | jsonify(4,4)}}
Example::