Skip to content

Commit

Permalink
Merge pull request #9 from lanbinshijie/dev
Browse files Browse the repository at this point in the history
今日午间更新结束,谢谢各位
  • Loading branch information
lanbinshijie authored Apr 13, 2023
2 parents c4711bb + 2aeb924 commit edf6d3e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
13 changes: 8 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,31 @@
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
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():
extra = ""
commandN = input(Colors.RED + extra + "IShell> " + Colors.END)
commandN = input(Colors.RED + extra + PS1.paraphraser() + 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)
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)

Expand Down
76 changes: 76 additions & 0 deletions misc/Info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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




20 changes: 20 additions & 0 deletions tools/Phraser.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit edf6d3e

Please sign in to comment.