Skip to content

Commit

Permalink
Store available and autostart modules lists as configuration. See #47
Browse files Browse the repository at this point in the history
NOTE: `ConfigParser` does not correctly store array values.
Store module lists as json.
  • Loading branch information
wolferCZ authored and xylix committed Mar 3, 2020
1 parent b7ed55a commit a974e2c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
36 changes: 36 additions & 0 deletions aw_qt/config.py
@@ -0,0 +1,36 @@
from configparser import ConfigParser

from aw_core.config import load_config
import json

default_settings = {
"possible_modules": json.dumps(["aw-server",
"aw-watcher-afk",
"aw-watcher-window", ]),
"autostart_modules": json.dumps(["aw-server",
"aw-watcher-afk",
"aw-watcher-window", ]),
}
default_testing_settings = {
"possible_modules": json.dumps(["aw-server",
"aw-watcher-afk",
"aw-watcher-window", ]),
"autostart_modules": json.dumps(["aw-server",
"aw-watcher-afk",
"aw-watcher-window", ]),
}

default_config = ConfigParser()
default_config['aw-qt'] = default_settings
default_config['aw-qt-testing'] = default_testing_settings
qt_config = load_config("aw-qt", default_config)


class QTSettings:
def __init__(self, testing: bool):
config_section = qt_config["aw-qt" if not testing else "aw-qt-testing"]

# TODO: Resolved available modules automatically.
# TODO: Filter away all modules not available on system
self.possible_modules = json.loads(config_section["possible_modules"])
self.autostart_modules = json.loads(config_section["autostart_modules"])
2 changes: 1 addition & 1 deletion aw_qt/main.py
Expand Up @@ -30,7 +30,7 @@ def parse_args():
help='Run the trayicon and services in testing mode')
parser.add_argument('--autostart-modules', dest='autostart_modules',
type=lambda s: [m for m in s.split(',') if m and m.lower() != "none"],
default=["aw-server", "aw-watcher-afk", "aw-watcher-window"],
default=None,
help='A comma-separated list of modules to autostart, or just `none` to not autostart anything')

return parser.parse_args()
31 changes: 15 additions & 16 deletions aw_qt/manager.py
Expand Up @@ -7,6 +7,8 @@

import aw_core

from .config import QTSettings

logger = logging.getLogger(__name__)


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


class Manager:
def __init__(self, testing: bool=False) -> None:
# TODO: Fetch these from somewhere appropriate (auto detect or a config file)
# Save to config wether they should autostart or not.
_possible_modules = [
"aw-server",
"aw-server-rust",
"aw-watcher-afk",
"aw-watcher-window",
# "aw-watcher-spotify",
# "aw-watcher-network"
]

self.modules = []
for name in _possible_modules:
if not _locate_executable(name):
self.modules.append(Module(name, testing=testing))
def __init__(self, testing: bool = False) -> None:
self.settings = QTSettings(testing)
self.modules = {}
for name in self.settings.possible_modules:
if _locate_executable(name):
self.modules[name] = Module(name, testing=testing)
else:
print("Module '{}' not found".format(name))

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

def autostart(self, autostart_modules):

if autostart_modules is None:
# Modules to start are not specified. Fallback on configuration.
autostart_modules = self.settings.autostart_modules

# Always start aw-server first
if "aw-server" in autostart_modules:
self.start("aw-server")
Expand Down

0 comments on commit a974e2c

Please sign in to comment.