-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
75 lines (68 loc) · 2.12 KB
/
config.py
File metadata and controls
75 lines (68 loc) · 2.12 KB
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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3 -u
"""
Description: Configuration loading and saving
Author: thnikk
"""
import os
import json
import re
def load(path):
""" Load config from file """
config_path = os.path.expanduser(path)
if not os.path.exists(config_path):
os.makedirs(config_path)
default_config = """// vim:ft=jsonc
{
"modules-left": ["workspaces", "privacy"],
"modules-center": ["updates"],
"modules-right": [
"weather", "volume", "network", "tray", "clock", "power"],
"popover-autohide": true,
"popover-exclusive": false,
"modules": {
"weather": {
"zip_code": "94102"
},
"updates": {
"interval": 300
},
"network": {
"always_show": true
}
}
}"""
with open(f"{config_path}/config.json", 'w', encoding='utf-8') as file:
file.write(default_config)
output = default_config
output = re.sub(re.compile(r"/\*.*?\*/", re.DOTALL), "", output)
output = re.sub(re.compile(r"^\s*//.*?$", re.MULTILINE), "", output)
return json.loads(output)
with open(
os.path.expanduser(f'{config_path}/config.json'),
'r', encoding='utf=8'
) as file:
output = file.read()
output = re.sub(re.compile(r"/\*.*?\*/", re.DOTALL), "", output)
output = re.sub(re.compile(r"^\s*//.*?$", re.MULTILINE), "", output)
return json.loads(output)
def save(path, config):
""" Save config to file """
config_path = os.path.expanduser(path)
if not os.path.exists(config_path):
os.makedirs(config_path)
# Clean up None values from config before saving
def clean_config(obj):
if isinstance(obj, dict):
return {
k: clean_config(v) for k, v in obj.items()
if v is not None
}
elif isinstance(obj, list):
return [clean_config(item) for item in obj]
return obj
cleaned = clean_config(config)
with open(
f"{config_path}/config.json", 'w', encoding='utf-8'
) as file:
json.dump(cleaned, file, indent=4)
file.write('\n')