void-service-control/VoidServiceControl/classes.py

177 lines
4.6 KiB
Python
Raw Normal View History

2024-03-25 02:20:41 +00:00
# -*- coding: utf-8 -*-
2024-03-25 04:14:53 +00:00
import importlib.resources
2024-03-25 03:45:03 +00:00
import json
2024-03-25 02:20:41 +00:00
from enum import Enum
from os import listdir as ls
from subprocess import run as execute
from subprocess import PIPE
from sys import argv
2024-03-25 04:14:53 +00:00
2024-03-25 02:20:41 +00:00
from VoidServiceControl.env import *
class Help(Exception):
2024-03-25 02:37:45 +00:00
def __str__(self) -> str:
"""
Returns the program usage help
:return: Program usage help
"""
2024-03-25 03:45:03 +00:00
return lang.messages["help"]
2024-03-25 02:20:41 +00:00
2024-03-25 02:37:45 +00:00
def Su() -> None:
"""
Checks if the user has administrator rights
"""
2024-03-25 02:20:41 +00:00
user = execute('whoami', shell=True, text=True, stdout=PIPE).stdout[:-1]
if user != 'root':
2024-03-25 03:45:03 +00:00
raise PermissionError(lang.errors["su"])
2024-03-25 02:20:41 +00:00
2024-03-25 02:37:45 +00:00
# Types of service actions
2024-03-25 02:20:41 +00:00
class Action(Enum):
ENABLE = ["enable", "e", "on", "up"]
DISABLE = ["disable", "d", "off", "down"]
2024-03-25 02:37:45 +00:00
# Types of run arguments
2024-03-25 02:20:41 +00:00
class Arg(Enum):
HELP = ["--help", "-h", "help"]
@classmethod
2024-03-25 02:37:45 +00:00
def all(cls) -> list[str]:
"""
Returns all types of run arguments
:return: All types of run arguments list
"""
2024-03-25 02:20:41 +00:00
return cls.HELP.value + cls.__action()
@classmethod
2024-03-25 02:37:45 +00:00
def __action(cls) -> list[str]:
"""
Returns all types of service actions
:return: All types of service actions list
"""
2024-03-25 02:20:41 +00:00
actions = list(map(lambda action: action.value, Action))
out = []
for lst in actions:
out.extend(lst)
return out
class Args(object):
2024-03-25 02:37:45 +00:00
def __init__(self) -> None:
2024-03-25 02:20:41 +00:00
self.__arguments = argv[1:]
self.__check()
def __check(self) -> None:
2024-03-25 02:37:45 +00:00
"""
Checks the arguments
"""
if len(self.__arguments) == 1 and self.__arguments[0] in Arg.HELP.value:
2024-03-25 02:20:41 +00:00
raise Help()
if len(self.__arguments) < 2:
raise TypeError(lang.errors["usage"])
2024-03-25 02:20:41 +00:00
if self.__arguments[0] not in Arg.all():
2024-03-25 03:45:03 +00:00
raise TypeError(lang.errors["args"])
2024-03-25 02:20:41 +00:00
def __action(self) -> Action:
2024-03-25 02:37:45 +00:00
"""
Returns the action to the service
:return: Service action
"""
2024-03-25 02:20:41 +00:00
actions = list(Action)
for action in actions:
if self.__arguments[0] in action.value:
return action
def __iter__(self):
yield self.__action()
yield self.__arguments[1:]
2024-03-25 02:20:41 +00:00
class Service(object):
__enabled = False
def __init__(self, name: str) -> None:
self.__name = name
self.__check()
def __check(self) -> None:
2024-03-25 02:37:45 +00:00
"""
Checks if the service exists and is enabled
"""
2024-03-25 02:20:41 +00:00
all_services = ls(SV_PATH)
enabled_services = ls(ENABLED_PATH)
if self.__name not in all_services:
2024-03-25 03:45:03 +00:00
raise ValueError(lang.errors["ne"].format(self.getname()))
2024-03-25 02:20:41 +00:00
if self.__name in enabled_services:
self.__enabled = True
def getname(self) -> str:
2024-03-25 02:37:45 +00:00
"""
Returns the service name
:return: Service name
"""
2024-03-25 02:20:41 +00:00
return self.__name
2024-03-25 02:37:45 +00:00
def enable(self) -> None:
"""
Enable the service
"""
2024-03-25 02:20:41 +00:00
if self.__enabled:
2024-03-25 03:45:03 +00:00
raise ValueError(lang.errors["enabled"].format(self.getname()))
2024-03-25 02:20:41 +00:00
execute(f'ln -s {SV_PATH}/{self.__name} {ENABLED_PATH}/', shell=True)
def disable(self):
2024-03-25 02:37:45 +00:00
"""
Disable the service
"""
2024-03-25 02:20:41 +00:00
if not self.__enabled:
2024-03-25 03:45:03 +00:00
raise ValueError(lang.errors["disabled"].format(self.getname()))
2024-03-25 02:20:41 +00:00
execute(f'rm {ENABLED_PATH}/{self.__name}', shell=True)
class Interface(object):
def __init__(self, service: str) -> None:
self.service = Service(service)
def action(self, action: Action) -> None:
2024-03-25 02:37:45 +00:00
"""
Performs an action on the service
:param action: Action on the service
"""
2024-03-25 02:20:41 +00:00
match action:
case Action.ENABLE:
self.service.enable()
2024-03-25 03:45:03 +00:00
print(lang.messages["enabled"].format(self.service.getname()))
2024-03-25 02:20:41 +00:00
case Action.DISABLE:
self.service.disable()
2024-03-25 03:45:03 +00:00
print(lang.messages["disabled"].format(self.service.getname()))
class Translate(object):
def __init__(self, language: str):
"""
:param language: Language (ex. en_US)
"""
self.lang = language
self.errors = self.__translation["errors"]
self.messages = self.__translation["messages"]
@property
def __translation(self):
try:
2024-03-25 04:14:53 +00:00
with importlib.resources.open_text(__package__, f"{self.lang}.json", "utf-8") as json_file:
2024-03-25 03:45:03 +00:00
return json.loads(json_file.read())
except FileNotFoundError:
2024-03-25 04:14:53 +00:00
with importlib.resources.open_text(__package__, f"en_US.json", "utf-8") as json_file:
2024-03-25 03:45:03 +00:00
return json.loads(json_file.read())
lang = Translate(LANG)