[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot]
2024-08-18 15:38:13 +00:00
parent b7a295b3e2
commit 7d3b1c6469
3 changed files with 42 additions and 8 deletions

View File

@@ -216,5 +216,6 @@ if os.getenv("PYAPP"):
cli.add_command(service) cli.add_command(service)
def app_start(): def app_start():
cli(obj={}) cli(obj={})

View File

@@ -1,63 +1,76 @@
import os import os
import sys import sys
import rich_click as click import rich_click as click
from .service_managers import select_service_manager from .service_managers import select_service_manager
@click.group() @click.group()
@click.pass_context @click.pass_context
def service(ctx): def service(ctx):
"""Manage the program as a service (executable only)""" """Manage the program as a service (executable only)"""
ctx.ensure_object(dict) ctx.ensure_object(dict)
if os.getenv("PYAPP") is None: if os.getenv("PYAPP") is None:
print("Service commands are only available in the executable version of the program") print(
"Service commands are only available in the executable version of the"
" program"
)
sys.exit(1) sys.exit(1)
ctx.obj["service_manager"] = select_service_manager()(os.getenv("PYAPP")) ctx.obj["service_manager"] = select_service_manager()(os.getenv("PYAPP"))
@service.command() @service.command()
@click.pass_context @click.pass_context
def start(ctx): def start(ctx):
"""Start the service""" """Start the service"""
ctx.obj["service_manager"].start() ctx.obj["service_manager"].start()
@service.command() @service.command()
@click.pass_context @click.pass_context
def stop(ctx): def stop(ctx):
"""Stop the service""" """Stop the service"""
ctx.obj["service_manager"].stop() ctx.obj["service_manager"].stop()
@service.command() @service.command()
@click.pass_context @click.pass_context
def restart(ctx): def restart(ctx):
"""Restart the service""" """Restart the service"""
ctx.obj["service_manager"].restart() ctx.obj["service_manager"].restart()
@service.command() @service.command()
@click.pass_context @click.pass_context
def status(ctx): def status(ctx):
"""Get the status of the service""" """Get the status of the service"""
ctx.obj["service_manager"].status() ctx.obj["service_manager"].status()
@service.command() @service.command()
@click.pass_context @click.pass_context
def install(ctx): def install(ctx):
"""Install the service""" """Install the service"""
ctx.obj["service_manager"].install() ctx.obj["service_manager"].install()
@service.command() @service.command()
@click.pass_context @click.pass_context
def uninstall(ctx): def uninstall(ctx):
"""Uninstall the service""" """Uninstall the service"""
ctx.obj["service_manager"].uninstall() ctx.obj["service_manager"].uninstall()
@service.command() @service.command()
@click.pass_context @click.pass_context
def enable(ctx): def enable(ctx):
"""Enable the service""" """Enable the service"""
ctx.obj["service_manager"].enable() ctx.obj["service_manager"].enable()
@service.command() @service.command()
@click.pass_context @click.pass_context
def disable(ctx): def disable(ctx):
"""Disable the service""" """Disable the service"""
ctx.obj["service_manager"].disable() ctx.obj["service_manager"].disable()

View File

@@ -1,9 +1,10 @@
import os import os
import plistlib import plistlib
import subprocess import subprocess
from platform import system
from appdirs import user_log_dir from appdirs import user_log_dir
from platform import system
def select_service_manager() -> "ServiceManager": def select_service_manager() -> "ServiceManager":
platform = system() platform = system()
@@ -14,23 +15,32 @@ def select_service_manager() -> "ServiceManager":
else: else:
raise NotImplementedError("Unsupported platform") raise NotImplementedError("Unsupported platform")
class ServiceManager():
class ServiceManager:
def __init__(self, executable_path, *args, **kwargs): def __init__(self, executable_path, *args, **kwargs):
self.executable_path = executable_path self.executable_path = executable_path
def start(self): def start(self):
pass pass
def stop(self): def stop(self):
pass pass
def restart(self): def restart(self):
pass pass
def status(self): def status(self):
pass pass
def install(self): def install(self):
pass pass
def uninstall(self): def uninstall(self):
pass pass
def enable(self): def enable(self):
pass pass
def disable(self): def disable(self):
pass pass
@@ -39,15 +49,22 @@ class Launchd(ServiceManager):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.service_name = "com.dmunozv04.iSponsorBlockTV" self.service_name = "com.dmunozv04.iSponsorBlockTV"
self.service_path = os.path.expanduser("~/Library/LaunchAgents/") + self.service_name + ".plist" self.service_path = (
os.path.expanduser("~/Library/LaunchAgents/") + self.service_name + ".plist"
)
def start(self): def start(self):
subprocess.run(["launchctl", "start", self.service_name]) subprocess.run(["launchctl", "start", self.service_name])
def stop(self): def stop(self):
subprocess.run(["launchctl", "stop", self.service_name]) subprocess.run(["launchctl", "stop", self.service_name])
def restart(self): def restart(self):
subprocess.run(["launchctl", "restart", self.service_name]) subprocess.run(["launchctl", "restart", self.service_name])
def status(self): def status(self):
subprocess.run(["launchctl", "list", self.service_name]) subprocess.run(["launchctl", "list", self.service_name])
def install(self): def install(self):
if os.path.exists(self.service_path): if os.path.exists(self.service_path):
print("Service already installed") print("Service already installed")
@@ -68,6 +85,7 @@ class Launchd(ServiceManager):
plistlib.dump(plist, fp) plistlib.dump(plist, fp)
print("Service installed") print("Service installed")
self.enable() self.enable()
def uninstall(self): def uninstall(self):
self.disable() self.disable()
# Remove the file # Remove the file
@@ -76,8 +94,10 @@ class Launchd(ServiceManager):
print("Service uninstalled") print("Service uninstalled")
except FileNotFoundError: except FileNotFoundError:
print("Service not found") print("Service not found")
def enable(self): def enable(self):
subprocess.run(["launchctl", "load", self.service_path]) subprocess.run(["launchctl", "load", self.service_path])
def disable(self): def disable(self):
subprocess.run(["launchctl", "stop", self.service_name]) subprocess.run(["launchctl", "stop", self.service_name])
subprocess.run(["launchctl", "unload", self.service_path]) subprocess.run(["launchctl", "unload", self.service_path])