-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Add ids
option for needimport
#1292
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
from sphinx_needs.defaults import string_to_boolean | ||
from sphinx_needs.filter_common import filter_single_need | ||
from sphinx_needs.logging import log_warning | ||
from sphinx_needs.needsfile import check_needs_file | ||
from sphinx_needs.needsfile import SphinxNeedsFileException, check_needs_data | ||
from sphinx_needs.utils import add_doc, import_prefix_link_edit, logger | ||
|
||
|
||
|
@@ -37,6 +37,7 @@ class NeedimportDirective(SphinxDirective): | |
"version": directives.unchanged_required, | ||
"hide": directives.flag, | ||
"collapse": string_to_boolean, | ||
"ids": directives.unchanged_required, | ||
"filter": directives.unchanged_required, | ||
"id_prefix": directives.unchanged_required, | ||
"tags": directives.unchanged_required, | ||
|
@@ -56,10 +57,6 @@ def run(self) -> Sequence[nodes.Node]: | |
filter_string = self.options.get("filter") | ||
id_prefix = self.options.get("id_prefix", "") | ||
|
||
tags = self.options.get("tags", []) | ||
if len(tags) > 0: | ||
tags = [tag.strip() for tag in re.split("[;,]", tags)] | ||
|
||
need_import_path = self.arguments[0] | ||
|
||
# check if given arguemnt is downloadable needs.json path | ||
|
@@ -115,21 +112,21 @@ def run(self) -> Sequence[nodes.Node]: | |
f"Could not load needs import file {correct_need_import_path}" | ||
) | ||
|
||
errors = check_needs_file(correct_need_import_path) | ||
try: | ||
with open(correct_need_import_path) as needs_file: | ||
needs_import_list = json.load(needs_file) | ||
except json.JSONDecodeError as e: | ||
chrisjsewell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# TODO: Add exception handling | ||
raise SphinxNeedsFileException(correct_need_import_path) from e | ||
|
||
errors = check_needs_data(needs_import_list) | ||
if errors.schema: | ||
logger.info( | ||
f"Schema validation errors detected in file {correct_need_import_path}:" | ||
) | ||
for error in errors.schema: | ||
logger.info(f' {error.message} -> {".".join(error.path)}') | ||
|
||
try: | ||
with open(correct_need_import_path) as needs_file: | ||
needs_import_list = json.load(needs_file) | ||
except json.JSONDecodeError as e: | ||
# TODO: Add exception handling | ||
raise e | ||
|
||
if version is None: | ||
try: | ||
version = needs_import_list["current_version"] | ||
|
@@ -146,6 +143,13 @@ def run(self) -> Sequence[nodes.Node]: | |
|
||
needs_config = NeedsSphinxConfig(self.config) | ||
data = needs_import_list["versions"][version] | ||
|
||
if ids := self.options.get("ids"): | ||
id_list = [i.strip() for i in ids.split(",") if i.strip()] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can IDs contain a dynamic function, so you would have to use the 'split with dyn' function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh no definitely not, no dynamic functions here; this is intended for fast filtering |
||
data["needs"] = { | ||
key: data["needs"][key] for key in id_list if key in data["needs"] | ||
} | ||
|
||
# TODO this is not exactly NeedsInfoType, because the export removes/adds some keys | ||
needs_list: dict[str, NeedsInfoType] = data["needs"] | ||
if schema := data.get("needs_schema"): | ||
|
@@ -184,8 +188,13 @@ def run(self) -> Sequence[nodes.Node]: | |
needs_list = needs_list_filtered | ||
|
||
# tags update | ||
for need in needs_list.values(): | ||
need["tags"] = need["tags"] + tags | ||
if tags := [ | ||
tag.strip() | ||
for tag in re.split("[;,]", self.options.get("tags", "")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, the new function should be used (or is that in a different open PR?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was already existing, but again no dynamic functions. |
||
if tag.strip() | ||
]: | ||
for need in needs_list.values(): | ||
need["tags"] = need["tags"] + tags | ||
|
||
import_prefix_link_edit(needs_list, id_prefix, needs_config.extra_links) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OSError
should also be caught, but there is already a TODOThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(knowing what exceptions to handle is always a pain, I was reading this with interest recently https://discuss.python.org/t/extend-type-hints-to-cover-exceptions/23788/51)