pymud 0.20.4__py3-none-any.whl → 0.21.0a1__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/main.py CHANGED
@@ -1,9 +1,10 @@
1
- import os, sys, json, platform, shutil, logging, argparse
1
+ import os, sys, json, platform, shutil, logging, argparse, locale
2
2
  from pathlib import Path
3
3
  from .pymud import PyMudApp
4
4
  from .settings import Settings
5
5
 
6
6
  CFG_TEMPLATE = {
7
+ "language" : "chs", # 语言设置,默认为简体中文
7
8
  "client": {
8
9
  "buffer_lines" : 5000, # 保留缓冲行数
9
10
 
@@ -41,50 +42,114 @@ CFG_TEMPLATE = {
41
42
  }
42
43
  }
43
44
 
45
+ def detect_system_language():
46
+ """
47
+ 检测系统语言,返回中文或英文"
48
+ """
49
+ lang = "chs"
50
+ try:
51
+ value = locale.getlocale()[0]
52
+ if value and (value.lower().startswith("zh") or value.lower().startswith("chinese")): # 中文
53
+ lang = "chs"
54
+ else:
55
+ lang = "eng"
56
+ except Exception as e:
57
+ # default is chs
58
+ pass
59
+
60
+ return lang
61
+
44
62
  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)
63
+ lang = detect_system_language()
64
+ if lang == "chs":
65
+ print(f"欢迎使用PyMUD, 版本{Settings.__version__}. 使用PyMUD时, 建议建立一个新目录(任意位置),并将自己的脚本以及配置文件放到该目录下.")
66
+ print("即将开始为首次运行初始化环境...")
67
+
68
+ dir = args.dir
69
+ if dir:
70
+ print(f"你已经指定了创建脚本的目录为 {args.dir}")
71
+ dir = Path(dir)
72
+ else:
73
+ dir = Path.home().joinpath('pkuxkx')
74
+
75
+ system = platform.system().lower()
76
+ dir_enter = input(f"检测到当前系统为 {system}, 请指定游戏脚本的目录(若目录不存在会自动创建),直接回车表示使用默认值 [{dir}]:")
77
+ if dir_enter:
78
+ dir = Path(dir_enter)
79
+
80
+ if dir.exists() and dir.is_dir():
81
+ print(f'检测到给定目录 {dir} 已存在,切换至此目录...')
82
+ else:
83
+ print(f'检测到给定目录 {dir} 不存在,正在创建并切换至目录...')
84
+ dir.mkdir()
85
+
86
+ os.chdir(dir)
67
87
 
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")
88
+ if os.path.exists('pymud.cfg'):
89
+ print(f'检测到脚本目录下已存在pymud.cfg文件,将直接使用此文件进入PyMUD...')
90
+ else:
91
+ print(f'检测到脚本目录下不存在pymud.cfg文件,将使用默认内容创建该配置文件...')
92
+ with open('pymud.cfg', mode = 'x') as fp:
93
+ fp.writelines(json.dumps(CFG_TEMPLATE, indent = 4))
94
+
95
+ if not os.path.exists('examples.py'):
96
+ from pymud import pkuxkx
97
+ module_dir = pkuxkx.__file__
98
+ shutil.copyfile(module_dir, 'examples.py')
99
+ print(f'已将样例脚本拷贝至脚本目录,并加入默认配置文件')
100
+
101
+ print(f"后续可自行修改 {dir} 目录下的 pymud.cfg 文件以进行配置。")
102
+ if system == "windows":
103
+ print(f"后续运行PyMUD, 请在 {dir} 目录下键入命令: python -m pymud,或直接使用快捷命令 pymud")
104
+ else:
105
+ print(f"后续运行PyMUD, 请在 {dir} 目录下键入命令: python3 -m pymud,或直接使用快捷命令 pymud")
86
106
 
