|
| 1 | +""" |
| 2 | +Sphinx extension for viewing feature toggle annotations. |
| 3 | +""" |
| 4 | +import os |
| 5 | + |
| 6 | +import pkg_resources |
| 7 | + |
| 8 | +from code_annotations.base import AnnotationConfig |
| 9 | +from code_annotations.find_static import StaticSearch |
| 10 | +from docutils import nodes |
| 11 | +from sphinx.util.docutils import SphinxDirective |
| 12 | + |
| 13 | + |
| 14 | +def find_feature_toggles(source_path): |
| 15 | + """ |
| 16 | + Find the feature toggles as defined in the configuration file. |
| 17 | +
|
| 18 | + Return: |
| 19 | + toggles (dict): feature toggles indexed by name. |
| 20 | + """ |
| 21 | + config_path = pkg_resources.resource_filename( |
| 22 | + "code_annotations", |
| 23 | + os.path.join("config_and_tools", "config", "feature_toggle_annotations.yaml"), |
| 24 | + ) |
| 25 | + config = AnnotationConfig( |
| 26 | + config_path, verbosity=0, source_path_override=source_path |
| 27 | + ) |
| 28 | + results = StaticSearch(config).search() |
| 29 | + |
| 30 | + toggles = {} |
| 31 | + current_entry = {} |
| 32 | + for filename, entries in results.items(): |
| 33 | + for entry in entries: |
| 34 | + key = entry["annotation_token"] |
| 35 | + value = entry["annotation_data"] |
| 36 | + if key == ".. toggle_name:": |
| 37 | + toggle_name = value |
| 38 | + toggles[toggle_name] = { |
| 39 | + "filename": filename, |
| 40 | + "line_number": entry["line_number"], |
| 41 | + } |
| 42 | + current_entry = toggles[toggle_name] |
| 43 | + else: |
| 44 | + current_entry[key] = value |
| 45 | + |
| 46 | + return toggles |
| 47 | + |
| 48 | + |
| 49 | +class FeatureToggles(SphinxDirective): |
| 50 | + """ |
| 51 | + Sphinx directive to list the feature toggles in a single documentation page. |
| 52 | +
|
| 53 | + Use this directive as follows:: |
| 54 | +
|
| 55 | + .. featuretoggles:: |
| 56 | +
|
| 57 | + This directive supports the following configuration parameters: |
| 58 | +
|
| 59 | + - ``featuretoggles_source_path``: absolute path to the repository file tree. E.g: |
| 60 | +
|
| 61 | + featuretoggles_source_path = os.path.join(os.path.dirname(__file__), "..", "..") |
| 62 | +
|
| 63 | + - ``featuretoggles_repo_url``: Github repository where the code is hosted. E.g: |
| 64 | +
|
| 65 | + featuretoggles_repo_url = "https://github.com/edx/myrepo" |
| 66 | +
|
| 67 | + - ``featuretoggles_repo_version``: current version of the git repository. E.g: |
| 68 | +
|
| 69 | + import git |
| 70 | + try: |
| 71 | + repo = git.Repo(search_parent_directories=True) |
| 72 | + featuretoggles_repo_version = repo.head.object.hexsha |
| 73 | + except git.InvalidGitRepositoryError: |
| 74 | + featuretoggles_repo_version = "master" |
| 75 | + """ |
| 76 | + |
| 77 | + required_arguments = 0 |
| 78 | + optional_arguments = 0 |
| 79 | + option_spec = {} |
| 80 | + |
| 81 | + def run(self): |
| 82 | + """ |
| 83 | + Public interface of the Directive class. |
| 84 | +
|
| 85 | + Return: |
| 86 | + nodes (list): nodes to be appended to the resulting document. |
| 87 | + """ |
| 88 | + toggle_nodes = list(self.iter_nodes()) |
| 89 | + return [nodes.section("", *toggle_nodes, ids=["featuretoggles"])] |
| 90 | + |
| 91 | + def iter_nodes(self): |
| 92 | + """ |
| 93 | + Iterate on the docutils nodes generated by this directive. |
| 94 | + """ |
| 95 | + toggles = find_feature_toggles(self.env.config.featuretoggles_source_path) |
| 96 | + for toggle_name in sorted(toggles): |
| 97 | + toggle = toggles[toggle_name] |
| 98 | + yield nodes.title(text=toggle_name) |
| 99 | + toggle_default_value = toggle.get(".. toggle_default:", "Not defined") |
| 100 | + toggle_default_node = nodes.literal(text=quote_value(toggle_default_value)) |
| 101 | + yield nodes.paragraph("", "Default: ", toggle_default_node) |
| 102 | + yield nodes.paragraph( |
| 103 | + "", |
| 104 | + "Source: ", |
| 105 | + nodes.reference( |
| 106 | + text="{} (line {})".format( |
| 107 | + toggle["filename"], toggle["line_number"] |
| 108 | + ), |
| 109 | + refuri="{}/blob/{}/{}#L{}".format( |
| 110 | + self.env.config.featuretoggles_repo_url, |
| 111 | + self.env.config.featuretoggles_repo_version, |
| 112 | + toggle["filename"], |
| 113 | + toggle["line_number"], |
| 114 | + ), |
| 115 | + ), |
| 116 | + ) |
| 117 | + yield nodes.paragraph(text=toggle.get(".. toggle_description:", "")) |
| 118 | + |
| 119 | + |
| 120 | +def quote_value(value): |
| 121 | + """ |
| 122 | + Quote a Python object if it is string-like. |
| 123 | + """ |
| 124 | + if value in ("True", "False", "None"): |
| 125 | + return str(value) |
| 126 | + try: |
| 127 | + float(value) |
| 128 | + return str(value) |
| 129 | + except ValueError: |
| 130 | + pass |
| 131 | + if isinstance(value, str): |
| 132 | + return '"{}"'.format(value) |
| 133 | + return str(value) |
| 134 | + |
| 135 | + |
| 136 | +def setup(app): |
| 137 | + """ |
| 138 | + Declare the Sphinx extension. |
| 139 | + """ |
| 140 | + app.add_config_value( |
| 141 | + "featuretoggles_source_path", os.path.abspath(".."), "env", |
| 142 | + ) |
| 143 | + app.add_config_value("featuretoggles_repo_url", "", "env") |
| 144 | + app.add_config_value("featuretoggles_repo_version", "master", "env") |
| 145 | + app.add_directive("featuretoggles", FeatureToggles) |
| 146 | + |
| 147 | + return { |
| 148 | + "version": "0.1", |
| 149 | + "parallel_read_safe": True, |
| 150 | + "parallel_write_safe": True, |
| 151 | + } |
0 commit comments