Skip to content

Commit

Permalink
👌 Remove some unnecessary keys from needs.json (#1053)
Browse files Browse the repository at this point in the history
Remove additional keys from the `needs.json`, that are not intended to be exposed to the user:
- back links for user defined link types (standard back links are already ommited),
- `id_parent` and `id_complete`, which are only relevant to parts expansion.
  • Loading branch information
chrisjsewell committed Oct 30, 2023
1 parent d8b0a28 commit bc0b8c9
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 284 deletions.
13 changes: 11 additions & 2 deletions sphinx_needs/needsfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from jsonschema import Draft7Validator
from sphinx.config import Config

from sphinx_needs.config import NeedsSphinxConfig
from sphinx_needs.data import NeedsFilterType, NeedsInfoType
from sphinx_needs.logging import get_logger

Expand All @@ -31,6 +32,10 @@ class NeedsList:
"hide_tags",
"content",
"content_node",
# id_parent, id_parent are added on calls to `prepare_need_list`
# but are only relevant to parts
"id_parent",
"id_complete",
}

JSON_KEY_EXCLUSIONS_FILTERS = {
Expand Down Expand Up @@ -60,6 +65,10 @@ def __init__(self, config: Config, outdir: str, confdir: str) -> None:
"versions": {},
}
self.log = log
# also exclude back links for link types dynamically set by the user
back_link_keys = {x["option"] + "_back" for x in NeedsSphinxConfig(config).extra_links}
self._exclude_need_keys = self.JSON_KEY_EXCLUSIONS_NEEDS | back_link_keys
self._exclude_filter_keys = self.JSON_KEY_EXCLUSIONS_FILTERS | back_link_keys

def update_or_add_version(self, version: str) -> None:
if version not in self.needs_list["versions"].keys():
Expand All @@ -78,14 +87,14 @@ def update_or_add_version(self, version: str) -> None:

def add_need(self, version: str, need_info: NeedsInfoType) -> None:
self.update_or_add_version(version)
writable_needs = {key: need_info[key] for key in need_info if key not in self.JSON_KEY_EXCLUSIONS_NEEDS} # type: ignore[literal-required]
writable_needs = {key: need_info[key] for key in need_info if key not in self._exclude_need_keys} # type: ignore[literal-required]
writable_needs["description"] = need_info["content"]
self.needs_list["versions"][version]["needs"][need_info["id"]] = writable_needs
self.needs_list["versions"][version]["needs_amount"] = len(self.needs_list["versions"][version]["needs"])

def add_filter(self, version: str, need_filter: NeedsFilterType) -> None:
self.update_or_add_version(version)
writable_filters = {key: need_filter[key] for key in need_filter if key not in self.JSON_KEY_EXCLUSIONS_FILTERS} # type: ignore[literal-required]
writable_filters = {key: need_filter[key] for key in need_filter if key not in self._exclude_filter_keys} # type: ignore[literal-required]
self.needs_list["versions"][version]["filters"][need_filter["export_id"].upper()] = writable_filters
self.needs_list["versions"][version]["filters_amount"] = len(self.needs_list["versions"][version]["filters"])

