1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#!/usr/bin/env python3
import importlib
from operator import attrgetter
from pathlib import Path
from pprint import pformat
from textwrap import indent
from types import SimpleNamespace
INCLUDES_DIR = Path(__file__).parent.resolve()
CERBERUS_DIR = INCLUDES_DIR.parent.parent / 'cerberus'
def load_module_members(name, path):
module_spec = importlib.util.spec_from_file_location(name, path)
_module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(_module)
return vars(_module)
errors_module = load_module_members('errors', CERBERUS_DIR / 'errors.py')
error_type = errors_module['ErrorDefinition']
error_definitions = []
for name, member in errors_module.items():
if not isinstance(member, error_type):
continue
error_definition = SimpleNamespace(**member._asdict())
error_definition.name = name
error_definitions.append(error_definition)
error_definitions.sort(key=attrgetter('code'))
with (INCLUDES_DIR / 'error-codes.rst').open('wt') as f:
print(
"""
.. list-table::
:header-rows: 1
* - Code (dec.)
- Code (hex.)
- Name
- Rule""".lstrip(
'\n'
),
file=f,
)
for error_definition in error_definitions:
print(
f"""
* - {error_definition.code}
- {hex(error_definition.code)}
- {error_definition.name}
- {error_definition.rule}""".lstrip(
'\n'
),
file=f,
)
print('Generated table with ErrorDefinitions.')
validator_module = load_module_members('validator', CERBERUS_DIR / 'validator.py')
validator = validator_module['Validator']()
schema_validation_schema = pformat(
validator.rules, width=68
) # width seems w/o effect, use black?
with (INCLUDES_DIR / 'schema-validation-schema.rst').open('wt') as f:
print(
'.. code-block:: python\n\n', indent(schema_validation_schema, ' '), file=f
)
print("Generated schema for a vanilla validator's, well, schema.")
|