Skip to content

Commit a8d0c34

Browse files
feat: separate private section in configuration
The generated configuration file is now divided in two sections: the first one, at the beginning of the file, contains user defined items and global parameters, while the second part, below a specific comment that warns about modification, contains all the private items (the ones whose names begin with `__When__private__` and should not be modified), so that the configuration file itself becomes more manageable
1 parent 7e7d3f7 commit a8d0c34

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

lib/configurator/writer.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,61 @@
44
from tomlkit import document, comment, item, aot
55
from time import strftime
66

7+
from ..utility import is_private_item_name
78
from ..i18n.strings import UI_APP
89

910

11+
# the comment text constants for the configuration file are defined here
12+
COMMENT_PUBLIC_SECTION = "global parameters and user defined items"
13+
COMMENT_PRIVATE_ITEMS = "private items: please do not modify below this line"
14+
15+
1016
# all items have an `as_table()` utility that converts them to TOML tables
17+
# this writer separates private items from user created ones
1118
def write_whenever_config(filename, tasks, conditions, events, globals) -> None:
19+
head = document()
1220
doc = document()
21+
priv = document()
1322
mod_time = strftime("%Y-%m-%d @%H:%M:%S")
14-
doc.add(comment(f"{UI_APP}: {mod_time}"))
23+
head.add(comment(f"{UI_APP}: {mod_time}"))
24+
head.add(comment(COMMENT_PUBLIC_SECTION))
1525
for k in globals:
1626
if globals[k] is not None:
1727
doc.add(k, item(globals[k]))
28+
priv.add(comment(COMMENT_PRIVATE_ITEMS))
29+
p = aot()
1830
t = aot()
1931
for elem in tasks:
20-
t.append(elem.as_table())
32+
if is_private_item_name(elem.name):
33+
p.append(elem.as_table())
34+
else:
35+
t.append(elem.as_table())
2136
doc.append("task", t)
37+
priv.append("task", p)
38+
p = aot()
2239
t = aot()
2340
for elem in conditions:
24-
t.append(elem.as_table())
41+
if is_private_item_name(elem.name):
42+
p.append(elem.as_table())
43+
else:
44+
t.append(elem.as_table())
2545
doc.append("condition", t)
46+
priv.append("condition", p)
47+
p = aot()
2648
t = aot()
2749
for elem in events:
28-
t.append(elem.as_table())
50+
if is_private_item_name(elem.name):
51+
p.append(elem.as_table())
52+
else:
53+
t.append(elem.as_table())
2954
doc.append("event", t)
55+
priv.append("event", p)
3056
with open(filename, "w") as f:
57+
f.write(head.as_string())
58+
f.write("\n")
3159
f.write(doc.as_string())
60+
f.write("\n")
61+
f.write(priv.as_string())
3262

3363

3464
# end.

0 commit comments

Comments
 (0)