Skip to content

Commit

Permalink
🔧 Add remove_node_from_tree utility function (#1063)
Browse files Browse the repository at this point in the history
Just de-duplicates some logic used in multiple places
  • Loading branch information
chrisjsewell authored Nov 7, 2023
1 parent 0888e08 commit 2140da0
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 103 deletions.
10 changes: 3 additions & 7 deletions sphinx_needs/directives/need.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
from sphinx_needs.data import NeedsInfoType, SphinxNeedsData
from sphinx_needs.debug import measure_time
from sphinx_needs.defaults import NEED_DEFAULT_OPTIONS
from sphinx_needs.directives.needextend import (
Needextend,
extend_needs_data,
remove_needextend_node,
)
from sphinx_needs.directives.needextend import Needextend, extend_needs_data
from sphinx_needs.functions import (
find_and_replace_node_content,
resolve_dynamic_values,
Expand All @@ -32,7 +28,7 @@
from sphinx_needs.logging import get_logger
from sphinx_needs.need_constraints import process_constraints
from sphinx_needs.nodes import Need
from sphinx_needs.utils import add_doc, profile
from sphinx_needs.utils import add_doc, profile, remove_node_from_tree

logger = get_logger(__name__)

Expand Down Expand Up @@ -407,7 +403,7 @@ def process_need_nodes(app: Sphinx, doctree: nodes.document, fromdocname: str) -
post_process_needs_data(app)

for extend_node in doctree.findall(Needextend):
remove_needextend_node(extend_node)
remove_node_from_tree(extend_node)

format_need_nodes(app, doctree, fromdocname, list(doctree.findall(Need)))

Expand Down
11 changes: 2 additions & 9 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 SphinxNeedsData
from sphinx_needs.filter_common import FilterBase, filter_needs, prepare_need_list
from sphinx_needs.utils import add_doc, save_matplotlib_figure
from sphinx_needs.utils import add_doc, remove_node_from_tree, save_matplotlib_figure

if not os.environ.get("DISPLAY"):
matplotlib.use("Agg")
Expand Down Expand Up @@ -175,14 +175,7 @@ def process_needbar(app: Sphinx, doctree: nodes.document, fromdocname: str, foun
# for node in doctree.findall(Needbar):
for node in found_nodes:
if not needs_config.include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
15 changes: 0 additions & 15 deletions sphinx_needs/directives/needextend.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,3 @@ def extend_needs_data(
need[option] = [i.strip() for i in re.split(";|,", value)]
else:
need[option] = value


def remove_needextend_node(node: Needextend) -> None:
"""
Remove needextend from docutils node-tree, so that no output gets generated for it.
Ok, this is really dirty.
If we replace a node, docutils checks, if it will not lose any attributes.
But this is here the case, because we are using the attribute "ids" of a node.
However, I do not understand, why losing an attribute is such a big deal, so we delete everything
before docutils claims about it.
"""

for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
11 changes: 2 additions & 9 deletions sphinx_needs/directives/needextract.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
from sphinx_needs.filter_common import FilterBase, process_filters
from sphinx_needs.layout import create_need
from sphinx_needs.utils import add_doc
from sphinx_needs.utils import add_doc, remove_node_from_tree


class Needextract(nodes.General, nodes.Element):
Expand Down Expand Up @@ -79,14 +79,7 @@ def process_needextract(

for node in found_nodes:
if not needs_config.include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
11 changes: 2 additions & 9 deletions sphinx_needs/directives/needfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sphinx_needs.data import SphinxNeedsData
from sphinx_needs.diagrams_common import create_legend
from sphinx_needs.filter_common import FilterBase, process_filters
from sphinx_needs.utils import add_doc, row_col_maker
from sphinx_needs.utils import add_doc, remove_node_from_tree, row_col_maker


class Needfilter(nodes.General, nodes.Element):
Expand Down Expand Up @@ -82,14 +82,7 @@ def process_needfilters(
# for node in doctree.findall(Needfilter):
for node in found_nodes:
if not needs_config.include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
16 changes: 7 additions & 9 deletions sphinx_needs/directives/needflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
from sphinx_needs.diagrams_common import calculate_link, create_legend
from sphinx_needs.filter_common import FilterBase, filter_single_need, process_filters
from sphinx_needs.logging import get_logger
from sphinx_needs.utils import add_doc, get_scale, split_link_types
from sphinx_needs.utils import (
add_doc,
get_scale,
remove_node_from_tree,
split_link_types,
)

logger = get_logger(__name__)

Expand Down Expand Up @@ -286,14 +291,7 @@ def process_needflow(app: Sphinx, doctree: nodes.document, fromdocname: str, fou
# for node in doctree.findall(Needflow):
for node in found_nodes:
if not needs_config.include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
11 changes: 2 additions & 9 deletions sphinx_needs/directives/needgantt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sphinx_needs.directives.utils import SphinxNeedsLinkTypeException
from sphinx_needs.filter_common import FilterBase, filter_single_need, process_filters
from sphinx_needs.logging import get_logger
from sphinx_needs.utils import MONTH_NAMES, add_doc
from sphinx_needs.utils import MONTH_NAMES, add_doc, remove_node_from_tree

logger = get_logger(__name__)

Expand Down Expand Up @@ -145,14 +145,7 @@ def process_needgantt(app: Sphinx, doctree: nodes.document, fromdocname: str, fo
# for node in doctree.findall(Needgantt):
for node in found_nodes:
if not needs_config.include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
15 changes: 6 additions & 9 deletions sphinx_needs/directives/needlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
used_filter_paragraph,
)
from sphinx_needs.filter_common import FilterBase, process_filters
from sphinx_needs.utils import add_doc, check_and_calc_base_url_rel_path
from sphinx_needs.utils import (
add_doc,
check_and_calc_base_url_rel_path,
remove_node_from_tree,
)


class Needlist(nodes.General, nodes.Element):
Expand Down Expand Up @@ -70,14 +74,7 @@ def process_needlist(app: Sphinx, doctree: nodes.document, fromdocname: str, fou
# for node in doctree.findall(Needlist):
for node in found_nodes:
if not include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
10 changes: 2 additions & 8 deletions sphinx_needs/directives/needpie.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from sphinx_needs.utils import (
add_doc,
check_and_get_external_filter_func,
remove_node_from_tree,
save_matplotlib_figure,
)

Expand Down Expand Up @@ -118,14 +119,7 @@ def process_needpie(app: Sphinx, doctree: nodes.document, fromdocname: str, foun
# for node in doctree.findall(Needpie):
for node in found_nodes:
if not include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
11 changes: 2 additions & 9 deletions sphinx_needs/directives/needsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from sphinx_needs.filter_common import FilterBase
from sphinx_needs.logging import get_logger
from sphinx_needs.utils import add_doc
from sphinx_needs.utils import add_doc, remove_node_from_tree

logger = get_logger(__name__)

Expand Down Expand Up @@ -89,14 +89,7 @@ def process_needsequence(
# for node in doctree.findall(Needsequence):
for node in found_nodes:
if not include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
12 changes: 2 additions & 10 deletions sphinx_needs/directives/needtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from sphinx_needs.filter_common import FilterBase, process_filters
from sphinx_needs.functions.functions import check_and_get_content
from sphinx_needs.utils import add_doc, profile, row_col_maker
from sphinx_needs.utils import add_doc, profile, remove_node_from_tree, row_col_maker


class Needtable(nodes.General, nodes.Element):
Expand Down Expand Up @@ -141,15 +141,7 @@ def process_needtables(
# for node in doctree.findall(Needtable):
for node in found_nodes:
if not needs_config.include_needs:
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])
remove_node_from_tree(node)
continue

id = node.attributes["ids"][0]
Expand Down
12 changes: 12 additions & 0 deletions sphinx_needs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,15 @@ def get_scale(options: Dict[str, Any], location: Any) -> str:
)
return "100"
return scale


def remove_node_from_tree(node: nodes.Element) -> None:
"""Remove a docutils node in-place from its node-tree."""
# Ok, this is really dirty.
# If we replace a node, docutils checks, if it will not lose any attributes.
# But this is here the case, because we are using the attribute "ids" of a node.
# However, I do not understand, why losing an attribute is such a big deal, so we delete everything
# before docutils claims about it.
for att in ("ids", "names", "classes", "dupnames"):
node[att] = []
node.replace_self([])

1 comment on commit 2140da0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 2140da0 Previous: 0888e08 Ratio
Small, basic Sphinx-Needs project 0.3506217609999567 s 0.22847071799998275 s 1.53

This comment was automatically generated by workflow using github-action-benchmark.

CC: @danwos

Please sign in to comment.