Skip to content

Commit 0040f9b

Browse files
committed
refactor: moved setpgrp, turned .format() into f-strings
1 parent 6ca46c3 commit 0040f9b

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

aw_qt/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import sys
23
import logging
34
import subprocess
@@ -34,6 +35,17 @@ def main(testing: bool, autostart_modules: Optional[str]) -> None:
3435
if platform.system() == "Darwin":
3536
subprocess.call("syslog -s 'aw-qt successfully started logging'", shell=True)
3637

38+
# Create a process group, become its leader
39+
# TODO: This shouldn't go here
40+
if sys.platform != "win32":
41+
# Running setpgrp when the python process is a session leader fails,
42+
# such as in a systemd service. See:
43+
# https://stackoverflow.com/a/51005084/1014208
44+
try:
45+
os.setpgrp()
46+
except PermissionError:
47+
pass
48+
3749
config = AwQtSettings(testing=testing)
3850
_autostart_modules = (
3951
[m.strip() for m in autostart_modules.split(",") if m and m.lower() != "none"]

aw_qt/manager.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121

2222
def _log_modules(modules: List["Module"]) -> None:
23-
for module in modules:
24-
logger.info(" - {} at {}".format(module.name, module.path))
23+
for m in modules:
24+
logger.info(f" - {m.name} at {m.path}")
2525

2626

2727
def is_executable(path: str, filename: str) -> bool:
@@ -53,7 +53,7 @@ def _discover_modules_in_directory(path: str) -> List["Module"]:
5353
modules.extend(_discover_modules_in_directory(path))
5454
else:
5555
logger.warning(
56-
"Found matching file but was not executable: {}".format(path)
56+
f"Found matching file but was not executable: {path}"
5757
)
5858
return modules
5959

@@ -68,7 +68,7 @@ def _discover_modules_bundled() -> List["Module"]:
6868
if platform.system() == "Darwin":
6969
macos_dir = os.path.abspath(os.path.join(_parent_dir, os.pardir, "MacOS"))
7070
search_paths.append(macos_dir)
71-
logger.info("Searching for bundled modules in: {}".format(search_paths))
71+
logger.info(f"Searching for bundled modules in: {search_paths}")
7272

7373
modules: List[Module] = []
7474
for path in search_paths:
@@ -87,7 +87,7 @@ def _discover_modules_system() -> List["Module"]:
8787
if _parent_dir in search_paths:
8888
search_paths.remove(_parent_dir)
8989

90-
logger.debug("Searching for system modules in PATH: {}".format(search_paths))
90+
logger.debug(f"Searching for system modules in PATH: {search_paths}")
9191
modules: List["Module"] = []
9292
paths = [p for p in search_paths if os.path.isdir(p)]
9393
for path in paths:
@@ -133,21 +133,10 @@ def __eq__(self, other: Hashable) -> bool:
133133
return hash(self) == hash(other)
134134

135135
def __repr__(self) -> str:
136-
return "<Module {} at {}>".format(self.name, self.path)
136+
return f"<Module {self.name} at {self.path}>"
137137

138138
def start(self, testing: bool) -> None:
139-
logger.info("Starting module {}".format(self.name))
140-
141-
# Create a process group, become its leader
142-
# TODO: This shouldn't go here
143-
if sys.platform != "win32":
144-
# Running setpgrp when the python process is a session leader fails,
145-
# such as in a systemd service. See:
146-
# https://stackoverflow.com/a/51005084/1014208
147-
try:
148-
os.setpgrp()
149-
except PermissionError:
150-
pass
139+
logger.info(f"Starting module {self.name}")
151140

152141
exec_cmd = [str(self.path)]
153142
if testing:
@@ -180,23 +169,21 @@ def stop(self) -> None:
180169
# TODO: What if a module doesn't stop? Add timeout to p.wait() and then do a p.kill() if timeout is hit
181170
if not self.started:
182171
logger.warning(
183-
"Tried to stop module {}, but it hasn't been started".format(self.name)
172+
f"Tried to stop module {self.name}, but it hasn't been started"
184173
)
185174
return
186175
elif not self.is_alive():
187-
logger.warning(
188-
"Tried to stop module {}, but it wasn't running".format(self.name)
189-
)
176+
logger.warning(f"Tried to stop module {self.name}, but it wasn't running")
190177
else:
191178
if not self._process:
192179
logger.error("No reference to process object")
193-
logger.debug("Stopping module {}".format(self.name))
180+
logger.debug(f"Stopping module {self.name}")
194181
if self._process:
195182
self._process.terminate()
196-
logger.debug("Waiting for module {} to shut down".format(self.name))
183+
logger.debug(f"Waiting for module {self.name} to shut down")
197184
if self._process:
198185
self._process.wait()
199-
logger.info("Stopped module {}".format(self.name))
186+
logger.info(f"Stopped module {self.name}")
200187

201188
assert not self.is_alive()
202189
self._last_process = self._process
@@ -270,7 +257,7 @@ def start(self, module_name: str) -> None:
270257
system[0].start(self.testing)
271258
else:
272259
logger.error(
273-
"Manager tried to start nonexistent module {}".format(module_name)
260+
f"Manager tried to start nonexistent module {module_name}"
274261
)
275262

276263
def autostart(self, autostart_modules: List[str]) -> None:

aw_qt/trayicon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(
8686
self.manager = manager
8787
self.testing = testing
8888

89-
self.root_url = "http://localhost:{port}".format(port=5666 if self.testing else 5600)
89+
self.root_url = f"http://localhost:{5666 if self.testing else 5600}"
9090
self.activated.connect(self.on_activated)
9191

9292
self._build_rootmenu()
@@ -133,7 +133,7 @@ def _build_rootmenu(self) -> None:
133133
def show_module_failed_dialog(module: Module) -> None:
134134
box = QMessageBox(self._parent)
135135
box.setIcon(QMessageBox.Warning)
136-
box.setText("Module {} quit unexpectedly".format(module.name))
136+
box.setText(f"Module {module.name} quit unexpectedly")
137137
box.setDetailedText(module.read_log(self.testing))
138138

139139
restart_button = QPushButton("Restart", box)

0 commit comments

Comments
 (0)