|
| 1 | +import sys |
| 2 | +from paths_cli.compiling.core import Parameter |
| 3 | +from .json_type_handlers import json_type_to_string |
| 4 | +from .config_handler import DocCategoryInfo |
| 5 | + |
| 6 | +PARAMETER_RST = """* **{p.name}**{type_str} - {p.description}{required}\n""" |
| 7 | + |
| 8 | + |
| 9 | +class DocsGenerator: |
| 10 | + """This generates the RST to describe options for compile input files. |
| 11 | + """ |
| 12 | + |
| 13 | + parameter_template = PARAMETER_RST |
| 14 | + _ANCHOR_SEP = "--" |
| 15 | + def __init__(self, config): |
| 16 | + self.config = config |
| 17 | + |
| 18 | + def format_parameter(self, parameter, type_str=None): |
| 19 | + required = " (required)" if parameter.required else "" |
| 20 | + return self.parameter_template.format( |
| 21 | + p=parameter, type_str=type_str, required=required |
| 22 | + ) |
| 23 | + |
| 24 | + def _get_cat_info(self, category_plugin): |
| 25 | + cat_info = self.config.get(category_plugin.label, None) |
| 26 | + if cat_info is None: |
| 27 | + cat_info = DocCategoryInfo(category_plugin.label) |
| 28 | + return cat_info |
| 29 | + |
| 30 | + def generate_category_rst(self, category_plugin, type_required=True): |
| 31 | + cat_info = self._get_cat_info(category_plugin) |
| 32 | + rst = f".. _compiling--{category_plugin.label}:\n\n" |
| 33 | + rst += f"{cat_info.header}\n{'=' * len(str(cat_info.header))}\n\n" |
| 34 | + if cat_info.description: |
| 35 | + rst += cat_info.description + "\n\n" |
| 36 | + rst += ".. contents:: :local:\n\n" |
| 37 | + for obj in category_plugin.type_dispatch.values(): |
| 38 | + rst += self.generate_plugin_rst( |
| 39 | + obj, category_plugin.label, type_required |
| 40 | + ) |
| 41 | + return rst |
| 42 | + |
| 43 | + def generate_plugin_rst(self, plugin, strategy_name, |
| 44 | + type_required=True): |
| 45 | + rst_anchor = f".. _{strategy_name}{self._ANCHOR_SEP}{plugin.name}:" |
| 46 | + rst = f"{rst_anchor}\n\n{plugin.name}\n{'-' * len(plugin.name)}\n\n" |
| 47 | + if plugin.description: |
| 48 | + rst += plugin.description + "\n\n" |
| 49 | + if type_required: |
| 50 | + type_param = Parameter( |
| 51 | + "type", |
| 52 | + json_type="", |
| 53 | + loader=None, |
| 54 | + description=(f"type identifier; must exactly match the " |
| 55 | + f"string ``{plugin.name}``"), |
| 56 | + ) |
| 57 | + rst += self.format_parameter( |
| 58 | + type_param, type_str="" |
| 59 | + ) |
| 60 | + |
| 61 | + name_param = Parameter( |
| 62 | + "name", |
| 63 | + json_type="string", |
| 64 | + loader=None, |
| 65 | + default="", |
| 66 | + description="name this object in order to reuse it", |
| 67 | + ) |
| 68 | + rst += self.format_parameter(name_param, type_str=" (*string*)") |
| 69 | + for param in plugin.parameters: |
| 70 | + type_str = f" ({json_type_to_string(param.json_type)})" |
| 71 | + rst += self.format_parameter(param, type_str) |
| 72 | + |
| 73 | + rst += "\n\n" |
| 74 | + return rst |
| 75 | + |
| 76 | + def _get_filename(self, cat_info): |
| 77 | + fname = str(cat_info.header).lower() |
| 78 | + fname = fname.translate(str.maketrans(' ', '_')) |
| 79 | + return f"{fname}.rst" |
| 80 | + |
| 81 | + def generate(self, category_plugins, stdout=False): |
| 82 | + for plugin in category_plugins: |
| 83 | + rst = self.generate_category_rst(plugin) |
| 84 | + if stdout: |
| 85 | + sys.stdout.write(rst) |
| 86 | + sys.stdout.flush() |
| 87 | + else: |
| 88 | + cat_info = self._get_cat_info(plugin) |
| 89 | + filename = self._get_filename(cat_info) |
| 90 | + with open(filename, 'w') as f: |
| 91 | + f.write(rst) |
0 commit comments