Commit fea9781b authored by Igor Merkulow's avatar Igor Merkulow
Browse files

WIP: spec2schema

parent 47304b2f
......@@ -5,12 +5,15 @@ from pprint import pprint as pp
from spectools.specparser import SpecParser
from spectools.spec2schema import col2list
from spectools.spec2schema import cols2dict
from spectools.spec2schema import entry2var
from spectools.spec2schema import char2bool
INFILE = "docs/spec-metrics2.md"
JSONFILE = "out/tables.json"
if __name__ == "__main__":
# ------ parsing --------
with open(INFILE, "r") as f:
DATA = f.readlines()
SPP = SpecParser(DATA)
......@@ -18,14 +21,36 @@ if __name__ == "__main__":
with open(JSONFILE, "w") as f:
json.dump(TABLES, f)
# ------ converting to schema -------
with open(JSONFILE, "r") as f:
DATA = json.load(f)
pp(DATA.keys())
TNAMES = col2list(DATA["table0"], "JSON name")
CONSTRAINTS = cols2dict(DATA["table1"], "Abbreviation", "Constraint")
UNITS = cols2dict(DATA["table2"], "Abbreviation", "Unit")
print(DATA.keys())
METRICS = {TNAMES[pos]: DATA[key] for pos, key in enumerate([*DATA][3:])}
pp(TNAMES)
CONSTRAINTS = cols2dict(DATA["table1"], "Abbreviation", "Constraint")
pp(CONSTRAINTS)
UNITS = cols2dict(DATA["table2"], "Abbreviation", "Unit")
pp(UNITS)
pp(METRICS)
METRICS = {TNAMES[pos]: DATA[key] for pos, key in enumerate([*DATA][3:])}
METRICS2 = dict()
for tname in METRICS:
table = dict()
for entry in METRICS[tname]:
key, val = entry2var(METRICS[tname][entry])
if key not in table:
table[key] = val
else:
print("Duplicate key: {}".format(key))
METRICS2[tname] = table
for tname in METRICS:
for entry in METRICS[tname]:
if "Required" in METRICS[tname][entry]:
METRICS[tname][entry]["Required"] = char2bool(
METRICS[tname][entry]["Required"]
)
if "Unit" in METRICS[tname][entry]:
METRICS[tname][entry]["Unit"] = UNITS.get(
METRICS[tname][entry]["Unit"], None
)
pp(METRICS2)
......@@ -9,6 +9,8 @@ from format.text import TextReport
from validator.validator import Validator
# TODO: pass spec version as parameter (1.0, demo, 2.0)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("not enough parameters")
......
......@@ -34,7 +34,20 @@ def col2list(table, col):
return None
if not isinstance(col, str):
return None
return [table[i][col] for i in table]
return [
table[i][col] for i in table if table[i].get(col, None) is not None
]
def entry2var(entry, varkey="Report metric"):
"""Extract metric name as the new key."""
if not isinstance(entry, dict):
return None
if entry.get(varkey, None) is None:
return None
varname = entry[varkey]
del entry[varkey]
return (varname, entry)
# def add_constraints(entry, constraint):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment