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": { "answer": {
"type": "object", "type": "object",
"title": "Answer",
"description": "The session description object", "description": "The session description object",
"properties": { "properties": {
"type": { "type": {

View file

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

View file

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

View file

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

View file

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