Skip to content

Commit 9a569fa

Browse files
authored
Merge pull request #64 from ActivityWatch/dev/fix-module-name-and-filtering
2 parents 02aba37 + 1924e3b commit 9a569fa

1 file changed

Lines changed: 38 additions & 16 deletions

File tree

aw_qt/manager.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ def _discover_modules_in_directory(path: str) -> List["Module"]:
2828
modules = []
2929
matches = glob(os.path.join(path, "aw-*"))
3030
for match in matches:
31-
if os.path.isfile(match) and os.access(match, os.X_OK):
32-
name = os.path.basename(match)
31+
filename = os.path.basename(match)
32+
if not _validate_module(filename):
33+
continue
34+
# NOTE: os.access(match, os.X_OK) seems to always return True on Windows
35+
elif os.path.isfile(match) and os.access(match, os.X_OK):
36+
name = _filename_to_name(filename)
3337
modules.append(Module(name, Path(match), "bundled"))
3438
elif os.path.isdir(match) and os.access(match, os.X_OK):
3539
modules.extend(_discover_modules_in_directory(match))
@@ -40,10 +44,29 @@ def _discover_modules_in_directory(path: str) -> List["Module"]:
4044
return modules
4145

4246

47+
def _filename_to_name(filename: str) -> str:
48+
return filename.replace(".exe", "")
49+
50+
51+
def _validate_module(fn: str) -> bool:
52+
# NOTE: Checks for bundled non-executable files that match aw-* are needed
53+
# since the os.access(..., os.X_OS) check will always return True on Windows.
54+
return (
55+
fn.startswith("aw-")
56+
and (".manifest" not in fn)
57+
and (".desktop" not in fn)
58+
and (".service" not in fn)
59+
)
60+
61+
62+
def _filter_filenames(filenames: List[str]) -> List[str]:
63+
return [fn for fn in filenames if _validate_module(fn)]
64+
65+
4366
def _discover_modules_bundled() -> List["Module"]:
4467
"""Use ``_discover_modules_in_directory`` to find all bundled modules """
4568
_search_paths = [_module_dir, _parent_dir]
46-
logger.info("Search paths: {}".format(_search_paths))
69+
logger.info("Searching for bundled modules in: {}".format(_search_paths))
4770
modules: List[Module] = []
4871
for path in _search_paths:
4972
modules += _discover_modules_in_directory(path)
@@ -61,18 +84,15 @@ def _discover_modules_system() -> List["Module"]:
6184
if _parent_dir in search_paths:
6285
search_paths.remove(_parent_dir)
6386

64-
logger.info(search_paths)
87+
logger.debug("Searching for system modules in PATH: {}".format(search_paths))
6588
modules: List["Module"] = []
66-
for path in search_paths:
67-
if os.path.isdir(path):
68-
files = os.listdir(path)
69-
for filename in files:
70-
if filename.startswith("aw-"):
71-
# Only pick the first match
72-
if filename not in [m.name for m in modules]:
73-
modules.append(
74-
Module(filename, Path(path) / filename, "system")
75-
)
89+
paths = [p for p in search_paths if os.path.isdir(p)]
90+
for path in paths:
91+
for filename in _filter_filenames(os.listdir(path)):
92+
name = _filename_to_name(filename)
93+
# Only pick the first match (to respect PATH priority)
94+
if name not in [m.name for m in modules]:
95+
modules.append(Module(name, Path(path) / filename, "system"))
7696

7797
logger.info("Found system modules:")
7898
_log_modules(modules)
@@ -205,7 +225,6 @@ def modules_bundled(self) -> List[Module]:
205225

206226
def discover_modules(self) -> None:
207227
# These should always be bundled with aw-qt
208-
# TODO: Find a way to differentiate between bundled and system (and prefer bundled when autostarting)
209228
found_modules = set(_discover_modules_bundled())
210229
found_modules |= set(_discover_modules_system())
211230
found_modules = {
@@ -222,7 +241,8 @@ def get_unexpected_stops(self) -> List[Module]:
222241
return list(filter(lambda x: x.started and not x.is_alive(), self.modules))
223242

224243
def start(self, module_name: str) -> None:
225-
# FIXME: Impossible to run system module if bundled version exists
244+
# NOTE: Will always prefer a bundled version, if available. This will not affect the
245+
# aw-qt menu since it directly calls the module's start() method.
226246
bundled = [m for m in self.modules_bundled if m.name == module_name]
227247
system = [m for m in self.modules_system if m.name == module_name]
228248
if bundled:
@@ -235,6 +255,8 @@ def start(self, module_name: str) -> None:
235255
)
236256

237257
def autostart(self, autostart_modules: List[str]) -> None:
258+
# NOTE: Currently impossible to autostart a system module if a bundled module with the same name exists
259+
238260
# We only want to autostart modules that are both in found modules and are asked to autostart.
239261
for name in autostart_modules:
240262
if name not in [m.name for m in self.modules]:

0 commit comments

Comments
 (0)