omdev 0.0.0.dev313__py3-none-any.whl → 0.0.0.dev314__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.
- omdev/.manifests.json +1 -1
- omdev/magic/styles.py +9 -2
- omdev/py/docstrings/LICENSE +16 -0
- omdev/py/docstrings/__init__.py +19 -0
- omdev/py/docstrings/attrdoc.py +151 -0
- omdev/py/docstrings/common.py +285 -0
- omdev/py/docstrings/epydoc.py +198 -0
- omdev/py/docstrings/google.py +329 -0
- omdev/py/docstrings/numpydoc.py +403 -0
- omdev/py/docstrings/parser.py +84 -0
- omdev/py/docstrings/rest.py +176 -0
- omdev/scripts/pyproject.py +9 -2
- omdev/tools/antlr/gen.py +1 -0
- omdev/tools/docker.py +11 -6
- {omdev-0.0.0.dev313.dist-info → omdev-0.0.0.dev314.dist-info}/METADATA +2 -2
- {omdev-0.0.0.dev313.dist-info → omdev-0.0.0.dev314.dist-info}/RECORD +20 -11
- {omdev-0.0.0.dev313.dist-info → omdev-0.0.0.dev314.dist-info}/WHEEL +0 -0
- {omdev-0.0.0.dev313.dist-info → omdev-0.0.0.dev314.dist-info}/entry_points.txt +0 -0
- {omdev-0.0.0.dev313.dist-info → omdev-0.0.0.dev314.dist-info}/licenses/LICENSE +0 -0
- {omdev-0.0.0.dev313.dist-info → omdev-0.0.0.dev314.dist-info}/top_level.txt +0 -0
omdev/.manifests.json
CHANGED
omdev/magic/styles.py
CHANGED
@@ -20,14 +20,21 @@ class MagicStyle:
|
|
20
20
|
|
21
21
|
PY_MAGIC_STYLE = MagicStyle(
|
22
22
|
name='py',
|
23
|
-
exts=frozenset([
|
23
|
+
exts=frozenset([
|
24
|
+
'py',
|
25
|
+
]),
|
24
26
|
line_prefix='# ',
|
25
27
|
)
|
26
28
|
|
27
29
|
|
28
30
|
C_MAGIC_STYLE = MagicStyle(
|
29
31
|
name='c',
|
30
|
-
exts=frozenset([
|
32
|
+
exts=frozenset([
|
33
|
+
'c',
|
34
|
+
'cc',
|
35
|
+
'cpp',
|
36
|
+
'cu',
|
37
|
+
]),
|
31
38
|
line_prefix='// ',
|
32
39
|
block_prefix_suffix=('/* ', '*/'),
|
33
40
|
)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Marcin Kurczewski
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
6
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
8
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
11
|
+
Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
14
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
15
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
16
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,19 @@
|
|
1
|
+
"""Parse docstrings as per Sphinx notation."""
|
2
|
+
# https://github.com/rr-/docstring_parser/tree/4951137875e79b438d52a18ac971ec0c28ef269c
|
3
|
+
|
4
|
+
from .common import ( # noqa
|
5
|
+
Docstring,
|
6
|
+
DocstringDeprecated,
|
7
|
+
DocstringMeta,
|
8
|
+
DocstringParam,
|
9
|
+
DocstringRaises,
|
10
|
+
DocstringReturns,
|
11
|
+
DocstringStyle,
|
12
|
+
ParseError,
|
13
|
+
RenderingStyle,
|
14
|
+
)
|
15
|
+
|
16
|
+
from .parser import ( # noqa
|
17
|
+
parse,
|
18
|
+
parse_from_object,
|
19
|
+
)
|
@@ -0,0 +1,151 @@
|
|
1
|
+
"""
|
2
|
+
Attribute docstrings parsing.
|
3
|
+
|
4
|
+
.. seealso:: https://peps.python.org/pep-0257/#what-is-a-docstring
|
5
|
+
"""
|
6
|
+
import ast
|
7
|
+
import inspect
|
8
|
+
import textwrap
|
9
|
+
import types
|
10
|
+
import typing as ta
|
11
|
+
|
12
|
+
from .common import Docstring
|
13
|
+
from .common import DocstringParam
|
14
|
+
|
15
|
+
|
16
|
+
##
|
17
|
+
|
18
|
+
|
19
|
+
_AST_CONSTANT_ATTR: ta.Mapping[type, str] = {
|
20
|
+
ast.Constant: 'value',
|
21
|
+
}
|
22
|
+
|
23
|
+
|
24
|
+
def ast_get_constant_value(node: ast.AST) -> ta.Any:
|
25
|
+
"""Return the constant's value if the given node is a constant."""
|
26
|
+
|
27
|
+
return getattr(node, _AST_CONSTANT_ATTR[type(node)])
|
28
|
+
|
29
|
+
|
30
|
+
def ast_unparse(node: ast.AST) -> str | None:
|
31
|
+
"""Convert the AST node to source code as a string."""
|
32
|
+
|
33
|
+
if hasattr(ast, 'unparse'):
|
34
|
+
return ast.unparse(node)
|
35
|
+
# Support simple cases in Python < 3.9
|
36
|
+
if isinstance(node, (ast.Num, ast.NameConstant, ast.Constant)):
|
37
|
+
return str(ast_get_constant_value(node))
|
38
|
+
if isinstance(node, ast.Name):
|
39
|
+
return node.id
|
40
|
+
return None
|
41
|
+
|
42
|
+
|
43
|
+
def ast_is_literal_str(node: ast.AST) -> bool:
|
44
|
+
"""Return True if the given node is a literal string."""
|
45
|
+
|
46
|
+
return (
|
47
|
+
isinstance(node, ast.Expr)
|
48
|
+
and isinstance(node.value, ast.Constant)
|
49
|
+
and isinstance(ast_get_constant_value(node.value), str)
|
50
|
+
)
|
51
|
+
|
52
|
+
|
53
|
+
def ast_get_attribute(
|
54
|
+
node: ast.AST,
|
55
|
+
) -> tuple[str, str | None, str | None] | None:
|
56
|
+
"""Return name, type and default if the given node is an attribute."""
|
57
|
+
|
58
|
+
if isinstance(node, (ast.Assign, ast.AnnAssign)):
|
59
|
+
target = node.targets[0] if isinstance(node, ast.Assign) else node.target
|
60
|
+
|
61
|
+
if isinstance(target, ast.Name):
|
62
|
+
type_str = None
|
63
|
+
if isinstance(node, ast.AnnAssign):
|
64
|
+
type_str = ast_unparse(node.annotation)
|
65
|
+
|
66
|
+
default = None
|
67
|
+
if node.value:
|
68
|
+
default = ast_unparse(node.value)
|
69
|
+
|
70
|
+
return target.id, type_str, default
|
71
|
+
|
72
|
+
return None
|
73
|
+
|
74
|
+
|
75
|
+
class AttributeDocstrings(ast.NodeVisitor):
|
76
|
+
"""An ast.NodeVisitor that collects attribute docstrings."""
|
77
|
+
|
78
|
+
attr_docs: ta.Any = None
|
79
|
+
prev_attr: ta.Any = None
|
80
|
+
|
81
|
+
def visit(self, node: ast.AST) -> None:
|
82
|
+
if self.prev_attr and ast_is_literal_str(node):
|
83
|
+
attr_name, attr_type, attr_default = self.prev_attr
|
84
|
+
|
85
|
+
self.attr_docs[attr_name] = (
|
86
|
+
ast_get_constant_value(node.value), # type: ignore[attr-defined]
|
87
|
+
attr_type,
|
88
|
+
attr_default,
|
89
|
+
)
|
90
|
+
|
91
|
+
self.prev_attr = ast_get_attribute(node)
|
92
|
+
|
93
|
+
if isinstance(node, (ast.ClassDef, ast.Module)):
|
94
|
+
self.generic_visit(node)
|
95
|
+
|
96
|
+
def get_attr_docs(
|
97
|
+
self,
|
98
|
+
component: ta.Any,
|
99
|
+
) -> dict[str, tuple[str, str | None, str | None]]:
|
100
|
+
"""
|
101
|
+
Get attribute docstrings from the given component.
|
102
|
+
|
103
|
+
:param component: component to process (class or module)
|
104
|
+
:returns: for each attribute docstring, a tuple with (description, type, default)
|
105
|
+
"""
|
106
|
+
|
107
|
+
self.attr_docs = {}
|
108
|
+
self.prev_attr = None
|
109
|
+
|
110
|
+
try:
|
111
|
+
source = textwrap.dedent(inspect.getsource(component))
|
112
|
+
|
113
|
+
except OSError:
|
114
|
+
pass
|
115
|
+
|
116
|
+
else:
|
117
|
+
tree = ast.parse(source)
|
118
|
+
|
119
|
+
if inspect.ismodule(component):
|
120
|
+
self.visit(tree)
|
121
|
+
|
122
|
+
elif isinstance(tree, ast.Module) and isinstance(tree.body[0], ast.ClassDef):
|
123
|
+
self.visit(tree.body[0])
|
124
|
+
|
125
|
+
return self.attr_docs
|
126
|
+
|
127
|
+
|
128
|
+
def add_attribute_docstrings(
|
129
|
+
obj: type | types.ModuleType,
|
130
|
+
docstring: Docstring,
|
131
|
+
) -> None:
|
132
|
+
"""
|
133
|
+
Add attribute docstrings found in the object's source code.
|
134
|
+
|
135
|
+
:param obj: object from which to parse attribute docstrings
|
136
|
+
:param docstring: Docstring object where found attributes are added
|
137
|
+
:returns: list with names of added attributes
|
138
|
+
"""
|
139
|
+
|
140
|
+
params = {p.arg_name for p in docstring.params}
|
141
|
+
for arg_name, (description, type_name, default) in AttributeDocstrings().get_attr_docs(obj).items():
|
142
|
+
if arg_name not in params:
|
143
|
+
param = DocstringParam(
|
144
|
+
args=['attribute', arg_name],
|
145
|
+
description=description,
|
146
|
+
arg_name=arg_name,
|
147
|
+
type_name=type_name,
|
148
|
+
is_optional=default is not None,
|
149
|
+
default=default,
|
150
|
+
)
|
151
|
+
docstring.meta.append(param)
|
@@ -0,0 +1,285 @@
|
|
1
|
+
"""Common methods for parsing."""
|
2
|
+
import enum
|
3
|
+
|
4
|
+
|
5
|
+
##
|
6
|
+
|
7
|
+
|
8
|
+
PARAM_KEYWORDS = {
|
9
|
+
'arg',
|
10
|
+
'argument',
|
11
|
+
'attribute',
|
12
|
+
'key',
|
13
|
+
'keyword',
|
14
|
+
'param',
|
15
|
+
'parameter',
|
16
|
+
}
|
17
|
+
|
18
|
+
RAISES_KEYWORDS = {
|
19
|
+
'except',
|
20
|
+
'exception',
|
21
|
+
'raise',
|
22
|
+
'raises',
|
23
|
+
}
|
24
|
+
|
25
|
+
DEPRECATION_KEYWORDS = {
|
26
|
+
'deprecated',
|
27
|
+
'deprecation',
|
28
|
+
}
|
29
|
+
|
30
|
+
RETURNS_KEYWORDS = {
|
31
|
+
'return',
|
32
|
+
'returns',
|
33
|
+
}
|
34
|
+
|
35
|
+
YIELDS_KEYWORDS = {
|
36
|
+
'yield',
|
37
|
+
'yields',
|
38
|
+
}
|
39
|
+
|
40
|
+
EXAMPLES_KEYWORDS = {
|
41
|
+
'example',
|
42
|
+
'examples',
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
##
|
47
|
+
|
48
|
+
|
49
|
+
class ParseError(RuntimeError):
|
50
|
+
"""Base class for all parsing related errors."""
|
51
|
+
|
52
|
+
|
53
|
+
##
|
54
|
+
|
55
|
+
|
56
|
+
class DocstringStyle(enum.Enum):
|
57
|
+
"""Docstring style."""
|
58
|
+
|
59
|
+
REST = 1
|
60
|
+
GOOGLE = 2
|
61
|
+
NUMPYDOC = 3
|
62
|
+
EPYDOC = 4
|
63
|
+
AUTO = 255
|
64
|
+
|
65
|
+
|
66
|
+
class RenderingStyle(enum.Enum):
|
67
|
+
"""Rendering style when unparsing parsed docstrings."""
|
68
|
+
|
69
|
+
COMPACT = 1
|
70
|
+
CLEAN = 2
|
71
|
+
EXPANDED = 3
|
72
|
+
|
73
|
+
|
74
|
+
##
|
75
|
+
|
76
|
+
|
77
|
+
class DocstringMeta:
|
78
|
+
"""
|
79
|
+
Docstring meta information.
|
80
|
+
|
81
|
+
Symbolizes lines in form of
|
82
|
+
|
83
|
+
:param arg: description
|
84
|
+
:raises ValueError: if something happens
|
85
|
+
"""
|
86
|
+
|
87
|
+
def __init__(
|
88
|
+
self,
|
89
|
+
args: list[str],
|
90
|
+
description: str | None,
|
91
|
+
) -> None:
|
92
|
+
"""
|
93
|
+
Initialize self.
|
94
|
+
|
95
|
+
:param args: list of arguments. The exact content of this variable is dependent on the kind of docstring; it's
|
96
|
+
used to distinguish between custom docstring meta information items.
|
97
|
+
:param description: associated docstring description.
|
98
|
+
"""
|
99
|
+
|
100
|
+
super().__init__()
|
101
|
+
|
102
|
+
self.args = args
|
103
|
+
self.description = description
|
104
|
+
|
105
|
+
|
106
|
+
class DocstringParam(DocstringMeta):
|
107
|
+
"""DocstringMeta symbolizing :param metadata."""
|
108
|
+
|
109
|
+
def __init__(
|
110
|
+
self,
|
111
|
+
args: list[str],
|
112
|
+
description: str | None,
|
113
|
+
arg_name: str,
|
114
|
+
type_name: str | None,
|
115
|
+
is_optional: bool | None,
|
116
|
+
default: str | None,
|
117
|
+
) -> None:
|
118
|
+
"""Initialize self."""
|
119
|
+
|
120
|
+
super().__init__(args, description)
|
121
|
+
|
122
|
+
self.arg_name = arg_name
|
123
|
+
self.type_name = type_name
|
124
|
+
self.is_optional = is_optional
|
125
|
+
self.default = default
|
126
|
+
|
127
|
+
|
128
|
+
class DocstringReturns(DocstringMeta):
|
129
|
+
"""DocstringMeta symbolizing :returns or :yields metadata."""
|
130
|
+
|
131
|
+
def __init__(
|
132
|
+
self,
|
133
|
+
args: list[str],
|
134
|
+
description: str | None,
|
135
|
+
type_name: str | None,
|
136
|
+
is_generator: bool,
|
137
|
+
return_name: str | None = None,
|
138
|
+
) -> None:
|
139
|
+
"""Initialize self."""
|
140
|
+
|
141
|
+
super().__init__(args, description)
|
142
|
+
|
143
|
+
self.type_name = type_name
|
144
|
+
self.is_generator = is_generator
|
145
|
+
self.return_name = return_name
|
146
|
+
|
147
|
+
|
148
|
+
class DocstringRaises(DocstringMeta):
|
149
|
+
"""DocstringMeta symbolizing :raises metadata."""
|
150
|
+
|
151
|
+
def __init__(
|
152
|
+
self,
|
153
|
+
args: list[str],
|
154
|
+
description: str | None,
|
155
|
+
type_name: str | None,
|
156
|
+
) -> None:
|
157
|
+
"""Initialize self."""
|
158
|
+
|
159
|
+
super().__init__(args, description)
|
160
|
+
|
161
|
+
self.type_name = type_name
|
162
|
+
self.description = description
|
163
|
+
|
164
|
+
|
165
|
+
class DocstringDeprecated(DocstringMeta):
|
166
|
+
"""DocstringMeta symbolizing deprecation metadata."""
|
167
|
+
|
168
|
+
def __init__(
|
169
|
+
self,
|
170
|
+
args: list[str],
|
171
|
+
description: str | None,
|
172
|
+
version: str | None,
|
173
|
+
) -> None:
|
174
|
+
"""Initialize self."""
|
175
|
+
|
176
|
+
super().__init__(args, description)
|
177
|
+
|
178
|
+
self.version = version
|
179
|
+
self.description = description
|
180
|
+
|
181
|
+
|
182
|
+
class DocstringExample(DocstringMeta):
|
183
|
+
"""DocstringMeta symbolizing example metadata."""
|
184
|
+
|
185
|
+
def __init__(
|
186
|
+
self,
|
187
|
+
args: list[str],
|
188
|
+
snippet: str | None,
|
189
|
+
description: str | None,
|
190
|
+
) -> None:
|
191
|
+
"""Initialize self."""
|
192
|
+
|
193
|
+
super().__init__(args, description)
|
194
|
+
|
195
|
+
self.snippet = snippet
|
196
|
+
self.description = description
|
197
|
+
|
198
|
+
|
199
|
+
##
|
200
|
+
|
201
|
+
|
202
|
+
class Docstring:
|
203
|
+
"""Docstring object representation."""
|
204
|
+
|
205
|
+
def __init__(
|
206
|
+
self,
|
207
|
+
style: DocstringStyle | None = None,
|
208
|
+
) -> None:
|
209
|
+
"""Initialize self."""
|
210
|
+
|
211
|
+
super().__init__()
|
212
|
+
|
213
|
+
self.short_description: str | None = None
|
214
|
+
self.long_description: str | None = None
|
215
|
+
self.blank_after_short_description = False
|
216
|
+
self.blank_after_long_description = False
|
217
|
+
self.meta: list[DocstringMeta] = []
|
218
|
+
self.style: DocstringStyle | None = style
|
219
|
+
|
220
|
+
@property
|
221
|
+
def description(self) -> str | None:
|
222
|
+
"""
|
223
|
+
Return the full description of the function
|
224
|
+
|
225
|
+
Returns None if the docstring did not include any description
|
226
|
+
"""
|
227
|
+
|
228
|
+
ret = []
|
229
|
+
if self.short_description:
|
230
|
+
ret.append(self.short_description)
|
231
|
+
if self.blank_after_short_description:
|
232
|
+
ret.append('')
|
233
|
+
if self.long_description:
|
234
|
+
ret.append(self.long_description)
|
235
|
+
|
236
|
+
if not ret:
|
237
|
+
return None
|
238
|
+
|
239
|
+
return '\n'.join(ret)
|
240
|
+
|
241
|
+
@property
|
242
|
+
def params(self) -> list[DocstringParam]:
|
243
|
+
"""Return a list of information on function params."""
|
244
|
+
|
245
|
+
return [item for item in self.meta if isinstance(item, DocstringParam)]
|
246
|
+
|
247
|
+
@property
|
248
|
+
def raises(self) -> list[DocstringRaises]:
|
249
|
+
"""Return a list of information on the exceptions that the function may raise."""
|
250
|
+
|
251
|
+
return [item for item in self.meta if isinstance(item, DocstringRaises)]
|
252
|
+
|
253
|
+
@property
|
254
|
+
def returns(self) -> DocstringReturns | None:
|
255
|
+
"""
|
256
|
+
Return a single information on function return.
|
257
|
+
|
258
|
+
Takes the first return information.
|
259
|
+
"""
|
260
|
+
|
261
|
+
for item in self.meta:
|
262
|
+
if isinstance(item, DocstringReturns):
|
263
|
+
return item
|
264
|
+
return None
|
265
|
+
|
266
|
+
@property
|
267
|
+
def many_returns(self) -> list[DocstringReturns]:
|
268
|
+
"""Return a list of information on function return."""
|
269
|
+
|
270
|
+
return [item for item in self.meta if isinstance(item, DocstringReturns)]
|
271
|
+
|
272
|
+
@property
|
273
|
+
def deprecation(self) -> DocstringDeprecated | None:
|
274
|
+
"""Return a single information on function deprecation notes."""
|
275
|
+
|
276
|
+
for item in self.meta:
|
277
|
+
if isinstance(item, DocstringDeprecated):
|
278
|
+
return item
|
279
|
+
return None
|
280
|
+
|
281
|
+
@property
|
282
|
+
def examples(self) -> list[DocstringExample]:
|
283
|
+
"""Return a list of information on function examples."""
|
284
|
+
|
285
|
+
return [item for item in self.meta if isinstance(item, DocstringExample)]
|
@@ -0,0 +1,198 @@
|
|
1
|
+
"""
|
2
|
+
Epyoc-style docstring parsing.
|
3
|
+
|
4
|
+
.. seealso:: http://epydoc.sourceforge.net/manual-fields.html
|
5
|
+
"""
|
6
|
+
import inspect
|
7
|
+
import re
|
8
|
+
import typing as ta
|
9
|
+
|
10
|
+
from .common import Docstring
|
11
|
+
from .common import DocstringMeta
|
12
|
+
from .common import DocstringParam
|
13
|
+
from .common import DocstringRaises
|
14
|
+
from .common import DocstringReturns
|
15
|
+
from .common import DocstringStyle
|
16
|
+
from .common import ParseError
|
17
|
+
|
18
|
+
|
19
|
+
##
|
20
|
+
|
21
|
+
|
22
|
+
def _clean_str(string: str) -> str | None:
|
23
|
+
string = string.strip()
|
24
|
+
if len(string) > 0:
|
25
|
+
return string
|
26
|
+
return None
|
27
|
+
|
28
|
+
|
29
|
+
_PARAM_PAT = re.compile(r'(param|keyword|type)(\s+[_A-z][_A-z0-9]*\??):')
|
30
|
+
_RAISE_PAT = re.compile(r'(raise)(\s+[_A-z][_A-z0-9]*\??)?:')
|
31
|
+
_RETURN_PAT = re.compile(r'(return|rtype|yield|ytype):')
|
32
|
+
_META_PAT = re.compile(r'([_A-z][_A-z0-9]+)((\s+[_A-z][_A-z0-9]*\??)*):')
|
33
|
+
|
34
|
+
|
35
|
+
def parse(text: str) -> Docstring:
|
36
|
+
"""
|
37
|
+
Parse the epydoc-style docstring into its components.
|
38
|
+
|
39
|
+
:returns: parsed docstring
|
40
|
+
"""
|
41
|
+
|
42
|
+
ret = Docstring(style=DocstringStyle.EPYDOC)
|
43
|
+
if not text:
|
44
|
+
return ret
|
45
|
+
|
46
|
+
text = inspect.cleandoc(text)
|
47
|
+
match = re.search('^@', text, flags=re.MULTILINE)
|
48
|
+
if match:
|
49
|
+
desc_chunk = text[: match.start()]
|
50
|
+
meta_chunk = text[match.start():]
|
51
|
+
else:
|
52
|
+
desc_chunk = text
|
53
|
+
meta_chunk = ''
|
54
|
+
|
55
|
+
parts = desc_chunk.split('\n', 1)
|
56
|
+
ret.short_description = parts[0] or None
|
57
|
+
if len(parts) > 1:
|
58
|
+
long_desc_chunk = parts[1] or ''
|
59
|
+
ret.blank_after_short_description = long_desc_chunk.startswith('\n')
|
60
|
+
ret.blank_after_long_description = long_desc_chunk.endswith('\n\n')
|
61
|
+
ret.long_description = long_desc_chunk.strip() or None
|
62
|
+
|
63
|
+
# tokenize
|
64
|
+
stream: list[tuple[str, str, list[str], str]] = []
|
65
|
+
for match in re.finditer(r'(^@.*?)(?=^@|\Z)', meta_chunk, flags=re.DOTALL | re.MULTILINE):
|
66
|
+
chunk = match.group(0)
|
67
|
+
if not chunk:
|
68
|
+
continue
|
69
|
+
|
70
|
+
param_match = re.search(_PARAM_PAT, chunk)
|
71
|
+
raise_match = re.search(_RAISE_PAT, chunk)
|
72
|
+
return_match = re.search(_RETURN_PAT, chunk)
|
73
|
+
meta_match = re.search(_META_PAT, chunk)
|
74
|
+
|
75
|
+
match = param_match or raise_match or return_match or meta_match
|
76
|
+
if not match:
|
77
|
+
raise ParseError(f'Error parsing meta information near "{chunk}".')
|
78
|
+
|
79
|
+
desc_chunk = chunk[match.end():]
|
80
|
+
|
81
|
+
key: str
|
82
|
+
if param_match:
|
83
|
+
base = 'param'
|
84
|
+
key = match.group(1)
|
85
|
+
args = [match.group(2).strip()]
|
86
|
+
|
87
|
+
elif raise_match:
|
88
|
+
base = 'raise'
|
89
|
+
key = match.group(1)
|
90
|
+
args = [] if match.group(2) is None else [match.group(2).strip()]
|
91
|
+
|
92
|
+
elif return_match:
|
93
|
+
base = 'return'
|
94
|
+
key = match.group(1)
|
95
|
+
args = []
|
96
|
+
|
97
|
+
else:
|
98
|
+
base = 'meta'
|
99
|
+
key = match.group(1)
|
100
|
+
token = _clean_str(match.group(2).strip())
|
101
|
+
args = [] if token is None else re.split(r'\s+', token)
|
102
|
+
|
103
|
+
# Make sure we didn't match some existing keyword in an incorrect way here:
|
104
|
+
if key in [
|
105
|
+
'param',
|
106
|
+
'keyword',
|
107
|
+
'type',
|
108
|
+
'return',
|
109
|
+
'rtype',
|
110
|
+
'yield',
|
111
|
+
'ytype',
|
112
|
+
]:
|
113
|
+
raise ParseError(f'Error parsing meta information near "{chunk}".')
|
114
|
+
|
115
|
+
desc = desc_chunk.strip()
|
116
|
+
if '\n' in desc:
|
117
|
+
first_line, rest = desc.split('\n', 1)
|
118
|
+
desc = first_line + '\n' + inspect.cleandoc(rest)
|
119
|
+
stream.append((base, key, args, desc))
|
120
|
+
|
121
|
+
# Combine type_name, arg_name, and description information
|
122
|
+
params: dict[str, dict[str, ta.Any]] = {}
|
123
|
+
for base, key, args, desc in stream:
|
124
|
+
if base not in ['param', 'return']:
|
125
|
+
continue # nothing to do
|
126
|
+
|
127
|
+
(arg_name,) = args or ('return',)
|
128
|
+
info = params.setdefault(arg_name, {})
|
129
|
+
info_key = 'type_name' if 'type' in key else 'description'
|
130
|
+
info[info_key] = desc
|
131
|
+
|
132
|
+
if base == 'return':
|
133
|
+
is_generator = key in {'ytype', 'yield'}
|
134
|
+
if info.setdefault('is_generator', is_generator) != is_generator:
|
135
|
+
raise ParseError(
|
136
|
+
f'Error parsing meta information for "{arg_name}".',
|
137
|
+
)
|
138
|
+
|
139
|
+
is_done: dict[str, bool] = {}
|
140
|
+
meta_item: DocstringMeta
|
141
|
+
for base, key, args, desc in stream:
|
142
|
+
if base == 'param' and not is_done.get(args[0], False):
|
143
|
+
(arg_name,) = args
|
144
|
+
info = params[arg_name]
|
145
|
+
type_name = info.get('type_name')
|
146
|
+
|
147
|
+
if type_name and type_name.endswith('?'):
|
148
|
+
is_optional = True
|
149
|
+
type_name = type_name[:-1]
|
150
|
+
else:
|
151
|
+
is_optional = False
|
152
|
+
|
153
|
+
match = re.match(r'.*defaults to (.+)', desc, flags=re.DOTALL)
|
154
|
+
default = match.group(1).rstrip('.') if match else None
|
155
|
+
|
156
|
+
meta_item = DocstringParam(
|
157
|
+
args=[key, arg_name],
|
158
|
+
description=info.get('description'),
|
159
|
+
arg_name=arg_name,
|
160
|
+
type_name=type_name,
|
161
|
+
is_optional=is_optional,
|
162
|
+
default=default,
|
163
|
+
)
|
164
|
+
is_done[arg_name] = True
|
165
|
+
|
166
|
+
elif base == 'return' and not is_done.get('return', False):
|
167
|
+
info = params['return']
|
168
|
+
meta_item = DocstringReturns(
|
169
|
+
args=[key],
|
170
|
+
description=info.get('description'),
|
171
|
+
type_name=info.get('type_name'),
|
172
|
+
is_generator=info.get('is_generator', False),
|
173
|
+
)
|
174
|
+
is_done['return'] = True
|
175
|
+
|
176
|
+
elif base == 'raise':
|
177
|
+
(type_name,) = args or (None,)
|
178
|
+
meta_item = DocstringRaises(
|
179
|
+
args=[key, *args],
|
180
|
+
description=desc,
|
181
|
+
type_name=type_name,
|
182
|
+
)
|
183
|
+
|
184
|
+
elif base == 'meta':
|
185
|
+
meta_item = DocstringMeta(
|
186
|
+
args=[key, *args],
|
187
|
+
description=desc,
|
188
|
+
)
|
189
|
+
|
190
|
+
else:
|
191
|
+
(key, *_) = args or ('return',)
|
192
|
+
if not is_done.get(key, False):
|
193
|
+
raise ParseError
|
194
|
+
continue # don't append
|
195
|
+
|
196
|
+
ret.meta.append(meta_item)
|
197
|
+
|
198
|
+
return ret
|