Skip to content

Commit a974e2c

Browse files
wolferCZxylix
authored andcommitted
Store available and autostart modules lists as configuration. See #47
NOTE: `ConfigParser` does not correctly store array values. Store module lists as json.
1 parent b7ed55a commit a974e2c

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

aw_qt/config.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from configparser import ConfigParser
2+
3+
from aw_core.config import load_config
4+
import json
5+
6+
default_settings = {
7+
"possible_modules": json.dumps(["aw-server",
8+
"aw-watcher-afk",
9+
"aw-watcher-window", ]),
10+
"autostart_modules": json.dumps(["aw-server",
11+
"aw-watcher-afk",
12+
"aw-watcher-window", ]),
13+
}
14+
default_testing_settings = {
15+
"possible_modules": json.dumps(["aw-server",
16+
"aw-watcher-afk",
17+
"aw-watcher-window", ]),
18+
"autostart_modules": json.dumps(["aw-server",
19+
"aw-watcher-afk",
20+
"aw-watcher-window", ]),
21+
}
22+
23+
default_config = ConfigParser()
24+
default_config['aw-qt'] = default_settings
25+
default_config['aw-qt-testing'] = default_testing_settings
26+
qt_config = load_config("aw-qt", default_config)
27+
28+
29+
class QTSettings:
30+
def __init__(self, testing: bool):
31+
config_section = qt_config["aw-qt" if not testing else "aw-qt-testing"]
32+
33+
# TODO: Resolved available modules automatically.
34+
# TODO: Filter away all modules not available on system
35+
self.possible_modules = json.loads(config_section["possible_modules"])
36+
self.autostart_modules = json.loads(config_section["autostart_modules"])

aw_qt/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def parse_args():
3030
help='Run the trayicon and services in testing mode')
3131
parser.add_argument('--autostart-modules', dest='autostart_modules',
3232
type=lambda s: [m for m in s.split(',') if m and m.lower() != "none"],
33-
default=["aw-server", "aw-watcher-afk", "aw-watcher-window"],
33+
default=None,
3434
help='A comma-separated list of modules to autostart, or just `none` to not autostart anything')
3535

3636
return parser.parse_args()

aw_qt/manager.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import aw_core
99

10+
from .config import QTSettings
11+
1012
logger = logging.getLogger(__name__)
1113

1214

@@ -126,22 +128,14 @@ def read_log(self) -> str:
126128

127129

128130
class Manager:
129-
def __init__(self, testing: bool=False) -> None:
130-
# TODO: Fetch these from somewhere appropriate (auto detect or a config file)
131-
# Save to config wether they should autostart or not.
132-
_possible_modules = [
133-
"aw-server",
134-
"aw-server-rust",
135-
"aw-watcher-afk",
136-
"aw-watcher-window",
137-
# "aw-watcher-spotify",
138-
# "aw-watcher-network"
139-
]
140-
141-
self.modules = []
142-
for name in _possible_modules:
143-
if not _locate_executable(name):
144-
self.modules.append(Module(name, testing=testing))
131+
def __init__(self, testing: bool = False) -> None:
132+
self.settings = QTSettings(testing)
133+
self.modules = {}
134+
for name in self.settings.possible_modules:
135+
if _locate_executable(name):
136+
self.modules[name] = Module(name, testing=testing)
137+
else:
138+
print("Module '{}' not found".format(name))
145139

146140
def get_unexpected_stops(self):
147141
return list(filter(lambda x: x.started and not x.is_alive(), self.modules.values()))
@@ -153,6 +147,11 @@ def start(self, module_name):
153147
logger.error("Unable to start module '{}': No such module".format(module_name))
154148

155149
def autostart(self, autostart_modules):
150+
151+
if autostart_modules is None:
152+
# Modules to start are not specified. Fallback on configuration.
153+
autostart_modules = self.settings.autostart_modules
154+
156155
# Always start aw-server first
157156
if "aw-server" in autostart_modules:
158157
self.start("aw-server")

0 commit comments

Comments
 (0)