Coverage for src/navdict/navdict.py: 72%
347 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-10-17 07:58 +0200
« prev ^ index » next coverage.py v7.8.2, created at 2025-10-17 07:58 +0200
1"""
2NavDict: A navigable dictionary with dot notation access and automatic file loading.
4NavDict extends Python's built-in dictionary to support convenient dot notation
5access (data.user.name) alongside traditional key access (data["user"]["name"]).
6It automatically loads data files and can instantiate classes dynamically based
7on configuration.
9Features:
10 - Dot notation access for nested data structures
11 - Automatic file loading (CSV, YAML, JSON, etc.)
12 - Dynamic class instantiation from configuration
13 - Full backward compatibility with standard dictionaries
15Example:
16 >>> from navdict import navdict
17 >>> data = navdict({"user": {"name": "Alice", "config_file": "yaml//settings.yaml"}})
18 >>> data.user.name # "Alice"
19 >>> data.user.config_file # Automatically loads and parses settings.yaml
20 >>> data["user"]["name"] # Still works with traditional access
22Author: Rik Huygen
23License: MIT
24"""
26from __future__ import annotations
28__all__ = [
29 "navdict", # noqa: ignore typo
30 "NavDict",
31 "NavigableDict",
32 "get_resource_location",
33]
35import csv
36import datetime
37import importlib
38import itertools
39import logging
40import os
41import textwrap
42import warnings
43from enum import IntEnum
44from pathlib import Path
45from typing import Any
46from typing import Callable
48from rich.text import Text
49from rich.tree import Tree
50from ruamel.yaml import YAML
51from ruamel.yaml.scanner import ScannerError
53from navdict.directive import is_directive
54from navdict.directive import unravel_directive
55from navdict.directive import get_directive_plugin
57logger = logging.getLogger("navdict")
60def load_class(class_name: str):
61 """
62 Find and returns a class based on the fully qualified name.
64 A class name can be preceded with the string `class//` or `factory//`. This is used in YAML
65 files where the class is then instantiated on load.
67 Args:
68 class_name (str): a fully qualified name for the class
69 """
70 if class_name.startswith("class//"): 70 ↛ 71line 70 didn't jump to line 71 because the condition on line 70 was never true
71 class_name = class_name[7:]
72 elif class_name.startswith("factory//"): 72 ↛ 73line 72 didn't jump to line 73 because the condition on line 72 was never true
73 class_name = class_name[9:]
75 module_name, class_name = class_name.rsplit(".", 1)
76 module = importlib.import_module(module_name)
77 return getattr(module, class_name)
80def get_resource_location(parent_location: Path | None, in_dir: str | None) -> Path:
81 """
82 Returns the resource location.
84 The resource location is the path to the file that is provided in a directive
85 such as `yaml//` or `csv//`. The location of the file can be given as an absolute
86 path or can be relative in which case there are two possibilities:
88 1. the parent location is not None.
89 In this case the resource location will be relative to the parent's location.
90 2. the parent location is None.
91 In this case the resource location is taken to be relative to the current working directory '.'
92 unless the environment variable NAVDICT_DEFAULT_RESOURCE_LOCATION is provided in which case
93 it is taken from that variable.
94 3. when both arguments are None, the resource location will be the current working directory '.'
95 unless the environment variable NAVDICT_DEFAULT_RESOURCE_LOCATION is provided in which case
96 it is taken from that variable.
98 Args:
99 parent_location: the location of the parent navdict, or None
100 in_dir: a location extracted from the directive's value.
102 Returns:
103 A Path object with the resource location.
105 """
107 match (parent_location, in_dir):
108 case (_, str()) if Path(in_dir).is_absolute():
109 location = Path(in_dir)
110 case (None, str()):
111 location = Path(os.getenv("NAVDICT_DEFAULT_RESOURCE_LOCATION", ".")) / in_dir
112 case (Path(), str()):
113 location = parent_location / in_dir
114 case (Path(), None):
115 location = parent_location
116 case _:
117 location = Path(os.getenv("NAVDICT_DEFAULT_RESOURCE_LOCATION", "."))
119 # logger.debug(f"{location=}, {fn=}")
121 return location
124def load_csv(resource_name: str, parent_location: Path | None, *args, **kwargs) -> list[list[str]]:
125 """
126 Find and return the content of a CSV file.
128 If the `resource_name` argument starts with the directive (`csv//`), it will be split off automatically.
129 The `kwargs` dictionary can contain the key `header_rows` which indicates the number of header rows to
130 be skipped when processing the file.
132 Returns:
133 A list of the split lines, i.e. a list of lists of strings.
134 """
136 # logger.debug(f"{resource_name=}, {parent_location=}")
138 if resource_name.startswith("csv//"): 138 ↛ 139line 138 didn't jump to line 139 because the condition on line 138 was never true
139 resource_name = resource_name[5:]
141 if not resource_name: 141 ↛ 142line 141 didn't jump to line 142 because the condition on line 141 was never true
142 raise ValueError(f"Resource name should not be empty, but contain a valid filename.")
144 parts = resource_name.rsplit("/", 1)
145 in_dir, fn = parts if len(parts) > 1 else (None, parts[0]) # use a tuple here to make Mypy happy
147 try:
148 n_header_rows = int(kwargs["header_rows"])
149 except KeyError:
150 n_header_rows = 0
152 csv_location = get_resource_location(parent_location, in_dir)
154 def filter_lines(file_obj, n_skip):
155 """
156 Generator that filters out comment lines and skips header lines.
157 The standard library csv module cannot handle this functionality.
158 """
160 for line in itertools.islice(file_obj, n_skip, None):
161 if not line.strip().startswith("#"):
162 yield line
164 try:
165 with open(csv_location / fn, "r", encoding="utf-8") as file:
166 filtered_lines = filter_lines(file, n_header_rows)
167 csv_reader = csv.reader(filtered_lines)
168 data = list(csv_reader)
169 except FileNotFoundError:
170 logger.error(f"Couldn't load resource '{resource_name}', file not found", exc_info=True)
171 raise
173 return data
176def load_int_enum(enum_name: str, enum_content) -> IntEnum:
177 """Dynamically build (and return) and IntEnum.
179 In the YAML file this will look like below.
180 The IntEnum directive (where <name> is the class name):
182 enum: int_enum//<name>
184 The IntEnum content:
186 content:
187 E:
188 alias: ['E_SIDE', 'RIGHT_SIDE']
189 value: 1
190 F:
191 alias: ['F_SIDE', 'LEFT_SIDE']
192 value: 0
194 Args:
195 - enum_name: Enumeration name (potentially prepended with "int_enum//").
196 - enum_content: Content of the enumeration, as read from the navdict field.
197 """
198 if enum_name.startswith("int_enum//"): 198 ↛ 199line 198 didn't jump to line 199 because the condition on line 198 was never true
199 enum_name = enum_name[10:]
201 definition = {}
202 for side_name, side_definition in enum_content.items():
203 if "alias" in side_definition: 203 ↛ 206line 203 didn't jump to line 206 because the condition on line 203 was always true
204 aliases = side_definition["alias"]
205 else:
206 aliases = []
207 value = side_definition["value"]
209 definition[side_name] = value
211 for alias in aliases:
212 definition[alias] = value
214 return IntEnum(enum_name, definition)
217def load_yaml(resource_name: str, parent_location: Path | None = None, *args, **kwargs) -> NavigableDict:
218 """Find and return the content of a YAML file."""
220 # logger.debug(f"{resource_name=}, {parent_location=}")
222 if resource_name.startswith("yaml//"): 222 ↛ 223line 222 didn't jump to line 223 because the condition on line 222 was never true
223 resource_name = resource_name[6:]
225 parts = resource_name.rsplit("/", 1)
227 in_dir, fn = parts if len(parts) > 1 else (None, parts[0]) # use a tuple here to make Mypy happy
229 yaml_location = get_resource_location(parent_location, in_dir)
231 try:
232 yaml = YAML(typ="safe")
233 with open(yaml_location / fn, "r") as file:
234 data = yaml.load(file)
236 except FileNotFoundError:
237 logger.error(f"Couldn't load resource '{resource_name}', file not found", exc_info=True)
238 raise
239 except IsADirectoryError:
240 logger.error(
241 f"Couldn't load resource '{resource_name}', file seems to be a directory",
242 exc_info=True,
243 )
244 raise
245 except ScannerError as exc:
246 msg = f"A error occurred while scanning the YAML file: {yaml_location / fn}."
247 logger.error(msg, exc_info=True)
248 raise IOError(msg) from exc
250 data = NavigableDict(data, _filename=yaml_location / fn)
252 # logger.debug(f"{data.get_private_attribute('_filename')=}")
254 return data
257def _get_attribute(self, name, default):
258 """
259 Safely retrieve an attribute from the object, returning a default if not found.
261 This method uses object.__getattribute__() to bypass any custom __getattr__
262 or __getattribute__ implementations on the class, accessing attributes directly
263 from the object's internal dictionary.
265 Args:
266 name (str): The name of the attribute to retrieve.
267 default: The value to return if the attribute does not exist.
269 Returns:
270 The attribute value if it exists, otherwise the default value.
272 Note:
273 This is typically used internally to avoid infinite recursion when
274 implementing custom attribute access methods.
275 """
276 try:
277 attr = object.__getattribute__(self, name)
278 except AttributeError:
279 attr = default
280 return attr
283class NavigableDict(dict):
284 """
285 A NavigableDict is a dictionary where all keys in the original dictionary are also accessible
286 as attributes to the class instance. So, if the original dictionary (setup) has a key
287 "site_id" which is accessible as `setup['site_id']`, it will also be accessible as
288 `setup.site_id`.
290 Args:
291 head (dict): the original dictionary
292 label (str): a label or name that is used when printing the navdict
294 Examples:
295 >>> setup = NavigableDict({'site_id': 'KU Leuven', 'version': "0.1.0"})
296 >>> assert setup['site_id'] == setup.site_id
297 >>> assert setup['version'] == setup.version
299 Note:
300 We always want **all** keys to be accessible as attributes, or none. That means all
301 keys of the original dictionary shall be of type `str`.
303 """
305 def __init__(
306 self,
307 head: dict | None = None,
308 label: str | None = None,
309 _filename: str | Path | None = None,
310 ):
311 head = head or {}
312 super().__init__(head)
313 self.__dict__["_memoized"] = {}
314 self.__dict__["_label"] = label
315 self.__dict__["_filename"] = Path(_filename) if _filename is not None else None
317 # TODO:
318 # if _filename was not given as an argument, we might want to check if the `head` has a `_filename` and do
319 # something like:
320 #
321 # if _filename is None and isinstance(head, navdict):
322 # _filename = head.__dict__["_filename"]
323 # self.__dict__["_filename"] = _filename
325 # By agreement, we only want the keys to be set as attributes if all keys are strings.
326 # That way we enforce that always all keys are navigable, or none.
328 if any(True for k in head.keys() if not isinstance(k, str)):
329 # invalid_keys = list(k for k in head.keys() if not isinstance(k, str))
330 # logger.warning(f"Dictionary will not be dot-navigable, not all keys are strings [{invalid_keys=}].")
331 return
333 for key, value in head.items():
334 if isinstance(value, dict):
335 value = NavigableDict(head.__getitem__(key), _filename=_filename)
336 setattr(self, key, value)
337 else:
338 setattr(self, key, super().__getitem__(key))
340 def get_label(self) -> str | None:
341 return self.__dict__["_label"]
343 def set_label(self, value: str):
344 self.__dict__["_label"] = value
346 def add(self, key: str, value: Any):
347 """Set a value for the given key.
349 If the value is a dictionary, it will be converted into a NavigableDict and the keys
350 will become available as attributes provided that all the keys are strings.
352 Args:
353 key (str): the name of the key / attribute to access the value
354 value (Any): the value to assign to the key
355 """
356 if isinstance(value, dict) and not isinstance(value, NavigableDict):
357 value = NavigableDict(value)
358 setattr(self, key, value)
360 def clear(self) -> None:
361 for key in list(self.keys()):
362 self.__delitem__(key)
364 def __repr__(self):
365 return f"{self.__class__.__name__}({super()!r}) [id={id(self)}]"
367 def __delitem__(self, key):
368 dict.__delitem__(self, key)
369 object.__delattr__(self, key)
371 def __setattr__(self, key, value):
372 # logger.info(f"called __setattr__({self!r}, {key}, {value})")
373 if isinstance(value, dict) and not isinstance(value, NavigableDict): 373 ↛ 374line 373 didn't jump to line 374 because the condition on line 373 was never true
374 value = NavigableDict(value)
375 self.__dict__[key] = value
376 super().__setitem__(key, value)
377 try:
378 del self.__dict__["_memoized"][key]
379 except KeyError:
380 pass
382 @staticmethod
383 def _alias_hook(key: str) -> str:
384 raise NotImplementedError
386 def set_alias_hook(self, hook: Callable[[str], str]):
387 """
388 Sets an alias (hook) function that maps the given argument, an attribute
389 or a dict key, to a valid attribute or key.
391 The `hook` function accepts a string argument and return a string for
392 which the argument is an alias. The returned argument is expected to
393 be a valid attribute or key for this navdict.
394 """
395 # setattr(self, "_alias_hook", hook)
396 self.__dict__["_alias_hook"] = hook
398 # This method is called:
399 # - for *every* single attribute access on an object using dot notation.
400 # - when using the `getattr(obj, 'name') function
401 # - accessing any kind of attributes, e.g. instance or class variables,
402 # methods, properties, dunder methods, ...
403 #
404 # Note: `__getattr__` is only called when an attribute cannot be found
405 # through normal means.
406 def __getattribute__(self, key):
407 # logger.info(f"called __getattribute__({key}) ...")
408 try:
409 value = object.__getattribute__(self, key)
410 except AttributeError:
411 try:
412 alias = self._alias_hook(key)
413 value = object.__getattribute__(self, alias)
414 except NotImplementedError:
415 raise AttributeError(f"{type(self).__name__!r} object has no attribute {key!r}")
417 if key.startswith("__"): # small optimization
418 return value
419 # We can not directly call the `_handle_directive` function here due to infinite recursion
420 if is_directive(value):
421 m = object.__getattribute__(self, "_handle_directive")
422 return m(key, value)
423 else:
424 return value
426 def __delattr__(self, item):
427 # logger.info(f"called __delattr__({self!r}, {item})")
428 object.__delattr__(self, item)
429 dict.__delitem__(self, item)
431 def __setitem__(self, key, value):
432 # logger.debug(f"called __setitem__({self!r}, {key}, {value})")
433 if isinstance(value, dict) and not isinstance(value, NavigableDict):
434 value = NavigableDict(value)
435 super().__setitem__(key, value)
436 self.__dict__[key] = value
437 try:
438 del self.__dict__["_memoized"][key]
439 except KeyError:
440 pass
442 # This method is called:
443 # - whenever square brackets `[]` are used on an object, e.g. indexing or slicing.
444 # - during iteration, if an object doesn't have __iter__ defined, Python will try
445 # to iterate using __getitem__ with successive integer indices starting from 0.
446 def __getitem__(self, key):
447 # logger.info(f"called __getitem__({self!r}, {key})")
448 try:
449 value = super().__getitem__(key)
450 except KeyError:
451 try:
452 alias = self._alias_hook(key)
453 value = super().__getitem__(alias)
454 except NotImplementedError:
455 raise KeyError(f"{type(self).__name__!r} has no key {key!r}")
457 if isinstance(key, str) and key.startswith("__"): 457 ↛ 458line 457 didn't jump to line 458 because the condition on line 457 was never true
458 return value
459 # no danger for recursion here, so we can directly call the function
460 if is_directive(value): 460 ↛ 461line 460 didn't jump to line 461 because the condition on line 460 was never true
461 return self._handle_directive(key, value)
462 else:
463 return value
465 def _handle_directive(self, key, value) -> Any:
466 """
467 This method will handle the available directives. This may be builtin directives
468 like `class/` or `factory//`, or it may be external directives that were provided
469 as a plugin. Some builtin directives have also been provided as a plugin, e.g.
470 'yaml//' and 'csv//'.
472 Args:
473 key: the key of the field that might contain a directive
474 value: the value which might be a directive
476 Returns:
477 This function will return the value, either the original value or the result of
478 evaluating and executing a directive.
479 """
480 # logger.debug(f"called _handle_directive({key}, {value!r}) [id={id(self)}]")
482 directive_key, directive_value = unravel_directive(value)
483 # logger.debug(f"{directive_key=}, {directive_value=}")
485 if directive := get_directive_plugin(directive_key):
486 # logger.debug(f"{directive.name=}")
488 if key in self.__dict__["_memoized"]:
489 return self.__dict__["_memoized"][key]
491 args, kwargs = self._get_args_and_kwargs(key)
492 parent_location = self._get_location()
493 result = directive.func(directive_value, parent_location, *args, **kwargs)
495 self.__dict__["_memoized"][key] = result
496 return result
498 match directive_key:
499 case "class":
500 args, kwargs = self._get_args_and_kwargs(key)
501 return load_class(directive_value)(*args, **kwargs)
503 case "factory": 503 ↛ 504line 503 didn't jump to line 504 because the pattern on line 503 never matched
504 factory_args = _get_attribute(self, f"{key}_args", {})
505 return load_class(directive_value)().create(**factory_args)
507 case "int_enum":
508 content = object.__getattribute__(self, "content")
509 return load_int_enum(directive_value, content)
511 case _:
512 return value
514 def _get_location(self):
515 """Returns the location of the file from which this NavDict was loaded or None if no location exists."""
516 try:
517 filename = self.__dict__["_filename"]
518 return filename.parent if filename else None
519 except KeyError:
520 return None
522 def _get_args_and_kwargs(self, key):
523 """
524 Read the args and kwargs that are associated with the key of a directive.
526 An example of such a directive:
528 hexapod:
529 device: class//egse.hexapod.PunaProxy
530 device_args: [PUNA_01]
531 device_kwargs:
532 sim: true
534 There might not be any positional nor keyword arguments provided in which
535 case and empty tuple and/or dictionary is returned.
537 Returns:
538 A tuple containing any positional arguments and a dictionary containing
539 keyword arguments.
540 """
541 try:
542 args = object.__getattribute__(self, f"{key}_args")
543 except AttributeError:
544 args = ()
545 try:
546 kwargs = object.__getattribute__(self, f"{key}_kwargs")
547 except AttributeError:
548 kwargs = {}
550 return args, kwargs
552 def set_private_attribute(self, key: str, value: Any) -> None:
553 """Sets a private attribute for this object.
555 The name in key will be accessible as an attribute for this object, but the key will not
556 be added to the dictionary and not be returned by methods like keys().
558 The idea behind this private attribute is to have the possibility to add status information
559 or identifiers to this classes object that can be used by save() or load() methods.
561 Args:
562 key (str): the name of the private attribute (must start with an underscore character).
563 value: the value for this private attribute
565 Examples:
566 >>> setup = NavigableDict({'a': 1, 'b': 2, 'c': 3})
567 >>> setup.set_private_attribute("_loaded_from_dict", True)
568 >>> assert "c" in setup
569 >>> assert "_loaded_from_dict" not in setup
570 >>> assert setup.get_private_attribute("_loaded_from_dict") == True
572 """
573 if key in self: 573 ↛ 574line 573 didn't jump to line 574 because the condition on line 573 was never true
574 raise ValueError(f"Invalid argument key='{key}', this key already exists in the dictionary.")
575 if not key.startswith("_"): 575 ↛ 576line 575 didn't jump to line 576 because the condition on line 575 was never true
576 raise ValueError(f"Invalid argument key='{key}', must start with underscore character '_'.")
577 self.__dict__[key] = value
579 def get_private_attribute(self, key: str) -> Any:
580 """Returns the value of the given private attribute.
582 Args:
583 key (str): the name of the private attribute (must start with an underscore character).
585 Returns:
586 the value of the private attribute given in `key` or None if the attribute doesn't exist.
588 Note:
589 Because of the implementation, this private attribute can also be accessed as a 'normal'
590 attribute of the object. This use is however discouraged as it will make your code less
591 understandable. Use the methods to access these 'private' attributes.
592 """
593 if not key.startswith("_"): 593 ↛ 594line 593 didn't jump to line 594 because the condition on line 593 was never true
594 raise ValueError(f"Invalid argument key='{key}', must start with underscore character '_'.")
595 try:
596 return self.__dict__[key]
597 except KeyError:
598 return None
600 def has_private_attribute(self, key) -> bool:
601 """
602 Check if the given key is defined as a private attribute.
604 Args:
605 key (str): the name of a private attribute (must start with an underscore)
606 Returns:
607 True if the given key is a known private attribute.
608 Raises:
609 ValueError: when the key doesn't start with an underscore.
610 """
611 if not key.startswith("_"):
612 raise ValueError(f"Invalid argument key='{key}', must start with underscore character '_'.")
614 # logger.debug(f"{self.__dict__.keys()} for [id={id(self)}]")
616 try:
617 _ = self.__dict__[key]
618 return True
619 except KeyError:
620 return False
622 def get_raw_value(self, key):
623 """
624 Returns the raw value of the given key.
626 Some keys have special values that are interpreted by the NavigableDict class. An example is
627 a value that starts with 'class//'. When you access these values, they are first converted
628 from their raw value into their expected value, e.g. the instantiated object in the above
629 example. This method allows you to access the raw value before conversion.
630 """
631 try:
632 return object.__getattribute__(self, key)
633 except AttributeError:
634 raise KeyError(f"The key '{key}' is not defined.")
636 def __str__(self):
637 return self._pretty_str()
639 def _pretty_str(self, indent: int = 0):
640 msg = ""
642 for k, v in self.items():
643 if isinstance(v, NavigableDict):
644 msg += f"{' ' * indent}{k}:\n"
645 msg += v._pretty_str(indent + 1)
646 else:
647 msg += f"{' ' * indent}{k}: {v}\n"
649 return msg
651 def __rich__(self) -> Tree:
652 tree = Tree(self.__dict__["_label"] or "NavigableDict", guide_style="dim")
653 _walk_dict_tree(self, tree, text_style="dark grey")
654 return tree
656 def _save(self, fd, indent: int = 0):
657 """
658 Recursive method to write the dictionary to the file descriptor.
660 Indentation is done in steps of four spaces, i.e. `' '*indent`.
662 Args:
663 fd: a file descriptor as returned by the open() function
664 indent (int): indentation level of each line [default = 0]
666 """
668 # Note that the .items() method returns the actual values of the keys and doesn't use the
669 # __getattribute__ or __getitem__ methods. So the raw value is returned and not the
670 # _processed_ value.
672 for k, v in self.items():
673 # history shall be saved last, skip it for now
675 if k == "history": 675 ↛ 676line 675 didn't jump to line 676 because the condition on line 675 was never true
676 continue
678 # make sure to escape a colon in the key name
680 if isinstance(k, str) and ":" in k: 680 ↛ 681line 680 didn't jump to line 681 because the condition on line 680 was never true
681 k = '"' + k + '"'
683 if isinstance(v, NavigableDict):
684 fd.write(f"{' ' * indent}{k}:\n")
685 v._save(fd, indent + 1)
686 fd.flush()
687 continue
689 if isinstance(v, float): 689 ↛ 690line 689 didn't jump to line 690 because the condition on line 689 was never true
690 v = f"{v:.6E}"
691 fd.write(f"{' ' * indent}{k}: {v}\n")
692 fd.flush()
694 # now save the history as the last item
696 if "history" in self: 696 ↛ 697line 696 didn't jump to line 697 because the condition on line 696 was never true
697 fd.write(f"{' ' * indent}history:\n")
698 self.history._save(fd, indent + 1) # noqa
700 def get_memoized_keys(self):
701 return list(self.__dict__["_memoized"].keys())
703 def del_memoized_key(self, key: str):
704 try:
705 del self.__dict__["_memoized"][key]
706 return True
707 except KeyError:
708 return False
710 @staticmethod
711 def from_dict(my_dict: dict, label: str | None = None) -> NavigableDict:
712 """Create a NavigableDict from a given dictionary.
714 Remember that all keys in the given dictionary shall be of type 'str' in order to be
715 accessible as attributes.
717 Args:
718 my_dict: a Python dictionary
719 label: a label that will be attached to this navdict
721 Examples:
722 >>> setup = navdict.from_dict({"ID": "my-setup-001", "version": "0.1.0"}, label="Setup")
723 >>> assert setup["ID"] == setup.ID == "my-setup-001"
725 """
726 return NavigableDict(my_dict, label=label)
728 @staticmethod
729 def from_yaml_string(yaml_content: str | None = None, label: str | None = None) -> NavigableDict:
730 """Creates a NavigableDict from the given YAML string.
732 This method is mainly used for easy creation of a navdict from strings during unit tests.
734 Args:
735 yaml_content: a string containing YAML
736 label: a label that will be attached to this navdict
738 Returns:
739 a navdict that was loaded from the content of the given string.
740 """
742 if not yaml_content:
743 raise ValueError("Invalid argument to function: No input string or None given.")
745 yaml = YAML(typ="safe")
746 try:
747 data = yaml.load(yaml_content)
748 except ScannerError as exc:
749 raise ValueError(f"Invalid YAML string: {exc}")
751 return NavigableDict(data, label=label)
753 @staticmethod
754 def from_yaml_file(filename: str | Path | None = None) -> NavigableDict:
755 """Creates a navigable dictionary from the given YAML file.
757 Args:
758 filename (str): the path of the YAML file to be loaded
760 Returns:
761 a navdict that was loaded from the given location.
763 Raises:
764 ValueError: when no filename is given.
765 """
767 # logger.debug(f"{filename=}")
769 if not filename: 769 ↛ 770line 769 didn't jump to line 770 because the condition on line 769 was never true
770 raise ValueError("Invalid argument to function: No filename or None given.")
772 # Make sure the filename exists and is a regular file
773 filename = Path(filename).expanduser().resolve()
774 if not filename.is_file():
775 raise ValueError(f"Invalid argument to function, filename does not exist: {filename!s}")
777 data = load_yaml(str(filename))
779 if data == {}: 779 ↛ 780line 779 didn't jump to line 780 because the condition on line 779 was never true
780 warnings.warn(f"Empty YAML file: {filename!s}")
782 return data
784 def to_yaml_file(self, filename: str | Path | None = None, header: str = None, top_level_group: str = None) -> None:
785 """Saves a NavigableDict to a YAML file.
787 When no filename is provided, this method will look for a 'private' attribute
788 `_filename` and use that to save the data.
790 Args:
791 filename (str|Path): the path of the YAML file where to save the data
792 header (str): Custom header for this navdict
793 top_level_group (str): name of the optional top-level group
795 Note:
796 This method will **overwrite** the original or given YAML file and therefore you might
797 lose proper formatting and/or comments.
799 """
800 if filename is None and self.get_private_attribute("_filename") is None:
801 raise ValueError("No filename given or known, can not save navdict.")
803 if header is None: 803 ↛ 815line 803 didn't jump to line 815 because the condition on line 803 was always true
804 header = textwrap.dedent(
805 f"""
806 # This YAML file is generated by:
807 #
808 # navdict.to_yaml_file(setup, filename="{filename}')
809 #
810 # Created on {datetime.datetime.now(tz=datetime.timezone.utc).isoformat()}
812 """
813 )
815 with Path(filename).open("w") as fd:
816 fd.write(header)
817 indent = 0
818 if top_level_group: 818 ↛ 819line 818 didn't jump to line 819 because the condition on line 818 was never true
819 fd.write(f"{top_level_group}:\n")
820 indent = 1
822 self._save(fd, indent=indent)
824 self.set_private_attribute("_filename", Path(filename))
826 def get_filename(self) -> str | None:
827 """Returns the filename for this navdict or None when no filename could be determined."""
828 return self.get_private_attribute("_filename")
831navdict = NavigableDict
832NavDict = NavigableDict
833"""Shortcuts for NavigableDict and more Pythonic."""
836def _walk_dict_tree(dictionary: dict, tree: Tree, text_style: str = "green"):
837 for k, v in dictionary.items():
838 if isinstance(v, dict):
839 branch = tree.add(f"[purple]{k}", style="", guide_style="dim")
840 _walk_dict_tree(v, branch, text_style=text_style)
841 else:
842 text = Text.assemble((str(k), "medium_purple1"), ": ", (str(v), text_style))
843 tree.add(text)