|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
"""Contains all the units for the spec."""
|
|
|
|
|
from . import AccessKeyStore
|
|
|
|
|
import inspect
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
@ -12,7 +13,31 @@ def prop(obj, path):
|
|
|
|
|
val = val.get(key, {})
|
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
def _load_common_event_fields():
|
|
|
|
|
class Units(object):
|
|
|
|
|
|
|
|
|
|
def __init__(self, debug=False):
|
|
|
|
|
self.debug = debug
|
|
|
|
|
|
|
|
|
|
def log(self, text):
|
|
|
|
|
if self.debug:
|
|
|
|
|
print text
|
|
|
|
|
|
|
|
|
|
def get_units(self, debug=False):
|
|
|
|
|
unit_list = inspect.getmembers(self, predicate=inspect.ismethod)
|
|
|
|
|
unit_dict = {}
|
|
|
|
|
for (func_name, func) in unit_list:
|
|
|
|
|
if not func_name.startswith("load_"):
|
|
|
|
|
continue
|
|
|
|
|
unit_key = func_name[len("load_"):]
|
|
|
|
|
unit_dict[unit_key] = func()
|
|
|
|
|
self.log("Generated unit '%s' : %s" % (
|
|
|
|
|
unit_key, json.dumps(unit_dict[unit_key])[:50].replace(
|
|
|
|
|
"\n",""
|
|
|
|
|
)
|
|
|
|
|
))
|
|
|
|
|
return unit_dict
|
|
|
|
|
|
|
|
|
|
def load_common_event_fields(self):
|
|
|
|
|
path = "../event-schemas/schema/v1/core"
|
|
|
|
|
event_types = {}
|
|
|
|
|
with open(path, "r") as f:
|
|
|
|
@ -34,7 +59,7 @@ def _load_common_event_fields():
|
|
|
|
|
event_types[event_type] = table
|
|
|
|
|
return event_types
|
|
|
|
|
|
|
|
|
|
def _load_examples():
|
|
|
|
|
def load_event_examples(self):
|
|
|
|
|
path = "../event-schemas/examples/v1"
|
|
|
|
|
examples = {}
|
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
@ -46,7 +71,7 @@ def _load_examples():
|
|
|
|
|
examples["m.room.message"] = examples[filename]
|
|
|
|
|
return examples
|
|
|
|
|
|
|
|
|
|
def _load_schemas():
|
|
|
|
|
def load_event_schemas(self):
|
|
|
|
|
path = "../event-schemas/schema/v1"
|
|
|
|
|
schemata = {}
|
|
|
|
|
|
|
|
|
@ -121,7 +146,7 @@ def _load_schemas():
|
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
|
if not filename.startswith("m."):
|
|
|
|
|
continue
|
|
|
|
|
print "Reading %s" % os.path.join(path, filename)
|
|
|
|
|
self.log("Reading %s" % os.path.join(path, filename))
|
|
|
|
|
with open(os.path.join(path, filename), "r") as f:
|
|
|
|
|
json_schema = json.loads(f.read())
|
|
|
|
|
schema = {
|
|
|
|
@ -173,7 +198,7 @@ def _load_schemas():
|
|
|
|
|
schemata[filename] = schema
|
|
|
|
|
return schemata
|
|
|
|
|
|
|
|
|
|
def _load_git_ver():
|
|
|
|
|
def load_git_version(self):
|
|
|
|
|
null = open(os.devnull, 'w')
|
|
|
|
|
cwd = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
try:
|
|
|
|
@ -221,19 +246,11 @@ def _load_git_ver():
|
|
|
|
|
return git_version.encode("ascii")
|
|
|
|
|
return "Unknown rev"
|
|
|
|
|
|
|
|
|
|
UNIT_DICT = {
|
|
|
|
|
"event-examples": _load_examples,
|
|
|
|
|
"event-schemas": _load_schemas,
|
|
|
|
|
"common-event-fields": _load_common_event_fields,
|
|
|
|
|
"git-version": _load_git_ver
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
units = Units()
|
|
|
|
|
unit_dict = units.get_units()
|
|
|
|
|
for unit_key in unit_dict:
|
|
|
|
|
store.add(unit_key, unit_dict[unit_key])
|
|
|
|
|
return store
|
|
|
|
|