基本目标
- 实现工作目录的切换;
- 实现非退出命令下的无限循环;
到目前为止还不能实现的功能
- 动态导入 py 文件中的模块
- 缺少组件
定义虚拟进程类
command.py
from ctypes import windll
from PySide6.QtWidgets import QWidget
from sys import exit as sys_exit
from os import path, chdir
from warnings import filterwarnings
from command_version import CommandVersion
class Command:
def __init__(self, UI: QWidget = None):
windll.kernel32.SetConsoleTitleW("FalseOS Command")
filterwarnings("ignore", category=SyntaxWarning)
def run(self, command: str):
if command == "":
return
command += " "
conlist = command.split(" ")
if "exit" == conlist[0].strip():
self.exit()
elif "#" in conlist[0].strip():
print(f"'{conlist[0].strip()}' 不是可导入的模块。")
return
try:
exec(f"self.{conlist[0].strip()}(conlist[1:])")
except:
print(f"'{conlist[0].strip()}' 不是可导入的模块。")
def path(self) -> str:
return path.abspath(".")
@staticmethod
def exit():
sys_exit(0)
@staticmethod
def about(args):
print(f"应用程序名称:{CommandVersion.name}\n"
f"应用程序版本:{CommandVersion.version}\n"
f"应用程序内部版本:{CommandVersion.build_ver}\n"
f"应用程序证书发行机构:{CommandVersion.creater}")
def cwd(self, args: list[str]):
args0 = args[0]
try:
try:
args0 = args[0].replace("*", " ")
except:
args0 = args[0]
try:
args0 = args0.replace("/", "\\")
except:
pass
chdir(args0)
except:
if not self.is_non_str(args0):
print(f"指定的路径无效:\n\tcwd {self.addlist(args)}\n\t {"^" * len(args0)}")
else:
print(f"指定的路径无效:路径为空")
return
@staticmethod
def is_non_str(string: str) -> bool:
for i in string:
if i != " " and i != " " and i != "":
return False
return True
@staticmethod
def addlist(list0: list[str]):
re0 = ""
for i in list0:
re0 += i
re0 +=" "
return re0
定义应用程序信息
command_version.py
class CommandVersion:
name = "FalseOS Command"
version = "0.0.1"
build_ver = "240.00001"
creater = "YX 6223"
定义在 真 操作系统下的终端测试文件
command_loop.py
from command import Command
from command_version import CommandVersion
print(f"{CommandVersion.name} {CommandVersion.version} (内部构建版本:{CommandVersion.build_ver})\n"
f"Created By {CommandVersion.creater},保留所有权利。")
cmd = Command()
while True:
try:
a = input(f"FC {cmd.path()} >>> ")
cmd.run(a)
except KeyboardInterrupt:
print("\n终端进程被终止。")
exit(-1)
except SystemExit:
exit(0)
except OSError:
print("\n系统错误。")
exit(-1)
标签:PySide,Python,3.12,CommandVersion,print,exit,import,args0,command
From: https://blog.csdn.net/2402_84665876/article/details/141439394