Skip to content

Commit

Permalink
✨ Add needs_import_keys configuration
Browse files Browse the repository at this point in the history
```python
needs_import_keys = {"my_key": "path/to/needs.json"}
```

Allows for the use of:

```restructuredtext
.. needimport:: my_key
```

As discussed with @arwedus
  • Loading branch information
chrisjsewell committed Jan 6, 2025
1 parent 9c5a6db commit d4d0842
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,14 @@ Default: ``False``.
needs_service_all_data = True
.. _needs_import_keys:
needs_import_keys
~~~~~~~~~~~~~~~~~
.. versionadded:: 4.2.0
For use with the :ref:`needimport` directive, mapping keys to file paths, see :ref:`needimport-keys`.
.. _needs_external_needs:
Expand Down
20 changes: 20 additions & 0 deletions docs/directives/needimport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,23 @@ So you can decide what kind of layout or style to use during import.
* template
* pre_template
* post_template

.. _needimport-keys:

Global keys
-----------
.. versionadded:: 4.2.0

The :ref:`needs_import_keys` configuration can be used to set global keys for use as the directive arguments.

For example:

.. code-block:: python
needs_import_keys = {"my_key": "path/to/needs.json"}
Allows for the use of:

.. code-block:: restructuredtext
.. needimport:: my_key
4 changes: 4 additions & 0 deletions sphinx_needs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ def warnings(
default=False, metadata={"rebuild": "html", "types": (bool,)}
)
"""If True, unknown sevice option data is shown in the need content."""
import_keys: dict[str, str] = field(
default_factory=dict, metadata={"rebuild": "html", "types": (dict,)}
)
"""Mapping of keys that can be used as needimport arguments and replaced by the value."""
external_needs: list[ExternalSource] = field(
default_factory=list, metadata={"rebuild": "html", "types": (list,)}
)
Expand Down
10 changes: 6 additions & 4 deletions sphinx_needs/directives/needimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ class NeedimportDirective(SphinxDirective):

@measure_time("needimport")
def run(self) -> Sequence[nodes.Node]:
# needs_list = {}
needs_config = NeedsSphinxConfig(self.config)

version = self.options.get("version")
filter_string = self.options.get("filter")
id_prefix = self.options.get("id_prefix", "")

need_import_path = self.arguments[0]
need_import_path = needs_config.import_keys.get(
self.arguments[0], self.arguments[0]
)

# check if given arguemnt is downloadable needs.json path
# check if given argument is downloadable needs.json path
url = urlparse(need_import_path)
if url.scheme and url.netloc:
# download needs.json
Expand Down Expand Up @@ -141,7 +144,6 @@ def run(self) -> Sequence[nodes.Node]:
f"Version {version} not found in needs import file {correct_need_import_path}"
)

needs_config = NeedsSphinxConfig(self.config)
data = needs_import_list["versions"][version]

if ids := self.options.get("ids"):
Expand Down
2 changes: 2 additions & 0 deletions tests/doc_test/import_doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
},
]

needs_import_keys = {"key": "needs_test.json"}

needs_template = """
.. _{{id}}:
Expand Down
2 changes: 1 addition & 1 deletion tests/doc_test/import_doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ COLLAPSED

TEST
----
.. needimport:: needs_test.json
.. needimport:: key
:id_prefix: test_
:tags: imported; new_tag

Expand Down

0 comments on commit d4d0842

Please sign in to comment.