Skip to content

Commit

Permalink
refactor: Support options / deprecated options mix-up
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed May 8, 2022
1 parent c20022e commit 7c71f26
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions src/mkdocstrings/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""
import re
from collections import ChainMap
from typing import Any, Mapping, MutableMapping, MutableSequence, Tuple
from typing import Any, MutableSequence, Tuple
from warnings import warn
from xml.etree.ElementTree import Element

Expand Down Expand Up @@ -174,15 +174,19 @@ def _process_block(
handler_config = self._handlers.get_handler_config(handler_name)
handler = self._handlers.get_handler(handler_name, handler_config)

options = ChainMap(config.get("options", {}), handler_config.get("options", {}))
if not options:
selection, rendering = get_item_configs(handler_config, config)
if selection or rendering:
warn(
"'selection' and 'rendering' are deprecated and merged into a single 'options' YAML key",
DeprecationWarning,
)
options = ChainMap(*selection.maps, *rendering.maps) # type: ignore[attr-defined]
global_options = handler_config.get("options", {})
local_options = config.get("options", {})
deprecated_global_options = ChainMap(handler_config.get("selection", {}), handler_config.get("rendering", {}))
deprecated_local_options = ChainMap(config.get("selection", {}), config.get("rendering", {}))

options = ChainMap(local_options, deprecated_local_options, global_options, deprecated_global_options)

if deprecated_global_options or deprecated_local_options:
warn(
"'selection' and 'rendering' are deprecated and merged into a single 'options' YAML key",
DeprecationWarning,
)

if heading_level:
options = ChainMap(options, {"heading_level": heading_level}) # like setdefault

Expand Down Expand Up @@ -213,21 +217,6 @@ def _process_block(
return rendered, handler, data


def get_item_configs(handler_config: dict, config: dict) -> Tuple[Mapping, MutableMapping]:
"""Get the selection and rendering configuration merged into the global configuration of the given handler.
Arguments:
handler_config: The global configuration of a handler. It can be an empty dictionary.
config: The configuration to merge into the global handler configuration.
Returns:
Two dictionaries: selection and rendering. The local configurations are merged into the global ones.
"""
item_selection_config = ChainMap(config.get("selection", {}), handler_config.get("selection", {}))
item_rendering_config = ChainMap(config.get("rendering", {}), handler_config.get("rendering", {}))
return item_selection_config, item_rendering_config


class _PostProcessor(Treeprocessor):
def run(self, root: Element):
carry_text = ""
Expand Down

0 comments on commit 7c71f26

Please sign in to comment.