From a716b4f5d8136a8f30730e5ef044d4fb17ac7dde Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 6 May 2023 20:18:01 +0800 Subject: [PATCH] refactor project struct --- __main__.py | 7 ++ logger/log_appender.py | 2 +- morny/__main__.py | 7 ++ define.py => morny/define.py | 1 - morny/server_main.py | 133 +++++++++++++++++++++++++++++++++++ start.py | 132 ---------------------------------- 6 files changed, 148 insertions(+), 134 deletions(-) create mode 100644 __main__.py create mode 100644 morny/__main__.py rename define.py => morny/define.py (99%) create mode 100644 morny/server_main.py delete mode 100644 start.py diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..65cea6a --- /dev/null +++ b/__main__.py @@ -0,0 +1,7 @@ +from morny import server_main + +def main() -> None: + server_main.main() + +if __name__ == "__main__": + main() diff --git a/logger/log_appender.py b/logger/log_appender.py index c359c00..21e6cda 100644 --- a/logger/log_appender.py +++ b/logger/log_appender.py @@ -1,7 +1,7 @@ from datetime import datetime import threading -from define import Coeur_Def +from morny.define import Coeur_Def from .log import * if Coeur_Def.is_file_logging() : diff --git a/morny/__main__.py b/morny/__main__.py new file mode 100644 index 0000000..0afd851 --- /dev/null +++ b/morny/__main__.py @@ -0,0 +1,7 @@ +from morny import server_main + +def main(): + server_main.main() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/define.py b/morny/define.py similarity index 99% rename from define.py rename to morny/define.py index 40d6939..ee0de4e 100644 --- a/define.py +++ b/morny/define.py @@ -1,6 +1,5 @@ import os - class Coeur_Def : '''Morny Coeur python 程序以及当前的版本的元信息''' diff --git a/morny/server_main.py b/morny/server_main.py new file mode 100644 index 0000000..0364f8a --- /dev/null +++ b/morny/server_main.py @@ -0,0 +1,133 @@ +import os +import sys +import threading +from morny.define import Coeur_Def +from logger.logger import * +from morny.morny_hello import morny_hello_text + +thread_morny_init = "morny-init" +prop_name_token_tg_key = "TELEGRAM_BOT_API_TOKEN" +prop_name_token_morny_key = "MORNY_TG_TOKEN" + +def main(): + + ## + ## + ## 启动参数的声明 + + versionEchoMode:bool = False + welcomeEchoMode:bool = False + showWelcome:bool = True + key:str|None = None + username:str|None = None; + outdatedBlock:bool = False + master:int = 793274677 + trustedReadersOfDinner:set = set() + trustedChat:int = -1001541451710 + autoCmdList:bool = False + autoCmdRemove:bool = False + api:str|None = None + api4File:str|None = None + + + ## + ## + ## 从命令行与环境变量读取启动参数值 + + i = 1; + while (i < len(sys.argv)) : + + if (sys.argv[i].startswith("-")): + + match sys.argv[i] : + case "--outdated-block" | "-ob" : + outdatedBlock = True + i+=1;continue + case "--no-hello" | "-hf" | "--quiet" | "-q" : + showWelcome = False + i+=1;continue + case "--only-hello" | "-ho" | "-o" | "-hi" : + welcomeEchoMode = True + i+=1;continue + case "--version" | "-v" : + versionEchoMode = True + i+=1;continue + case "--token" | "-t" : + i+=1; key = sys.argv[i] + i+=1;continue + case "--username" | "-u" : + i+=1; username = sys.argv[i] + i+=1;continue + case "--master" | "-mm" : + i+=1; master = int(sys.argv[i]) + i+=1;continue + case "--trusted-chat" | "-trs" : + i+=1; trustedChat = int(sys.argv[i]) + i+=1;continue + case "--trusted-reader-dinner" | "-trsd" : + i+=1; trustedReadersOfDinner.add(int(sys.argv[i])) + i+=1;continue + case "--auto-cmd" | "-cmd" | "-c" : + autoCmdList = True + autoCmdRemove = True + i+=1;continue + case "--auto-cmd-list" | "-ca" : + autoCmdList = True + i+=1;continue + case "--auto-cmd-remove" | "-cr" : + autoCmdRemove = True + i+=1;continue + case "--api" | "-a" : + i+=1; api = sys.argv[i] + i+=1;continue + case "--api-files" | "files-api" | "-af" : + i+=1; api4File = sys.argv[i] + i+=1;continue + + warn(f"Can't understand arg to some meaning :\n {sys.argv[i]}") + i+=1 + + + '''从系统环境变量设置的 bot token 值''' + propToken:str|None = None + '''表明 bot token 值的来源是哪个系统环境变量''' + propTokenKey:str|None = None + + for iKey in [prop_name_token_tg_key, prop_name_token_morny_key] : + if (os.getenv(iKey) != None) : + propToken = os.getenv(iKey) + propTokenKey = iKey + + ## + ## + ## 启动参数的检查和处理 + + if versionEchoMode : + info(f"""Morny Cono Version + - version : + {Coeur_Def.VERSION} {Coeur_Def.CODE.upper()} + - md5hash : + + - rw.time : + {Coeur_Def.TIMETAG} [UTC+8]""" + ); exit() + + if showWelcome : info(morny_hello_text()) + if welcomeEchoMode : exit() + + info(f"""morny/server_main.py Executed >>> + - version {Coeur_Def.VERSION} [{Coeur_Def.TIMETAG}] + - Morny {Coeur_Def.CODE.upper()}""") + + ## + ## + ## Coeur 参数检查以及正式呼叫主程序 + + if (propToken != None) : + key = propToken + info(f"Parameter set by EnvVar ${propTokenKey}") + if (key == None) : + info("Parameter required has no value:\n --token.") + exit() + threading.current_thread().name = thread_morny_init + #todo call coeur main diff --git a/start.py b/start.py deleted file mode 100644 index bcaa662..0000000 --- a/start.py +++ /dev/null @@ -1,132 +0,0 @@ -import os -import sys -import threading -from define import Coeur_Def -from logger.logger import * -from morny.morny_hello import morny_hello_text - - -thread_morny_init = "morny-init" -prop_name_token_tg_key = "TELEGRAM_BOT_API_TOKEN" -prop_name_token_morny_key = "MORNY_TG_TOKEN" - -## -## -## 启动参数的声明 - -versionEchoMode:bool = False -welcomeEchoMode:bool = False -showWelcome:bool = True -key:str = None -username:str = None; -outdatedBlock:bool = False -master:int = 793274677 -trustedReadersOfDinner:set = set() -trustedChat:int = -1001541451710 -autoCmdList:bool = False -autoCmdRemove:bool = False -api:str = None -api4File:str = None - - -## -## -## 从命令行与环境变量读取启动参数值 - -i = 1; -while (i < len(sys.argv)) : - - if (sys.argv[i].startswith("-")): - - match sys.argv[i] : - case "--outdated-block" | "-ob" : - outdatedBlock = True - i+=1;continue - case "--no-hello" | "-hf" | "--quiet" | "-q" : - showWelcome = False - i+=1;continue - case "--only-hello" | "-ho" | "-o" | "-hi" : - welcomeEchoMode = True - i+=1;continue - case "--version" | "-v" : - versionEchoMode = True - i+=1;continue - case "--token" | "-t" : - i+=1; key = sys.argv[i] - i+=1;continue - case "--username" | "-u" : - i+=1; username = sys.argv[i] - i+=1;continue - case "--master" | "-mm" : - i+=1; master = int(sys.argv[i]) - i+=1;continue - case "--trusted-chat" | "-trs" : - i+=1; trustedChat = int(sys.argv[i]) - i+=1;continue - case "--trusted-reader-dinner" | "-trsd" : - i+=1; trustedReadersOfDinner.add(int(sys.argv[i])) - i+=1;continue - case "--auto-cmd" | "-cmd" | "-c" : - autoCmdList = True - autoCmdRemove = True - i+=1;continue - case "--auto-cmd-list" | "-ca" : - autoCmdList = True - i+=1;continue - case "--auto-cmd-remove" | "-cr" : - autoCmdRemove = True - i+=1;continue - case "--api" | "-a" : - i+=1; api = sys.argv[i] - i+=1;continue - case "--api-files" | "files-api" | "-af" : - i+=1; api4File = sys.argv[i] - i+=1;continue - - warn(f"Can't understand arg to some meaning :\n {sys.argv[i]}") - i+=1 - - -propToken:str = None -'''从系统环境变量设置的 bot token 值''' -propTokenKey:str = None -'''表明 bot token 值的来源是哪个系统环境变量''' - -for iKey in [prop_name_token_tg_key, prop_name_token_morny_key] : - if (os.getenv(iKey) != None) : - propToken = os.getenv(iKey) - propTokenKey = iKey - -## -## -## 启动参数的检查和处理 - -if versionEchoMode : - info(f"""Morny Cono Version -- version : - {Coeur_Def.VERSION} {Coeur_Def.CODE.upper()} -- md5hash : - -- rw.time : - {Coeur_Def.TIMETAG} [UTC+8]""" - ); exit() - -if showWelcome : info(morny_hello_text()) -if welcomeEchoMode : exit() - -info(f"""start.py Executed >>> -- version {Coeur_Def.VERSION} [{Coeur_Def.TIMETAG}] -- Morny {Coeur_Def.CODE.upper()}""") - -## -## -## Coeur 参数检查以及正式呼叫主程序 - -if (propToken != None) : - key = propToken - info(f"Parameter set by EnvVar ${propTokenKey}") -if (key == None) : - info("Parameter required has no value:\n --token.") - exit() -threading.current_thread().name = thread_morny_init -#todo call coeur main