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

WIP: validator schema generation

parent 37a8a24f
This diff is collapsed.
......@@ -9,6 +9,7 @@ Additional values, not covered by this specification, are allowed, but currently
## Data subsets
In the combined interface for all the reports, we have to deal with multiple kinds of data, each used in the different context. At the moment we can identify 5 of them:
|JSON name|Explanation|
| --- | --- |
|general|global job data - is valid for the job and doesn't change during the entire job runtime (e.g. job-ID, user name etc.).|
......
#! /usr/bin/env python3
"""Module for converting parsed spec to validator schema."""
import json
from pprint import pprint as pp
import conf.config as cc
......@@ -13,32 +15,6 @@ def is_required(req):
return None
def get_unit(unit):
"""
Convert table strings into metric units.
Arguments
---------
string unit -- abbreviation as defined in the specification
Returns
-------
real metric unit
"""
if "BYTE" in unit:
return "byte(s)"
if "SEC" in unit:
return "s"
if "HZ" in unit:
return "Hz"
if "NSEC" in unit:
return "ns"
if "MBS" in unit:
return "MB/s"
return None
def add_constraints(entry, constraint):
"""
Convert constraint strings into rules.
......@@ -90,46 +66,54 @@ def add_constraints(entry, constraint):
return entry
def datatable2list(table, tname=None):
"""Convert multi-column entries into according dicts."""
res = []
for j in table:
entry = dict()
entry["name"] = j[0][1:-1]
if not entry["name"].startswith("pfit"):
continue
entry["label"] = j[5]
entry["type"] = j[1]
entry["unit"] = get_unit(j[3])
entry["required"] = is_required(j[4])
entry = add_constraints(entry, j[2])
res.append(entry)
if tname is None:
return {"table": res}
return {tname: res}
def tnames2list(table):
"""Extract the names of other tables."""
return [i[0] for i in table[2:]]
def constraints2dict(table):
"""Convert constraint table into a dict."""
return {i[0]: i[1] for i in table[2:]}
def units2dict(table):
"""
Convert first and second columns of the units table into a dict.
Arguments
---------
table -- list of list of strings with table contents
Returns
-------
dict -- placeholders (first col) paired with units (second col)
"""
return {i[0]: i[1] for i in table[2:]}
# def datatable2list(table, tname=None):
# """Convert multi-column entries into according dicts."""
# res = []
# for j in table:
# entry = dict()
# entry["name"] = j[0][1:-1]
# if not entry["name"].startswith("pfit"):
# continue
# entry["label"] = j[5]
# entry["type"] = j[1]
# entry["unit"] = get_unit(j[3])
# entry["required"] = is_required(j[4])
# entry = add_constraints(entry, j[2])
# res.append(entry)
# if tname is None:
# return {"table": res}
# return {tname: res}
def cols2dict(table, keycol, valcol):
return {table[i][keycol]: table[i][valcol] for i in table}
def col2list(table, col):
return [table[i][col] for i in table]
if __name__ == "__main__":
import sys
with open(sys.argv[1], "r") as f:
DATA = json.load(f)
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)
pp(CONSTRAINTS)
pp(UNITS)
pp(METRICS)
# TNAMES = tnames2list(TABLES[0])
# print(TNAMES)
# CONSTRAINTS = constraints2dict(TABLES[1])
# print(CONSTRAINTS)
# UNITS = units2dict(TABLES[2])
# print(UNITS)
# for pair in zip(TABLES[3:], TNAMES):
# TBL = datatable2list(pair[0], pair[1])
# pp(TBL)
# print()
......@@ -6,7 +6,8 @@
test data set from it.
"""
from pprint import pprint as pp
# from pprint import pprint as pp
import json
def extract_tables(md_data, prefix="table"):
......@@ -27,7 +28,7 @@ def extract_tables(md_data, prefix="table"):
res = dict()
table = []
for line in md_data:
line2 = line.strip()
line2 = line.strip().replace("`", "")
if line2.startswith("|"):
if not block:
block = True
......@@ -45,14 +46,13 @@ def extract_tables(md_data, prefix="table"):
return res
def organise_tables(tbl_data, prefix="row"):
def organise_tables(tbl_data, prefix="entry"):
"""Extract table structure."""
for key in tbl_data:
tmp = dict()
table = tbl_data[key]
tmp["columns"] = table[0]
for pos, row in enumerate(table[2:]):
tmp[prefix + str(pos)] = row
tmp[prefix + str(pos)] = dict(zip(table[0], row))
tbl_data[key] = tmp
return tbl_data
......@@ -64,7 +64,8 @@ if __name__ == "__main__":
DATA = f.readlines()
TABLES = extract_tables(DATA)
TABLES1 = organise_tables(TABLES)
pp(TABLES1)
with open("out/tables.json", "w") as f:
json.dump(TABLES1, f)
# TNAMES = tnames2list(TABLES[0])
# print(TNAMES)
# CONSTRAINTS = constraints2dict(TABLES[1])
......
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