Skip to content

Commit f6d26c7

Browse files
committed
fix: minor cleanup
1 parent 0a359bd commit f6d26c7

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

aw_qt/config.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@
55
import json
66

77
default_settings = {
8-
"autostart_modules": json.dumps(["aw-server-rust",
9-
"aw-server",
10-
"aw-watcher-afk",
11-
"aw-watcher-window", ]),
8+
"autostart_modules": json.dumps(
9+
["aw-server", "aw-watcher-afk", "aw-watcher-window",]
10+
),
1211
}
1312

1413
default_config = ConfigParser()
15-
default_config['aw-qt'] = default_settings
14+
default_config["aw-qt"] = default_settings
1615
# Currently there's no reason to make them differ
17-
default_config['aw-qt-testing'] = default_settings
18-
qt_config = load_config("aw-qt", default_config)
16+
default_config["aw-qt-testing"] = default_settings
17+
1918

20-
"""
21-
An instance of loaded settings, containing a list of modules to autostart.
22-
Constructor takes a `testing` boolean as an argument
23-
"""
2419
class AwQtSettings:
2520
def __init__(self, testing: bool):
21+
"""
22+
An instance of loaded settings, containing a list of modules to autostart.
23+
Constructor takes a `testing` boolean as an argument
24+
"""
25+
qt_config = load_config("aw-qt", default_config)
2626
config_section = qt_config["aw-qt" if not testing else "aw-qt-testing"]
2727

28-
self.autostart_modules: List[str] = json.loads(config_section["autostart_modules"])
28+
self.autostart_modules: List[str] = json.loads(
29+
config_section["autostart_modules"]
30+
)

aw_qt/manager.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ def _locate_executable(name: str) -> Optional[str]:
4848
elif _is_system_module(name): # Check if it's in PATH
4949
return name
5050
else:
51-
logger.warning("Could not find module '{}' in installation directory or PATH".format(name))
51+
logger.warning(
52+
"Could not find module '{}' in installation directory or PATH".format(name)
53+
)
5254
return None
5355

5456

55-
""" Look for modules in given directory path and recursively in subdirs matching aw-* """
5657
def _discover_modules_in_directory(modules: List[str], search_path: str) -> None:
58+
"""Look for modules in given directory path and recursively in subdirs matching aw-*"""
5759
matches = glob(os.path.join(search_path, "aw-*"))
5860
for match in matches:
5961
if os.path.isfile(match) and os.access(match, os.X_OK):
@@ -62,10 +64,13 @@ def _discover_modules_in_directory(modules: List[str], search_path: str) -> None
6264
elif os.path.isdir(match) and os.access(match, os.X_OK):
6365
_discover_modules_in_directory(modules, match)
6466
else:
65-
logger.warning("Found matching file but was not executable: {}".format(match))
67+
logger.warning(
68+
"Found matching file but was not executable: {}".format(match)
69+
)
70+
6671

67-
""" Use _discover_modules_in_directory to find all bundled modules """
6872
def _discover_modules_bundled() -> List[str]:
73+
"""Use ``_discover_modules_in_directory`` to find all bundled modules """
6974
modules: List[str] = []
7075
cwd = os.getcwd()
7176
_discover_modules_in_directory(modules, cwd)
@@ -76,8 +81,9 @@ def _discover_modules_bundled() -> List[str]:
7681
logger.info("Found no bundles modules")
7782
return modules
7883

79-
""" Find all aw- modules in PATH """
84+
8085
def _discover_modules_system() -> List[str]:
86+
"""Find all aw- modules in PATH"""
8187
search_paths = os.get_exec_path()
8288
modules = []
8389
for path in search_paths:
@@ -94,7 +100,9 @@ def _discover_modules_system() -> List[str]:
94100
class Module:
95101
def __init__(self, name: str, testing: bool = False) -> None:
96102
self.name = name
97-
self.started = False # Should be True if module is supposed to be running, else False
103+
self.started = (
104+
False # Should be True if module is supposed to be running, else False
105+
)
98106
self.testing = testing
99107
self.location = "system" if _is_system_module(name) else "bundled"
100108
self._process: Optional[subprocess.Popen[str]] = None
@@ -121,16 +129,19 @@ def start(self) -> None:
121129
# See: https://github.com/ActivityWatch/activitywatch/issues/212
122130
startupinfo = None
123131
if platform.system() == "Windows":
124-
startupinfo = subprocess.STARTUPINFO() #type: ignore
125-
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW #type: ignore
132+
startupinfo = subprocess.STARTUPINFO() # type: ignore
133+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # type: ignore
126134
elif platform.system() == "Darwin":
127135
logger.info("Macos: Disable dock icon")
128136
import AppKit
137+
129138
AppKit.NSBundle.mainBundle().infoDictionary()["LSBackgroundOnly"] = "1"
130139

