From 2564f6a054beb0c3afb2ebee90c009f9ceb89202 Mon Sep 17 00:00:00 2001 From: lanbinshijie Date: Thu, 13 Apr 2023 17:22:09 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E5=8A=A0=E5=85=A5=E4=BA=86?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=A7=A3=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- misc/Info.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/misc/Info.py b/misc/Info.py index bfaf3e9..88d57e4 100644 --- a/misc/Info.py +++ b/misc/Info.py @@ -4,6 +4,8 @@ # @Software: Vscode # @Time : 2023-1-13 14:18 +import os + class ProgramInfo: # Program Version version = "v1.0.0-alphav1.2.1" @@ -12,4 +14,78 @@ class ProgramInfo: models_path = r"./models/" registered_modules = ["print", "downlib", "dellib", "scan", "models", "shell", "*"] debug_mode = True + ssr_path = r"./ssr/" + +class SSR_Reader: + # Shell System Resource Reader + # 用来读取IceShell的配置文件和调用的系统资源信息 + # 主要是配置 + # Init 函数,检查ProgramInfo中对应的配置文件是否存在,如果不存在则创建 + # 地址为ProgramInfo.ssr_path + def __init__(self): + # 检查配置文件夹是否存在 + if not os.path.exists(ProgramInfo.ssr_path): + os.mkdir(ProgramInfo.ssr_path) + # 检查配置文件是否存在 + if not os.path.exists(ProgramInfo.ssr_path + "config.ssr"): + with open(ProgramInfo.ssr_path + "config.ssr", "w") as f: + # 写入默认配置(如现在ProgramInfo中的配置) + # 写入的内容要用双引号括起来,这样读取时可以直接eval + f.write("version=\"" + ProgramInfo.version + "\"\n") + f.write("author=\"" + ProgramInfo.author + "\"\n") + f.write("using_libs=" + str(ProgramInfo.using_libs) + "\n") + f.write("models_path=\"" + ProgramInfo.models_path + "\"\n") + f.write("registered_modules=" + str(ProgramInfo.registered_modules) + "\n") + f.write("debug_mode=" + str(ProgramInfo.debug_mode) + "\n") + f.write("ssr_path=\"" + ProgramInfo.ssr_path + "\"\n") + + # 读取配置文件 + self.config = self.paraphraser(ProgramInfo.ssr_path + "config.ssr") + # 更新配置 + self.update_config() + + + # 解码器:将配置文件中的格式转化成字典 + # 格式示例(都是合法的): + # key=value + # key = value + # 请注意,“#”是注释符号,不会被解析,一般独占一行的开头 + # 请注意,如果value中有“=”号,那么value中的“=”号不会被解析为=号而是作为普通字符 + + def paraphraser(self, config_file: str): + # 返回值:配置字典 + # 读取文件 + with open(config_file, "r") as f: + config = f.read() + # 解析文件 + config = config.split("\n") + config_dict = {} + for line in config: + if line != "": + if line[0] != "#": + line = line.split("=") + # 去除空格 + line[0] = line[0].strip() + line[1] = line[1].strip() + # 添加到字典 + config_dict[line[0]] = eval(line[1]) + return config_dict + + # 写入器:将字典写入配置文件 + def writer(self, config_file: str, config_dict: dict): + # 返回值:Bool 是否成功 + # 捕捉错误 + try: + # 打开文件 + with open(config_file, "w") as f: + # 写入 + for key in config_dict: + f.write(key + "=" + config_dict[key] + "\n") + return True + except: + return False + + + + From efa06802791db8a95c497deb75647a410655a7df Mon Sep 17 00:00:00 2001 From: lanbinshijie Date: Thu, 13 Apr 2023 17:28:05 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=BD=BF=E7=94=A8=E5=A4=9A=E6=96=87=E4=BB=B6=E5=90=8E?= =?UTF-8?q?=E5=8D=95=E6=96=87=E4=BB=B6=E6=A8=A1=E5=9D=97=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.py b/main.py index ee9d4d7..48813c1 100644 --- a/main.py +++ b/main.py @@ -14,12 +14,15 @@ def ExecuteModel(args, moduleName): if not SelfCheck.CheckModel(moduleName): return os.chdir(r"./models") + is_mutiple = False if not os.path.exists(f"{moduleName}.py"): os.chdir(rf"./{moduleName}") command = sys.executable + f" ./main.py " + args + is_mutiple = True else: command = sys.executable + f" ./{moduleName}.py " + args os.system(command) + if is_mutiple: os.chdir(r"..") os.chdir(r"..") def IceShell(): From 58095c2b3bcd2fb74b7fdcb80391ea94382264f8 Mon Sep 17 00:00:00 2001 From: lanbinshijie Date: Thu, 13 Apr 2023 17:30:25 +0800 Subject: [PATCH 3/5] =?UTF-8?q?style:=20=E5=88=A0=E9=99=A4=E4=BA=86?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.py b/main.py index 48813c1..fe84fbe 100644 --- a/main.py +++ b/main.py @@ -30,8 +30,7 @@ def IceShell(): commandN = input(Colors.RED + extra + "IShell> " + Colors.END) command = commandN.split(" ") if command[0] in ProgramInfo.registered_modules or "*" in ProgramInfo.registered_modules: - ExecuteModel(" ".join(command[1:]), command[0]) # 这个代码是不是有点问题? - # ExecuteModel("",commandN) + ExecuteModel(" ".join(command[1:]), command[0]) elif command[0] == "q": print("Bye~") exit(0) From 14c812b865ff17d537924e513a404809ac0c3567 Mon Sep 17 00:00:00 2001 From: lanbinshijie Date: Thu, 13 Apr 2023 17:31:40 +0800 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=AD=A3=E5=B8=B8=E9=80=80=E5=87=BA=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index fe84fbe..980480e 100644 --- a/main.py +++ b/main.py @@ -29,11 +29,11 @@ def IceShell(): extra = "" commandN = input(Colors.RED + extra + "IShell> " + Colors.END) command = commandN.split(" ") - if command[0] in ProgramInfo.registered_modules or "*" in ProgramInfo.registered_modules: - ExecuteModel(" ".join(command[1:]), command[0]) - elif command[0] == "q": + if command[0] == "q": print("Bye~") exit(0) + elif command[0] in ProgramInfo.registered_modules or "*" in ProgramInfo.registered_modules: + ExecuteModel(" ".join(command[1:]), command[0]) else: Error.printError(10000) From 2aeb9240eeb9925455a0e612074310dadbb53015 Mon Sep 17 00:00:00 2001 From: lanbinshijie Date: Thu, 13 Apr 2023 17:43:47 +0800 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89Shell?= =?UTF-8?q?=E5=89=8D=E7=BC=80=EF=BC=88Linux=20PS1=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 3 ++- tools/Phraser.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tools/Phraser.py diff --git a/main.py b/main.py index 980480e..5971c68 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ from tools.SelfCheck import SelfCheck from misc.Info import ProgramInfo from misc.Error import Error +from tools.Phraser import PS1 def ExecuteModel(args, moduleName): if not SelfCheck.CheckModel(moduleName): return @@ -27,7 +28,7 @@ def ExecuteModel(args, moduleName): def IceShell(): extra = "" - commandN = input(Colors.RED + extra + "IShell> " + Colors.END) + commandN = input(Colors.RED + extra + PS1.paraphraser() + Colors.END) command = commandN.split(" ") if command[0] == "q": print("Bye~") diff --git a/tools/Phraser.py b/tools/Phraser.py new file mode 100644 index 0000000..e9aec22 --- /dev/null +++ b/tools/Phraser.py @@ -0,0 +1,20 @@ + +# 字符替换工具 + +from misc.Info import ProgramInfo + +class PS1: + def paraphraser(prompt="[IShell] %u> "): + # 返回值:替换后的字符串 + # 将prompt中的特殊字符替换成对应的内容 + # 特殊字符: + # %n 程序名:默认为IShell + # %v 版本号:默认为ProgramInfo.version + # %u 用户:默认为Lanbin + + # 替换 + prompt = prompt.replace("%n", "IShell") + prompt = prompt.replace("%v", ProgramInfo.version) + prompt = prompt.replace("%u", "Lanbin") + return prompt +