87
- input('所有内容已初始化完毕, 请按回车进入PyMUD.')
107
+ input('所有内容已初始化完毕, 请按回车进入PyMUD.')
108
+
109
+ else:
110
+ print(f"Welcome to PyMUD, version {Settings.__version__}. When using pymud, it is suggested that a new folder should be created (in any place), and the cfg configuration and all the scripts have been placed in the directory.")
111
+ print("Starting to initialize the environment for the first time...")
112
+ dir = args.dir
113
+ if dir:
114
+ print(f"You have specified the directory to create the script as {args.dir}")
115
+ dir = Path(dir)
116
+ else:
117
+ dir = Path.home().joinpath('pkuxkx')
118
+
119
+ system = platform.system().lower()
120
+ dir_enter = input(f"Detected the current system is {system}, please specify the directory of the game script (if the directory does not exist, it will be automatically created), press Enter to use the default value [{dir}]:")
121
+ if dir_enter:
122
+ dir = Path(dir_enter)
123
+
124
+ if dir.exists() and dir.is_dir():
125
+ print(f'Detected that the given directory {dir} already exists, switching to this directory...')
126
+ else:
127
+ print(f'Detected that the given directory {dir} does not exist, creating and switching to the directory...')
128
+ dir.mkdir()
129
+
130
+ os.chdir(dir)
131
+
132
+ if os.path.exists('pymud.cfg'):
133
+ print(f'Detected that the pymud.cfg file already exists in the script directory, entering PyMUD directly using this file...')
134
+ else:
135
+ print(f'Detected that the pymud.cfg file does not exist in the script directory, creating the configuration file using the default content...')
136
+ with open('pymud.cfg', mode = 'x') as fp:
137
+ CFG_TEMPLATE["language"] = "eng"
138
+ fp.writelines(json.dumps(CFG_TEMPLATE, indent = 4))
139
+
140
+ if not os.path.exists('examples.py'):
141
+ from pymud import pkuxkx
142
+ module_dir = pkuxkx.__file__
143
+ shutil.copyfile(module_dir, 'examples.py')
144
+ print(f'The sample script has been copied to the script directory and added to the default configuration file')
145
+
146
+ print(f"Afterwards, you can modify the pymud.cfg file in the {dir} directory for configuration.")
147
+ if system == "windows":
148
+ print(f"Afterwards, please type the command 'python -m pymud' in the {dir} directory to run PyMUD, or use the shortcut command pymud")
149
+ else:
150
+ print(f"Afterwards, please type the command 'python3 -m pymud' in the {dir} directory to run PyMUD, or use the shortcut command pymud")
151
+
152
+ input('Press Enter to enter PyMUD.')
88
153
 
89
154
  startApp(args)
90
155
 
pymud/modules.py CHANGED
@@ -2,8 +2,126 @@
2
2
  import importlib, importlib.util
3
3
  from abc import ABC, ABCMeta
4
4
  from typing import Any
5
- from .objects import BaseObject, Command
5
+ from .objects import BaseObject, Command, Trigger, Alias, Timer, GMCPTrigger
6
+ from .settings import Settings
7
+ from .extras import DotDict
6
8
 
