Add docstrings
This commit is contained in:
parent
73617c67dd
commit
776d025e72
|
@ -9,31 +9,48 @@ from VoidServiceControl.env import *
|
||||||
|
|
||||||
|
|
||||||
class Help(Exception):
|
class Help(Exception):
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
|
"""
|
||||||
|
Returns the program usage help
|
||||||
|
:return: Program usage help
|
||||||
|
"""
|
||||||
return ("Usage:\n---\nvsc {e/enable/on/up} <service_name> - Run service and add it to autostart\nvsc {"
|
return ("Usage:\n---\nvsc {e/enable/on/up} <service_name> - Run service and add it to autostart\nvsc {"
|
||||||
"d/disable/off/down <service_name> - Stop service and remove it from autostart")
|
"d/disable/off/down <service_name> - Stop service and remove it from autostart")
|
||||||
|
|
||||||
|
|
||||||
def Su():
|
def Su() -> None:
|
||||||
|
"""
|
||||||
|
Checks if the user has administrator rights
|
||||||
|
"""
|
||||||
user = execute('whoami', shell=True, text=True, stdout=PIPE).stdout[:-1]
|
user = execute('whoami', shell=True, text=True, stdout=PIPE).stdout[:-1]
|
||||||
if user != 'root':
|
if user != 'root':
|
||||||
raise PermissionError("Error: Access denied")
|
raise PermissionError("Error: Access denied")
|
||||||
|
|
||||||
|
|
||||||
|
# Types of service actions
|
||||||
class Action(Enum):
|
class Action(Enum):
|
||||||
ENABLE = ["enable", "e", "on", "up"]
|
ENABLE = ["enable", "e", "on", "up"]
|
||||||
DISABLE = ["disable", "d", "off", "down"]
|
DISABLE = ["disable", "d", "off", "down"]
|
||||||
|
|
||||||
|
|
||||||
|
# Types of run arguments
|
||||||
class Arg(Enum):
|
class Arg(Enum):
|
||||||
HELP = ["--help", "-h", "help"]
|
HELP = ["--help", "-h", "help"]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def all(cls):
|
def all(cls) -> list[str]:
|
||||||
|
"""
|
||||||
|
Returns all types of run arguments
|
||||||
|
:return: All types of run arguments list
|
||||||
|
"""
|
||||||
return cls.HELP.value + cls.__action()
|
return cls.HELP.value + cls.__action()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def __action(cls):
|
def __action(cls) -> list[str]:
|
||||||
|
"""
|
||||||
|
Returns all types of service actions
|
||||||
|
:return: All types of service actions list
|
||||||
|
"""
|
||||||
actions = list(map(lambda action: action.value, Action))
|
actions = list(map(lambda action: action.value, Action))
|
||||||
out = []
|
out = []
|
||||||
for lst in actions:
|
for lst in actions:
|
||||||
|
@ -43,11 +60,14 @@ class Arg(Enum):
|
||||||
|
|
||||||
class Args(object):
|
class Args(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.__arguments = argv[1:]
|
self.__arguments = argv[1:]
|
||||||
self.__check()
|
self.__check()
|
||||||
|
|
||||||
def __check(self) -> None:
|
def __check(self) -> None:
|
||||||
|
"""
|
||||||
|
Checks the arguments
|
||||||
|
"""
|
||||||
if len(self.__arguments) != 2 and self.__arguments[0] not in Arg.HELP.value:
|
if len(self.__arguments) != 2 and self.__arguments[0] not in Arg.HELP.value:
|
||||||
raise TypeError("Error: Bad Usage")
|
raise TypeError("Error: Bad Usage")
|
||||||
if self.__arguments[0] in Arg.HELP.value:
|
if self.__arguments[0] in Arg.HELP.value:
|
||||||
|
@ -56,6 +76,10 @@ class Args(object):
|
||||||
raise TypeError("Error: Invalid args")
|
raise TypeError("Error: Invalid args")
|
||||||
|
|
||||||
def __action(self) -> Action:
|
def __action(self) -> Action:
|
||||||
|
"""
|
||||||
|
Returns the action to the service
|
||||||
|
:return: Service action
|
||||||
|
"""
|
||||||
actions = list(Action)
|
actions = list(Action)
|
||||||
for action in actions:
|
for action in actions:
|
||||||
if self.__arguments[0] in action.value:
|
if self.__arguments[0] in action.value:
|
||||||
|
@ -74,6 +98,9 @@ class Service(object):
|
||||||
self.__check()
|
self.__check()
|
||||||
|
|
||||||
def __check(self) -> None:
|
def __check(self) -> None:
|
||||||
|
"""
|
||||||
|
Checks if the service exists and is enabled
|
||||||
|
"""
|
||||||
all_services = ls(SV_PATH)
|
all_services = ls(SV_PATH)
|
||||||
enabled_services = ls(ENABLED_PATH)
|
enabled_services = ls(ENABLED_PATH)
|
||||||
if self.__name not in all_services:
|
if self.__name not in all_services:
|
||||||
|
@ -82,14 +109,24 @@ class Service(object):
|
||||||
self.__enabled = True
|
self.__enabled = True
|
||||||
|
|
||||||
def getname(self) -> str:
|
def getname(self) -> str:
|
||||||
|
"""
|
||||||
|
Returns the service name
|
||||||
|
:return: Service name
|
||||||
|
"""
|
||||||
return self.__name
|
return self.__name
|
||||||
|
|
||||||
def enable(self):
|
def enable(self) -> None:
|
||||||
|
"""
|
||||||
|
Enable the service
|
||||||
|
"""
|
||||||
if self.__enabled:
|
if self.__enabled:
|
||||||
raise ValueError(f"Error: Service {self.__name} already enabled")
|
raise ValueError(f"Error: Service {self.__name} already enabled")
|
||||||
execute(f'ln -s {SV_PATH}/{self.__name} {ENABLED_PATH}/', shell=True)
|
execute(f'ln -s {SV_PATH}/{self.__name} {ENABLED_PATH}/', shell=True)
|
||||||
|
|
||||||
def disable(self):
|
def disable(self):
|
||||||
|
"""
|
||||||
|
Disable the service
|
||||||
|
"""
|
||||||
if not self.__enabled:
|
if not self.__enabled:
|
||||||
raise ValueError(f"Error: Service {self.__name} already disabled")
|
raise ValueError(f"Error: Service {self.__name} already disabled")
|
||||||
execute(f'rm {ENABLED_PATH}/{self.__name}', shell=True)
|
execute(f'rm {ENABLED_PATH}/{self.__name}', shell=True)
|
||||||
|
@ -101,6 +138,10 @@ class Interface(object):
|
||||||
self.service = Service(service)
|
self.service = Service(service)
|
||||||
|
|
||||||
def action(self, action: Action) -> None:
|
def action(self, action: Action) -> None:
|
||||||
|
"""
|
||||||
|
Performs an action on the service
|
||||||
|
:param action: Action on the service
|
||||||
|
"""
|
||||||
match action:
|
match action:
|
||||||
case Action.ENABLE:
|
case Action.ENABLE:
|
||||||
self.service.enable()
|
self.service.enable()
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Edit this file if you are using custom paths to Runit services
|
||||||
|
|
||||||
SV_PATH = '/etc/sv'
|
SV_PATH = '/etc/sv'
|
||||||
ENABLED_PATH = '/var/service'
|
ENABLED_PATH = '/var/service'
|
||||||
|
|
Loading…
Reference in New Issue