-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ NEW: Add NeedsLookUpTableBuilder
#961
base: master
Are you sure you want to change the base?
Changes from 11 commits
bdc4d77
37deab7
4095a0d
b144bc3
0af5175
c9a10fd
db592c0
99dc2dd
9452708
9d6badf
adfc885
02e65be
5199edc
d4e4f04
390dd36
7177c03
61dcbef
98030a8
25d42f0
994b6b1
4f5d185
78aedd7
e86bb39
3a0f270
c6f0c2d
4babb2d
4cec93b
09b14ca
0230f3d
271f6a6
036c723
d3e0f90
e144909
d229036
4ead31a
c6723ea
d0e7849
2faeec3
3d32679
de90ff2
569d5a6
830c12a
a93f75f
ff74834
b69da59
27bd083
c77162c
5fdcd3c
fd1f612
6649155
c3bb82c
0a20d81
4472aed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1870,7 +1870,7 @@ needs_permalink_file | |
The option specifies the name of the permalink html file, | ||
which will be copied to the html build directory during build. | ||
|
||
The permalink web site will load a ``needs.json`` file as specified | ||
The permalink web site will load a ``needs.json`` or ``needs_lut.json`` file as specified | ||
by :ref:`needs_permalink_data` and re-direct the web browser to the html document | ||
of the need, which is specified by appending the need ID as a query | ||
parameter, e.g., ``http://localhost:8000/permalink.html?id=REQ_4711``. | ||
|
@@ -1902,6 +1902,7 @@ an absolute path (on the web server) or an URL. | |
|
||
Default value: ``needs.json`` | ||
|
||
You can choice option ``needs_lut.json`` as detail :ref:`needs_lut_build_json` | ||
nhatnamnguyengtvthcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. _needs_constraints: | ||
|
||
|
@@ -2338,3 +2339,22 @@ If true, need options like status, tags or links are collapsed and shown only af | |
Default value: True | ||
|
||
Can be overwritten for each single need by setting :ref:`need_collapse`. | ||
|
||
.. __needs_lut_build_json: | ||
|
||
needs_lut_build_json | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. headline too long |
||
Builds a ``needs_lut.json`` file during other builds, like ``html``. Different from ``needs.json``, ``needs_lut.json`` only include list of key ``id`` and value in list [``docname``, ``external_url``]. | ||
A helpful load data fastly when you need improve performance. | ||
nhatnamnguyengtvthcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Default: False | ||
|
||
Example: | ||
|
||
.. code-block:: python | ||
|
||
needs_lut_build_json = False | ||
|
||
.. hint:: | ||
|
||
The created ``needs_lut.json`` file gets stored in the ``outdir`` of the current builder. | ||
So if ``html`` is used as builder, the final location is e.g. ``_build/html/needs_lut.json``. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import json | ||
import os | ||
from typing import Iterable, Optional, Set | ||
|
||
|
@@ -80,7 +81,6 @@ def get_target_uri(self, _docname: str, _typ: Optional[str] = None) -> str: | |
|
||
|
||
def build_needs_json(app: Sphinx, _exception: Exception) -> None: | ||
|
||
env = unwrap(app.env) | ||
|
||
if not env.config.needs_build_json: | ||
|
@@ -158,3 +158,66 @@ def build_needumls_pumls(app: Sphinx, _exception: Exception) -> None: | |
needs_builder.set_environment(env) | ||
|
||
needs_builder.finish() | ||
|
||
|
||
class NeedsLookUpTableBuilder(Builder): | ||
danwos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name = "needs_lut" | ||
format = "json" | ||
file_suffix = ".txt" | ||
links_suffix = None | ||
|
||
def write_doc(self, docname: str, doctree: nodes.document) -> None: | ||
pass | ||
|
||
def finish(self) -> None: | ||
env = unwrap(self.env) | ||
needs = env.needs_all_needs.values() | ||
needs_dict = {} | ||
for need in needs: | ||
if need["is_external"]: | ||
needs_dict[need["id"]] = need["external_url"] | ||
else: | ||
needs_dict[need["id"]] = need["docname"] | ||
|
||
try: | ||
fname = os.path.join(self.outdir, "needs_lut.json") | ||
with open(fname, "w", encoding="utf-8") as f: | ||
json.dump(needs_dict, f, indent=4) | ||
except Exception as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add this part (dict creation + json.dump) also to NeedsList as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should stay with |
||
log.error(f"Error during writing json file: {e}") | ||
else: | ||
log.info("Needs doc lookup table json successfully created") | ||
nhatnamnguyengtvthcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def get_outdated_docs(self) -> Iterable[str]: | ||
return [] | ||
|
||
def prepare_writing(self, _docnames: Set[str]) -> None: | ||
pass | ||
|
||
def write_doc_serialized(self, _docname: str, _doctree: nodes.document) -> None: | ||
pass | ||
|
||
def cleanup(self) -> None: | ||
pass | ||
|
||
def get_target_uri(self, _docname: str, _typ: Optional[str] = None) -> str: | ||
return "" | ||
|
||
|
||
def build_needs_look_up_json(app: Sphinx, _exception: Exception) -> None: | ||
env = unwrap(app.env) | ||
|
||
if not env.config.needs_lut_build_json: | ||
return | ||
|
||
# Do not create an additional look up table json, if builder is already in use. | ||
if isinstance(app.builder, NeedsLookUpTableBuilder): | ||
return | ||
|
||
try: | ||
needs_lut_builder = NeedsLookUpTableBuilder(app, env) | ||
except TypeError: | ||
needs_lut_builder = NeedsLookUpTableBuilder(app) | ||
needs_lut_builder.set_environment(env) | ||
|
||
needs_lut_builder.finish() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,10 @@ | |
from sphinx_needs.api.configuration import add_extra_option | ||
from sphinx_needs.builder import ( | ||
NeedsBuilder, | ||
NeedsLookUpTableBuilder, | ||
NeedumlsBuilder, | ||
build_needs_json, | ||
build_needs_look_up_json, | ||
build_needumls_pumls, | ||
) | ||
from sphinx_needs.config import NEEDS_CONFIG | ||
|
@@ -141,6 +143,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: | |
|
||
app.add_builder(NeedsBuilder) | ||
app.add_builder(NeedumlsBuilder) | ||
app.add_builder(NeedsLookUpTableBuilder) | ||
app.add_config_value( | ||
"needs_types", | ||
[ | ||
|
@@ -256,6 +259,8 @@ def setup(app: Sphinx) -> Dict[str, Any]: | |
|
||
app.add_config_value("needs_build_needumls", "", "html", types=[str]) | ||
|
||
app.add_config_value("needs_lut_build_json", False, "html", types=[bool]) | ||
|
||
# Permalink related config values. | ||
# path to permalink.html; absolute path from web-root | ||
app.add_config_value("needs_permalink_file", "permalink.html", "html") | ||
|
@@ -279,6 +284,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: | |
# | ||
app.add_config_value("needs_debug_measurement", False, "html", types=[dict]) | ||
|
||
app.add_config_value("needs_permalink_url", None, "html") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this used and documented? |
||
# Define nodes | ||
app.add_node(Need, html=(html_visit, html_depart), latex=(latex_visit, latex_depart)) | ||
app.add_node( | ||
|
@@ -370,6 +376,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: | |
app.connect("build-finished", process_warnings) | ||
app.connect("build-finished", build_needs_json) | ||
app.connect("build-finished", build_needumls_pumls) | ||
app.connect("build-finished", build_needs_look_up_json) | ||
app.connect("build-finished", debug.process_timing) | ||
app.connect("env-updated", install_lib_static_files) | ||
app.connect("env-updated", install_permalink_file) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_app", [{"buildername": "needs_lut", "srcdir": "doc_test/doc_needs_builder"}], indirect=True | ||
) | ||
def test_doc_needs_id_builder(test_app): | ||
app = test_app | ||
app.build() | ||
|
||
needs_json = Path(app.outdir, "needs_lut.json") | ||
with open(needs_json) as needs_file: | ||
needs_file_content = needs_file.read() | ||
|
||
needs_list = json.loads(needs_file_content) | ||
assert needs_list["TC_NEG_001"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not needed for our Sphinx-Needs docs?