pymud 0.20.0a2__py3-none-any.whl → 0.20.0a4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pymud/__init__.py +2 -1
- pymud/__main__.py +2 -131
- pymud/logger.py +1 -1
- pymud/main.py +142 -0
- pymud/objects.py +18 -2
- pymud/pkuxkx.py +4 -7
- pymud/pymud.py +71 -49
- pymud/session.py +253 -68
- pymud/settings.py +1 -1
- {pymud-0.20.0a2.dist-info → pymud-0.20.0a4.dist-info}/METADATA +218 -199
- pymud-0.20.0a4.dist-info/RECORD +18 -0
- {pymud-0.20.0a2.dist-info → pymud-0.20.0a4.dist-info}/WHEEL +1 -1
- pymud-0.20.0a2.dist-info/RECORD +0 -17
- {pymud-0.20.0a2.dist-info → pymud-0.20.0a4.dist-info}/LICENSE.txt +0 -0
- {pymud-0.20.0a2.dist-info → pymud-0.20.0a4.dist-info}/entry_points.txt +0 -0
- {pymud-0.20.0a2.dist-info → pymud-0.20.0a4.dist-info}/top_level.txt +0 -0
pymud/__init__.py
CHANGED
@@ -4,7 +4,8 @@ from .objects import CodeBlock, Alias, SimpleAlias, Trigger, SimpleTrigger, Comm
|
|
4
4
|
from .extras import DotDict
|
5
5
|
from .session import Session
|
6
6
|
from .logger import Logger
|
7
|
+
from .main import main
|
7
8
|
|
8
9
|
__all__ = [
|
9
|
-
"PyMudApp", "Settings", "CodeBlock", "Alias", "SimpleAlias", "Trigger", "SimpleTrigger", "Command", "SimpleCommand", "Timer", "SimpleTimer", "GMCPTrigger", "Session", "PyMudApp", "DotDict", "Logger"
|
10
|
+
"PyMudApp", "Settings", "CodeBlock", "Alias", "SimpleAlias", "Trigger", "SimpleTrigger", "Command", "SimpleCommand", "Timer", "SimpleTimer", "GMCPTrigger", "Session", "PyMudApp", "DotDict", "Logger", "main"
|
10
11
|
]
|
pymud/__main__.py
CHANGED
@@ -1,133 +1,4 @@
|
|
1
|
-
|
2
|
-
from pathlib import Path
|
3
|
-
from .pymud import main
|
4
|
-
from .settings import Settings
|
5
|
-
|
6
|
-
CFG_TEMPLATE = {
|
7
|
-
"client": {
|
8
|
-
"buffer_lines" : 5000, # 保留缓冲行数
|
9
|
-
|
10
|
-
"interval" : 10, # 在自动执行中,两次命令输入中的间隔时间(ms)
|
11
|
-
"auto_connect" : True, # 创建会话后,是否自动连接
|
12
|
-
"auto_reconnect" : False, # 在会话异常断开之后,是否自动重连
|
13
|
-
"var_autosave" : True, # 断开时自动保存会话变量
|
14
|
-
"var_autoload" : True, # 初始化时自动加载会话变量
|
15
|
-
|
16
|
-
"echo_input" : False,
|
17
|
-
"beautify" : True, # 专门为解决控制台下PKUXKX字符画对不齐的问题
|
18
|
-
|
19
|
-
"status_display" : 1, # 状态窗口显示情况设置,0-不显示,1-显示在下方,2-显示在右侧
|
20
|
-
"status_height" : 4, # 下侧状态栏的高度
|
21
|
-
"status_width" : 30, # 右侧状态栏的宽度
|
22
|
-
|
23
|
-
},
|
24
|
-
"sessions" : {
|
25
|
-
"pkuxkx" : {
|
26
|
-
"host" : "mud.pkuxkx.net",
|
27
|
-
"port" : "8081",
|
28
|
-
"encoding" : "utf8",
|
29
|
-
"autologin" : "{0};{1}",
|
30
|
-
"default_script": "examples",
|
31
|
-
"chars" : {
|
32
|
-
"display_title" : ["yourid", "yourpassword", ""],
|
33
|
-
}
|
34
|
-
}
|
35
|
-
},
|
36
|
-
"keys" : {
|
37
|
-
"f3" : "#ig",
|
38
|
-
"f4" : "#clear",
|
39
|
-
"f11" : "#close",
|
40
|
-
"f12" : "#exit",
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
def init_pymud_env(args):
|
45
|
-
print(f"欢迎使用PyMUD, 版本{Settings.__version__}. 使用PyMUD时, 建议建立一个新目录(任意位置),并将自己的脚本以及配置文件放到该目录下.")
|
46
|
-
print("即将开始为首次运行初始化环境...")
|
47
|
-
|
48
|
-
dir = args.dir
|
49
|
-
if dir:
|
50
|
-
print(f"你已经指定了创建脚本的目录为 {args.dir}")
|
51
|
-
dir = Path(dir)
|
52
|
-
else:
|
53
|
-
dir = Path.home().joinpath('pkuxkx')
|
54
|
-
|
55
|
-
system = platform.system().lower()
|
56
|
-
dir_enter = input(f"检测到当前系统为 {system}, 请指定游戏脚本的目录(若目录不存在会自动创建),直接回车表示使用默认值 [{dir}]:")
|
57
|
-
if dir_enter:
|
58
|
-
dir = Path(dir_enter)
|
59
|
-
|
60
|
-
if dir.exists() and dir.is_dir():
|
61
|
-
print(f'检测到给定目录 {dir} 已存在,切换至此目录...')
|
62
|
-
else:
|
63
|
-
print(f'检测到给定目录 {dir} 不存在,正在创建并切换至目录...')
|
64
|
-
dir.mkdir()
|
65
|
-
|
66
|
-
os.chdir(dir)
|
67
|
-
|
68
|
-
if os.path.exists('pymud.cfg'):
|
69
|
-
print(f'检测到脚本目录下已存在pymud.cfg文件,将直接使用此文件进入PyMUD...')
|
70
|
-
else:
|
71
|
-
print(f'检测到脚本目录下不存在pymud.cfg文件,将使用默认内容创建该配置文件...')
|
72
|
-
with open('pymud.cfg', mode = 'x') as fp:
|
73
|
-
fp.writelines(json.dumps(CFG_TEMPLATE, indent = 4))
|
74
|
-
|
75
|
-
if not os.path.exists('examples.py'):
|
76
|
-
from pymud import pkuxkx
|
77
|
-
module_dir = pkuxkx.__file__
|
78
|
-
shutil.copyfile(module_dir, 'examples.py')
|
79
|
-
print(f'已将样例脚本拷贝至脚本目录,并加入默认配置文件')
|
80
|
-
|
81
|
-
print(f"后续可自行修改 {dir} 目录下的 pymud.cfg 文件以进行配置。")
|
82
|
-
if system == "windows":
|
83
|
-
print(f"后续运行PyMUD, 请在 {dir} 目录下键入命令: python -m pymud")
|
84
|
-
else:
|
85
|
-
print(f"后续运行PyMUD, 请在 {dir} 目录下键入命令: python3 -m pymud")
|
86
|
-
|
87
|
-
input('所有内容已初始化完毕, 请按回车进入PyMUD.')
|
88
|
-
|
89
|
-
module_entry(args)
|
90
|
-
|
91
|
-
def module_entry(args):
|
92
|
-
if args.debug:
|
93
|
-
logging.basicConfig(level = logging.NOTSET,
|
94
|
-
format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
|
95
|
-
datefmt = '%m-%d %H:%M',
|
96
|
-
filename = args.logfile,
|
97
|
-
filemode = 'a' if args.filemode else 'w',
|
98
|
-
encoding = "utf-8"
|
99
|
-
)
|
100
|
-
|
101
|
-
else:
|
102
|
-
logging.basicConfig(level = logging.NOTSET,
|
103
|
-
format = '%(asctime)s %(name)-12s: %(message)s',
|
104
|
-
datefmt = '%m-%d %H:%M',
|
105
|
-
handlers = [logging.NullHandler()],
|
106
|
-
)
|
107
|
-
|
108
|
-
cfg = "pymud.cfg"
|
109
|
-
if os.path.exists(cfg):
|
110
|
-
with open(cfg, "r", encoding="utf8", errors="ignore") as fp:
|
111
|
-
cfg_data = json.load(fp)
|
112
|
-
main(cfg_data)
|
113
|
-
else:
|
114
|
-
main()
|
115
|
-
|
1
|
+
from .main import main
|
116
2
|
|
117
3
|
if __name__ == "__main__":
|
118
|
-
|
119
|
-
subparsers = parser.add_subparsers(help = 'init用于初始化运行环境')
|
120
|
-
|
121
|
-
par_init = subparsers.add_parser('init', usage = "python -m pymud init [-h] [-d dir]", description = '初始化pymud运行环境, 包括建立脚本目录, 创建默认配置文件, 创建样例脚本等.')
|
122
|
-
par_init.add_argument('-d', '--dir', dest = 'dir', metavar = 'dir', type = str, default = '', help = '指定构建脚本目录的名称, 不指定时会根据操作系统选择不同默认值')
|
123
|
-
par_init.set_defaults(func = init_pymud_env)
|
124
|
-
|
125
|
-
parser.add_argument('-d', '--debug', dest = 'debug', action = 'store_true', default = False, help = '指定以调试模式进入PyMUD。此时,系统log等级将设置为logging.NOTSET, 所有log数据均会被记录。默认不启用。')
|
126
|
-
parser.add_argument('-l', '--logfile', dest = 'logfile', metavar = 'logfile', default = 'pymud.log', help = '指定调试模式下记录文件名,不指定时,默认为当前目录下的pymud.log')
|
127
|
-
parser.add_argument('-a', '--appendmode', dest = 'filemode', action = 'store_true', default = True, help = '指定log文件的访问模式是否为append尾部添加模式,默认为True。当为False时,使用w模式,即每次运行清空之前记录')
|
128
|
-
|
129
|
-
args=parser.parse_args()
|
130
|
-
if hasattr(args, 'func'):
|
131
|
-
args.func(args)
|
132
|
-
else:
|
133
|
-
module_entry(args)
|
4
|
+
main()
|
pymud/logger.py
CHANGED
@@ -15,7 +15,7 @@ class Logger:
|
|
15
15
|
:param raw: 记录带ANSI标记的原始内容,还是记录纯文本内容,默认为True,即记录带ANSI标记的原始内容。
|
16
16
|
"""
|
17
17
|
|
18
|
-
_esc_regx = re.compile("\x1b
|
18
|
+
_esc_regx = re.compile(r"\x1b\[[\d;]+[abcdmz]", flags = re.IGNORECASE)
|
19
19
|
|
20
20
|
def __init__(self, name, mode = 'a', encoding = "utf-8", errors = "ignore", raw = False):
|
21
21
|
self._name = name
|
pymud/main.py
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
import os, sys, json, platform, shutil, logging, argparse
|
2
|
+
from pathlib import Path
|
3
|
+
from .pymud import PyMudApp
|
4
|
+
from .settings import Settings
|
5
|
+
|
6
|
+
CFG_TEMPLATE = {
|
7
|
+
"client": {
|
8
|
+
"buffer_lines" : 5000, # 保留缓冲行数
|
9
|
+
|
10
|
+
"interval" : 10, # 在自动执行中,两次命令输入中的间隔时间(ms)
|
11
|
+
"auto_connect" : True, # 创建会话后,是否自动连接
|
12
|
+
"auto_reconnect" : False, # 在会话异常断开之后,是否自动重连
|
13
|
+
"var_autosave" : True, # 断开时自动保存会话变量
|
14
|
+
"var_autoload" : True, # 初始化时自动加载会话变量
|
15
|
+
|
16
|
+
"echo_input" : False,
|
17
|
+
"beautify" : True, # 专门为解决控制台下PKUXKX字符画对不齐的问题
|
18
|
+
|
19
|
+
"status_display" : 1, # 状态窗口显示情况设置,0-不显示,1-显示在下方,2-显示在右侧
|
20
|
+
"status_height" : 4, # 下侧状态栏的高度
|
21
|
+
"status_width" : 30, # 右侧状态栏的宽度
|
22
|
+
|
23
|
+
},
|
24
|
+
"sessions" : {
|
25
|
+
"pkuxkx" : {
|
26
|
+
"host" : "mud.pkuxkx.net",
|
27
|
+
"port" : "8081",
|
28
|
+
"encoding" : "utf8",
|
29
|
+
"autologin" : "{0};{1}",
|
30
|
+
"default_script": "examples",
|
31
|
+
"chars" : {
|
32
|
+
"display_title" : ["yourid", "yourpassword", ""],
|
33
|
+
}
|
34
|
+
}
|
35
|
+
},
|
36
|
+
"keys" : {
|
37
|
+
"f3" : "#ig",
|
38
|
+
"f4" : "#clear",
|
39
|
+
"f11" : "#close",
|
40
|
+
"f12" : "#exit",
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
def init_pymud_env(args):
|
45
|
+
print(f"欢迎使用PyMUD, 版本{Settings.__version__}. 使用PyMUD时, 建议建立一个新目录(任意位置),并将自己的脚本以及配置文件放到该目录下.")
|
46
|
+
print("即将开始为首次运行初始化环境...")
|
47
|
+
|
48
|
+
dir = args.dir
|
49
|
+
if dir:
|
50
|
+
print(f"你已经指定了创建脚本的目录为 {args.dir}")
|
51
|
+
dir = Path(dir)
|
52
|
+
else:
|
53
|
+
dir = Path.home().joinpath('pkuxkx')
|
54
|
+
|
55
|
+
system = platform.system().lower()
|
56
|
+
dir_enter = input(f"检测到当前系统为 {system}, 请指定游戏脚本的目录(若目录不存在会自动创建),直接回车表示使用默认值 [{dir}]:")
|
57
|
+
if dir_enter:
|
58
|
+
dir = Path(dir_enter)
|
59
|
+
|
60
|
+
if dir.exists() and dir.is_dir():
|
61
|
+
print(f'检测到给定目录 {dir} 已存在,切换至此目录...')
|
62
|
+
else:
|
63
|
+
print(f'检测到给定目录 {dir} 不存在,正在创建并切换至目录...')
|
64
|
+
dir.mkdir()
|
65
|
+
|
66
|
+
os.chdir(dir)
|
67
|
+
|
68
|
+
if os.path.exists('pymud.cfg'):
|
69
|
+
print(f'检测到脚本目录下已存在pymud.cfg文件,将直接使用此文件进入PyMUD...')
|
70
|
+
else:
|
71
|
+
print(f'检测到脚本目录下不存在pymud.cfg文件,将使用默认内容创建该配置文件...')
|
72
|
+
with open('pymud.cfg', mode = 'x') as fp:
|
73
|
+
fp.writelines(json.dumps(CFG_TEMPLATE, indent = 4))
|
74
|
+
|
75
|
+
if not os.path.exists('examples.py'):
|
76
|
+
from pymud import pkuxkx
|
77
|
+
module_dir = pkuxkx.__file__
|
78
|
+
shutil.copyfile(module_dir, 'examples.py')
|
79
|
+
print(f'已将样例脚本拷贝至脚本目录,并加入默认配置文件')
|
80
|
+
|
81
|
+
print(f"后续可自行修改 {dir} 目录下的 pymud.cfg 文件以进行配置。")
|
82
|
+
if system == "windows":
|
83
|
+
print(f"后续运行PyMUD, 请在 {dir} 目录下键入命令: python -m pymud")
|
84
|
+
else:
|
85
|
+
print(f"后续运行PyMUD, 请在 {dir} 目录下键入命令: python3 -m pymud")
|
86
|
+
|
87
|
+
input('所有内容已初始化完毕, 请按回车进入PyMUD.')
|
88
|
+
|
89
|
+
startApp(args)
|
90
|
+
|
91
|
+
def startApp(args):
|
92
|
+
startup_path = Path(args.startup_dir).resolve()
|
93
|
+
sys.path.append(f"{startup_path}")
|
94
|
+
os.chdir(startup_path)
|
95
|
+
|
96
|
+
if args.debug:
|
97
|
+
logging.basicConfig(level = logging.NOTSET,
|
98
|
+
format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
|
99
|
+
datefmt = '%m-%d %H:%M',
|
100
|
+
filename = args.logfile,
|
101
|
+
filemode = 'a' if args.filemode else 'w',
|
102
|
+
encoding = "utf-8"
|
103
|
+
)
|
104
|
+
|
105
|
+
else:
|
106
|
+
logging.basicConfig(level = logging.NOTSET,
|
107
|
+
format = '%(asctime)s %(name)-12s: %(message)s',
|
108
|
+
datefmt = '%m-%d %H:%M',
|
109
|
+
handlers = [logging.NullHandler()],
|
110
|
+
)
|
111
|
+
|
112
|
+
cfg = startup_path.joinpath("pymud.cfg")
|
113
|
+
cfg_data = None
|
114
|
+
if os.path.exists(cfg):
|
115
|
+
with open(cfg, "r", encoding="utf8", errors="ignore") as fp:
|
116
|
+
cfg_data = json.load(fp)
|
117
|
+
|
118
|
+
app = PyMudApp(cfg_data)
|
119
|
+
app.run()
|
120
|
+
|
121
|
+
def main():
|
122
|
+
parser = argparse.ArgumentParser(prog = "pymud", description = "PyMUD命令行参数帮助")
|
123
|
+
subparsers = parser.add_subparsers(help = 'init用于初始化运行环境')
|
124
|
+
|
125
|
+
par_init = subparsers.add_parser('init', description = '初始化pymud运行环境, 包括建立脚本目录, 创建默认配置文件, 创建样例脚本等.')
|
126
|
+
par_init.add_argument('-d', '--dir', dest = 'dir', metavar = 'dir', type = str, default = '', help = '指定构建脚本目录的名称, 不指定时会根据操作系统选择不同默认值')
|
127
|
+
par_init.set_defaults(func = init_pymud_env)
|
128
|
+
|
129
|
+
parser.add_argument('-d', '--debug', dest = 'debug', action = 'store_true', default = False, help = '指定以调试模式进入PyMUD。此时,系统log等级将设置为logging.NOTSET, 所有log数据均会被记录。默认不启用。')
|
130
|
+
parser.add_argument('-l', '--logfile', dest = 'logfile', metavar = 'logfile', default = 'pymud.log', help = '指定调试模式下记录文件名,不指定时,默认为当前目录下的pymud.log')
|
131
|
+
parser.add_argument('-a', '--appendmode', dest = 'filemode', action = 'store_true', default = True, help = '指定log文件的访问模式是否为append尾部添加模式,默认为True。当为False时,使用w模式,即每次运行清空之前记录')
|
132
|
+
parser.add_argument('-s', '--startup_dir', dest = 'startup_dir', metavar = 'startup_dir', default = '.', help = '指定启动目录,默认为当前目录。使用该参数可以在任何目录下,通过指定脚本目录来启动')
|
133
|
+
|
134
|
+
args=parser.parse_args()
|
135
|
+
|
136
|
+
if hasattr(args, 'func'):
|
137
|
+
args.func(args)
|
138
|
+
else:
|
139
|
+
startApp(args)
|
140
|
+
|
141
|
+
if __name__ == "__main__":
|
142
|
+
main()
|
pymud/objects.py
CHANGED
@@ -147,7 +147,7 @@ class CodeLine:
|
|
147
147
|
return new_code_str, new_code
|
148
148
|
|
149
149
|
async def async_execute(self, session, *args, **kwargs):
|
150
|
-
await session.exec_code_async(self, *args, **kwargs)
|
150
|
+
return await session.exec_code_async(self, *args, **kwargs)
|
151
151
|
|
152
152
|
class CodeBlock:
|
153
153
|
"""
|
@@ -277,14 +277,16 @@ class CodeBlock:
|
|
277
277
|
"""
|
278
278
|
以异步方式执行该 CodeBlock。参数与 execute 相同。
|
279
279
|
"""
|
280
|
+
result = None
|
280
281
|
for code in self.codes:
|
281
282
|
if isinstance(code, CodeLine):
|
282
|
-
await code.async_execute(session, *args, **kwargs)
|
283
|
+
result = await code.async_execute(session, *args, **kwargs)
|
283
284
|
|
284
285
|
if Settings.client["interval"] > 0:
|
285
286
|
await asyncio.sleep(Settings.client["interval"] / 1000.0)
|
286
287
|
|
287
288
|
session.clean_finished_tasks()
|
289
|
+
return result
|
288
290
|
|
289
291
|
class BaseObject:
|
290
292
|
"""
|
@@ -716,6 +718,20 @@ class Command(MatchObject):
|
|
716
718
|
super().__init__(session, patterns, sync = False, *args, **kwargs)
|
717
719
|
self._tasks = set()
|
718
720
|
|
721
|
+
def __unload__(self):
|
722
|
+
"""
|
723
|
+
当从会话中移除任务时,会自动调用该函数。
|
724
|
+
可以将命令管理的各子类对象在此处清除。
|
725
|
+
该函数需要在子类中覆盖重写。
|
726
|
+
"""
|
727
|
+
pass
|
728
|
+
|
729
|
+
def unload(self):
|
730
|
+
"""
|
731
|
+
与__unload__方法相同,子类仅需覆盖一种方法就可以
|
732
|
+
"""
|
733
|
+
pass
|
734
|
+
|
719
735
|
def create_task(self, coro, *args, name = None):
|
720
736
|
"""
|
721
737
|
创建并管理任务。由 Command 创建的任务,同时也被 Session 所管理。
|
pymud/pkuxkx.py
CHANGED
@@ -17,9 +17,9 @@ class Configuration:
|
|
17
17
|
|
18
18
|
# hpbrief long情况下的含义
|
19
19
|
HP_KEYS = (
|
20
|
-
"
|
21
|
-
"
|
22
|
-
"
|
20
|
+
"combat_exp", "potential", "max_neili", "neili", "max_jingli", "jingli",
|
21
|
+
"max_qi", "eff_qi", "qi", "max_jing", "eff_jing", "jing",
|
22
|
+
"vigour/qi", "vigour/yuan", "food", "water", "fighting", "busy"
|
23
23
|
)
|
24
24
|
|
25
25
|
# 类的构造函数,传递参数session,是会话本身
|
@@ -54,7 +54,7 @@ class Configuration:
|
|
54
54
|
|
55
55
|
# 1. fullme的链接对应的触发器,匹配URL
|
56
56
|
# 当匹配成功后,调用ontri_webpage
|
57
|
-
self._triggers["tri_webpage"] = self.tri_webpage = Trigger(self.session, id = 'tri_webpage', patterns = r'^http://fullme.pkuxkx.net/robot.php.+$', group = "sys", onSuccess =
|
57
|
+
self._triggers["tri_webpage"] = self.tri_webpage = Trigger(self.session, id = 'tri_webpage', patterns = r'^http://fullme.pkuxkx.net/robot.php.+$', group = "sys", onSuccess = lambda id, line, wildcards: webbrowser.open(line))
|
58
58
|
# 2. fullme的链接对应的触发器,因为要进行多行匹配(3行),因此匹配模式pattern为3个正则表达式模式构成的元组(所有列表类型均可识别),无需像MushClient一样要指定multiline标识和linesToMatch数量
|
59
59
|
# 当匹配成功后,调用ontri_hpbrief
|
60
60
|
# 特别说明:此处的hpbrief触发匹配,需要set hpbrief long后才可以支持
|
@@ -130,9 +130,6 @@ class Configuration:
|
|
130
130
|
def onTimer(self, name, *args, **kwargs):
|
131
131
|
self.session.info("每2秒都会打印本信息", "定时器测试")
|
132
132
|
|
133
|
-
def ontri_webpage(self, name, line, wildcards):
|
134
|
-
webbrowser.open(line)
|
135
|
-
|
136
133
|
def ontri_hpbrief(self, name, line, wildcards):
|
137
134
|
self.session.setVariables(self.HP_KEYS, wildcards)
|
138
135
|
|
pymud/pymud.py
CHANGED
@@ -318,8 +318,8 @@ class PyMudApp:
|
|
318
318
|
),
|
319
319
|
|
320
320
|
MenuItem(
|
321
|
-
"", # 增加一个空名称MenuItem
|
322
|
-
|
321
|
+
"", # 增加一个空名称MenuItem,单机后焦点移动至命令行输入处,阻止右侧空白栏点击响应
|
322
|
+
handler = lambda : self.app.layout.focus(self.commandLine)
|
323
323
|
)
|
324
324
|
],
|
325
325
|
floats=[
|
@@ -340,34 +340,9 @@ class PyMudApp:
|
|
340
340
|
ss = Settings.sessions
|
341
341
|
|
342
342
|
for key, site in ss.items():
|
343
|
-
host = site["host"]
|
344
|
-
port = site["port"]
|
345
|
-
encoding = site["encoding"]
|
346
|
-
autologin = site["autologin"]
|
347
|
-
scripts = list()
|
348
|
-
default_script = site["default_script"]
|
349
|
-
|
350
|
-
def_scripts = list()
|
351
|
-
if isinstance(default_script, str):
|
352
|
-
def_scripts.extend(default_script.split(","))
|
353
|
-
elif isinstance(default_script, (list, tuple)):
|
354
|
-
def_scripts.extend(default_script)
|
355
|
-
|
356
343
|
menu = MenuItem(key)
|
357
|
-
for name
|
358
|
-
|
359
|
-
sess_scripts = list()
|
360
|
-
sess_scripts.extend(def_scripts)
|
361
|
-
|
362
|
-
if len(info) == 3:
|
363
|
-
session_script = info[2]
|
364
|
-
if session_script:
|
365
|
-
if isinstance(session_script, str):
|
366
|
-
sess_scripts.extend(session_script.split(","))
|
367
|
-
elif isinstance(session_script, (list, tuple)):
|
368
|
-
sess_scripts.extend(session_script)
|
369
|
-
|
370
|
-
sub = MenuItem(name, handler = functools.partial(self.create_session, name, host, port, encoding, after_connect, sess_scripts, info[0]))
|
344
|
+
for name in site["chars"].keys():
|
345
|
+
sub = MenuItem(name, handler = functools.partial(self._quickHandleSession, key, name))
|
371
346
|
menu.children.append(sub)
|
372
347
|
menus.append(menu)
|
373
348
|
|
@@ -506,7 +481,7 @@ class PyMudApp:
|
|
506
481
|
line = self.mudFormatProc.line_correction(b.document.current_line)
|
507
482
|
start = max(0, scol)
|
508
483
|
end = min(ecol, len(line))
|
509
|
-
line_plain = re.sub("\x1b
|
484
|
+
line_plain = re.sub(r"\x1b\[[\d;]+[abcdmz]", "", line, flags = re.IGNORECASE).replace("\r", "").replace("\x00", "")
|
510
485
|
#line_plain = re.sub("\x1b\\[[^mz]+[mz]", "", line).replace("\r", "").replace("\x00", "")
|
511
486
|
selection = line_plain[start:end]
|
512
487
|
self.app.clipboard.set_text(selection)
|
@@ -518,7 +493,7 @@ class PyMudApp:
|
|
518
493
|
lines = []
|
519
494
|
for row in range(srow, erow + 1):
|
520
495
|
line = b.document.lines[row]
|
521
|
-
line_plain = re.sub("\x1b
|
496
|
+
line_plain = re.sub(r"\x1b\[[\d;]+[abcdmz]", "", line, flags = re.IGNORECASE).replace("\r", "").replace("\x00", "")
|
522
497
|
lines.append(line_plain)
|
523
498
|
|
524
499
|
self.app.clipboard.set_text("\n".join(lines))
|
@@ -919,6 +894,50 @@ class PyMudApp:
|
|
919
894
|
self.status_message = msg
|
920
895
|
self.app.invalidate()
|
921
896
|
|
897
|
+
def _quickHandleSession(self, group, name):
|
898
|
+
'''
|
899
|
+
根据指定的组名和会话角色名,从Settings内容,创建一个会话
|
900
|
+
'''
|
901
|
+
handled = False
|
902
|
+
if name in self.sessions.keys():
|
903
|
+
self.activate_session(name)
|
904
|
+
handled = True
|
905
|
+
|
906
|
+
else:
|
907
|
+
site = Settings.sessions[group]
|
908
|
+
if name in site["chars"].keys():
|
909
|
+
host = site["host"]
|
910
|
+
port = site["port"]
|
911
|
+
encoding = site["encoding"]
|
912
|
+
autologin = site["autologin"]
|
913
|
+
default_script = site["default_script"]
|
914
|
+
|
915
|
+
def_scripts = list()
|
916
|
+
if isinstance(default_script, str):
|
917
|
+
def_scripts.extend(default_script.split(","))
|
918
|
+
elif isinstance(default_script, (list, tuple)):
|
919
|
+
def_scripts.extend(default_script)
|
920
|
+
|
921
|
+
charinfo = site["chars"][name]
|
922
|
+
|
923
|
+
after_connect = autologin.format(charinfo[0], charinfo[1])
|
924
|
+
sess_scripts = list()
|
925
|
+
sess_scripts.extend(def_scripts)
|
926
|
+
|
927
|
+
if len(charinfo) == 3:
|
928
|
+
session_script = charinfo[2]
|
929
|
+
if session_script:
|
930
|
+
if isinstance(session_script, str):
|
931
|
+
sess_scripts.extend(session_script.split(","))
|
932
|
+
elif isinstance(session_script, (list, tuple)):
|
933
|
+
sess_scripts.extend(session_script)
|
934
|
+
|
935
|
+
self.create_session(name, host, port, encoding, after_connect, sess_scripts, charinfo[0])
|
936
|
+
handled = True
|
937
|
+
|
938
|
+
return handled
|
939
|
+
|
940
|
+
|
922
941
|
def handle_session(self, *args):
|
923
942
|
'''
|
924
943
|
嵌入命令 #session 的执行函数,创建一个远程连接会话。
|
@@ -929,12 +948,18 @@ class PyMudApp:
|
|
929
948
|
- 当不指定 Encoding: 时, 默认使用utf-8编码
|
930
949
|
- 可以直接使用 #{名称} 切换会话和操作会话命令
|
931
950
|
|
951
|
+
- #session {group}.{name}
|
952
|
+
- 相当于直接点击菜单{group}下的{name}菜单来创建会话. 当该会话已存在时,切换到该会话
|
953
|
+
|
932
954
|
参数:
|
933
955
|
:name: 会话名称
|
934
956
|
:host: 服务器域名或IP地址
|
935
957
|
:port: 端口号
|
936
958
|
:encoding: 编码格式,不指定时默认为 utf8
|
937
959
|
|
960
|
+
:group: 组名, 即配置文件中, sessions 字段下的某个关键字
|
961
|
+
:name: 会话快捷名称, 上述 group 关键字下的 chars 字段中的某个关键字
|
962
|
+
|
938
963
|
示例:
|
939
964
|
``#session {名称} {宿主机} {端口} {编码}``
|
940
965
|
创建一个远程连接会话,使用指定编码格式连接到远程宿主机的指定端口并保存为 {名称} 。其中,编码可以省略,此时使用Settings.server["default_encoding"]的值,默认为utf8
|
@@ -947,6 +972,9 @@ class PyMudApp:
|
|
947
972
|
``#newstart give miui gold``
|
948
973
|
使名称为newstart的会话执行give miui gold指令,但不切换到该会话
|
949
974
|
|
975
|
+
``#session pkuxkx.newstart``
|
976
|
+
通过指定快捷配置创建会话,相当于点击 世界->pkuxkx->newstart 菜单创建会话。若该会话存在,则切换到该会话
|
977
|
+
|
950
978
|
相关命令:
|
951
979
|
- #close
|
952
980
|
- #exit
|
@@ -954,8 +982,17 @@ class PyMudApp:
|
|
954
982
|
'''
|
955
983
|
|
956
984
|
nothandle = True
|
985
|
+
errmsg = "错误的#session命令"
|
986
|
+
if len(args) == 1:
|
987
|
+
host_session = args[0]
|
988
|
+
if '.' in host_session:
|
989
|
+
group, name = host_session.split('.')
|
990
|
+
nothandle = not self._quickHandleSession(group, name)
|
957
991
|
|
958
|
-
|
992
|
+
else:
|
993
|
+
errmsg = f'通过单一参数快速创建会话时,要使用 group.name 形式,如 #session pkuxkx.newstart'
|
994
|
+
|
995
|
+
elif len(args) >= 3:
|
959
996
|
session_name = args[0]
|
960
997
|
session_host = args[1]
|
961
998
|
session_port = int(args[2])
|
@@ -968,7 +1005,7 @@ class PyMudApp:
|
|
968
1005
|
nothandle = False
|
969
1006
|
|
970
1007
|
if nothandle:
|
971
|
-
self.set_status(
|
1008
|
+
self.set_status(errmsg)
|
972
1009
|
|
973
1010
|
def enter_pressed(self, buffer: Buffer):
|
974
1011
|
"命令行回车按键处理"
|
@@ -1162,21 +1199,6 @@ class PyMudApp:
|
|
1162
1199
|
plugin.onSessionCreate(session)
|
1163
1200
|
|
1164
1201
|
|
1165
|
-
def
|
1202
|
+
def startApp(cfg_data = None):
|
1166
1203
|
app = PyMudApp(cfg_data)
|
1167
1204
|
app.run()
|
1168
|
-
|
1169
|
-
if __name__ == "__main__":
|
1170
|
-
|
1171
|
-
cfg = "pymud.cfg"
|
1172
|
-
import sys
|
1173
|
-
args = sys.argv
|
1174
|
-
if len(args) > 1:
|
1175
|
-
cfg = args[1]
|
1176
|
-
|
1177
|
-
if os.path.exists(cfg):
|
1178
|
-
with open(cfg, "r", encoding="utf8", errors="ignore") as fp:
|
1179
|
-
cfg_data = json.load(fp)
|
1180
|
-
main(cfg_data)
|
1181
|
-
else:
|
1182
|
-
main()
|