body-oc 2.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- body/__init__.py +20 -0
- body/__main__.py +1 -0
- body/constants.py +20 -0
- body/docs/__init__.py +361 -0
- body/docs/__main__.py +105 -0
- body/docs/parser.py +117 -0
- body/docs/templates/markdown.j2 +83 -0
- body/errors.py +51 -0
- body/external.py +427 -0
- body/regex.py +19 -0
- body/response.py +289 -0
- body/rest.py +634 -0
- body/service.py +198 -0
- body_oc-2.1.1.dist-info/METADATA +38 -0
- body_oc-2.1.1.dist-info/RECORD +17 -0
- body_oc-2.1.1.dist-info/WHEEL +4 -0
- body_oc-2.1.1.dist-info/licenses/LICENSE +8 -0
body/__init__.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# coding=utf8
|
|
2
|
+
"""Body
|
|
3
|
+
|
|
4
|
+
Shared methods for accessing the brain and other shared formats
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
__author__ = "Chris Nasr"
|
|
8
|
+
__copyright__ = "Ouroboros Coding Inc."
|
|
9
|
+
__email__ = "chris@ouroboroscoding.com"
|
|
10
|
+
__created__ = "2022-08-29"
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
'create', 'read', 'update', 'delete',
|
|
14
|
+
'Service', 'Error', 'Response', 'ResponseException'
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
# Import external calls and Service
|
|
18
|
+
from body.external import create, delete, read, update
|
|
19
|
+
from body.response import Error, Response, ResponseException
|
|
20
|
+
from body.service import Service
|
body/__main__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
print('I compile, but I don\'t necessarily run')
|
body/constants.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# coding=utf8
|
|
2
|
+
""" Constants
|
|
3
|
+
|
|
4
|
+
Shared constants
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
__author__ = "Chris Nasr"
|
|
8
|
+
__copyright__ = "Ouroboros Coding Inc."
|
|
9
|
+
__email__ = "chris@ouroboroscoding.com"
|
|
10
|
+
__created__ = "2023-03-09"
|
|
11
|
+
|
|
12
|
+
EMPTY_TUUID = '00000000000040008000000000000000'
|
|
13
|
+
"""Empty, but valid, Trimmed UUID"""
|
|
14
|
+
EMPTY_UUID = '00000000-0000-4000-8000-000000000000'
|
|
15
|
+
"""Empty, but valid, Full UUID"""
|
|
16
|
+
|
|
17
|
+
SECONDS_HOUR = 3600
|
|
18
|
+
SECONDS_DAY = 86400
|
|
19
|
+
SECONDS_WEEK = 604800
|
|
20
|
+
"""Seconds related constants"""
|
body/docs/__init__.py
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
# coding=utf8
|
|
2
|
+
""" Docs
|
|
3
|
+
|
|
4
|
+
Generates markdown file of all request points in a service
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
__author__ = "Chris Nasr"
|
|
8
|
+
__version__ = "1.0.0"
|
|
9
|
+
__copyright__ = "Ouroboros Coding Inc."
|
|
10
|
+
__email__ = "chris@ouroboroscoding.com"
|
|
11
|
+
__created__ = "2025-02-23"
|
|
12
|
+
|
|
13
|
+
# Ouroboros imports
|
|
14
|
+
from strings import to_file
|
|
15
|
+
|
|
16
|
+
# Python imports
|
|
17
|
+
import ast
|
|
18
|
+
from os import path
|
|
19
|
+
import re
|
|
20
|
+
from sys import exit, stderr
|
|
21
|
+
from typing import List, Literal
|
|
22
|
+
|
|
23
|
+
# Local imports
|
|
24
|
+
from .parser import parse_google
|
|
25
|
+
|
|
26
|
+
# Regular expressions
|
|
27
|
+
_service = re.compile(r'request:\s*(\w+)', re.IGNORECASE)
|
|
28
|
+
_data = re.compile(
|
|
29
|
+
r'^([^,]+)\s*,\s*([^,]+)\s*,\s*(no|yes)\s*,\s*(.+)$', re.IGNORECASE
|
|
30
|
+
)
|
|
31
|
+
_error = re.compile(r'^(\d+)\s*,\s*((?:[a-z0-9_]+\.)?[A-Z0-9_]+)\s*,\s*(.+)$')
|
|
32
|
+
_response = re.compile(r'^([^,]+)\s*,\s*([^,]+)\s*,\s*(.+)$')
|
|
33
|
+
|
|
34
|
+
# Constants
|
|
35
|
+
EXTENSIONS = { 'markdown': 'md' }
|
|
36
|
+
|
|
37
|
+
def handle_class(
|
|
38
|
+
_class: ast.ClassDef,
|
|
39
|
+
parser: Literal['google']
|
|
40
|
+
) -> dict | Literal[False]:
|
|
41
|
+
"""Handle Class
|
|
42
|
+
|
|
43
|
+
Looks through a Service class and generates the data for the documentation
|
|
44
|
+
|
|
45
|
+
Arguments:
|
|
46
|
+
_class (ast.ClassDef): The AST definition of the class we will step
|
|
47
|
+
through
|
|
48
|
+
parser ('google'): The parser to use for the docstrings
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
dict | False
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
# If it's not a Service class
|
|
55
|
+
if not any([ o.id == 'Service' for o in _class.bases ]):
|
|
56
|
+
return False
|
|
57
|
+
|
|
58
|
+
# Init the return
|
|
59
|
+
dRet = {
|
|
60
|
+
'name': _class.name,
|
|
61
|
+
'file': _class.name.lower(),
|
|
62
|
+
'uri': _class.name.lower(),
|
|
63
|
+
'requests': [ ]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
# Get the docstring and store it
|
|
67
|
+
sDoc = ast.get_docstring(_class, True)
|
|
68
|
+
|
|
69
|
+
# Parse the docstring to get the sections
|
|
70
|
+
if parser == 'google':
|
|
71
|
+
dSections = parse_google(sDoc)
|
|
72
|
+
else:
|
|
73
|
+
raise ValueError('parser', 'invalid parser "%s"' % parser)
|
|
74
|
+
|
|
75
|
+
# If we have a name override
|
|
76
|
+
if 'docs-file' in dSections:
|
|
77
|
+
dRet['file'] = dSections['docs-file']
|
|
78
|
+
elif 'docs_file' in dSections:
|
|
79
|
+
dRet['file'] = dSections['docs_file']
|
|
80
|
+
|
|
81
|
+
# Add the description
|
|
82
|
+
dRet['description'] = dSections['description']
|
|
83
|
+
|
|
84
|
+
# If we have a body overwrite
|
|
85
|
+
if 'docs-body' in dSections:
|
|
86
|
+
dRet['body'] = dSections['docs-body']
|
|
87
|
+
elif 'docs_body' in dSections:
|
|
88
|
+
dRet['body'] = dSections['docs_body']
|
|
89
|
+
|
|
90
|
+
# Step through each child of the class
|
|
91
|
+
for oMethod in _class.body:
|
|
92
|
+
|
|
93
|
+
# If it's not a function
|
|
94
|
+
if not isinstance(oMethod, ast.FunctionDef):
|
|
95
|
+
continue
|
|
96
|
+
|
|
97
|
+
# Handle the method
|
|
98
|
+
mRes = handle_method(oMethod, parser)
|
|
99
|
+
if mRes:
|
|
100
|
+
dRet['requests'].append(mRes)
|
|
101
|
+
|
|
102
|
+
# Return OK
|
|
103
|
+
return dRet
|
|
104
|
+
|
|
105
|
+
def handle_file(
|
|
106
|
+
file: str,
|
|
107
|
+
parser: Literal['google']
|
|
108
|
+
) -> List[dict] | Literal[False]:
|
|
109
|
+
"""Handle File
|
|
110
|
+
|
|
111
|
+
Looks through a single file for Service classes. Returns False is anything
|
|
112
|
+
fails, else a list of services found in the file
|
|
113
|
+
|
|
114
|
+
Arguments:
|
|
115
|
+
file (str): The path to the file
|
|
116
|
+
parser ('google'): The parser to use for the docstrings
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
dict[] | False
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
# If the file does not exist
|
|
123
|
+
if not path.exists(file):
|
|
124
|
+
print('File "%s" does not exist' % file, file = stderr)
|
|
125
|
+
return False
|
|
126
|
+
|
|
127
|
+
# Open the file
|
|
128
|
+
with open(file) as oF:
|
|
129
|
+
|
|
130
|
+
# Get the abstract syntax tree for the file
|
|
131
|
+
try:
|
|
132
|
+
oAST = ast.parse(oF.read(), file)
|
|
133
|
+
|
|
134
|
+
# Init list of services
|
|
135
|
+
lServices = []
|
|
136
|
+
|
|
137
|
+
# Go through each node in the tree
|
|
138
|
+
for oClass in ast.iter_child_nodes(oAST):
|
|
139
|
+
|
|
140
|
+
# If it's a class
|
|
141
|
+
if isinstance(oClass, ast.ClassDef):
|
|
142
|
+
|
|
143
|
+
# Handle the class
|
|
144
|
+
mRes = handle_class(oClass, parser)
|
|
145
|
+
if mRes:
|
|
146
|
+
lServices.append(mRes)
|
|
147
|
+
|
|
148
|
+
# If there's no services
|
|
149
|
+
if not lServices:
|
|
150
|
+
return False
|
|
151
|
+
|
|
152
|
+
# Return OK
|
|
153
|
+
return lServices
|
|
154
|
+
|
|
155
|
+
# Catch syntax errors from broken code
|
|
156
|
+
except SyntaxError as e:
|
|
157
|
+
print('Syntax Error parsing "%s" at line %d, column %d\n' % (
|
|
158
|
+
file,
|
|
159
|
+
e.args[1][1],
|
|
160
|
+
e.args[1][2]
|
|
161
|
+
), file = stderr)
|
|
162
|
+
return False
|
|
163
|
+
|
|
164
|
+
# Strange
|
|
165
|
+
print('Unknown issue reading "%s"' % file, file = stderr)
|
|
166
|
+
return False
|
|
167
|
+
|
|
168
|
+
def handle_method(
|
|
169
|
+
method: ast.FunctionDef,
|
|
170
|
+
parser: Literal['google']
|
|
171
|
+
) -> dict | Literal[False]:
|
|
172
|
+
"""Handle Method
|
|
173
|
+
|
|
174
|
+
Looks through a service method and generates the data for the documentation
|
|
175
|
+
|
|
176
|
+
Arguments:
|
|
177
|
+
method (ast.FunctionDef): The AST definition of the method
|
|
178
|
+
parser ('google'): The parser to use for the docstrings
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
dict | False
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
# Split the name and store the parts
|
|
185
|
+
lParts = method.name.split('_')
|
|
186
|
+
|
|
187
|
+
# If it's not a valid action
|
|
188
|
+
if lParts[-1] not in [ 'create', 'delete', 'read', 'update' ]:
|
|
189
|
+
return False
|
|
190
|
+
|
|
191
|
+
# If it doesn't have the expected arguments
|
|
192
|
+
try:
|
|
193
|
+
lArgs = [ o.arg for o in method.args.args ]
|
|
194
|
+
if lArgs != [ 'self', 'req' ]:
|
|
195
|
+
return False
|
|
196
|
+
except Exception:
|
|
197
|
+
return False
|
|
198
|
+
|
|
199
|
+
# Init the return
|
|
200
|
+
dRet = {
|
|
201
|
+
'action': lParts[-1],
|
|
202
|
+
'noun': ' '.join(lParts[:-1]),
|
|
203
|
+
'uri': '/'.join(lParts[:-1])
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
# Get the docstring and store it
|
|
207
|
+
sDoc = ast.get_docstring(method, True)
|
|
208
|
+
|
|
209
|
+
# Parse the docstring to get the sections
|
|
210
|
+
if parser == 'google':
|
|
211
|
+
dSections = parse_google(sDoc)
|
|
212
|
+
else:
|
|
213
|
+
raise ValueError('parser', 'invalid parser "%s"' % parser)
|
|
214
|
+
|
|
215
|
+
# Add the name and description
|
|
216
|
+
dRet['name'] = dSections['name']
|
|
217
|
+
dRet['description'] = 'description' in dSections and \
|
|
218
|
+
dSections['description'] or ''
|
|
219
|
+
|
|
220
|
+
# If we have a data section
|
|
221
|
+
if 'data' in dSections:
|
|
222
|
+
|
|
223
|
+
# Split data into lines, strip whitespace
|
|
224
|
+
lLines = [ s.strip() for s in dSections['data'].split('\n') ]
|
|
225
|
+
|
|
226
|
+
# Init data
|
|
227
|
+
lData = []
|
|
228
|
+
|
|
229
|
+
# Go through each line
|
|
230
|
+
for s in lLines:
|
|
231
|
+
|
|
232
|
+
# Match it
|
|
233
|
+
m = _data.match(s)
|
|
234
|
+
if not m:
|
|
235
|
+
print('Invalid "data" line in "%s" docstring: %s' % (
|
|
236
|
+
method.name, s
|
|
237
|
+
), file = stderr)
|
|
238
|
+
else:
|
|
239
|
+
lData.append({
|
|
240
|
+
'name': m.group(1),
|
|
241
|
+
'type': m.group(2),
|
|
242
|
+
'optional': m.group(3),
|
|
243
|
+
'descr': m.group(4)
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
# If we have any, add it
|
|
247
|
+
if lData:
|
|
248
|
+
dRet['data'] = lData
|
|
249
|
+
|
|
250
|
+
# Else, just pass the original string
|
|
251
|
+
else:
|
|
252
|
+
dRet['data'] = dSections['data']
|
|
253
|
+
|
|
254
|
+
# If we have a response section
|
|
255
|
+
if 'response' in dSections:
|
|
256
|
+
|
|
257
|
+
# Split response into lines, strip whitespace
|
|
258
|
+
lLines = [ s.strip() for s in dSections['response'].split('\n') ]
|
|
259
|
+
|
|
260
|
+
# Init response
|
|
261
|
+
lResponse = []
|
|
262
|
+
|
|
263
|
+
# Go through each line
|
|
264
|
+
for s in lLines:
|
|
265
|
+
|
|
266
|
+
# Match it
|
|
267
|
+
m = _response.match(s)
|
|
268
|
+
if m:
|
|
269
|
+
lResponse.append({
|
|
270
|
+
'name': m.group(1),
|
|
271
|
+
'type': m.group(2),
|
|
272
|
+
'descr': m.group(3)
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
# If we have any, add it
|
|
276
|
+
if lResponse:
|
|
277
|
+
dRet['response'] = lResponse
|
|
278
|
+
|
|
279
|
+
# Else, just pass the original string
|
|
280
|
+
else:
|
|
281
|
+
dRet['response'] = dSections['response']
|
|
282
|
+
|
|
283
|
+
# If we have a response example
|
|
284
|
+
if 'response_example' in dSections:
|
|
285
|
+
dRet['response_example'] = dSections['response_example']
|
|
286
|
+
|
|
287
|
+
# If we have an error section
|
|
288
|
+
if 'error' in dSections:
|
|
289
|
+
|
|
290
|
+
# Split error into lines, strip whitespace
|
|
291
|
+
lLines = [ s.strip() for s in dSections['error'].split('\n') ]
|
|
292
|
+
|
|
293
|
+
# Init error
|
|
294
|
+
lError = []
|
|
295
|
+
|
|
296
|
+
# Go through each line
|
|
297
|
+
for s in lLines:
|
|
298
|
+
|
|
299
|
+
# Match it
|
|
300
|
+
m = _error.match(s)
|
|
301
|
+
if not m:
|
|
302
|
+
print('Invalid "error" line in "%s" docstring: %s' % (
|
|
303
|
+
method.name, s
|
|
304
|
+
), file = stderr)
|
|
305
|
+
else:
|
|
306
|
+
lError.append({
|
|
307
|
+
'code': m.group(1),
|
|
308
|
+
'const': m.group(2),
|
|
309
|
+
'descr': m.group(3)
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
# If we have any, add it
|
|
313
|
+
if lError:
|
|
314
|
+
dRet['error'] = lError
|
|
315
|
+
|
|
316
|
+
# If we have an example section
|
|
317
|
+
if 'example' in dSections:
|
|
318
|
+
dRet['example_raw'] = dSections['example']
|
|
319
|
+
elif 'data_example' in dSections:
|
|
320
|
+
dRet['data_example'] = dSections['data_example']
|
|
321
|
+
|
|
322
|
+
# Return the info
|
|
323
|
+
return dRet
|
|
324
|
+
|
|
325
|
+
def generate_service(service: dict, format: str, output: str):
|
|
326
|
+
"""Generate Service
|
|
327
|
+
|
|
328
|
+
Generates a single file with all documentation for the service
|
|
329
|
+
|
|
330
|
+
Arguments:
|
|
331
|
+
service (dict): The service info with the nouns
|
|
332
|
+
format ('markdown'): The format used to generate the docs
|
|
333
|
+
output (str): The directory to store the docs in
|
|
334
|
+
"""
|
|
335
|
+
|
|
336
|
+
# Load and init Jinja2
|
|
337
|
+
try:
|
|
338
|
+
from jinja2 import Environment, PackageLoader, select_autoescape
|
|
339
|
+
oJinja = Environment(
|
|
340
|
+
autoescape = select_autoescape(),
|
|
341
|
+
loader = PackageLoader('body.docs'),
|
|
342
|
+
lstrip_blocks = True,
|
|
343
|
+
trim_blocks = True
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
# Notify the user to install Jinja2 in development
|
|
347
|
+
except ModuleNotFoundError as e:
|
|
348
|
+
print('Using "body-docs" requires that Jinja2 be installed. It is ' \
|
|
349
|
+
'not installed by default to save space on production installs.\n' \
|
|
350
|
+
'To install jinja2 run the following in your venv:\n\n' \
|
|
351
|
+
'pip install jinja2', file = stderr)
|
|
352
|
+
exit(1)
|
|
353
|
+
|
|
354
|
+
# Get the template based on the format
|
|
355
|
+
oTpl = oJinja.get_template('%s.j2' % format)
|
|
356
|
+
|
|
357
|
+
# Render the template and save it to the output folder
|
|
358
|
+
return to_file(
|
|
359
|
+
'%s/%s.%s' % ( output, service['file'], EXTENSIONS[format] ),
|
|
360
|
+
oTpl.render(**service)
|
|
361
|
+
)
|
body/docs/__main__.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# coding=utf8
|
|
2
|
+
""" Body Docs
|
|
3
|
+
|
|
4
|
+
Main entry into calling the rest documentation generator
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
__author__ = "Chris Nasr"
|
|
8
|
+
__version__ = "1.0.0"
|
|
9
|
+
__copyright__ = "Ouroboros Coding Inc."
|
|
10
|
+
__email__ = "chris@ouroboroscoding.com"
|
|
11
|
+
__created__ = "2025-02-23"
|
|
12
|
+
|
|
13
|
+
# Python imports
|
|
14
|
+
from argparse import ArgumentParser
|
|
15
|
+
from sys import exit, stderr
|
|
16
|
+
|
|
17
|
+
# Constants
|
|
18
|
+
EXTENSIONS = { 'markdown': 'md' }
|
|
19
|
+
|
|
20
|
+
# Local impors
|
|
21
|
+
from . import handle_file, generate_service
|
|
22
|
+
|
|
23
|
+
def cli() -> int:
|
|
24
|
+
"""CLI
|
|
25
|
+
|
|
26
|
+
Handles generating a service's documentation via the command line interface
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
int
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
# Setup the argument parser
|
|
33
|
+
oArgParser = ArgumentParser(
|
|
34
|
+
description='Body - Generate REST Documentation'
|
|
35
|
+
)
|
|
36
|
+
oArgParser.add_argument(
|
|
37
|
+
'-f', '--format', nargs=1, default='markdown',
|
|
38
|
+
help='The format to out the documentation in. Only supports ' \
|
|
39
|
+
'"markdown" at present'
|
|
40
|
+
)
|
|
41
|
+
oArgParser.add_argument(
|
|
42
|
+
'-o', '--output', default='./',
|
|
43
|
+
help='The folder to store generated documentation in'
|
|
44
|
+
)
|
|
45
|
+
oArgParser.add_argument(
|
|
46
|
+
'-p', '--parser', nargs=1, default='google',
|
|
47
|
+
help='The parser to use for docstrings. Only supports "google" at ' \
|
|
48
|
+
'present'
|
|
49
|
+
)
|
|
50
|
+
oArgParser.add_argument(
|
|
51
|
+
'file', nargs="+",
|
|
52
|
+
help='The file(s) to parse and generate documentation from'
|
|
53
|
+
)
|
|
54
|
+
oArgs = oArgParser.parse_args()
|
|
55
|
+
|
|
56
|
+
# If the format is anything other than markdown
|
|
57
|
+
if oArgs.format != 'markdown':
|
|
58
|
+
print('Invalid option for --format: "%s"' % oArgs.format,
|
|
59
|
+
file = stderr)
|
|
60
|
+
print('body-docs only supports "markdown" generator')
|
|
61
|
+
return 0
|
|
62
|
+
|
|
63
|
+
# If the parser is anything other than markdown
|
|
64
|
+
if oArgs.parser != 'google':
|
|
65
|
+
print('Invalid option for --parser: "%s"' % oArgs.format,
|
|
66
|
+
file = stderr)
|
|
67
|
+
print('body-docs only supports "google" docstring format')
|
|
68
|
+
return 0
|
|
69
|
+
|
|
70
|
+
# Init the list of services
|
|
71
|
+
lServices = []
|
|
72
|
+
|
|
73
|
+
# Go through each file passed
|
|
74
|
+
for sFile in oArgs.file:
|
|
75
|
+
|
|
76
|
+
# Handle the file
|
|
77
|
+
mRes = handle_file(sFile, oArgs.parser)
|
|
78
|
+
if not mRes:
|
|
79
|
+
print('Failed to handle file "%s"' % sFile, file = stderr)
|
|
80
|
+
return 1
|
|
81
|
+
|
|
82
|
+
# Extend the list
|
|
83
|
+
lServices.extend(mRes)
|
|
84
|
+
|
|
85
|
+
# If we have services
|
|
86
|
+
if lServices:
|
|
87
|
+
|
|
88
|
+
# Generate a file for each
|
|
89
|
+
for dService in lServices:
|
|
90
|
+
print('Generating "%s"' % dService['name'])
|
|
91
|
+
if not generate_service(
|
|
92
|
+
dService,
|
|
93
|
+
oArgs.format,
|
|
94
|
+
oArgs.output.rstrip('/')
|
|
95
|
+
):
|
|
96
|
+
print('Failed to generate "%s"' % dService['name'],
|
|
97
|
+
file = stderr)
|
|
98
|
+
return 1
|
|
99
|
+
|
|
100
|
+
# Return OK
|
|
101
|
+
return 0
|
|
102
|
+
|
|
103
|
+
# Only run if called directly
|
|
104
|
+
if __name__ == '__main__':
|
|
105
|
+
exit(cli())
|
body/docs/parser.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# coding=utf8
|
|
2
|
+
""" Parser
|
|
3
|
+
|
|
4
|
+
Parses a method's docstring to get all the parts
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
__author__ = "Chris Nasr"
|
|
8
|
+
__version__ = "1.0.0"
|
|
9
|
+
__copyright__ = "Ouroboros Coding Inc."
|
|
10
|
+
__email__ = "chris@ouroboroscoding.com"
|
|
11
|
+
__created__ = "2025-02-23"
|
|
12
|
+
|
|
13
|
+
# Python imports
|
|
14
|
+
import re
|
|
15
|
+
|
|
16
|
+
# Regular expressions
|
|
17
|
+
_google_section = re.compile(r'^([^\s:]+(?: [^\s:]+)*):\s*(.*)$')
|
|
18
|
+
_google_whitespace = re.compile(r'^(\s+)')
|
|
19
|
+
|
|
20
|
+
def parse_google(s: str) -> dict:
|
|
21
|
+
"""Google
|
|
22
|
+
|
|
23
|
+
Parsers google style docstring and returns the sections found
|
|
24
|
+
|
|
25
|
+
Arguments:
|
|
26
|
+
s (str): The docstring to parse
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
dict
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
# Split the docstring into lines
|
|
33
|
+
lLines = s.split('\n')
|
|
34
|
+
|
|
35
|
+
# Init the return with the name
|
|
36
|
+
dSections = {
|
|
37
|
+
'name': lLines[0].strip(),
|
|
38
|
+
'description': []
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
# Start with the description
|
|
42
|
+
sKey = 'description'
|
|
43
|
+
|
|
44
|
+
# Init the whitespace regex
|
|
45
|
+
oWhitespace = None
|
|
46
|
+
|
|
47
|
+
# Loop through the remaining lines
|
|
48
|
+
for sLine in lLines[1:]:
|
|
49
|
+
|
|
50
|
+
# If the line is a new section header
|
|
51
|
+
m = _google_section.match(sLine)
|
|
52
|
+
if m:
|
|
53
|
+
|
|
54
|
+
# If we need to skip it
|
|
55
|
+
if sLine[0] == '-' and sLine[-1] == '-':
|
|
56
|
+
sLine = sLine[1:-1]
|
|
57
|
+
|
|
58
|
+
# Else, continue processing the new section
|
|
59
|
+
else:
|
|
60
|
+
|
|
61
|
+
# Check the last line of the previous section, if it's an empty line
|
|
62
|
+
# delete it
|
|
63
|
+
if len(dSections[sKey]) and dSections[sKey][-1].strip() == '':
|
|
64
|
+
dSections[sKey].pop()
|
|
65
|
+
|
|
66
|
+
# If the previous section is empty
|
|
67
|
+
if not dSections[sKey]:
|
|
68
|
+
del dSections[sKey]
|
|
69
|
+
|
|
70
|
+
# Clear the whitespace
|
|
71
|
+
oWhitespace = None
|
|
72
|
+
|
|
73
|
+
# Store the new section name
|
|
74
|
+
sKey = m.group(1).lower().replace(' ', '_')
|
|
75
|
+
dSections[sKey] = []
|
|
76
|
+
|
|
77
|
+
# If we have a group 2, set it as the new line
|
|
78
|
+
if m.group(2) is not None:
|
|
79
|
+
sLine = m.group(2)
|
|
80
|
+
|
|
81
|
+
# Else, loop back around to the next line
|
|
82
|
+
else:
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
# If we have nothing in the section yet
|
|
86
|
+
if not dSections[sKey]:
|
|
87
|
+
|
|
88
|
+
# If the line is empty, skip it
|
|
89
|
+
if sLine.strip() == '':
|
|
90
|
+
continue
|
|
91
|
+
|
|
92
|
+
# If the line has whitespace
|
|
93
|
+
m = _google_whitespace.match(sLine)
|
|
94
|
+
if m:
|
|
95
|
+
oWhitespace = re.compile('^(?:%s)?(.*)' % m.group(1))
|
|
96
|
+
|
|
97
|
+
# If we have whitespace
|
|
98
|
+
if oWhitespace:
|
|
99
|
+
|
|
100
|
+
# Strip it off
|
|
101
|
+
m = oWhitespace.match(sLine)
|
|
102
|
+
sLine = m.group(1)
|
|
103
|
+
|
|
104
|
+
# Add the line to the current section
|
|
105
|
+
dSections[sKey].append(sLine)
|
|
106
|
+
|
|
107
|
+
# Check the last section
|
|
108
|
+
if not dSections[sKey]:
|
|
109
|
+
del dSections[sKey]
|
|
110
|
+
|
|
111
|
+
# Go through each section and turn lines into a string
|
|
112
|
+
for s in dSections:
|
|
113
|
+
if isinstance(dSections[s], list):
|
|
114
|
+
dSections[s] = '\n'.join(dSections[s])
|
|
115
|
+
|
|
116
|
+
# Return the sections
|
|
117
|
+
return dSections
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# {{ name }}
|
|
2
|
+
{{ description }}
|
|
3
|
+
|
|
4
|
+
## Requests
|
|
5
|
+
{% for req in requests %}
|
|
6
|
+
- [{{ req.name }}](#{{ req.name|lower|replace(' ', '-') }})
|
|
7
|
+
{% endfor %}
|
|
8
|
+
{% for req in requests %}
|
|
9
|
+
|
|
10
|
+
## {{ req.name }}
|
|
11
|
+
{{ req.description }}
|
|
12
|
+
|
|
13
|
+
{% if req.example_raw %}
|
|
14
|
+
{{ req.example_raw }}
|
|
15
|
+
{% else %}
|
|
16
|
+
{% if body %}
|
|
17
|
+
```javascript
|
|
18
|
+
import {{ body }} from '@ouroboros/{{ body }}';
|
|
19
|
+
{% if req.data_example %}const request = {{ req.data_example }};
|
|
20
|
+
{% endif %}
|
|
21
|
+
{{ body }}.{{ req.action }}(
|
|
22
|
+
'{{ req.uri }}'{% if req.data %}, request{% endif %}
|
|
23
|
+
).then(data => {}, error => {});
|
|
24
|
+
{% else %}
|
|
25
|
+
```javascript
|
|
26
|
+
import body from '@ouroboros/body';
|
|
27
|
+
{% if req.data_example %}const request = {{ req.data_example }};{% endif %}
|
|
28
|
+
body.{{ req.action }}(
|
|
29
|
+
'{{ uri }}', '{{ req.uri }}'{% if req.data %}, request{% endif %}
|
|
30
|
+
).then(data => {}, error => {});
|
|
31
|
+
{% endif %}
|
|
32
|
+
```
|
|
33
|
+
{% endif %}
|
|
34
|
+
{% if req.data %}
|
|
35
|
+
|
|
36
|
+
{% if req.data is string %}
|
|
37
|
+
### Request
|
|
38
|
+
{{ req.data }}
|
|
39
|
+
{% else %}
|
|
40
|
+
### Request variables
|
|
41
|
+
| name | type | optional | description |
|
|
42
|
+
| ---- | ---- | -------- | ----------- |
|
|
43
|
+
{% for var in req.data %}
|
|
44
|
+
| {{ var.name }} | {{ var.type|replace('|', '\|') }} | {{ var.optional }} | {{ var.descr}} |
|
|
45
|
+
{% endfor %}
|
|
46
|
+
{% endif %}
|
|
47
|
+
{% endif %}
|
|
48
|
+
{% if req.response_example %}
|
|
49
|
+
|
|
50
|
+
### Response example
|
|
51
|
+
```json
|
|
52
|
+
{{ req.response_example }}
|
|
53
|
+
```
|
|
54
|
+
{% endif %}
|
|
55
|
+
{% if req.response %}
|
|
56
|
+
|
|
57
|
+
{% if req.response is string %}
|
|
58
|
+
### Response
|
|
59
|
+
{{ req.response }}
|
|
60
|
+
{% else %}
|
|
61
|
+
### Response variables
|
|
62
|
+
| name | type | description |
|
|
63
|
+
| ---- | ---- | ----------- |
|
|
64
|
+
{% for var in req.response %}
|
|
65
|
+
| {{ var.name }} | {{ var.type|replace('|', '\|') }} | {{ var.descr}} |
|
|
66
|
+
{% endfor %}
|
|
67
|
+
{% endif %}
|
|
68
|
+
{% endif %}
|
|
69
|
+
{% if req.error %}
|
|
70
|
+
|
|
71
|
+
{% if req.error is string %}
|
|
72
|
+
### Error
|
|
73
|
+
{{ req.error }}
|
|
74
|
+
{% else %}
|
|
75
|
+
### Error codes
|
|
76
|
+
| code | constant | description |
|
|
77
|
+
| ---- | -------- | ----------- |
|
|
78
|
+
{% for var in req.error %}
|
|
79
|
+
| {{ var.code }} | {{ var.const }} | {{ var.descr}} |
|
|
80
|
+
{% endfor %}
|
|
81
|
+
{% endif %}
|
|
82
|
+
{% endif %}
|
|
83
|
+
{% endfor %}
|