Skip to content

Commit

Permalink
change logic write_lut_json and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatnamnguyengtvthcm committed Sep 25, 2023
1 parent 5fdcd3c commit fd1f612
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/builders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ needs_id
The **needs_id** builder exports all found needs and selected filter results to a set json files of each need with the name is ``id`` of need.
The build creates a folder called :ref:``needs_build_json_per_id_path`` and all file json of each need inside the given build-folder.
The build creates a folder called :ref:`needs_build_json_per_id_path` and all file json of each need inside the given build-folder.
Usage
+++++
Expand Down
16 changes: 4 additions & 12 deletions sphinx_needs/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,18 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None:
def finish(self) -> None:
env = self.env
data = SphinxNeedsData(env)
needs_dict = {}
needs = data.get_or_create_needs().values() # We need a list of needs for later filter checks
needs_config = NeedsSphinxConfig(env.config)
filter_string = needs_config.builder_filter
from sphinx_needs.filter_common import filter_needs

version = getattr(env.config, "version", "unset")
needs_list = NeedsList(env.config, self.outdir, self.srcdir)
needs_list.wipe_version(version)
filtered_needs: List[NeedsInfoType] = filter_needs(self.app, data.get_or_create_needs().values(), filter_string)
filtered_needs = filter_needs(self.app, needs, filter_string)
for need in filtered_needs:
if need["is_external"]:
needs_dict = {"id": need["id"], "docname": need["external_url"], "content": need["content"]}

else:
needs_dict = {"id": need["id"], "docname": need["docname"], "content": need["content"]}
need["docname"] = need["external_url"]

needs_list.add_need(version, needs_dict)
needs_list.add_need(version, need)
try:
needs_list.write_json("needs_lut.json")
needs_list.write_lut_json(version, "needs_lut.json")
except Exception as e:
log.error(f"Error during writing json file: {e}")
else:
Expand Down
20 changes: 17 additions & 3 deletions sphinx_needs/needsfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import sys
from datetime import datetime
from typing import Any, Dict, List
from typing import Any, List

from jsonschema import Draft7Validator
from sphinx.config import Config
Expand Down Expand Up @@ -131,8 +131,22 @@ def load_json(self, file: str) -> None:

self.log.debug(f"needs.json file loaded: {file}")

def add_lut_need(self, version: str, needs_lut: Dict[str, Any]) -> None:
self.needs_list = needs_lut
def write_lut_json(self, version: str, needs_file: str = "needs.json") -> None:
self.needs_list["created"] = datetime.now().isoformat()
self.needs_list["current_version"] = self.current_version
self.needs_list["project"] = self.project
needs_lut = self.needs_list["versions"][version]["needs"]

for need_id in list(needs_lut.keys()):
if needs_lut[need_id].get("is_external"):
self.needs_list["versions"][version]["needs"][need_id] = needs_lut[need_id].get("external")
else:
self.needs_list["versions"][version]["needs"][need_id] = needs_lut[need_id].get("docname")

needs_dir = self.outdir

with open(os.path.join(needs_dir, needs_file), "w") as f:
json.dump(self.needs_list, f, indent=4, sort_keys=True)


class Errors:
Expand Down
9 changes: 8 additions & 1 deletion sphinx_needs/templates/permalink.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@
keys.forEach((key, index) => {
if (key === id) {
const need = needs_obj[key];
docname = need['docname'];
if (typeof need === "object"){
// need.json file
docname = need['docname'];
}
else {
// need_lut file only value is docname or external_link
docname = need
}
return;
}
});
Expand Down
18 changes: 3 additions & 15 deletions tests/__snapshots__/test_needs_look_up.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,9 @@
}),
'filters_amount': 0,
'needs': dict({
'TC_001': dict({
'description': '',
'docname': 'index',
'id': 'TC_001',
}),
'TC_NEG_001': dict({
'description': '',
'docname': 'index',
'id': 'TC_NEG_001',
}),
'US_63252': dict({
'description': '',
'docname': 'index',
'id': 'US_63252',
}),
'TC_001': 'index',
'TC_NEG_001': 'index',
'US_63252': 'index',
}),
'needs_amount': 3,
}),
Expand Down
2 changes: 0 additions & 2 deletions tests/test_needs_look_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ def test_doc_needs_lut_builder(test_app, snapshot):
app = test_app
app.build()
needs_list = json.loads(Path(app.outdir, "needs_lut.json").read_text())
print(needs_list)
print(snapshot(exclude=props("created")))
assert needs_list == snapshot(exclude=props("created"))

0 comments on commit fd1f612

Please sign in to comment.