Skip to content
Permalink
Browse files

refactor: Use ChainMap instead of copying dicts

  • Loading branch information
oprypin committed Nov 1, 2020
1 parent 9815d5a commit c634d2ce6377de26caa553048bb28ef1e672f7aa
Showing with 9 additions and 11 deletions.
  1. +5 −6 src/mkdocstrings/extension.py
  2. +4 −5 src/mkdocstrings/handlers/python.py
@@ -23,7 +23,8 @@
```
"""
import re
from typing import Any, Tuple
from collections import ChainMap
from typing import Any, Mapping, Tuple
from xml.etree.ElementTree import XML, Element, ParseError # noqa: S405 (we choose to trust the XML input)

import yaml
@@ -246,7 +247,7 @@ def get_handler_config(self, handler_name: str) -> dict:
return {}


def get_item_configs(handler_config: dict, config: dict) -> Tuple[dict, dict]:
def get_item_configs(handler_config: dict, config: dict) -> Tuple[Mapping, Mapping]:
"""
Get the selection and rendering configuration merged into the global configuration of the given handler.
@@ -257,10 +258,8 @@ def get_item_configs(handler_config: dict, config: dict) -> Tuple[dict, dict]:
Returns:
Two dictionaries: selection and rendering. The local configurations are merged into the global ones.
"""
item_selection_config = dict(handler_config.get("selection", {}))
item_selection_config.update(config.get("selection", {}))
item_rendering_config = dict(handler_config.get("rendering", {}))
item_rendering_config.update(config.get("rendering", {}))
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


@@ -7,6 +7,7 @@
import json
import os
import sys
from collections import ChainMap
from subprocess import PIPE, Popen # noqa: S404 (what other option, more secure that PIPE do we have? sockets?)
from typing import Any, List, Optional

@@ -65,15 +66,14 @@ class PythonRenderer(BaseRenderer):
""" # noqa: E501

def render(self, data: Any, config: dict) -> str: # noqa: D102 (ignore missing docstring)
final_config = dict(self.default_config)
final_config.update(config)
final_config = ChainMap(config, self.default_config)

template = self.env.get_template(f"{data['category']}.html")

# Heading level is a "state" variable, that will change at each step
# of the rendering recursion. Therefore, it's easier to use it as a plain value
# than as an item in a dictionary.
heading_level = final_config.pop("heading_level")
heading_level = final_config["heading_level"]

return template.render(
**{"config": final_config, data["category"]: data, "heading_level": heading_level, "root": True},
@@ -192,8 +192,7 @@ def collect(self, identifier: str, config: dict) -> Any:
Returns:
The collected object-tree.
"""
final_config = dict(self.default_config)
final_config.update(config)
final_config = ChainMap(config, self.default_config)

log.debug("Preparing input")
json_input = json.dumps({"objects": [{"path": identifier, **final_config}]})

0 comments on commit c634d2c

Please sign in to comment.