plusdeck.dbus.config

StagedAttr dataclass

Bases: Generic[T]

A staged attribute. Shows both the active and target value, and how the value is expected to change when applied.

Source code in plusdeck/dbus/config.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@dataclass
class StagedAttr(Generic[T]):
    """
    A staged attribute. Shows both the active and target value, and how the value is
    expected to change when applied.
    """

    type: StageType
    active: T
    target: T

    def __repr__(self: Self) -> str:
        target: str = (
            self.target if type(self.target) is str else json.dumps(self.target)
        )

        if self.type is None:
            return target

        active: str = (
            self.active if type(self.active) is str else json.dumps(self.active)
        )

        return f"{active} ~> {target}"

StagedConfig

A staged configuration. Shows both the active and target configurations, and how the attributes are expected to change.

Source code in plusdeck/dbus/config.py
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class StagedConfig:
    """
    A staged configuration. Shows both the active and target configurations, and how
    the attributes are expected to change.
    """

    def __init__(self: Self, active_config: Config, target_config: Config) -> None:
        # The configuration currently loaded by the service
        self.active_config: Config = active_config
        # The configuration as per the file
        self.target_config: Config = target_config
        self.dirty = False

    def reload_target(self: Self) -> None:
        """
        Reload the target config from file.
        """

        self.target_config = Config.from_file(self.file)
        self._check_config_dirty()

    def _check_attr_dirty(self: Self, name: str) -> None:
        if self.target_config.get(name) != self.active_config.get(name):
            self.dirty = True

    def _check_config_dirty(self: Self) -> None:
        for f in fields(self.target_config):
            self._check_attr_dirty(f.name)

    @property
    def file(self: Self) -> str:
        file = self.target_config.file
        assert file is not None, "Target config must be from a file"
        return file

    def get(self: Self, name: str) -> StagedAttr[Any]:
        """
        Get the staged status of an attribute.
        """

        active_attr = self.active_config.get(name)
        target_attr = self.target_config.get(name)

        type_: StageType = None
        if active_attr != target_attr:
            if target_attr is None:
                type_ = "unset"
            else:
                type_ = "set"

        return StagedAttr(type=type_, active=active_attr, target=target_attr)

    def set(self: Self, name: str, value: str) -> None:
        """
        Stage a new value for an attribute.
        """

        self.target_config.set(name, value)
        self._check_attr_dirty(name)

    def unset(self: Self, name: str) -> None:
        """
        Stage the unsetting of a value for an attribute.
        """

        self.target_config.unset(name)
        self._check_attr_dirty(name)

    def as_dict(self: Self) -> Dict[str, Any]:
        d: Dict[str, Any] = dict()

        for f in fields(self.target_config):
            d[f.name] = asdict(self.get(f.name))

        return d

    def __repr__(self: Self) -> str:
        d: Dict[str, Any] = dict()

        for f in fields(self.target_config):
            d[f.name] = repr(self.get(f.name))

        dump = yaml.dump(d, Dumper=Dumper)
        return "\n".join(
            [f"~ {line}" if "~>" in line else f"  {line}" for line in dump.split("\n")]
        )

    def to_file(self: Self) -> None:
        """
        Save the target config to file.
        """

        self.target_config.to_file()

get(name)

Get the staged status of an attribute.

Source code in plusdeck/dbus/config.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def get(self: Self, name: str) -> StagedAttr[Any]:
    """
    Get the staged status of an attribute.
    """

    active_attr = self.active_config.get(name)
    target_attr = self.target_config.get(name)

    type_: StageType = None
    if active_attr != target_attr:
        if target_attr is None:
            type_ = "unset"
        else:
            type_ = "set"

    return StagedAttr(type=type_, active=active_attr, target=target_attr)

reload_target()

Reload the target config from file.

Source code in plusdeck/dbus/config.py
57
58
59
60
61
62
63
def reload_target(self: Self) -> None:
    """
    Reload the target config from file.
    """

    self.target_config = Config.from_file(self.file)
    self._check_config_dirty()

set(name, value)

Stage a new value for an attribute.

Source code in plusdeck/dbus/config.py
 96
 97
 98
 99
100
101
102
def set(self: Self, name: str, value: str) -> None:
    """
    Stage a new value for an attribute.
    """

    self.target_config.set(name, value)
    self._check_attr_dirty(name)

to_file()

Save the target config to file.

Source code in plusdeck/dbus/config.py
131
132
133
134
135
136
def to_file(self: Self) -> None:
    """
    Save the target config to file.
    """

    self.target_config.to_file()

unset(name)

Stage the unsetting of a value for an attribute.

Source code in plusdeck/dbus/config.py
104
105
106
107
108
109
110
def unset(self: Self, name: str) -> None:
    """
    Stage the unsetting of a value for an attribute.
    """

    self.target_config.unset(name)
    self._check_attr_dirty(name)