Skip to content

Commit d3567cc

Browse files
committed
docstrings
1 parent e19b5a0 commit d3567cc

File tree

4 files changed

+137
-3
lines changed

4 files changed

+137
-3
lines changed

paths_cli/compiling/_gendocs/config_handler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77

88
def load_config(config_file):
9+
"""Load a configuration file for gendocs.
10+
11+
The configuration file should be YAML or JSON, and should map each
12+
category name to the headings necessary to fill a DocCategoryInfo
13+
instance.
14+
15+
Parameters
16+
----------
17+
config_file : str
18+
name of YAML or JSON file
19+
"""
920
loader = select_loader(config_file)
1021
with open(config_file, mode='r') as f:
1122
dct = loader(f)

paths_cli/compiling/_gendocs/docs_generator.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from .config_handler import DocCategoryInfo
55

66
PARAMETER_RST = """* **{p.name}**{type_str} - {p.description}{required}\n"""
7-
87
# TODO: add templates here instead of adding up strings in the code
98

9+
1010
class DocsGenerator:
1111
"""This generates the RST to describe options for compile input files.
1212
@@ -19,6 +19,7 @@ class DocsGenerator:
1919

2020
parameter_template = PARAMETER_RST
2121
_ANCHOR_SEP = "--"
22+
2223
def __init__(self, config):
2324
self.config = config
2425

@@ -37,6 +38,19 @@ def _get_cat_info(self, category_plugin):
3738
return cat_info
3839

3940
def generate_category_rst(self, category_plugin, type_required=True):
41+
"""Generate the RST for a given category plugin.
42+
43+
Parameters
44+
----------
45+
category_plugin : :class:`.CategoryPlugin`
46+
the plugin for which we should generate the RST page
47+
48+
Returns
49+
-------
50+
str :
51+
RST string for this category
52+
"""
53+
# TODO: move type_required to DocCategoryInfo (default True)
4054
cat_info = self._get_cat_info(category_plugin)
4155
rst = f".. _compiling--{category_plugin.label}:\n\n"
4256
rst += f"{cat_info.header}\n{'=' * len(str(cat_info.header))}\n\n"
@@ -51,6 +65,23 @@ def generate_category_rst(self, category_plugin, type_required=True):
5165

5266
def generate_plugin_rst(self, plugin, category_name,
5367
type_required=True):
68+
"""Generate the RST for a given object plugin.
69+
70+
Parameters
71+
----------
72+
plugin : class:`.InstanceCompilerPlugin`
73+
the object plugin for to generate the RST for
74+
category_name : str
75+
the name of the category for this object
76+
type_required : bool
77+
whether the ``type`` parameter is required in the dict input for
78+
compiling this type of object (usually category-dependent)
79+
80+
Returns
81+
-------
82+
str :
83+
RST string for this object plugin
84+
"""
5485
rst_anchor = f".. _{category_name}{self._ANCHOR_SEP}{plugin.name}:"
5586
rst = f"{rst_anchor}\n\n{plugin.name}\n{'-' * len(plugin.name)}\n\n"
5687
if plugin.description:
@@ -82,12 +113,27 @@ def generate_plugin_rst(self, plugin, category_name,
82113
rst += "\n\n"
83114
return rst
84115

85-
def _get_filename(self, cat_info):
116+
@staticmethod
117+
def _get_filename(cat_info):
86118
fname = str(cat_info.header).lower()
87119
fname = fname.translate(str.maketrans(' ', '_'))
88120
return f"{fname}.rst"
89121

90122
def generate(self, category_plugins, stdout=False):
123+
"""Generate RST output for the given plugins.
124+
125+
This is the main method used to generate the entire set of
126+
documentation.
127+
128+
Parameters
129+
----------
130+
category_plugin : List[:class:`.CategoryPlugin`]
131+
list of category plugins document
132+
stdout : bool
133+
if False (default) a separate output file is generated for each
134+
category plugin. If True, all text is output to stdout
135+
(particularly useful for debugging/dry runs).
136+
"""
91137
for plugin in category_plugins:
92138
rst = self.generate_category_rst(plugin)
93139
if stdout:

paths_cli/compiling/_gendocs/json_type_handlers.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
class JsonTypeHandler:
2+
"""Abstract class to obtain documentation type from JSON schema type.
3+
4+
Parameters
5+
----------
6+
is_my_type : Callable[Any] -> bool
7+
return True if this instance should handle the given input type
8+
handler : Callable[Any] -> str
9+
convert the input type to a string suitable for the RST docs
10+
"""
211
def __init__(self, is_my_type, handler):
312
self._is_my_type = is_my_type
413
self.handler = handler
514

615
def is_my_type(self, json_type):
16+
"""Determine whether this instance should handle this type.
17+
18+
Parameters
19+
----------
20+
json_type : Any
21+
input type from JSON schema
22+
23+
Returns
24+
-------
25+
bool :
26+
whether to handle this type with this instance
27+
"""
728
return self._is_my_type(json_type)
829

930
def __call__(self, json_type):
@@ -21,7 +42,7 @@ def __call__(self, json_type):
2142
def _is_listof(json_type):
2243
try:
2344
return json_type["type"] == "array"
24-
except:
45+
except: # any exception should return false (mostly Key/Type Error)
2546
return False
2647

2748

@@ -33,6 +54,18 @@ def _is_listof(json_type):
3354

3455

3556
class RefTypeHandler(JsonTypeHandler):
57+
"""Handle JSON types of the form {"$ref": "#/definitions/..."}
58+
59+
Parameters
60+
----------
61+
type_name : str
62+
the name to use in the RST type
63+
def_string : str
64+
the string following "#/definitions/" in the JSON type definition
65+
link_to : str or None
66+
if not None, the RST type will be linked with a ``:ref:`` pointing
67+
to the anchor given by ``link_to``
68+
"""
3669
def __init__(self, type_name, def_string, link_to):
3770
self.type_name = type_name
3871
self.def_string = def_string
@@ -51,6 +84,17 @@ def _refhandler(self, json_type):
5184

5285

5386
class CategoryHandler(RefTypeHandler):
87+
"""Handle JSON types for OPS category definitions.
88+
89+
OPS category definitions show up with JSON references pointing to
90+
"#/definitions/{CATEGORY}_type". This provides a convenience class over
91+
the :class:RefTypeHandler to treat OPS categories.
92+
93+
Parameters
94+
----------
95+
category : str
96+
name of the category
97+
"""
5498
def __init__(self, category):
5599
self.category = category
56100
def_string = f"{category}_type"
@@ -61,6 +105,22 @@ def __init__(self, category):
61105

62106

63107
class EvalHandler(RefTypeHandler):
108+
"""Handle JSON types for OPS custom evaluation definitions.
109+
110+
Some parameters for the OPS compiler use the OPS custom evaluation
111+
mechanism, which evaluates certain Python-like string input. These are
112+
treated as special definition types in the JSON schema, and this object
113+
provides a convenience class over :class:`.RefTypeHandler` to treat
114+
custom evaluation types.
115+
116+
Parameters
117+
----------
118+
type_name : str
119+
name of the custom evaluation type
120+
link_to : str or None
121+
if not None, the RST type will be linked with a ``:ref:`` pointing
122+
to the anchor given by ``link_to``
123+
"""
64124
def __init__(self, type_name, link_to=None):
65125
super().__init__(
66126
type_name=type_name, def_string=type_name, link_to=link_to
@@ -79,6 +139,21 @@ def __init__(self, type_name, link_to=None):
79139

80140

81141
def json_type_to_string(json_type):
142+
"""Convert JSON schema type to string for RST docs.
143+
144+
This is the primary public-facing method for dealing with JSON schema
145+
types in RST document generation.
146+
147+
Parameters
148+
----------
149+
json_type : Any
150+
the type from the JSON schema
151+
152+
Returns
153+
-------
154+
str :
155+
the type string description to be used in the RST document
156+
"""
82157
for handler in JSON_TYPE_HANDLERS:
83158
handled = handler(json_type)
84159
if handled != json_type:

paths_cli/compiling/gendocs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from paths_cli.compiling.root_compiler import _COMPILERS
44
from paths_cli.commands.compile import register_installed_plugins
55

6+
67
@click.command()
78
@click.argument("config_file")
89
@click.option("--stdout", type=bool, is_flag=True, default=False)
@@ -13,5 +14,6 @@ def main(config_file, stdout):
1314
generator = DocsGenerator(config)
1415
generator.generate(_COMPILERS.values(), stdout)
1516

17+
1618
if __name__ == "__main__":
1719
main()

0 commit comments

Comments
 (0)