PagerMaid-Pyro/pagermaid/config.py

86 lines
3.8 KiB
Python
Raw Normal View History

2022-05-23 12:40:30 +00:00
import os
from json import load as load_json
2022-05-25 04:19:25 +00:00
import sys
2022-05-23 12:40:30 +00:00
from yaml import load, FullLoader, safe_load
from shutil import copyfile
2022-05-25 04:19:25 +00:00
2022-08-01 16:04:45 +00:00
def strtobool(val, default=False):
2022-05-25 04:19:25 +00:00
"""Convert a string representation of truth to true (1) or false (0).
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return 1
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return 0
else:
2022-08-01 16:04:45 +00:00
print("[Degrade] invalid truth value %r" % (val,))
return default
2022-05-23 12:40:30 +00:00
try:
2022-07-05 12:49:28 +00:00
config = load(open(r"config.yml", encoding="utf-8"), Loader=FullLoader)
2022-05-23 12:40:30 +00:00
except FileNotFoundError:
print("The configuration file does not exist, and a new configuration file is being generated.")
copyfile(f"{os.getcwd()}{os.sep}config.gen.yml", "config.yml")
2022-05-25 04:19:25 +00:00
sys.exit(1)
2022-05-23 12:40:30 +00:00
2022-05-25 04:19:25 +00:00
class Config:
2022-05-23 12:40:30 +00:00
try:
API_ID = int(os.environ.get("API_ID", config["api_id"]))
API_HASH = os.environ.get("API_HASH", config["api_hash"])
2022-05-25 04:19:25 +00:00
STRING_SESSION = os.environ.get("STRING_SESSION")
2022-06-08 03:18:21 +00:00
DEBUG = strtobool(os.environ.get("PGM_DEBUG", config["debug"]))
2022-08-01 16:04:45 +00:00
ERROR_REPORT = strtobool(os.environ.get("PGM_ERROR_REPORT", config["error_report"]), True)
2022-06-08 03:18:21 +00:00
LANGUAGE = os.environ.get("PGM_LANGUAGE", config["application_language"])
REGION = os.environ.get("PGM_REGION", config["application_region"])
TTS = os.environ.get("PGM_TTS", config["application_tts"])
LOG = strtobool(os.environ.get("PGM_LOG", config["log"]))
LOG_ID = int(os.environ.get("PGM_LOG_ID", config["log_chatid"]))
IPV6 = strtobool(os.environ.get("PGM_IPV6", config["ipv6"]))
2022-08-01 16:04:45 +00:00
ALLOW_ANALYTIC = strtobool(os.environ.get("PGM_ALLOW_ANALYTIC", config["allow_analytic"]), True)
SENTRY_API = "https://0785960e63e04279a694d0486d47d9ea@o1342815.ingest.sentry.io/6617119"
MIXPANEL_API = "c79162511383b0fa1e9c062a2a86c855"
2022-06-08 03:18:21 +00:00
TIME_FORM = os.environ.get("PGM_TIME_FORM", config["time_form"])
DATE_FORM = os.environ.get("PGM_DATE_FORM", config["date_form"])
START_FORM = os.environ.get("PGM_START_FORM", config["start_form"])
2022-08-01 16:04:45 +00:00
SILENT = strtobool(os.environ.get("PGM_PGM_SILENT", config["silent"]), True)
2022-06-08 03:18:21 +00:00
PROXY_ADDRESS = os.environ.get("PGM_PROXY_ADDRESS", config["proxy_addr"])
PROXY_PORT = os.environ.get("PGM_PROXY_PORT", config["proxy_port"])
2022-05-23 12:40:30 +00:00
PROXY = None
if PROXY_ADDRESS and PROXY_PORT:
PROXY = dict(
scheme="socks5",
2022-05-23 12:40:30 +00:00
hostname=PROXY_ADDRESS,
port=int(PROXY_PORT)
2022-05-23 12:40:30 +00:00
)
2022-06-08 03:18:21 +00:00
GIT_SOURCE = os.environ.get("PGM_GIT_SOURCE", config["git_source"])
2022-06-09 14:54:05 +00:00
GIT_SOURCE = GIT_SOURCE.replace("TeamPGM/PagerMaid_Plugins/", "TeamPGM/PagerMaid_Plugins_Pyro/")
2022-05-23 12:40:30 +00:00
try:
with open(f"languages{os.sep}built-in{os.sep}{LANGUAGE}.yml", "r", encoding="utf-8") as f:
lang_dict = safe_load(f)
except Exception as e:
2022-06-08 03:18:21 +00:00
print("[Degrade] Reading language YAML file failed, try to use the english language file.")
2022-05-23 12:40:30 +00:00
print(e)
2022-06-08 03:18:21 +00:00
try:
with open(f"languages{os.sep}built-in{os.sep}{LANGUAGE}.yml", "r", encoding="utf-8") as f:
lang_dict = safe_load(f)
except Exception as e:
print("[Error] Reading English language YAML file failed.")
print(e)
sys.exit(1)
2022-05-23 12:40:30 +00:00
try:
with open(f"data{os.sep}alias.json", encoding="utf-8") as f:
alias_dict = load_json(f)
except Exception as e:
2022-06-08 03:18:21 +00:00
print(f"[Degrade] Reading alias file failed{e}")
2022-05-23 12:40:30 +00:00
alias_dict = {}
except ValueError as e:
print(e)
2022-05-25 04:19:25 +00:00
sys.exit(1)