Expand Down
8 changes: 0 additions & 8 deletions tests/__snapshots__/test_basic_doc.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'ST_001',
'id_complete': 'ST_001',
'id_parent': 'ST_001',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -52,8 +50,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand Down Expand Up @@ -106,8 +102,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'US_38823',
'id_complete': 'US_38823',
'id_parent': 'US_38823',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -124,8 +118,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand Down
70 changes: 0 additions & 70 deletions tests/__snapshots__/test_export_id.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@
'blocks': list([
'REQ_003',
]),
'blocks_back': list([
'REQ_005',
]),
'closed_at': '',
'completion': '',
'constraints': list([
Expand All @@ -143,8 +140,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'REQ_001',
'id_complete': 'REQ_001',
'id_parent': 'REQ_001',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -164,8 +159,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand All @@ -188,10 +181,6 @@
'template': None,
'tests': list([
]),
'tests_back': list([
'TEST_001',
'TEST_002',
]),
'title': 'My requirement',
'type': 'story',
'type_name': 'User Story',
Expand All @@ -206,8 +195,6 @@
'avatar': '',
'blocks': list([
]),
'blocks_back': list([
]),
'closed_at': '',
'completion': '',
'constraints': list([
Expand All @@ -229,8 +216,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'REQ_002',
'id_complete': 'REQ_002',
'id_parent': 'REQ_002',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -247,8 +232,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand All @@ -271,8 +254,6 @@
'template': None,
'tests': list([
]),
'tests_back': list([
]),
'title': 'My requirement 2',
'type': 'story',
'type_name': 'User Story',
Expand All @@ -287,9 +268,6 @@
'avatar': '',
'blocks': list([
]),
'blocks_back': list([
'REQ_001',
]),
'closed_at': '',
'completion': '',
'constraints': list([
Expand All @@ -311,8 +289,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'REQ_003',
'id_complete': 'REQ_003',
'id_parent': 'REQ_003',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -329,8 +305,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand All @@ -353,9 +327,6 @@
'template': None,
'tests': list([
]),
'tests_back': list([
'TEST_001',
]),
'title': 'My requirement 3',
'type': 'story',
'type_name': 'User Story',
Expand All @@ -370,8 +341,6 @@
'avatar': '',
'blocks': list([
]),
'blocks_back': list([
]),
'closed_at': '',
'completion': '',
'constraints': list([
Expand All @@ -393,8 +362,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'REQ_004',
'id_complete': 'REQ_004',
'id_parent': 'REQ_004',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -411,8 +378,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand All @@ -435,8 +400,6 @@
'template': None,
'tests': list([
]),
'tests_back': list([
]),
'title': 'My requirement 4',
'type': 'story',
'type_name': 'User Story',
Expand All @@ -452,8 +415,6 @@
'blocks': list([
'REQ_001',
]),
'blocks_back': list([
]),
'closed_at': '',
'completion': '',
'constraints': list([
Expand All @@ -479,8 +440,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'REQ_005',
'id_complete': 'REQ_005',
'id_parent': 'REQ_005',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -499,8 +458,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
'1': dict({
'content': ' awesome part',
Expand Down Expand Up @@ -552,9 +509,6 @@
'template': None,
'tests': list([
]),
'tests_back': list([
'TEST_003',
]),
'title': 'Req 5',
'type': 'story',
'type_name': 'User Story',
Expand All @@ -569,8 +523,6 @@
'avatar': '',
'blocks': list([
]),
'blocks_back': list([
]),
'closed_at': '',
'completion': '',
'constraints': list([
Expand All @@ -592,8 +544,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'TEST_001',
'id_complete': 'TEST_001',
'id_parent': 'TEST_001',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -610,8 +560,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand All @@ -636,8 +584,6 @@
'REQ_001',
'REQ_003',
]),
'tests_back': list([
]),
'title': 'Test of requirements',
'type': 'test',
'type_name': 'Test Case',
Expand All @@ -652,8 +598,6 @@
'avatar': '',
'blocks': list([
]),
'blocks_back': list([
]),
'closed_at': '',
'completion': '',
'constraints': list([
Expand All @@ -675,8 +619,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'TEST_002',
'id_complete': 'TEST_002',
'id_parent': 'TEST_002',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -694,8 +636,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand All @@ -719,8 +659,6 @@
'tests': list([
'REQ_001',
]),
'tests_back': list([
]),
'title': 'Test of requirements2',
'type': 'test',
'type_name': 'Test Case',
Expand All @@ -735,8 +673,6 @@
'avatar': '',
'blocks': list([
]),
'blocks_back': list([
]),
'closed_at': '',
'completion': '',
'constraints': list([
Expand All @@ -758,8 +694,6 @@
'has_forbidden_dead_links': '',
'hidden': '',
'id': 'TEST_003',
'id_complete': 'TEST_003',
'id_parent': 'TEST_003',
'id_prefix': '',
'is_external': False,
'is_modified': False,
Expand All @@ -777,8 +711,6 @@
'parent_need': '',
'parent_needs': list([
]),
'parent_needs_back': list([
]),
'parts': dict({
}),
'post_template': None,
Expand All @@ -803,8 +735,6 @@
'REQ_005.1',
'REQ_005.cool',
]),
'tests_back': list([
]),
'title': 'Test of requirements 5',
'type': 'test',
'type_name': 'Test Case',
Expand Down
Loading

0 comments on commit bc0b8c9

Please sign in to comment.