Skip to content

Commit 792f661

Browse files
fix: external needs graph checks & misc fixes (#78)
1 parent 1876492 commit 792f661

File tree

9 files changed

+29
-29
lines changed

9 files changed

+29
-29
lines changed

docs/example/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This is a rendered example of the 'examples/linking-both' folder using the `docs
3030

3131
Some content to make sure we also can render this
3232
This is a link to an external need inside the 'score' documentation.
33-
:need:`SCORE_gd_req__req__attr_safety`
33+
:need:`SCORE_feat_req__kvs__config_file`.
3434
Note how it starts with the defined prefix but in UPPERCASE. This comes from sphinx-needs, `see here <https://github.com/useblocks/sphinx-needs/blob/master/sphinx_needs/external_needs.py#L119>`_
3535

3636

docs/example/testing/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This example will help catch things and bugs when rst's are defined inside a fol
2525

2626
Some content to make sure we also can render this.
2727
This is a link to an external need inside the 'score' documentation.
28-
:need:`SCORE_gd_req__req__attr_safety`
28+
:need:`SCORE_feat_req__kvs__config_file`.
2929
Note how it starts with the defined prefix but in UPPERCASE. This comes from sphinx-needs, `see here <https://github.com/useblocks/sphinx-needs/blob/master/sphinx_needs/external_needs.py#L119>`_
3030

3131

examples/linking-both/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This is a simple example of a documentation page using the `docs` tool.
3333

3434
Some content to make sure we also can render this
3535
This is a link to an external need inside the 'score' documentation.
36-
:need:`SCORE_gd_req__req__attr_safety`
36+
:need:`SCORE_feat_req__kvs__config_file`
3737
Note how it starts with the defined prefix but in UPPERCASE. This comes from sphinx-needs, `see here <https://github.com/useblocks/sphinx-needs/blob/master/sphinx_needs/external_needs.py#L119>`_
3838

3939

examples/linking-both/testing/test.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This example will help catch things and bugs when rst's are defined inside a fol
2525

2626
Some content to make sure we also can render this.
2727
This is a link to an external need inside the 'score' documentation.
28-
:need:`SCORE_gd_req__req__attr_safety`
28+
:need:`SCORE_feat_req__kvs__config_file`.
2929
Note how it starts with the defined prefix but in UPPERCASE. This comes from sphinx-needs, `see here <https://github.com/useblocks/sphinx-needs/blob/master/sphinx_needs/external_needs.py#L119>`_
3030

3131

examples/linking-latest/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ This is a simple example of a documentation page using the `docs` tool.
2525

2626
Some content to make sure we also can render this
2727
This is a link to an external need inside the 'score' documentation.
28-
:need:`SCORE_gd_req__req__attr_safety`.
28+
:need:`SCORE_feat_req__kvs__config_file`.
2929
Note how it starts with the defined prefix but in UPPERCASE. This comes from sphinx-needs, `see here <https://github.com/useblocks/sphinx-needs/blob/master/sphinx_needs/external_needs.py#L119>`_
3030

src/extensions/score_metamodel/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
from ruamel.yaml import YAML
2323
from sphinx.application import Sphinx
2424
from sphinx_needs import logging
25-
from sphinx_needs.data import NeedsInfoType, SphinxNeedsData
26-
25+
from sphinx_needs.data import NeedsInfoType, SphinxNeedsData, NeedsView
2726
from .log import CheckLogger
2827

2928
logger = logging.get_logger(__name__)
3029

3130
local_check_function = Callable[[Sphinx, NeedsInfoType, CheckLogger], None]
32-
graph_check_function = Callable[[Sphinx, list[NeedsInfoType], CheckLogger], None]
31+
graph_check_function = Callable[[Sphinx, NeedsView, CheckLogger], None]
3332

3433
local_checks: list[local_check_function] = []
3534
graph_checks: list[graph_check_function] = []
@@ -78,9 +77,7 @@ def _run_checks(app: Sphinx, exception: Exception | None) -> None:
7877
return
7978

8079
# Filter out external needs, as checks are only intended to be run on internal needs.
81-
needs_all_needs = (
82-
SphinxNeedsData(app.env).get_needs_view().filter_is_external(False)
83-
)
80+
needs_all_needs = SphinxNeedsData(app.env).get_needs_view()
8481

8582
logger.debug(f"Running checks for {len(needs_all_needs)} needs")
8683

@@ -95,20 +92,22 @@ def is_check_enabled(check: local_check_function | graph_check_function):
9592

9693
enabled_local_checks = [c for c in local_checks if is_check_enabled(c)]
9794

95+
needs_local_needs = (
96+
SphinxNeedsData(app.env).get_needs_view().filter_is_external(False)
97+
)
9898
# Need-Local checks: checks which can be checked file-local, without a
9999
# graph of other needs.
100-
for need in needs_all_needs.values():
100+
for need in needs_local_needs.values():
101101
for check in enabled_local_checks:
102102
logger.debug(f"Running local check {check} for need {need['id']}")
103103
check(app, need, log)
104104

105105
# Graph-Based checks: These warnings require a graph of all other needs to
106106
# be checked.
107107

108-
needs = list(needs_all_needs.values())
109108
for check in [c for c in graph_checks if is_check_enabled(c)]:
110109
logger.debug(f"Running graph check {check} for all needs")
111-
check(app, needs, log)
110+
check(app, needs_all_needs, log)
112111

113112
if log.has_warnings:
114113
log.warning("Some needs have issues. See the log for more information.")

src/extensions/score_metamodel/checks/graph_checks.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import Any, Literal
1616

1717
from sphinx.application import Sphinx
18-
from sphinx_needs.data import NeedsInfoType
18+
from sphinx_needs.data import NeedsInfoType, NeedsView
1919

2020
from score_metamodel import (
2121
CheckLogger,
@@ -130,19 +130,20 @@ def get_need_selection(
130130
@graph_check
131131
def check_metamodel_graph(
132132
app: Sphinx,
133-
all_needs: list[NeedsInfoType],
133+
all_needs: NeedsView,
134134
log: CheckLogger,
135135
):
136136
graph_checks_global = app.config.graph_checks
137137
# Convert list to dictionary for easy lookup
138-
needs_dict = {need["id"]: need for need in all_needs}
138+
needs_dict_all = {need["id"]: need for need in all_needs.values()}
139+
needs_local = list(all_needs.filter_is_external(False).values())
139140

140141
# Iterate over all graph checks
141142
for check in graph_checks_global.items():
142143
apply, eval = check[1].values()
143144

144145
# Get all needs that match the selection criteria
145-
selected_needs = get_need_selection(all_needs, apply, log)
146+
selected_needs = get_need_selection(needs_local, apply, log)
146147

147148
for need in selected_needs:
148149
for parent_relation in list(eval.keys()):
@@ -153,7 +154,7 @@ def check_metamodel_graph(
153154
parent_ids = need[parent_relation]
154155

155156
for parent_id in parent_ids:
156-
parent_need = needs_dict.get(parent_id)
157+
parent_need = needs_dict_all.get(parent_id)
157158
if parent_need is None:
158159
msg = f"Parent need `{parent_id}` not found in needs_dict."
159160
log.warning_for_need(need, msg)

src/extensions/score_metamodel/metamodel.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ needs_types:
187187
security: "^(YES|NO)$"
188188
realizes: "^wp__.+$"
189189
# The following 3 guidance requirements enforce the requirement structure and attributes:
190-
# req-Id: gd_req__req__structure
191-
# req-Id: gd_req__requirements_attr_description
192-
# req-Id: gd_req__req__linkage
190+
# req- Id: gd_req__req__structure
191+
# req- Id: gd_req__requirements_attr_description
192+
# req- Id: gd_req__req__linkage
193193
# Requirements
194194
stkh_req:
195195
title: "Stakeholder Requirement"
@@ -219,7 +219,7 @@ needs_types:
219219
safety: "^(QM|ASIL_B|ASIL_D)$"
220220
status: "^(valid|invalid)$"
221221
mandatory_links:
222-
# req-Id: gd_req__req__linkage_fulfill
222+
# req- Id: gd_req__req__linkage_fulfill
223223
satisfies: "^stkh_req__.*$"
224224
optional_options:
225225
codelink: "^.*$"
@@ -560,10 +560,10 @@ needs_extra_links:
560560
# - condition: defines the condition that should be checked
561561
# - [and / or / xor / not]
562562
##############################################################
563-
# req-Id: gd_req__req__linkage_architecture
564-
# req-Id: gd_req__req__linkage_safety
563+
# req- Id: gd_req__req__linkage_architecture
564+
# req- Id: gd_req__req__linkage_safety
565565
graph_checks:
566-
# req-Id: gd_req__req__linkage_safety
566+
# req- Id: gd_req__req__linkage_safety
567567
req_safety_linkage:
568568
needs:
569569
include: "comp_req, feat_req"
@@ -582,7 +582,7 @@ graph_checks:
582582
condition: "status == valid"
583583
check:
584584
satisfies: "status == valid"
585-
# req-Id: gd_req__req__linkage_architecture
585+
# req- Id: gd_req__req__linkage_architecture
586586
arch_safety_linkage:
587587
needs:
588588
include: "comp_req, feat_req"

src/extensions/score_metamodel/tests/test_rules_file_based.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_rst_files(
175175
"Unable to extract test data from the rst file: "
176176
f"{rst_file}. Please check the file for the correct format."
177177
)
178-
print(f"RST Data: {rst_data}")
178+
# print(f"RST Data: {rst_data}")
179179
app: SphinxTestApp = sphinx_app_setup(RST_DIR / rst_file)
180180
os.chdir(app.srcdir) # Change working directory to the source directory
181181

@@ -185,7 +185,7 @@ def test_rst_files(
185185

186186
# Collect the warnings
187187
warnings = app.warning.getvalue().splitlines()
188-
print(f"Warnings: {warnings}")
188+
# print(f"Warnings: {warnings}")
189189

190190
# Check if the expected warnings are present
191191
for warning_info in rst_data.warning_infos:

0 commit comments

Comments
 (0)