1"""NotebookNode - adding attribute access to dicts"""
2
3from __future__ import annotations
4
5from collections.abc import Mapping
6
7from ._struct import Struct
8
9
10class NotebookNode(Struct):
11 """A dict-like node with attribute-access"""
12
13 def __setitem__(self, key, value):
14 """Set an item on the notebook."""
15 if isinstance(value, Mapping) and not isinstance(value, NotebookNode):
16 value = from_dict(value)
17 super().__setitem__(key, value)
18
19 def update(self, *args, **kwargs):
20 """
21 A dict-like update method based on CPython's MutableMapping `update`
22 method.
23 """
24 if len(args) > 1:
25 raise TypeError("update expected at most 1 arguments, got %d" % len(args))
26 if args:
27 other = args[0]
28 if isinstance(other, Mapping): # noqa: SIM114
29 for key in other:
30 self[key] = other[key]
31 elif hasattr(other, "keys"):
32 for key in other:
33 self[key] = other[key]
34 else:
35 for key, value in other:
36 self[key] = value
37 for key, value in kwargs.items():
38 self[key] = value
39
40
41def from_dict(d):
42 """Convert dict to dict-like NotebookNode
43
44 Recursively converts any dict in the container to a NotebookNode.
45 This does not check that the contents of the dictionary make a valid
46 notebook or part of a notebook.
47 """
48 if isinstance(d, dict):
49 return NotebookNode({k: from_dict(v) for k, v in d.items()})
50 if isinstance(d, (tuple, list)):
51 return [from_dict(i) for i in d]
52 return d