Skip to content

Commit

Permalink
refactor: moved setpgrp, turned .format() into f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Aug 12, 2022
1 parent 6ca46c3 commit 0040f9b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
12 changes: 12 additions & 0 deletions aw_qt/main.py
@@ -1,3 +1,4 @@
import os
import sys
import logging
import subprocess
Expand Down Expand Up @@ -34,6 +35,17 @@ def main(testing: bool, autostart_modules: Optional[str]) -> None:
if platform.system() == "Darwin":
subprocess.call("syslog -s 'aw-qt successfully started logging'", shell=True)

# Create a process group, become its leader
# TODO: This shouldn't go here
if sys.platform != "win32":
# Running setpgrp when the python process is a session leader fails,
# such as in a systemd service. See:
# https://stackoverflow.com/a/51005084/1014208
try:
os.setpgrp()
except PermissionError:
pass

config = AwQtSettings(testing=testing)
_autostart_modules = (
[m.strip() for m in autostart_modules.split(",") if m and m.lower() != "none"]
Expand Down
39 changes: 13 additions & 26 deletions aw_qt/manager.py
Expand Up @@ -20,8 +20,8 @@


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


def is_executable(path: str, filename: str) -> bool:
Expand Down Expand Up @@ -53,7 +53,7 @@ def _discover_modules_in_directory(path: str) -> List["Module"]:
modules.extend(_discover_modules_in_directory(path))
else:
logger.warning(
"Found matching file but was not executable: {}".format(path)
f"Found matching file but was not executable: {path}"
)
return modules

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

modules: List[Module] = []
for path in search_paths:
Expand All @@ -87,7 +87,7 @@ def _discover_modules_system() -> List["Module"]:
if _parent_dir in search_paths:
search_paths.remove(_parent_dir)

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

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

def start(self, testing: bool) -> None:
logger.info("Starting module {}".format(self.name))

# Create a process group, become its leader
# TODO: This shouldn't go here
if sys.platform != "win32":
# Running setpgrp when the python process is a session leader fails,
# such as in a systemd service. See:
# https://stackoverflow.com/a/51005084/1014208
try:
os.setpgrp()
except PermissionError:
pass
logger.info(f"Starting module {self.name}")

exec_cmd = [str(self.path)]
if testing:
Expand Down Expand Up @@ -180,23 +169,21 @@ def stop(self) -> None:
# TODO: What if a module doesn't stop? Add timeout to p.wait() and then do a p.kill() if timeout is hit
if not self.started:
logger.warning(
"Tried to stop module {}, but it hasn't been started".format(self.name)
f"Tried to stop module {self.name}, but it hasn't been started"
)
return
elif not self.is_alive():
logger.warning(
"Tried to stop module {}, but it wasn't running".format(self.name)
)
logger.warning(f"Tried to stop module {self.name}, but it wasn't running")
else:
if not self._process:
logger.error("No reference to process object")
logger.debug("Stopping module {}".format(self.name))
logger.debug(f"Stopping module {self.name}")
if self._process:
self._process.terminate()
logger.debug("Waiting for module {} to shut down".format(self.name))
logger.debug(f"Waiting for module {self.name} to shut down")
if self._process:
self._process.wait()
logger.info("Stopped module {}".format(self.name))
logger.info(f"Stopped module {self.name}")

assert not self.is_alive()
self._last_process = self._process
Expand Down Expand Up @@ -270,7 +257,7 @@ def start(self, module_name: str) -> None:
system[0].start(self.testing)
else:
logger.error(
"Manager tried to start nonexistent module {}".format(module_name)
f"Manager tried to start nonexistent module {module_name}"
)

def autostart(self, autostart_modules: List[str]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions aw_qt/trayicon.py
Expand Up @@ -86,7 +86,7 @@ def __init__(
self.manager = manager
self.testing = testing

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

self._build_rootmenu()
Expand Down Expand Up @@ -133,7 +133,7 @@ def _build_rootmenu(self) -> None:
def show_module_failed_dialog(module: Module) -> None:
box = QMessageBox(self._parent)
box.setIcon(QMessageBox.Warning)
box.setText("Module {} quit unexpectedly".format(module.name))
box.setText(f"Module {module.name} quit unexpectedly")
box.setDetailedText(module.read_log(self.testing))

restart_button = QPushButton("Restart", box)
Expand Down

0 comments on commit 0040f9b

Please sign in to comment.