1
0
mirror of https://github.com/Eyre-S/Coeur-Morny-Cono-python.git synced 2024-09-27 18:56:20 +08:00
Coeur-Morny-Cono-python/morny/server_main.py

145 lines
3.9 KiB
Python
Raw Permalink Normal View History

2023-05-06 20:18:01 +08:00
import os
import sys
import threading
2023-05-06 23:44:08 +08:00
from morny.util.str import strm
from morny.morny_system import Coeur_Def
from morny import log
from morny.log import logger
2023-05-06 20:18:01 +08:00
from morny.morny_hello import morny_hello_text
2023-05-06 23:44:08 +08:00
from morny.morny_config import MornyConfigPrototype
2023-05-06 20:18:01 +08:00
thread_morny_init = "morny-init"
prop_name_token_tg_key = "TELEGRAM_BOT_API_TOKEN"
prop_name_token_morny_key = "MORNY_TG_TOKEN"
def main():
##
##
## 启动参数的声明
2023-05-06 23:44:08 +08:00
_config: MornyConfigPrototype = MornyConfigPrototype()
_printmode_version: bool = False
_printmode_hello: bool = False
_showHello: bool = True
2023-05-06 20:18:01 +08:00
2023-05-06 23:44:08 +08:00
# Todo: set startup time
2023-05-06 20:18:01 +08:00
##
##
## 从命令行与环境变量读取启动参数值
2023-05-06 23:44:08 +08:00
__i = 1;
_unknownArgs: list[str] = []
while (__i < len(sys.argv)) :
2023-05-06 20:18:01 +08:00
2023-05-06 23:44:08 +08:00
if (sys.argv[__i].startswith("-")):
2023-05-06 20:18:01 +08:00
2023-05-06 23:44:08 +08:00
match sys.argv[__i] :
case "-d" | "--dbg", "--debug":
log.set_debug_mode(True)
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--outdated-block" | "-ob" :
2023-05-06 23:44:08 +08:00
_config.eventIgnoreOutdated = True
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--no-hello" | "-hf" | "--quiet" | "-q" :
2023-05-06 23:44:08 +08:00
_showHello = False
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--only-hello" | "-ho" | "-o" | "-hi" :
2023-05-06 23:44:08 +08:00
_printmode_hello = True
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--version" | "-v" :
2023-05-06 23:44:08 +08:00
_printmode_version = True
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--token" | "-t" :
2023-05-06 23:44:08 +08:00
__i+=1; _config.telegram_bot_key = sys.argv[__i]
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--username" | "-u" :
2023-05-06 23:44:08 +08:00
__i+=1; _config.telegram_bot_username = sys.argv[__i]
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--master" | "-mm" :
2023-05-06 23:44:08 +08:00
__i+=1; _config.trusted_master = int(sys.argv[__i])
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--trusted-chat" | "-trs" :
2023-05-06 23:44:08 +08:00
__i+=1; _config.trusted_master = int(sys.argv[__i])
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--trusted-reader-dinner" | "-trsd" :
2023-05-06 23:44:08 +08:00
__i+=1; _config.trusted_dinnerReaders.add(int(sys.argv[__i]))
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--auto-cmd" | "-cmd" | "-c" :
2023-05-06 23:44:08 +08:00
_config.commandRefresh_onLogin = True
_config.commandRefresh_onLogout = True
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--auto-cmd-list" | "-ca" :
2023-05-06 23:44:08 +08:00
_config.commandRefresh_onLogin = True
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--auto-cmd-remove" | "-cr" :
2023-05-06 23:44:08 +08:00
_config.commandRefresh_onLogout = True
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--api" | "-a" :
2023-05-06 23:44:08 +08:00
__i+=1; _config.telegram_botApi_server = sys.argv[__i]
__i+=1;continue
2023-05-06 20:18:01 +08:00
case "--api-files" | "files-api" | "-af" :
2023-05-06 23:44:08 +08:00
__i+=1; _config.telegram_botApi_server4File = sys.argv[__i]
__i+=1;continue
2023-05-06 20:18:01 +08:00
2023-05-06 23:44:08 +08:00
_unknownArgs.append(sys.argv[__i])
__i+=1
2023-05-06 20:18:01 +08:00
2023-05-06 23:44:08 +08:00
if (_showHello):
logger.info(morny_hello_text())
if (_printmode_hello):
exit(0)
if (_unknownArgs.count != 0):
logger.warn("Can't understand arg to some meaning :")
for __arg in _unknownArgs:
logger.warn(f" {__arg}")
2023-05-11 20:40:32 +08:00
if (log.is_debug_mode()):
2023-05-06 23:44:08 +08:00
logger.warn("Debug log output enabled.\n It may lower your performance, make sure that you are not in production environment.")
logger.debug("Debug log output enabled.")
2023-05-06 20:18:01 +08:00
'''从系统环境变量设置的 bot token 值'''
2023-05-06 23:44:08 +08:00
_propToken:str|None = None
2023-05-06 20:18:01 +08:00
'''表明 bot token 值的来源是哪个系统环境变量'''
2023-05-06 23:44:08 +08:00
_propToken_key:str|None = None
for __key in [prop_name_token_tg_key, prop_name_token_morny_key] :
if (os.getenv(__key) != None) :
_propToken = os.getenv(__key)
_propToken_key = __key
2023-05-06 20:18:01 +08:00
##
##
## 启动参数的检查和处理
2023-05-06 23:44:08 +08:00
if _printmode_version :
logger.info(strm(
f"Morny Cono Version",
f"- version :",
f" {Coeur_Def.VERSION} {Coeur_Def.CODE.upper()}",
f"- md5hash :",
f" <unavailable_in_python_implementation>",
f"- rw.time :",
f" {Coeur_Def.TIMETAG} [UTC+8]",
));
exit()
2023-05-06 20:18:01 +08:00
2023-05-06 23:44:08 +08:00
logger.info(strm(
f"morny/server_main.py Executed >>>",
f"- version {Coeur_Def.VERSION} [{Coeur_Def.TIMETAG}]",
f"- Morny {Coeur_Def.CODE.upper()}",
))
2023-05-06 20:18:01 +08:00
##
##
## Coeur 参数检查以及正式呼叫主程序
2023-05-06 23:44:08 +08:00
if (_propToken != None) :
logger.info(f"Parameter <token> set by EnvVar ${_propToken_key}")
2023-05-06 20:18:01 +08:00
threading.current_thread().name = thread_morny_init
2023-05-06 23:44:08 +08:00
#todo: call coeur main