Skip to content

Commit

Permalink
🔧 change prepare_need_list to expand_needs_view function (#1258)
Browse files Browse the repository at this point in the history
Rename `prepare_need_list` to `expand_needs_view`, to make its purpose clearer,
and change its input argument to a `NeedsView`, so we can do the expansion more performantly.
  • Loading branch information
chrisjsewell authored Aug 30, 2024
1 parent 26bcdc4 commit 32fd0b5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
7 changes: 3 additions & 4 deletions sphinx_needs/directives/needbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from sphinx_needs.config import NeedsSphinxConfig
from sphinx_needs.data import NeedsBarType, SphinxNeedsData
from sphinx_needs.filter_common import FilterBase, filter_needs, prepare_need_list
from sphinx_needs.filter_common import FilterBase, expand_needs_view, filter_needs
from sphinx_needs.logging import get_logger, log_warning
from sphinx_needs.utils import (
add_doc,
Expand Down Expand Up @@ -291,9 +291,8 @@ def process_needbar(

# 5. process content
local_data_number = []
need_list = list(
prepare_need_list(needs_data.get_needs_view().values())
) # adds parts to need_list
# adds parts to need_list
need_list = expand_needs_view(needs_data.get_needs_view())

for line in local_data:
line_number = []
Expand Down
7 changes: 3 additions & 4 deletions sphinx_needs/directives/needpie.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sphinx_needs.data import NeedsPieType, SphinxNeedsData
from sphinx_needs.debug import measure_time
from sphinx_needs.directives.utils import no_needs_found_paragraph
from sphinx_needs.filter_common import FilterBase, filter_needs, prepare_need_list
from sphinx_needs.filter_common import FilterBase, expand_needs_view, filter_needs
from sphinx_needs.logging import get_logger, log_warning
from sphinx_needs.utils import (
add_doc,
Expand Down Expand Up @@ -157,9 +157,8 @@ def process_needpie(
content = current_needpie["content"]

sizes = []
need_list = list(
prepare_need_list(needs_data.get_needs_view().values())
) # adds parts to need_list
# adds parts to need_list
need_list = expand_needs_view(needs_data.get_needs_view())
if content and not current_needpie["filter_func"]:
for line in content:
if line.isdigit():
Expand Down
31 changes: 12 additions & 19 deletions sphinx_needs/filter_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,13 @@ def process_filters(
found_needs: list[NeedsInfoType]

# check if include external needs
all_needs: Iterable[NeedsInfoType] = needs_view.values()
if not include_external:
all_needs = [need for need in all_needs if not need["is_external"]]

found_needs_by_options: list[NeedsInfoType] = []
needs_view = NeedsView(
{id: need for id, need in needs_view.items() if not need["is_external"]}
)

# Add all need_parts of given needs to the search list
all_needs_incl_parts = prepare_need_list(all_needs)
all_needs_incl_parts = expand_needs_view(needs_view)

# Check if external filter code is defined
try:
Expand All @@ -154,6 +153,7 @@ def process_filters(

if (not filter_code or filter_code.isspace()) and not filter_func_sig:
if bool(filter_data["status"] or filter_data["tags"] or filter_data["types"]):
found_needs_by_options: list[NeedsInfoType] = []
for need_info in all_needs_incl_parts:
status_filter_passed = False
if (
Expand Down Expand Up @@ -277,20 +277,13 @@ def process_filters(
return found_needs


def prepare_need_list(need_list: Iterable[NeedsInfoType]) -> list[NeedsInfoType]:
# all_needs_incl_parts = need_list.copy()
all_needs_incl_parts: list[NeedsInfoType]
try:
all_needs_incl_parts = need_list[:] # type: ignore
except TypeError:
try:
all_needs_incl_parts = need_list.copy() # type: ignore
except AttributeError:
all_needs_incl_parts = list(need_list)[:]

for need in need_list:
for filter_part in iter_need_parts(need):
all_needs_incl_parts.append(filter_part)
def expand_needs_view(needs_view: NeedsView) -> list[NeedsInfoType]:
"""Turns a needs view into a list of needs, expanding all need["parts"] to be items of the list."""
all_needs_incl_parts: list[NeedsInfoType] = []
for need in needs_view.values():
all_needs_incl_parts.append(need)
for need_part in iter_need_parts(need):
all_needs_incl_parts.append(need_part)

return all_needs_incl_parts

Expand Down
10 changes: 5 additions & 5 deletions sphinx_needs/roles/need_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sphinx_needs.api.exceptions import NeedsInvalidFilter
from sphinx_needs.config import NeedsSphinxConfig
from sphinx_needs.data import SphinxNeedsData
from sphinx_needs.filter_common import filter_needs, prepare_need_list
from sphinx_needs.filter_common import expand_needs_view, filter_needs
from sphinx_needs.logging import get_logger

log = get_logger(__name__)
Expand All @@ -30,13 +30,13 @@ def process_need_count(
) -> None:
needs_config = NeedsSphinxConfig(app.config)
for node_need_count in found_nodes:
all_needs = list(SphinxNeedsData(app.env).get_needs_view().values())
needs_view = SphinxNeedsData(app.env).get_needs_view()
filter = node_need_count["reftarget"]

if filter:
filters = filter.split(" ? ")
if len(filters) == 1:
need_list = prepare_need_list(all_needs) # adds parts to need_list
need_list = expand_needs_view(needs_view) # adds parts to need_list
amount = str(
len(
filter_needs(
Expand All @@ -48,7 +48,7 @@ def process_need_count(
)
)
elif len(filters) == 2:
need_list = prepare_need_list(all_needs) # adds parts to need_list
need_list = expand_needs_view(needs_view) # adds parts to need_list
amount_1 = len(
filter_needs(
need_list, needs_config, filters[0], location=node_need_count
Expand All @@ -66,7 +66,7 @@ def process_need_count(
'Use " ? " only once to separate filters.'
)
else:
amount = str(len(all_needs))
amount = str(len(needs_view))

new_node_count = nodes.Text(amount)
node_need_count.replace_self(new_node_count)

0 comments on commit 32fd0b5

Please sign in to comment.