|
| 1 | +""" |
| 2 | +Sphinx extension for viewing (non-toggle) setting annotations. |
| 3 | +""" |
| 4 | +import os |
| 5 | + |
| 6 | +import pkg_resources |
| 7 | + |
| 8 | +from docutils import nodes |
| 9 | +from docutils.parsers.rst import directives |
| 10 | +from sphinx.util.docutils import SphinxDirective |
| 11 | + |
| 12 | +from .base import find_annotations, quote_value |
| 13 | + |
| 14 | + |
| 15 | +def find_settings(source_path): |
| 16 | + """ |
| 17 | + Find the Django settings as defined in the configuration file. |
| 18 | +
|
| 19 | + Return: |
| 20 | + settings (dict): Django settings indexed by name. |
| 21 | + """ |
| 22 | + config_path = pkg_resources.resource_filename( |
| 23 | + "code_annotations", |
| 24 | + os.path.join("contrib", "config", "setting_annotations.yaml"), |
| 25 | + ) |
| 26 | + return find_annotations(source_path, config_path, ".. setting_name:") |
| 27 | + |
| 28 | + |
| 29 | +class Settings(SphinxDirective): |
| 30 | + """ |
| 31 | + Sphinx directive to document Django settings in a single documentation page. |
| 32 | +
|
| 33 | + Use this directive as follows:: |
| 34 | +
|
| 35 | + .. settings:: |
| 36 | + :folder_path: lms/envs/common.py |
| 37 | +
|
| 38 | + This directive supports the following configuration parameters: |
| 39 | +
|
| 40 | + - ``settings_source_path``: absolute path to the repository file tree. E.g: |
| 41 | +
|
| 42 | + settings_source_path = os.path.join(os.path.dirname(__file__), "..", "..") |
| 43 | +
|
| 44 | + - ``settings_repo_url``: Github repository where the code is hosted. E.g: |
| 45 | +
|
| 46 | + settings_repo_url = "https://github.com/edx/myrepo" |
| 47 | +
|
| 48 | + - ``settings_repo_version``: current version of the git repository. E.g: |
| 49 | +
|
| 50 | + import git |
| 51 | + try: |
| 52 | + repo = git.Repo(search_parent_directories=True) |
| 53 | + settings_repo_version = repo.head.object.hexsha |
| 54 | + except git.InvalidGitRepositoryError: |
| 55 | + settings_repo_version = "master" |
| 56 | + """ |
| 57 | + |
| 58 | + required_arguments = 0 |
| 59 | + optional_arguments = 1 |
| 60 | + option_spec = {"folder_path": directives.unchanged} |
| 61 | + |
| 62 | + def run(self): |
| 63 | + """ |
| 64 | + Public interface of the Directive class. |
| 65 | +
|
| 66 | + Return: |
| 67 | + nodes (list): nodes to be appended to the resulting document. |
| 68 | + """ |
| 69 | + toggle_nodes = list(self.iter_nodes()) |
| 70 | + return [nodes.section("", *toggle_nodes, ids=["settings"])] |
| 71 | + |
| 72 | + def iter_nodes(self): |
| 73 | + """ |
| 74 | + Iterate on the docutils nodes generated by this directive. |
| 75 | + """ |
| 76 | + source_path = os.path.join( |
| 77 | + self.env.config.settings_source_path, self.options.get("folder_path", "") |
| 78 | + ) |
| 79 | + settings = find_settings(source_path) |
| 80 | + for setting_name in sorted(settings): |
| 81 | + setting = settings[setting_name] |
| 82 | + yield nodes.title(text=setting_name) |
| 83 | + setting_default_value = setting.get(".. setting_default:", "Not defined") |
| 84 | + setting_default_node = nodes.literal( |
| 85 | + text=quote_value(setting_default_value) |
| 86 | + ) |
| 87 | + yield nodes.paragraph("", "Default: ", setting_default_node) |
| 88 | + yield nodes.paragraph( |
| 89 | + "", |
| 90 | + "Source: ", |
| 91 | + nodes.reference( |
| 92 | + text="{} (line {})".format( |
| 93 | + setting["filename"], setting["line_number"] |
| 94 | + ), |
| 95 | + refuri="{}/blob/{}/{}#L{}".format( |
| 96 | + self.env.config.settings_repo_url, |
| 97 | + self.env.config.settings_repo_version, |
| 98 | + setting["filename"], |
| 99 | + setting["line_number"], |
| 100 | + ), |
| 101 | + ), |
| 102 | + ) |
| 103 | + yield nodes.paragraph(text=setting.get(".. setting_description:", "")) |
| 104 | + if setting.get(".. setting_warning:") not in (None, "None", "n/a", "N/A"): |
| 105 | + yield nodes.warning( |
| 106 | + "", nodes.paragraph("", setting[".. setting_warning:"]) |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def setup(app): |
| 111 | + """ |
| 112 | + Declare the Sphinx extension. |
| 113 | + """ |
| 114 | + app.add_config_value( |
| 115 | + "settings_source_path", os.path.abspath(".."), "env", |
| 116 | + ) |
| 117 | + app.add_config_value("settings_repo_url", "", "env") |
| 118 | + app.add_config_value("settings_repo_version", "master", "env") |
| 119 | + app.add_directive("settings", Settings) |
| 120 | + |
| 121 | + return { |
| 122 | + "version": "0.1", |
| 123 | + "parallel_read_safe": True, |
| 124 | + "parallel_write_safe": True, |
| 125 | + } |
0 commit comments