mirror of
https://github.com/matrix-org/matrix-spec
synced 2025-12-24 01:58:36 +01:00
Start encapsulating Units too. Add debug option which controls logging.
This commit is contained in:
parent
96671ce833
commit
8e1d6899c2
|
|
@ -10,14 +10,33 @@ class Sections(object):
|
||||||
e.g. "render_room_events" has the section key "room_events"
|
e.g. "render_room_events" has the section key "room_events"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, env, units):
|
def __init__(self, env, units, debug=False):
|
||||||
self.env = env
|
self.env = env
|
||||||
self.units = units
|
self.units = units
|
||||||
|
self.debug = debug
|
||||||
|
|
||||||
|
def log(self, text):
|
||||||
|
if self.debug:
|
||||||
|
print text
|
||||||
|
|
||||||
|
def get_sections(self):
|
||||||
|
render_list = inspect.getmembers(self, predicate=inspect.ismethod)
|
||||||
|
section_dict = {}
|
||||||
|
for (func_name, func) in render_list:
|
||||||
|
if not func_name.startswith("render_"):
|
||||||
|
continue
|
||||||
|
section_key = func_name[len("render_"):]
|
||||||
|
section = func()
|
||||||
|
section_dict[section_key] = section
|
||||||
|
self.log("Generated section '%s' : %s" % (
|
||||||
|
section_key, section[:60].replace("\n","")
|
||||||
|
))
|
||||||
|
return section_dict
|
||||||
|
|
||||||
def render_room_events(self):
|
def render_room_events(self):
|
||||||
template = self.env.get_template("events.tmpl")
|
template = self.env.get_template("events.tmpl")
|
||||||
examples = self.units.get("event-examples")
|
examples = self.units.get("event_examples")
|
||||||
schemas = self.units.get("event-schemas")
|
schemas = self.units.get("event_schemas")
|
||||||
sections = []
|
sections = []
|
||||||
for event_name in sorted(schemas):
|
for event_name in sorted(schemas):
|
||||||
if not event_name.startswith("m.room"):
|
if not event_name.startswith("m.room"):
|
||||||
|
|
@ -30,11 +49,11 @@ class Sections(object):
|
||||||
|
|
||||||
# pass through git ver so it'll be dropped in the input file
|
# pass through git ver so it'll be dropped in the input file
|
||||||
def render_git_version(self):
|
def render_git_version(self):
|
||||||
return self.units.get("git-version")
|
return self.units.get("git_version")
|
||||||
|
|
||||||
def _render_ce_type(self, type):
|
def _render_ce_type(self, type):
|
||||||
template = self.env.get_template("common-event-fields.tmpl")
|
template = self.env.get_template("common-event-fields.tmpl")
|
||||||
ce_types = self.units.get("common-event-fields")
|
ce_types = self.units.get("common_event_fields")
|
||||||
return template.render(common_event=ce_types[type])
|
return template.render(common_event=ce_types[type])
|
||||||
|
|
||||||
def render_common_event_fields(self):
|
def render_common_event_fields(self):
|
||||||
|
|
@ -50,14 +69,7 @@ class Sections(object):
|
||||||
def load(env, units):
|
def load(env, units):
|
||||||
store = AccessKeyStore()
|
store = AccessKeyStore()
|
||||||
sections = Sections(env, units)
|
sections = Sections(env, units)
|
||||||
render_list = inspect.getmembers(sections, predicate=inspect.ismethod)
|
section_dict = sections.get_sections()
|
||||||
for (func_name, func) in render_list:
|
for section_key in section_dict:
|
||||||
if not func_name.startswith("render_"):
|
store.add(section_key, section_dict[section_key])
|
||||||
continue
|
|
||||||
section_key = func_name[len("render_"):]
|
|
||||||
section = func()
|
|
||||||
print "Generated section '%s' : %s" % (
|
|
||||||
section_key, section[:60].replace("\n","")
|
|
||||||
)
|
|
||||||
store.add(section_key, section)
|
|
||||||
return store
|
return store
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
"""Contains all the units for the spec."""
|
"""Contains all the units for the spec."""
|
||||||
from . import AccessKeyStore
|
from . import AccessKeyStore
|
||||||
|
import inspect
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
@ -12,7 +13,31 @@ def prop(obj, path):
|
||||||
val = val.get(key, {})
|
val = val.get(key, {})
|
||||||
return val
|
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"
|
path = "../event-schemas/schema/v1/core"
|
||||||
event_types = {}
|
event_types = {}
|
||||||
with open(path, "r") as f:
|
with open(path, "r") as f:
|
||||||
|
|
@ -34,7 +59,7 @@ def _load_common_event_fields():
|
||||||
event_types[event_type] = table
|
event_types[event_type] = table
|
||||||
return event_types
|
return event_types
|
||||||
|
|
||||||
def _load_examples():
|
def load_event_examples(self):
|
||||||
path = "../event-schemas/examples/v1"
|
path = "../event-schemas/examples/v1"
|
||||||
examples = {}
|
examples = {}
|
||||||
for filename in os.listdir(path):
|
for filename in os.listdir(path):
|
||||||
|
|
@ -46,7 +71,7 @@ def _load_examples():
|
||||||
examples["m.room.message"] = examples[filename]
|
examples["m.room.message"] = examples[filename]
|
||||||
return examples
|
return examples
|
||||||
|
|
||||||
def _load_schemas():
|
def load_event_schemas(self):
|
||||||
path = "../event-schemas/schema/v1"
|
path = "../event-schemas/schema/v1"
|
||||||
schemata = {}
|
schemata = {}
|
||||||
|
|
||||||
|
|
@ -121,7 +146,7 @@ def _load_schemas():
|
||||||
for filename in os.listdir(path):
|
for filename in os.listdir(path):
|
||||||
if not filename.startswith("m."):
|
if not filename.startswith("m."):
|
||||||
continue
|
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:
|
with open(os.path.join(path, filename), "r") as f:
|
||||||
json_schema = json.loads(f.read())
|
json_schema = json.loads(f.read())
|
||||||
schema = {
|
schema = {
|
||||||
|
|
@ -173,7 +198,7 @@ def _load_schemas():
|
||||||
schemata[filename] = schema
|
schemata[filename] = schema
|
||||||
return schemata
|
return schemata
|
||||||
|
|
||||||
def _load_git_ver():
|
def load_git_version(self):
|
||||||
null = open(os.devnull, 'w')
|
null = open(os.devnull, 'w')
|
||||||
cwd = os.path.dirname(os.path.abspath(__file__))
|
cwd = os.path.dirname(os.path.abspath(__file__))
|
||||||
try:
|
try:
|
||||||
|
|
@ -221,19 +246,11 @@ def _load_git_ver():
|
||||||
return git_version.encode("ascii")
|
return git_version.encode("ascii")
|
||||||
return "Unknown rev"
|
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():
|
def load():
|
||||||
store = AccessKeyStore()
|
store = AccessKeyStore()
|
||||||
for unit_key in UNIT_DICT:
|
units = Units()
|
||||||
unit = UNIT_DICT[unit_key]()
|
unit_dict = units.get_units()
|
||||||
print "Generated unit '%s' : %s" % (
|
for unit_key in unit_dict:
|
||||||
unit_key, json.dumps(unit)[:50].replace("\n","")
|
store.add(unit_key, unit_dict[unit_key])
|
||||||
)
|
|
||||||
store.add(unit_key, unit)
|
|
||||||
return store
|
return store
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue