Add multi-lang support
This commit is contained in:
parent
776d025e72
commit
321f506fcb
|
@ -1,4 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import json
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from os import listdir as ls
|
from os import listdir as ls
|
||||||
from subprocess import run as execute
|
from subprocess import run as execute
|
||||||
|
@ -14,8 +15,7 @@ class Help(Exception):
|
||||||
Returns the program usage help
|
Returns the program usage help
|
||||||
:return: 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 lang.messages["help"]
|
||||||
"d/disable/off/down <service_name> - Stop service and remove it from autostart")
|
|
||||||
|
|
||||||
|
|
||||||
def Su() -> None:
|
def Su() -> None:
|
||||||
|
@ -24,7 +24,7 @@ def Su() -> None:
|
||||||
"""
|
"""
|
||||||
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(lang.errors["su"])
|
||||||
|
|
||||||
|
|
||||||
# Types of service actions
|
# Types of service actions
|
||||||
|
@ -69,11 +69,11 @@ class Args(object):
|
||||||
Checks the arguments
|
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(lang.errors["usage"])
|
||||||
if self.__arguments[0] in Arg.HELP.value:
|
if self.__arguments[0] in Arg.HELP.value:
|
||||||
raise Help()
|
raise Help()
|
||||||
if self.__arguments[0] not in Arg.all():
|
if self.__arguments[0] not in Arg.all():
|
||||||
raise TypeError("Error: Invalid args")
|
raise TypeError(lang.errors["args"])
|
||||||
|
|
||||||
def __action(self) -> Action:
|
def __action(self) -> Action:
|
||||||
"""
|
"""
|
||||||
|
@ -104,7 +104,7 @@ class Service(object):
|
||||||
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:
|
||||||
raise ValueError(f"Error: Service {self.__name} doesn't exists")
|
raise ValueError(lang.errors["ne"].format(self.getname()))
|
||||||
if self.__name in enabled_services:
|
if self.__name in enabled_services:
|
||||||
self.__enabled = True
|
self.__enabled = True
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ class Service(object):
|
||||||
Enable the service
|
Enable the service
|
||||||
"""
|
"""
|
||||||
if self.__enabled:
|
if self.__enabled:
|
||||||
raise ValueError(f"Error: Service {self.__name} already enabled")
|
raise ValueError(lang.errors["enabled"].format(self.getname()))
|
||||||
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):
|
||||||
|
@ -128,7 +128,7 @@ class Service(object):
|
||||||
Disable the service
|
Disable the service
|
||||||
"""
|
"""
|
||||||
if not self.__enabled:
|
if not self.__enabled:
|
||||||
raise ValueError(f"Error: Service {self.__name} already disabled")
|
raise ValueError(lang.errors["disabled"].format(self.getname()))
|
||||||
execute(f'rm {ENABLED_PATH}/{self.__name}', shell=True)
|
execute(f'rm {ENABLED_PATH}/{self.__name}', shell=True)
|
||||||
|
|
||||||
|
|
||||||
|
@ -145,7 +145,30 @@ class Interface(object):
|
||||||
match action:
|
match action:
|
||||||
case Action.ENABLE:
|
case Action.ENABLE:
|
||||||
self.service.enable()
|
self.service.enable()
|
||||||
print(f"Service '{self.service.getname()}' successfully enabled")
|
print(lang.messages["enabled"].format(self.service.getname()))
|
||||||
case Action.DISABLE:
|
case Action.DISABLE:
|
||||||
self.service.disable()
|
self.service.disable()
|
||||||
print(f"Service '{self.service.getname()}' successfully disabled")
|
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:
|
||||||
|
with open(f"VoidServiceControl/languages/{self.lang}.json", "r", encoding="utf-8") as json_file:
|
||||||
|
return json.loads(json_file.read())
|
||||||
|
except FileNotFoundError:
|
||||||
|
with open(f"VoidServiceControl/languages/en_US.json", "r", encoding="utf-8") as json_file:
|
||||||
|
return json.loads(json_file.read())
|
||||||
|
|
||||||
|
|
||||||
|
lang = Translate(LANG)
|
||||||
|
|
|
@ -1,6 +1,22 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from os import environ
|
||||||
|
|
||||||
# Edit this file if you are using custom paths to Runit services
|
# Edit these lines 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'
|
||||||
|
|
||||||
|
|
||||||
|
# Lang settings
|
||||||
|
def lang() -> str:
|
||||||
|
"""
|
||||||
|
Get system lang
|
||||||
|
:return: system lang
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return environ["LANG"][:5]
|
||||||
|
except KeyError:
|
||||||
|
return "en_US"
|
||||||
|
|
||||||
|
|
||||||
|
LANG = lang()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"errors": {"su": "Error: Access denied", "usage": "Bad usage", "args": "Error: Invalid args", "ne": "Error: Service '{}' doesn't exists", "enabled": "Error: Service '{}' already enabled", "disabled": "Error: Service '{}' already disabled"}, "messages": {"enabled": "Service '{}' successfully enabled", "disabled": "Service '{}' successfully disabled", "help": "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"}}
|
Loading…
Reference in New Issue