Make the templating system work(!)
This commit is contained in:
parent
4e64d9e340
commit
7563f1058b
7 changed files with 124 additions and 12 deletions
|
@ -1,18 +1,101 @@
|
|||
"""Contains all the units for the spec."""
|
||||
from . import AccessKeyStore
|
||||
import json
|
||||
import os
|
||||
|
||||
def prop(obj, path):
|
||||
# Helper method to extract nested property values
|
||||
nested_keys = path.split("/")
|
||||
val = obj
|
||||
for key in nested_keys:
|
||||
val = val.get(key, {})
|
||||
return val
|
||||
|
||||
def _load_examples():
|
||||
path = "../event-schemas/examples"
|
||||
examples = {}
|
||||
for filename in os.listdir(path):
|
||||
with open(filename, "r") as f:
|
||||
print filename
|
||||
if not filename.startswith("m."):
|
||||
continue
|
||||
with open(os.path.join(path, filename), "r") as f:
|
||||
examples[filename] = json.loads(f.read())
|
||||
if filename == "m.room.message_m.text":
|
||||
examples["m.room.message"] = examples[filename]
|
||||
return examples
|
||||
|
||||
def _load_schemas():
|
||||
path = "../event-schemas/schema"
|
||||
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")
|
||||
)
|
||||
}
|
||||
enum_text = ""
|
||||
# add on enum info
|
||||
enum = obj.get("enum")
|
||||
if enum:
|
||||
if len(enum) > 1:
|
||||
obj_type = "<enum>"
|
||||
enum_text = " (" + "|".join(enum) + ")"
|
||||
else:
|
||||
obj_type = enum[0]
|
||||
|
||||
return obj_type + enum_text
|
||||
|
||||
for filename in os.listdir(path):
|
||||
if not filename.startswith("m."):
|
||||
continue
|
||||
with open(os.path.join(path, filename), "r") as f:
|
||||
json_schema = json.loads(f.read())
|
||||
schema = {
|
||||
"typeof": None,
|
||||
"type": None,
|
||||
"summary": None,
|
||||
"desc": None,
|
||||
"json_format": None
|
||||
}
|
||||
|
||||
# add typeof
|
||||
base_defs = {
|
||||
"core#/definitions/room_event": "Room Event",
|
||||
"core#/definitions/state_event": "State Event"
|
||||
}
|
||||
if type(json_schema.get("allOf")) == list:
|
||||
schema["typeof"] = base_defs.get(
|
||||
json_schema["allOf"][0].get("$ref")
|
||||
)
|
||||
|
||||
# add type
|
||||
schema["type"] = prop(json_schema, "properties/type/enum")[0]
|
||||
|
||||
# add summary and desc
|
||||
schema["summary"] = 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)
|
||||
|
||||
|
||||
schemata[filename] = schema
|
||||
return schemata
|
||||
|
||||
|
||||
UNIT_DICT = {
|
||||
"event-examples": _load_examples
|
||||
"event-examples": _load_examples,
|
||||
"event-schemas": _load_schemas
|
||||
}
|
||||
|
||||
|
||||
|
@ -20,5 +103,8 @@ def load():
|
|||
store = AccessKeyStore()
|
||||
for unit_key in UNIT_DICT:
|
||||
unit = UNIT_DICT[unit_key]()
|
||||
print "Generated unit '%s' : %s" % (
|
||||
unit_key, json.dumps(unit)[:50].replace("\n","")
|
||||
)
|
||||
store.add(unit_key, unit)
|
||||
return store
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue