mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
generalize generate_schema_documentation.py
This commit is contained in:
parent
0d8a53cb68
commit
31be5b3c8a
@ -5,7 +5,7 @@ import os.path
|
||||
|
||||
import yaml
|
||||
|
||||
from bigchaindb.common.schema import TX_SCHEMA_YAML
|
||||
from bigchaindb.common.schema import TX_SCHEMA_PATH
|
||||
|
||||
|
||||
TPL_PROP = """\
|
||||
@ -18,7 +18,28 @@ TPL_PROP = """\
|
||||
"""
|
||||
|
||||
|
||||
TPL_DOC = """\
|
||||
TPL_STYLES = """
|
||||
.. raw:: html
|
||||
|
||||
<style>
|
||||
#%(container)s h2 {
|
||||
border-top: solid 3px #6ab0de;
|
||||
background-color: #e7f2fa;
|
||||
padding: 5px;
|
||||
}
|
||||
#%(container)s h3 {
|
||||
background: #f0f0f0;
|
||||
border-left: solid 3px #ccc;
|
||||
font-weight: bold;
|
||||
padding: 6px;
|
||||
font-size: 100%%;
|
||||
font-family: monospace;
|
||||
}
|
||||
</style>
|
||||
"""
|
||||
|
||||
|
||||
TPL_TRANSACTION = """\
|
||||
.. This file was auto generated by %(file)s
|
||||
|
||||
==================
|
||||
@ -35,23 +56,6 @@ Transaction Schema
|
||||
|
||||
* Metadata_
|
||||
|
||||
.. raw:: html
|
||||
|
||||
<style>
|
||||
#transaction-schema h2 {
|
||||
border-top: solid 3px #6ab0de;
|
||||
background-color: #e7f2fa;
|
||||
padding: 5px;
|
||||
}
|
||||
#transaction-schema h3 {
|
||||
background: #f0f0f0;
|
||||
border-left: solid 3px #ccc;
|
||||
font-weight: bold;
|
||||
padding: 6px;
|
||||
font-size: 100%%;
|
||||
font-family: monospace;
|
||||
}
|
||||
</style>
|
||||
|
||||
Transaction
|
||||
-----------
|
||||
@ -77,10 +81,20 @@ Metadata
|
||||
--------
|
||||
|
||||
%(metadata)s
|
||||
"""
|
||||
""" + TPL_STYLES
|
||||
|
||||
|
||||
def ordered_load_yaml(stream):
|
||||
TPL_VOTE = """\
|
||||
.. This file was auto generated by %(file)s
|
||||
|
||||
===========
|
||||
Vote Schema
|
||||
===========
|
||||
|
||||
""" + TPL_STYLES
|
||||
|
||||
|
||||
def ordered_load_yaml(path):
|
||||
""" Custom YAML loader to preserve key order """
|
||||
class OrderedLoader(yaml.SafeLoader):
|
||||
pass
|
||||
@ -91,13 +105,43 @@ def ordered_load_yaml(stream):
|
||||
OrderedLoader.add_constructor(
|
||||
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
||||
construct_mapping)
|
||||
return yaml.load(stream, OrderedLoader)
|
||||
with open(path) as handle:
|
||||
return yaml.load(handle, OrderedLoader)
|
||||
|
||||
|
||||
TX_SCHEMA = ordered_load_yaml(TX_SCHEMA_YAML)
|
||||
def load_schema(path):
|
||||
global DEFS
|
||||
schema = ordered_load_yaml(path)
|
||||
DEFS = schema['definitions']
|
||||
return schema
|
||||
|
||||
|
||||
DEFINITION_BASE_PATH = '#/definitions/'
|
||||
def generate_transaction_docs():
|
||||
schema = load_schema(TX_SCHEMA_PATH)
|
||||
defs = schema['definitions']
|
||||
|
||||
doc = TPL_TRANSACTION % {
|
||||
'transaction': render_section('Transaction', schema),
|
||||
'condition': render_section('Condition', defs['condition']),
|
||||
'fulfillment': render_section('Fulfillment', defs['fulfillment']),
|
||||
'asset': render_section('Asset', defs['asset']),
|
||||
'metadata': render_section('Metadata', defs['metadata']['anyOf'][0]),
|
||||
'container': 'transaction-schema',
|
||||
'file': os.path.basename(__file__),
|
||||
}
|
||||
|
||||
write_schema_doc('transaction', doc)
|
||||
|
||||
|
||||
def write_schema_doc(name, doc):
|
||||
# Check base path exists
|
||||
base_path = os.path.join(os.path.dirname(__file__), 'source/schema')
|
||||
if not os.path.exists(base_path):
|
||||
os.makedirs(base_path)
|
||||
# Write doc
|
||||
path = os.path.join(base_path, '%s.rst' % name)
|
||||
with open(path, 'w') as handle:
|
||||
handle.write(doc)
|
||||
|
||||
|
||||
def render_section(section_name, obj):
|
||||
@ -141,32 +185,18 @@ def property_type(prop):
|
||||
raise ValueError("Could not resolve property type")
|
||||
|
||||
|
||||
DEFINITION_BASE_PATH = '#/definitions/'
|
||||
|
||||
|
||||
def resolve_ref(ref):
|
||||
""" Resolve definition reference """
|
||||
assert ref.startswith(DEFINITION_BASE_PATH)
|
||||
return TX_SCHEMA['definitions'][ref[len(DEFINITION_BASE_PATH):]]
|
||||
return DEFS[ref[len(DEFINITION_BASE_PATH):]]
|
||||
|
||||
|
||||
def main():
|
||||
""" Main function """
|
||||
defs = TX_SCHEMA['definitions']
|
||||
doc = TPL_DOC % {
|
||||
'transaction': render_section('Transaction', TX_SCHEMA),
|
||||
'condition': render_section('Condition', defs['condition']),
|
||||
'fulfillment': render_section('Fulfillment', defs['fulfillment']),
|
||||
'asset': render_section('Asset', defs['asset']),
|
||||
'metadata': render_section('Metadata', defs['metadata']['anyOf'][0]),
|
||||
'file': os.path.basename(__file__),
|
||||
}
|
||||
|
||||
base_path = os.path.join(os.path.dirname(__file__), 'source/schema')
|
||||
path = os.path.join(base_path, 'transaction.rst')
|
||||
|
||||
if not os.path.exists(base_path):
|
||||
os.makedirs(base_path)
|
||||
|
||||
with open(path, 'w') as handle:
|
||||
handle.write(doc)
|
||||
generate_transaction_docs()
|
||||
|
||||
|
||||
def setup(*_):
|
||||
|
Loading…
x
Reference in New Issue
Block a user