-
Notifications
You must be signed in to change notification settings - Fork 7
/
docs.py
executable file
·115 lines (80 loc) · 2.79 KB
/
docs.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import io
import os
from re import compile
SCHEMA_PATH = compile(r'dgf/schemas/(?P<path>[a-zA-Z0-9_/]+)$')
VERSION_RE = compile(r'schema_(?P<version>\d+).json')
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
"""
Create tree for json schema in docs
"""
main_schema_template = u"""
.. index:: Schemas
.. _Schemas:
=======
Schemas
=======
All schemas:
.. toctree::
:maxdepth: 20
{doctree}
"""
schemas_rst_template = u"""
======================
Schema {schema_number}
======================
Main page :ref:`schemas`
{schemas}
"""
schema_template = u"""
version {version}
-----------
.. jsonschema:: ../../../{static_path}openprocurement/schemas/dgf/schemas/{schema_path}/{file}
In jsonschema format:
.. include:: ../../../{static_path}openprocurement/schemas/dgf/schemas/{schema_path}/{file}
:code:
"""
def create_doc_file(path, file_name, rst):
with io.open(os.path.join(path, file_name), 'w') as f:
f.write(schemas_rst_template.format(schema_number=file_name[:-4],
schemas=''.join(rst)))
def create_dir(path):
""" Create directory by path if it not exists """
if not os.path.exists(path):
os.makedirs(path)
def create_doctree(path):
doctree = []
for path, dirs, files in os.walk(path):
if not SCHEMA_PATH.search(path):
continue
path = SCHEMA_PATH.search(path).groupdict()['path']
doctree.append("schemas/{}/{}".format(path, path.replace('/', '')))
return "\n ".join(doctree)
def create_schemas_docs():
for index, (path, dirs, files) in enumerate(os.walk('./openprocurement/schemas/dgf/schemas')):
if not SCHEMA_PATH.search(path):
continue
path = SCHEMA_PATH.search(path).groupdict()['path']
print("Find schemas in {path}".format(path=path))
schema_number = path.replace('/', '')
rst = [schema_template.format(
file=file,
dir_path=DIR_PATH,
schema_number=schema_number,
schema_path=path,
static_path="../" * index,
version=VERSION_RE.search(file).groupdict()['version'])
for file in files]
path_for_docs = os.path.join(os.getcwd(), 'docs', 'source', 'schemas', path)
create_dir(path_for_docs)
create_doc_file(path_for_docs,
"{schema_number}.rst".format(schema_number=schema_number),
rst)
with io.open('./docs/source/schemas.rst', 'w') as f:
f.write(main_schema_template.format(
doctree=create_doctree('./openprocurement/schemas/dgf/schemas')))
if __name__ == '__main__':
print("Create schemas rst files.")
create_schemas_docs()
print("End create schemas rst files.")