diff --git a/sphinx_needs/directives/needbar.py b/sphinx_needs/directives/needbar.py index 1d7f212a4..fde308db3 100644 --- a/sphinx_needs/directives/needbar.py +++ b/sphinx_needs/directives/needbar.py @@ -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, @@ -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 = [] diff --git a/sphinx_needs/directives/needpie.py b/sphinx_needs/directives/needpie.py index 838d3b66d..1e4faafe7 100644 --- a/sphinx_needs/directives/needpie.py +++ b/sphinx_needs/directives/needpie.py @@ -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, @@ -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(): diff --git a/sphinx_needs/filter_common.py b/sphinx_needs/filter_common.py index 153ddd33e..897bc434b 100644 --- a/sphinx_needs/filter_common.py +++ b/sphinx_needs/filter_common.py @@ -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: @@ -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 ( @@ -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 diff --git a/sphinx_needs/roles/need_count.py b/sphinx_needs/roles/need_count.py index 2df0105d0..595093663 100644 --- a/sphinx_needs/roles/need_count.py +++ b/sphinx_needs/roles/need_count.py @@ -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__) @@ -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( @@ -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 @@ -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)