|
| 1 | +"""Patches process_generate_options in sphinx.ext.autosummary to add change |
| 2 | +the suffix of items in configuration option |
| 3 | +custom_autosummary_names_with_new_suffix to |
| 4 | +custom_autosummary_new_suffix. For example: |
| 5 | +
|
| 6 | +```python |
| 7 | +custom_autosummary_names_with_new_suffix = { |
| 8 | + "sklearn.cluster.dbscan", |
| 9 | + "sklearn.cluster.optics", |
| 10 | + "sklearn.covariance.oas", |
| 11 | + "sklearn.decomposition.fastica" |
| 12 | +} |
| 13 | +custom_autosummary_new_suffix = "-lowercase.rst" |
| 14 | +custom_autosummary_generated_dirname = os.path.join('modules', 'generated') |
| 15 | +
|
| 16 | +This extension monkeypatches `os.path.join` used by |
| 17 | +https://github.com/sphinx-doc/sphinx/blob/7ffd6ccee8b0c6316159c4295e2f44f8c57b90d6/sphinx/ext/autosummary/generate.py#L168 |
| 18 | +to return a filename with a new suffix. |
| 19 | +``` |
| 20 | +""" |
| 21 | +import os |
| 22 | +import inspect |
| 23 | +from contextlib import contextmanager |
| 24 | + |
| 25 | +from sphinx.ext.autosummary import get_rst_suffix |
| 26 | +from sphinx.ext.autosummary import process_generate_options |
| 27 | + |
| 28 | + |
| 29 | +@contextmanager |
| 30 | +def patch_os_path_join(generated_dirname, filename_map): |
| 31 | + orig_os_path_join = os.path.join |
| 32 | + |
| 33 | + def custom_os_path_join(a, *p): |
| 34 | + path = orig_os_path_join(a, *p) |
| 35 | + if len(p) != 1: |
| 36 | + return path |
| 37 | + |
| 38 | + dirname, name_with_suffix = os.path.split(path) |
| 39 | + if not (dirname.endswith(generated_dirname) and |
| 40 | + name_with_suffix in filename_map): |
| 41 | + return path |
| 42 | + |
| 43 | + return orig_os_path_join(dirname, filename_map[name_with_suffix]) |
| 44 | + |
| 45 | + os.path.join = custom_os_path_join |
| 46 | + yield |
| 47 | + os.path.join = orig_os_path_join |
| 48 | + |
| 49 | + |
| 50 | +def process_generate_options_custom_files(app): |
| 51 | + |
| 52 | + orig_suffix = get_rst_suffix(app) |
| 53 | + new_suffix = app.config.custom_autosummary_new_suffix |
| 54 | + generated_dirname = app.config.custom_autosummary_generated_dirname |
| 55 | + filename_map = { |
| 56 | + name + orig_suffix: name + new_suffix |
| 57 | + for name in app.config.custom_autosummary_names_with_new_suffix |
| 58 | + } |
| 59 | + |
| 60 | + with patch_os_path_join(generated_dirname, filename_map): |
| 61 | + process_generate_options(app) |
| 62 | + |
| 63 | + |
| 64 | +def setup(app): |
| 65 | + app.setup_extension('sphinx.ext.autosummary') |
| 66 | + app.add_config_value( |
| 67 | + 'custom_autosummary_names_with_new_suffix', set(), None) |
| 68 | + app.add_config_value( |
| 69 | + 'custom_autosummary_new_suffix', '-lowercase.rst', None) |
| 70 | + app.add_config_value( |
| 71 | + 'custom_autosummary_generated_dirname', '', None) |
| 72 | + |
| 73 | + # Override process_generate_options added by sphinx.ext.autosummary |
| 74 | + builder_inited_listeners = app.events.listeners["builder-inited"] |
| 75 | + |
| 76 | + for listener_id, obj in builder_inited_listeners.items(): |
| 77 | + if (inspect.isfunction(obj) |
| 78 | + and obj.__name__ == "process_generate_options"): |
| 79 | + builder_inited_listeners[listener_id] = \ |
| 80 | + process_generate_options_custom_files |
| 81 | + break |
0 commit comments