Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/docstring/parser.py
469 views
1
"""The main parsing routine."""
2
import inspect
3
import typing as T
4
5
from . import epydoc
6
from . import google
7
from . import numpydoc
8
from . import rest
9
from .attrdoc import add_attribute_docstrings
10
from .common import Docstring
11
from .common import DocstringStyle
12
from .common import ParseError
13
from .common import RenderingStyle
14
15
_STYLE_MAP = {
16
DocstringStyle.REST: rest,
17
DocstringStyle.GOOGLE: google,
18
DocstringStyle.NUMPYDOC: numpydoc,
19
DocstringStyle.EPYDOC: epydoc,
20
}
21
22
23
def parse(
24
text: T.Optional[str], style: DocstringStyle = DocstringStyle.AUTO,
25
) -> Docstring:
26
"""Parse the docstring into its components.
27
28
:param text: docstring text to parse
29
:param style: docstring style
30
:returns: parsed docstring representation
31
"""
32
if style != DocstringStyle.AUTO:
33
return _STYLE_MAP[style].parse(text)
34
35
exc: T.Optional[Exception] = None
36
rets = []
37
for module in _STYLE_MAP.values():
38
try:
39
ret = module.parse(text)
40
except ParseError as ex:
41
exc = ex
42
else:
43
rets.append(ret)
44
45
if not rets and exc is not None:
46
raise exc
47
48
return sorted(rets, key=lambda d: len(d.meta), reverse=True)[0]
49
50
51
def parse_from_object(
52
obj: T.Any,
53
style: DocstringStyle = DocstringStyle.AUTO,
54
) -> Docstring:
55
"""Parse the object's docstring(s) into its components.
56
57
The object can be anything that has a ``__doc__`` attribute. In contrast to
58
the ``parse`` function, ``parse_from_object`` is able to parse attribute
59
docstrings which are defined in the source code instead of ``__doc__``.
60
61
Currently only attribute docstrings defined at class and module levels are
62
supported. Attribute docstrings defined in ``__init__`` methods are not
63
supported.
64
65
When given a class, only the attribute docstrings of that class are parsed,
66
not its inherited classes. This is a design decision. Separate calls to
67
this function should be performed to get attribute docstrings of parent
68
classes.
69
70
:param obj: object from which to parse the docstring(s)
71
:param style: docstring style
72
:returns: parsed docstring representation
73
"""
74
docstring = parse(obj.__doc__, style=style)
75
76
if inspect.isclass(obj) or inspect.ismodule(obj):
77
add_attribute_docstrings(obj, docstring)
78
79
return docstring
80
81
82
def compose(
83
docstring: Docstring,
84
style: DocstringStyle = DocstringStyle.AUTO,
85
rendering_style: RenderingStyle = RenderingStyle.COMPACT,
86
indent: str = ' ',
87
) -> str:
88
"""Render a parsed docstring into docstring text.
89
90
:param docstring: parsed docstring representation
91
:param style: docstring style to render
92
:param indent: the characters used as indentation in the docstring string
93
:returns: docstring text
94
"""
95
st = docstring.style if style == DocstringStyle.AUTO else style
96
if st is None:
97
raise ValueError('Docstring style must be specified')
98
return _STYLE_MAP[st].compose(
99
docstring, rendering_style=rendering_style, indent=indent,
100
)
101
102