9
+ class PymudDecorator:
10
+ """
11
+ 装饰器基类。使用装饰器可以快捷创建各类Pymud基础对象。
12
+
13
+ :param type: 装饰器的类型,用于区分不同的装饰器,为字符串类型。
14
+ :param args: 可变位置参数,用于传递额外的参数。
15
+ :param kwargs: 可变关键字参数,用于传递额外的键值对参数。
16
+ """
17
+ def __init__(self, type: str, *args, **kwargs):
18
+ self.type = type
19
+ self.args = args
20
+ self.kwargs = kwargs
21
+
22
+ def __call__(self, func):
23
+ decos = getattr(func, "__pymud_decorators__", [])
24
+ decos.append(self)
25
+ func.__pymud_decorators__ = decos
26
+ return func
27
+ return self
28
+
29
+ def __repr__(self):
30
+ return f"<{self.__class__.__name__} type = {self.type} args = {self.args} kwargs = {self.kwargs}>"
31
+
32
+
33
+ def alias(patterns, id = None, group = "", enabled = True, ignoreCase = False, isRegExp = True, keepEval = False, expandVar = True, priority = 100, oneShot = False, *args, **kwargs):
34
+ """
35
+ 使用装饰器创建一个别名(Alias)对象。被装饰的函数将在别名成功匹配时调用。
36
+ 被装饰的函数,除第一个参数为类实例本身self之外,另外包括id, line, wildcards三个参数。
37
+ 其中id为别名对象的唯一标识符,line为匹配的文本行,wildcards为匹配的通配符列表。
38
+
39
+ :param patterns: 别名匹配的模式。
40
+ :param id: 别名对象的唯一标识符,不指定时将自动生成唯一标识符。
41
+ :param group: 别名所属的组名,默认为空字符串。
42
+ :param enabled: 别名是否启用,默认为 True。
43
+ :param ignoreCase: 匹配时是否忽略大小写,默认为 False。
44
+ :param isRegExp: 模式是否为正则表达式,默认为 True。
45
+ :param keepEval: 若存在多个可匹配的别名时,是否持续匹配,默认为 False。
46
+ :param expandVar: 是否展开变量,默认为 True。
47
+ :param priority: 别名的优先级,值越小优先级越高,默认为 100。
48
+ :param oneShot: 别名是否只触发一次后自动失效,默认为 False。
49
+ :param args: 可变位置参数,用于传递额外的参数。
50
+ :param kwargs: 可变关键字参数,用于传递额外的键值对参数。
51
+ :return: PymudDecorator 实例,类型为 "alias"。
52
+ """
53
+ # 将传入的参数更新到 kwargs 字典中
54
+ kwargs.update({
55
+ "patterns": patterns,
56
+ "id": id,
57
+ "group": group,
58
+ "enabled": enabled,
59
+ "ignoreCase": ignoreCase,
60
+ "isRegExp": isRegExp,
61
+ "keepEval": keepEval,
62
+ "expandVar": expandVar,
63
+ "priority": priority,
64
+ "oneShot": oneShot})
65
+
66
+ # 如果 id 为 None,则从 kwargs 中移除 "id" 键
67
+ if not id:
68
+ kwargs.pop("id")
69
+
70
+ return PymudDecorator("alias", *args, **kwargs)
71
+ def trigger(patterns, id = None, group = "", enabled = True, ignoreCase = False, isRegExp = True, keepEval = False, expandVar = True, raw = False, priority = 100, oneShot = False, *args, **kwargs):
72
+ """
73
+ 使用装饰器创建一个触发器(Trigger)对象。
74
+
75
+ :param patterns: 触发器匹配的模式。单行模式可以是字符串或正则表达式,多行模式必须是元组或列表,其中每个元素都是字符串或正则表达式。
76
+ :param id: 触发器对象的唯一标识符,不指定时将自动生成唯一标识符。
77
+ :param group: 触发器所属的组名,默认为空字符串。
78
+ :param enabled: 触发器是否启用,默认为 True。
79
+ :param ignoreCase: 匹配时是否忽略大小写,默认为 False。
80
+ :param isRegExp: 模式是否为正则表达式,默认为 True。
81
+ :param keepEval: 若存在多个可匹配的触发器时,是否持续匹配,默认为 False。
82
+ :param expandVar: 是否展开变量,默认为 True。
83
+ :param raw: 是否使用原始匹配方式,默认为 False。原始匹配方式下,不对 VT100 下的 ANSI 颜色进行解码,因此可以匹配颜色;正常匹配仅匹配文本。
84
+ :param priority: 触发器的优先级,值越小优先级越高,默认为 100。
85
+ :param oneShot: 触发器是否只触发一次后自动失效,默认为 False。
86
+ :param args: 可变位置参数,用于传递额外的参数。
87
+ :param kwargs: 可变关键字参数,用于传递额外的键值对参数。
88
+ :return: PymudDecorator 实例,类型为 "trigger"。
89
+ """
90
+ # 将传入的参数更新到 kwargs 字典中
91
+ kwargs.update({
92
+ "patterns": patterns,
93
+ "id": id,
94
+ "group": group,
95
+ "enabled": enabled,
96
+ "ignoreCase": ignoreCase,
97
+ "isRegExp": isRegExp,
98
+ "keepEval": keepEval,
99
+ "expandVar": expandVar,
100
+ "raw": raw,
101
+ "priority": priority,
102
+ "oneShot": oneShot})
103
+ if not id:
104
+ kwargs.pop("id")
105
+ return PymudDecorator("trigger", *args, **kwargs)
106
+
107
+ def timer(timeout, *args, **kwargs):
108
+ kwargs.update({"timeout": timeout})
109
+ return PymudDecorator("timer", *args, **kwargs)
110
+
111
+ def gmcp(name, *args, **kwargs):
112
+ kwargs.update({"id": name})
113
+ return PymudDecorator("gmcp", *args, **kwargs)
114
+ class PymudMeta(type):
115
+ def __new__(cls, name, bases, attrs):
116
+ decorator_funcs = {}
117
+ for name, value in attrs.items():
118
+ if hasattr(value, "__pymud_decorators__"):
119
+ decorator_funcs[value.__name__] = getattr(value, "__pymud_decorators__", [])
120
+
121
+ attrs["_decorator_funcs"] = decorator_funcs
122
+
123
+ return super().__new__(cls, name, bases, attrs)
124
+
7
125
  class ModuleInfo:
