diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-05-27 14:42:09 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-05-28 10:38:23 +0200 |
commit | e720d8418a2154a0786b62d77e2859624aff5a9e (patch) | |
tree | 4fddeb73dcdee633b0ad142176b2b4979b1bbbc4 | |
parent | f156ed10afd9125e0dfe1a0b9edb69bf828f4887 (diff) |
sphinxcontent: Add SphinxMapper.add_get_reference()
-rw-r--r-- | rtemsqual/sphinxcontent.py | 27 | ||||
-rw-r--r-- | rtemsqual/tests/spec-sphinx/y.yml | 3 | ||||
-rw-r--r-- | rtemsqual/tests/test_content_sphinx.py | 4 |
3 files changed, 27 insertions, 7 deletions
diff --git a/rtemsqual/sphinxcontent.py b/rtemsqual/sphinxcontent.py index 670b32b5..e2b2bbc8 100644 --- a/rtemsqual/sphinxcontent.py +++ b/rtemsqual/sphinxcontent.py @@ -25,8 +25,10 @@ # POSSIBILITY OF SUCH DAMAGE. from contextlib import contextmanager +import os import re -from typing import Any, Iterable, Iterator, List, Optional, Union +from typing import Any, Dict, Callable, Iterable, Iterator, List, Optional, \ + Union from rtemsqual.content import Content, make_lines from rtemsqual.items import Item, ItemMapper @@ -191,15 +193,28 @@ class SphinxContent(Content): self.prepend([f".. SPDX-License-Identifier: {self._license}", ""]) +def _get_ref_term(value: Any, key: str) -> str: + return f":term:`{value[key]}`" + + class SphinxMapper(ItemMapper): """ Sphinx mapper. """ def __init__(self, item: Item): super().__init__(item) + self._get_ref = { + "glossary:/term": _get_ref_term + } # type: Dict[str, Callable[[Any, str], str]] + + def add_get_reference(self, type_name: str, path: str, + get_ref: Callable[[Any, str], str]) -> None: + """ + Adds a function to get a reference to the specified path for items of + the specified type. + """ + self._get_ref[f"{type_name}:{path}"] = get_ref - def get_value(self, _item: Item, _path: str, value: Any, key: str, + def get_value(self, item: Item, path: str, value: Any, key: str, _index: Optional[int]) -> Any: """ Gets a value by key and optional index. """ - # pylint: disable=no-self-use - if key == "term": - return f":term:`{value[key]}`" - raise KeyError + return self._get_ref[f"{item['type']}:{os.path.join(path, key)}"]( + value, key) diff --git a/rtemsqual/tests/spec-sphinx/y.yml b/rtemsqual/tests/spec-sphinx/y.yml new file mode 100644 index 00000000..4c142141 --- /dev/null +++ b/rtemsqual/tests/spec-sphinx/y.yml @@ -0,0 +1,3 @@ +name: bar +links: [] +type: foo diff --git a/rtemsqual/tests/test_content_sphinx.py b/rtemsqual/tests/test_content_sphinx.py index c4b6dfe3..83f4cbd9 100644 --- a/rtemsqual/tests/test_content_sphinx.py +++ b/rtemsqual/tests/test_content_sphinx.py @@ -251,4 +251,6 @@ def test_substitute(tmpdir): mapper = SphinxMapper(item_cache["/x"]) with pytest.raises(KeyError): mapper.substitute("${x:/y}") - assert ":term:`y`" == mapper.substitute("${x:/term}") + assert mapper.substitute("${x:/term}") == ":term:`y`" + mapper.add_get_reference("foo", "/name", lambda x, y: x[y]) + assert mapper.substitute("${y:/name}") == "bar" |