Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 18, 2024
1 parent b7a295b commit 7d3b1c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/iSponsorBlockTV/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,6 @@ def start(ctx):

cli.add_command(service)


def app_start():
cli(obj={})
21 changes: 17 additions & 4 deletions src/iSponsorBlockTV/service/service_helpers.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,76 @@
import os
import sys

import rich_click as click

from .service_managers import select_service_manager


@click.group()
@click.pass_context
def service(ctx):
"""Manage the program as a service (executable only)"""
ctx.ensure_object(dict)
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)
ctx.obj["service_manager"] = select_service_manager()(os.getenv("PYAPP"))


@service.command()
@click.pass_context
def start(ctx):
"""Start the service"""
ctx.obj["service_manager"].start()



@service.command()
@click.pass_context
def stop(ctx):
"""Stop the service"""
ctx.obj["service_manager"].stop()


@service.command()
@click.pass_context
def restart(ctx):
"""Restart the service"""
ctx.obj["service_manager"].restart()



@service.command()
@click.pass_context
def status(ctx):
"""Get the status of the service"""
ctx.obj["service_manager"].status()


@service.command()
@click.pass_context
def install(ctx):
"""Install the service"""
ctx.obj["service_manager"].install()


@service.command()
@click.pass_context
def uninstall(ctx):
"""Uninstall the service"""
ctx.obj["service_manager"].uninstall()


@service.command()
@click.pass_context
def enable(ctx):
"""Enable the service"""
ctx.obj["service_manager"].enable()


@service.command()
@click.pass_context
def disable(ctx):
"""Disable the service"""
ctx.obj["service_manager"].disable()

28 changes: 24 additions & 4 deletions src/iSponsorBlockTV/service/service_managers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import plistlib
import subprocess
from platform import system

from appdirs import user_log_dir
from platform import system


def select_service_manager() -> "ServiceManager":
platform = system()
Expand All @@ -14,23 +15,32 @@ def select_service_manager() -> "ServiceManager":
else:
raise NotImplementedError("Unsupported platform")

class ServiceManager():

class ServiceManager:
def __init__(self, executable_path, *args, **kwargs):
self.executable_path = executable_path

def start(self):
pass

def stop(self):
pass

def restart(self):
pass

def status(self):
pass

def install(self):
pass

def uninstall(self):
pass

def enable(self):
pass

def disable(self):
pass

Expand All @@ -39,15 +49,22 @@ class Launchd(ServiceManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
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):
subprocess.run(["launchctl", "start", self.service_name])

def stop(self):
subprocess.run(["launchctl", "stop", self.service_name])

def restart(self):
subprocess.run(["launchctl", "restart", self.service_name])

def status(self):
subprocess.run(["launchctl", "list", self.service_name])

def install(self):
if os.path.exists(self.service_path):
print("Service already installed")
Expand All @@ -68,6 +85,7 @@ def install(self):
plistlib.dump(plist, fp)
print("Service installed")
self.enable()

def uninstall(self):
self.disable()
# Remove the file
Expand All @@ -76,12 +94,14 @@ def uninstall(self):
print("Service uninstalled")
except FileNotFoundError:
print("Service not found")

def enable(self):
subprocess.run(["launchctl", "load", self.service_path])

def disable(self):
subprocess.run(["launchctl", "stop", self.service_name])
subprocess.run(["launchctl", "unload", self.service_path])


class Systemd(ServiceManager):
pass
pass

0 comments on commit 7d3b1c6

Please sign in to comment.