8
126
  """
9
127
  模块管理类。对加载的模块文件进行管理。该类型由Session类进行管理,无需人工创建和干预。
@@ -33,10 +151,14 @@ class ModuleInfo:
33
151
  if (attr_name == "Configuration") or issubclass(attr, IConfig):
34
152
  try:
35
153
  self._config[f"{self.name}.{attr_name}"] = attr(self.session, reload = reload)
36
- self.session.info(f"配置对象 {self.name}.{attr_name} {'重新' if reload else ''}创建成功.")
154
+ if not reload:
155
+ self.session.info(Settings.gettext("configuration_created", self.name, attr_name))
156
+ else:
157
+ self.session.info(Settings.gettext("configuration_recreated", self.name, attr_name))
158
+
37
159
  except Exception as e:
38
160
  result = False
39
- self.session.error(f"配置对象 {self.name}.{attr_name} 创建失败. 错误信息为: {e}")
161
+ self.session.error(Settings.gettext("configuration_fail", self.name, attr_name, e))
40
162
  self._ismainmodule = (self._config != {})
41
163
  return result
42
164
 
@@ -65,21 +187,21 @@ class ModuleInfo:
65
187
  def load(self):
66
188
  "加载模块内容"
67
189
  if self._load():
68
- self.session.info(f"{'' if self.ismainmodule else ''}配置模块 {self.name} 加载完成.")
190
+ self.session.info(f"{Settings.gettext('entity_module' if self.ismainmodule else 'non_entity_module')} {self.name} {Settings.gettext('load_ok')}")
69
191
  else:
70
- self.session.error(f"{'' if self.ismainmodule else ''}配置模块 {self.name} 加载失败.")
192
+ self.session.info(f"{Settings.gettext('entity_module' if self.ismainmodule else 'non_entity_module')} {self.name} {Settings.gettext('load_fail')}")
71
193
 
72
194
  def unload(self):
73
195
  "卸载模块内容"
74
196
  self._unload()
75
197
  self._loaded = False
76
- self.session.info(f"{'' if self.ismainmodule else ''}配置模块 {self.name} 卸载完成.")
198
+ self.session.info(f"{Settings.gettext('entity_module' if self.ismainmodule else 'non_entity_module')} {self.name} {Settings.gettext('unload_ok')}")
77
199
 
78
200
  def reload(self):
79
201
  "模块文件更新后调用,重新加载已加载的模块内容"
80
202
  self._unload()
81
203
  self._load(reload = True)
82
- self.session.info(f"{'' if self.ismainmodule else ''}配置模块 {self.name} 重新加载完成.")
204
+ self.session.info(f"{Settings.gettext('entity_module' if self.ismainmodule else 'non_entity_module')} {self.name} {Settings.gettext('reload_ok')}")
83
205
 
84
206
  @property
85
207
  def name(self):
@@ -101,7 +223,7 @@ class ModuleInfo:
101
223
  "只读属性,区分是否主模块(即包含具体config的模块)"
102
224
  return self._ismainmodule
103
225
 
104
- class IConfig(metaclass = ABCMeta):
226
+ class IConfig(metaclass = PymudMeta):
105
227
  """
106
228
  用于提示PyMUD应用是否自动创建该配置类型的基础类(模拟接口)。
107
229
 
@@ -111,11 +233,48 @@ class IConfig(metaclass = ABCMeta):
111
233
  可以从kwargs 中获取该参数,并针对性的设计相应代码。例如,重新加载相关联的其他模块等。
