yuuki/libs/config.py

88 lines
2.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
Yuuki_Libs
(c) 2020 Star Inc.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
2020-02-26 18:56:40 +08:00
import os
import yaml
class Yuuki_Config:
2020-02-26 18:56:40 +08:00
"""
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! DO NOT TOUCH DEFAULT SETTINGS !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Please config the value you want to set though `config.yaml`.
It will overwrite these settings.
"""
connectInfo = {
"Host": "",
"Command_Path": "",
"LongPoll_path": "",
}
connectHeader = {
"X-Line-Application": "",
"X-Line-Access": "",
"User-Agent": ""
}
systemConfig = {
"name": "Yuuki",
2020-02-26 06:37:31 +08:00
"version": "v6.5.3-alpha_RC1",
"version_check": True,
"project_url": "https://tinyurl.com/syb-yuuki",
"man_page": "https://tinyurl.com/yuuki-manual",
"privacy_page": "OpenSource - Licensed under MPL 2.0",
"copyright": "(c)2020 Star Inc.",
"Seq": 0,
"Admin": [],
2020-02-09 17:09:13 +08:00
"Advanced": False,
"SecurityService": False,
"Hour_KickLimit": 10,
"Hour_CancelLimit": 10,
"Default_Language": "en",
"GroupMebers_Demand": 100,
"helper_LINE_ACCESS_KEYs": [],
}
def __init__(self, config_path="config.yaml"):
2020-02-26 18:56:40 +08:00
assert os.path.isfile(config_path), "The configure file, `config.yaml` was not found."
with open(config_path, "r") as configfile:
2020-02-12 20:26:47 +08:00
self.config = yaml.load(configfile, Loader=yaml.FullLoader)
2020-02-01 14:27:17 +08:00
self._yuuki_config()
2020-02-01 14:27:17 +08:00
def _yuuki_config(self):
2020-02-13 14:34:12 +08:00
assert self.config is not None, "Invalid configure file"
2020-02-01 14:11:50 +08:00
if "Yuuki" in self.config:
for key in self.config["Yuuki"]:
if key in self.systemConfig:
2020-02-01 14:11:50 +08:00
self.systemConfig[key] = self.config["Yuuki"][key]
2020-02-09 17:09:13 +08:00
return self._server_config()
2020-02-01 14:27:17 +08:00
def _server_config(self):
2020-02-01 14:11:50 +08:00
if "Server" in self.config.get("LINE"):
for key in self.config["LINE"]["Server"]:
2020-02-02 00:34:26 +08:00
if key in self.connectInfo:
2020-02-01 14:11:50 +08:00
self.connectInfo[key] = self.config["LINE"]["Server"][key]
2020-02-09 17:09:13 +08:00
return self._account_config()
2020-02-01 14:27:17 +08:00
def _account_config(self):
2020-02-01 14:11:50 +08:00
if "Account" in self.config.get("LINE"):
for key in self.config["LINE"]["Account"]:
if key in ["X-Line-Application", "User-Agent"]:
self.config["LINE"]["Account"][key] = self.config["LINE"]["Account"][key].replace("\\t", "\t")
2020-02-02 00:34:26 +08:00
if key in self.connectHeader:
2020-02-01 14:11:50 +08:00
self.connectHeader[key] = self.config["LINE"]["Account"][key]
2020-02-01 14:11:50 +08:00
if "helper_LINE_ACCESS_KEYs" in self.config.get("LINE"):
self.systemConfig["helper_LINE_ACCESS_KEYs"] = self.config["LINE"]["helper_LINE_ACCESS_KEYs"]