File: FrmFlatToNestedBase.py

package info (click to toggle)
python-nxtomomill 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,340 kB
  • sloc: python: 17,207; makefile: 15; sh: 3
file content (31 lines) | stat: -rw-r--r-- 1,053 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from __future__ import annotations

from nxtomomill.utils.io import deprecated
from .ConfigBase import ConfigBase


class FrmFlatToNestedBase(ConfigBase):
    """
    Base class defining behavior for models needing to be dumped with sections.

    Internally they are represented as 'flat' but externally they are represented as nested to ease user
    setting values.
    This is the historical design. Maybe in the future we will use nested models internally as well.
    """

    def to_cfg_file(self, file_path: str, filter_sections: tuple[str] = tuple()):
        nested_model = self.to_nested_model()
        nested_model.to_cfg_file(
            file_path=file_path,
            filter_sections=filter_sections,
        )

    def to_nested_model(self):
        raise NotImplementedError

    @deprecated(
        reason="Replaced by pydantic function 'model_dump'", since_version="2.0"
    )
    def to_dict(self) -> dict:
        config = self.to_nested_model()
        return {key.upper(): value for key, value in config.model_dump().items()}