112
234
  """
113
235
  def __init__(self, session, *args, **kwargs):
114
- self.session = session
236
+ from .session import Session
237
+ if isinstance(session, Session):
238
+ self.session = session
239
+ self.__inline_objects__ = DotDict()
240
+
241
+ if hasattr(self, "_decorator_funcs"):
242
+ for func_name, decorators in self._decorator_funcs.items():
243
+ func = getattr(self, func_name)
244
+ for deco in decorators:
245
+ if isinstance(deco, PymudDecorator):
246
+ if deco.type == "alias":
247
+ patterns = deco.kwargs.pop("patterns")
248
+ ali = Alias(self.session, patterns, *deco.args, **deco.kwargs, onSccess = func)
249
+ self.__inline_objects__[ali.id] = ali
250
+
251
+ elif deco.type == "trigger":
252
+ patterns = deco.kwargs.pop("patterns")
253
+ tri = Trigger(self.session, patterns, *deco.args, **deco.kwargs, onSuccess = func)
254
+ self.__inline_objects__[tri.id] = tri
255
+
256
+ elif deco.type == "timer":
257
+ tim = Timer(self.session, *deco.args, **deco.kwargs, onSuccess = func)
258
+ self.__inline_objects__[tim.id] = tim
259
+
260
+ elif deco.type == "gmcp":
261
+ gmcp = GMCPTrigger(self.session, deco.kwargs.get("id"), *deco.args, **deco.kwargs, onSuccess = func)
262
+ self.__inline_objects__[gmcp.id] = gmcp
115
263
 
116
264
  def __unload__(self):
117
265
  if self.session:
118
- self.session.delObject(self)
266
+ self.session.delObjects(self.__inline_objects__)
267
+ if isinstance(self, BaseObject):
268
+ self.session.delObject(self)
269
+
270
+ @property
271
+ def objs(self) -> DotDict:
272
+ "返回内联自动创建的对象字典"
273
+ return self.__inline_objects__
274
+
275
+ def obj(self, obj_id: str) -> BaseObject:
276
+ "根据对象ID返回内联自动创建的对象"
277
+ return self.__inline_objects__.get(obj_id)
119
278
 
120
279
  class Plugin:
121
280
  """
pymud/objects.py CHANGED
@@ -37,7 +37,7 @@ class CodeLine:
37
37
  elif ch == "}":
38
38
  brace_count -= 1
39
39
  if brace_count < 0:
40
- raise Exception("错误的代码块,大括号数量不匹配")
40
+ raise Exception(Settings.gettext("excpetion_brace_not_matched"))
41
41
  arg += ch
42
42
  elif ch == "'":
43
43
  if single_quote == 0:
@@ -60,7 +60,7 @@ class CodeLine:
60
60
  arg += ch
61
61
 
62
62
  if (single_quote > 0) or (double_quote > 0):
63
- raise Exception("引号的数量不匹配")
63
+ raise Exception(Settings.gettext("exception_quote_not_matched"))
64
64
 
65
65
  if arg:
66
66
  code_params.append(arg)
@@ -178,7 +178,7 @@ class CodeBlock:
178
178
  elif ch == "}":
179
179
  brace_count -= 1
180
180
  if brace_count < 0:
181
- raise Exception("错误的代码块,大括号数量不匹配")
181
+ raise Exception(Settings.gettext("excpetion_brace_not_matched"))
182
182
  line += ch
183
183
  elif ch == ";":
184
184
  if brace_count == 0:
@@ -264,7 +264,7 @@ class CodeBlock:
264
264
  elif self.syncmode == "sync":
265
265
  sync = True
266
266
  elif self.syncmode == "conflict":
267
- session.warning("该命令中同时存在强制同步命令和强制异步命令,将使用异步执行,同步命令将失效。")
267
+ session.warning(Settings.gettext("exception_forced_async"))
268
268
  sync = False
269
269
 
270
270
  if sync:
@@ -326,7 +326,7 @@ class BaseObject:
326
326
  if isinstance(session, Session):
327
327
  self.session = session
328
328
  else:
329
- assert("session must be an instance of class Session!")
329
+ assert(Settings.gettext("exception_session_type_fail"))
330
330
 
331
331
  self._enabled = True # give a default value
332
332
  self.log = logging.getLogger(f"pymud.{self.__class__.__name__}")
@@ -625,7 +625,7 @@ class MatchObject(BaseObject):
625
625
  await self.event.wait()
626
626
  self.reset()
627
627
  except Exception as e:
628
- self.error(f"异步执行中遇到异常, {e}")
628
+ self.error(Settings.gettext("exception_in_async", e))
629
629
 
630
630
  return self.state
631
631