Manage a DBus service configuration.
Configuration for the DBus service is a little different than for the serial client.
This is because the DBus service doesn't live reload a config after it changes. In
other words, if you edit the config file, the DBus service's loaded config will
show drift.
This is captured in the StagedConfig class, which holds both the config as served
by the live DBus service, and the config as loaded from the same file. These are
called the "active" and "target" config, respectively.
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.
| Attributes: |
-
type
(StageType)
–
The type of staged change. Either "set", "unset" or None.
-
active
(T)
–
The attribute value from the active config.
-
target
(T)
–
The attribute value from the target config.
|
Source code in crystalfontz/dbus/config.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 | @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.
Attributes:
type (StageType): The type of staged change. Either "set", "unset" or None.
active (T): The attribute value from the active config.
target (T): The attribute value from the target config.
"""
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.
| Attributes: |
-
active_config
(Config)
–
The active configuration, as loaded from the live
DBus service.
-
target_config
(Config)
–
The target configuration, as loaded from the service's
config file.
-
dirty
(bool)
–
When true, there is drift between the active and target config.
|
Source code in crystalfontz/dbus/config.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 | class StagedConfig:
"""
A staged configuration. Shows both the active and target configurations, and how
the attributes are expected to change.
Attributes:
active_config (Config): The active configuration, as loaded from the live
DBus service.
target_config (Config): The target configuration, as loaded from the service's
config file.
dirty (bool): When true, there is drift between the active and target config.
"""
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:
"""
The path to the config file.
"""
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()
|
file
property
The path to the config file.
get(name)
Get the staged status of an attribute.
Source code in crystalfontz/dbus/config.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 | 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 crystalfontz/dbus/config.py
| 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 crystalfontz/dbus/config.py
126
127
128
129
130
131
132 | 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 crystalfontz/dbus/config.py
| 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 crystalfontz/dbus/config.py
134
135
136
137
138
139
140 | 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)
|