Skip to content

Commit

Permalink
🐛 Fix needreport usage numbers (#1285)
Browse files Browse the repository at this point in the history
these need to be post-processed
  • Loading branch information
chrisjsewell authored Sep 12, 2024
1 parent a0d7abd commit 5cf181c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
10 changes: 8 additions & 2 deletions sphinx_needs/directives/needreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from sphinx.util.docutils import SphinxDirective

from sphinx_needs.config import NeedsSphinxConfig
from sphinx_needs.directives.utils import analyse_needs_metrics
from sphinx_needs.logging import log_warning
from sphinx_needs.utils import add_doc

Expand Down Expand Up @@ -44,7 +43,14 @@ def run(self) -> Sequence[nodes.raw]:
"types": needs_config.types if "types" in self.options else [],
"options": needs_config.extra_options if "options" in self.options else [],
"links": needs_config.extra_links if "links" in self.options else [],
"usage": analyse_needs_metrics(env) if "usage" in self.options else {},
# note the usage dict format here is just to keep backwards compatibility,
# but actually this is now post-processed so we only really need the need types
"usage": {
"needs_amount": 0,
"needs_types": {t["directive"]: 0 for t in needs_config.types},
}
if "usage" in self.options
else {},
"report_directive": "dropdown",
}
report_info.update(**needs_config.render_context)
Expand Down
10 changes: 5 additions & 5 deletions sphinx_needs/directives/needreport_template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{# Output for needs_options #}

{# Output for needs metrics #}
{% if usage|length != 0 %}
{% if usage %}

.. {{ report_directive }}:: Need Metrics
Expand All @@ -66,11 +66,11 @@
* - NEEDS TYPES
- NEEDS PER TYPE
{% for k, v in usage["needs_types"].items() %}
* - {{ k | capitalize }}
- {{ v }}
{% for type in usage["needs_types"] %}
* - {{ type | capitalize }}
- :need_count:`type=="{{ type }}"`
{% endfor %}
* - **Total Needs Amount**
- {{ usage.get("needs_amount") }}
- :need_count:`True`
{% endif %}
{# Output for needs metrics #}
25 changes: 1 addition & 24 deletions sphinx_needs/directives/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
from typing import Any

from docutils import nodes
from sphinx.environment import BuildEnvironment

from sphinx_needs.config import NeedsSphinxConfig
from sphinx_needs.data import NeedsFilteredBaseType, SphinxNeedsData
from sphinx_needs.data import NeedsFilteredBaseType
from sphinx_needs.defaults import TITLE_REGEX


Expand Down Expand Up @@ -84,26 +82,5 @@ def get_option_list(options: dict[str, Any], name: str) -> list[str]:
return values_list


def analyse_needs_metrics(env: BuildEnvironment) -> dict[str, Any]:
"""
Function to generate metrics about need objects.
:param env: Sphinx build environment
:return: Dictionary consisting of needs metrics.
"""
needs = SphinxNeedsData(env).get_needs_view()
metric_data: dict[str, Any] = {"needs_amount": len(needs)}
needs_types = {i["directive"]: 0 for i in NeedsSphinxConfig(env.config).types}

for i in needs.values():
if i["type"] in needs_types:
needs_types[i["type"]] += 1

metric_data["needs_types"] = {
i[0]: i[1] for i in sorted(needs_types.items(), key=lambda x: x[0])
}
return metric_data


class SphinxNeedsLinkTypeException(BaseException):
"""Raised if problems with link types happen"""
20 changes: 13 additions & 7 deletions tests/test_needreport.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import os
from pathlib import Path

import pytest
from sphinx.util.console import strip_colors


@pytest.mark.parametrize(
"test_app",
[{"buildername": "html", "srcdir": "doc_test/doc_needreport"}],
[{"buildername": "html", "srcdir": "doc_test/doc_needreport", "no_plantuml": True}],
indirect=True,
)
def test_doc_needarch(test_app):
app = test_app
app.build()
# check for warning about missing options
warnings = app._warning.getvalue()
assert (
"index.rst:6: WARNING: No options specified to generate need report [needs.report]"
in warnings
)
assert "index.rst:8: WARNING: Could not load needs report template file" in warnings
warnings = (
strip_colors(test_app._warning.getvalue())
.replace(str(test_app.srcdir) + os.path.sep, "<srcdir>/")
.strip()
).splitlines()
assert warnings == [
"<srcdir>/index.rst:6: WARNING: No options specified to generate need report [needs.report]",
"<srcdir>/index.rst:8: WARNING: Could not load needs report template file <srcdir>/unknown.rst [needs.report]",
]

html = Path(app.outdir, "index.html").read_text(encoding="utf8")
assert "Need Types" in html
assert "Need Extra Links" in html
Expand Down

0 comments on commit 5cf181c

Please sign in to comment.