131140
# There is a very good reason stdout and stderr is not PIPE here
132141
# See: https://github.com/ActivityWatch/aw-server/issues/27
133-
self._process = subprocess.Popen(exec_cmd, universal_newlines=True, startupinfo=startupinfo)
142+
self._process = subprocess.Popen(
143+
exec_cmd, universal_newlines=True, startupinfo=startupinfo
144+
)
134145
self.started = True
135146

136147
def stop(self) -> None:
@@ -139,13 +150,17 @@ def stop(self) -> None:
139150
"""
140151
# TODO: What if a module doesn't stop? Add timeout to p.wait() and then do a p.kill() if timeout is hit
141152
if not self.started:
142-
logger.warning("Tried to stop module {}, but it hasn't been started".format(self.name))
153+
logger.warning(
154+
"Tried to stop module {}, but it hasn't been started".format(self.name)
155+
)
143156
return
144157
elif not self.is_alive():
145-
logger.warning("Tried to stop module {}, but it wasn't running".format(self.name))
158+
logger.warning(
159+
"Tried to stop module {}, but it wasn't running".format(self.name)
160+
)
146161
else:
147162
if not self._process:
148-
logger.error("")
163+
logger.error("No reference to process object")
149164
logger.debug("Stopping module {}".format(self.name))
150165
if self._process:
151166
self._process.terminate()
@@ -203,19 +218,25 @@ def discover_modules(self) -> None:
203218
self.modules[m_name] = Module(m_name, testing=self.testing)
204219

205220
def get_unexpected_stops(self) -> List[Module]:
206-
return list(filter(lambda x: x.started and not x.is_alive(), self.modules.values()))
221+
return list(
222+
filter(lambda x: x.started and not x.is_alive(), self.modules.values())
223+
)
207224

208225
def start(self, module_name: str) -> None:
209226
if module_name in self.modules.keys():
210227
self.modules[module_name].start()
211228
else:
212-
logger.debug("Manager tried to start nonexistent module {}".format(module_name))
229+
logger.debug(
230+
"Manager tried to start nonexistent module {}".format(module_name)
231+
)
213232

214233
def autostart(self, autostart_modules: Optional[List[str]]) -> None:
215234
if autostart_modules is None:
216235
autostart_modules = []
217236
if len(autostart_modules) > 0:
218-
logger.info("Modules to start weren't specified in CLI arguments. Falling back to configuration.")
237+
logger.info(
238+
"Modules to start weren't specified in CLI arguments. Falling back to configuration."
239+
)
219240
autostart_modules = self.settings.autostart_modules
220241
# We only want to autostart modules that are both in found modules and are asked to autostart.
221242
modules_to_start = set(autostart_modules).intersection(set(self.modules.keys()))

aw_qt/trayicon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import subprocess
66
from collections import defaultdict
7-
from typing import Any, DefaultDict, List, Optional
7+
from typing import Any, DefaultDict, List, Optional, Dict
88
import webbrowser
99

1010
from PyQt5 import QtCore
@@ -25,7 +25,7 @@
2525
logger = logging.getLogger(__name__)
2626

2727

28-
def get_env():
28+
def get_env() -> Dict[str, str]:
2929
"""
3030
Necessary for xdg-open to work properly when PyInstaller overrides LD_LIBRARY_PATH
3131
@@ -43,7 +43,7 @@ def get_env():
4343
return env
4444

4545

46-
def open_url(url):
46+
def open_url(url: str) -> None:
4747
if sys.platform == "linux":
4848
env = get_env()
4949
subprocess.Popen(["xdg-open", url], env=env)

0 commit comments

Comments
 (0)