pymud 0.20.2a5__py3-none-any.whl → 0.20.3__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/modules.py CHANGED
@@ -1,188 +1,188 @@
1
-
2
- import importlib, importlib.util
3
- from abc import ABC, ABCMeta
4
- from typing import Any
5
- from .objects import BaseObject, Command
6
-
7
- class ModuleInfo:
8
- """
9
- 模块管理类。对加载的模块文件进行管理。该类型由Session类进行管理,无需人工创建和干预。
10
-
11
- 有关模块的分类和使用的详细信息,请参见 `脚本 <scripts.html>`_
12
-
13
- :param module_name: 模块的名称, 应与 import xxx 语法中的 xxx 保持一致
14
- :param session: 加载/创建本模块的会话
15
-
16
- """
17
- def __init__(self, module_name: str, session):
18
- self.session = session
19
- self._name = module_name
20
- self._ismainmodule = False
21
- self.load()
22
-
23
- def _load(self, reload = False):
24
- result = True
25
- if reload:
26
- self._module = importlib.reload(self._module)
27
- else:
28
- self._module = importlib.import_module(self.name)
29
- self._config = {}
30
- for attr_name in dir(self._module):
31
- attr = getattr(self._module, attr_name)
32
- if isinstance(attr, type) and attr.__module__ == self._module.__name__:
33
- if (attr_name == "Configuration") or issubclass(attr, IConfig):
34
- try:
35
- self._config[f"{self.name}.{attr_name}"] = attr(self.session, reload = reload)
36
- self.session.info(f"配置对象 {self.name}.{attr_name} {'重新' if reload else ''}创建成功.")
37
- except Exception as e:
38
- result = False
39
- self.session.error(f"配置对象 {self.name}.{attr_name} 创建失败. 错误信息为: {e}")
40
- self._ismainmodule = (self._config != {})
41
- return result
42
-
43
- def _unload(self):
44
- for key, config in self._config.items():
45
- if isinstance(config, Command):
46
- # Command 对象在从会话中移除时,自动调用其 unload 系列方法,因此不能产生递归
47
- self.session.delObject(config)
48
-
49
- else:
50
-
51
- if hasattr(config, "__unload__"):
52
- unload = getattr(config, "__unload__", None)
53
- if callable(unload): unload()
54
-
55
- if hasattr(config, "unload"):
56
- unload = getattr(config, "unload", None)
57
- if callable(unload): unload()
58
-
59
- if isinstance(config, BaseObject):
60
- self.session.delObject(config)
61
-
62
- del config
63
- self._config.clear()
64
-
65
- def load(self):
66
- "加载模块内容"
67
- if self._load():
68
- self.session.info(f"{'主' if self.ismainmodule else '从'}配置模块 {self.name} 加载完成.")
69
- else:
70
- self.session.error(f"{'主' if self.ismainmodule else '从'}配置模块 {self.name} 加载失败.")
71
-
72
- def unload(self):
73
- "卸载模块内容"
74
- self._unload()
75
- self._loaded = False
76
- self.session.info(f"{'主' if self.ismainmodule else '从'}配置模块 {self.name} 卸载完成.")
77
-
78
- def reload(self):
79
- "模块文件更新后调用,重新加载已加载的模块内容"
80
- self._unload()
81
- self._load(reload = True)
82
- self.session.info(f"{'主' if self.ismainmodule else '从'}配置模块 {self.name} 重新加载完成.")
83
-
84
- @property
85
- def name(self):
86
- "只读属性,模块名称"
87
- return self._name
88
-
89
- @property
90
- def module(self):
91
- "只读属性,模块文件的 ModuleType 对象"
92
- return self._module
93
-
94
- @property
95
- def config(self):
96
- "只读字典属性,根据模块文件 ModuleType 对象创建的其中名为 Configuration 的类型或继承自 IConfig 的子类型实例(若有)"
97
- return self._config
98
-
99
- @property
100
- def ismainmodule(self):
101
- "只读属性,区分是否主模块(即包含具体config的模块)"
102
- return self._ismainmodule
103
-
104
- class IConfig(metaclass = ABCMeta):
105
- """
106
- 用于提示PyMUD应用是否自动创建该配置类型的基础类(模拟接口)。
107
-
108
- 继承 IConfig 类型让应用自动管理该类型,唯一需要的是,构造函数中,仅存在一个必须指定的参数 Session。
109
-
110
- 在应用自动创建 IConfig 实例时,除 session 参数外,还会传递一个 reload 参数 (bool类型),表示是首次加载还是重新加载特性。
111
- 可以从kwargs 中获取该参数,并针对性的设计相应代码。例如,重新加载相关联的其他模块等。
112
- """
113
- def __init__(self, session, *args, **kwargs):
114
- self.session = session
115
-
116
- def __unload__(self):
117
- if self.session:
118
- self.session.delObject(self)
119
-
120
- class Plugin:
121
- """
122
- 插件管理类。对加载的插件文件进行管理。该类型由PyMudApp进行管理,无需人工创建。
123
-
124
- 有关插件的详细信息,请参见 `插件 <plugins.html>`_
125
-
126
- :param name: 插件的文件名, 如'myplugin.py'
127
- :param location: 插件所在的目录。自动加载的插件包括PyMUD包目录下的plugins目录以及当前目录下的plugins目录
128
-
129
- """
130
- def __init__(self, name, location):
131
- self._plugin_file = name
132
- self._plugin_loc = location
133
-
134
- self.reload()
135
-
136
- def reload(self):
137
- "加载/重新加载插件对象"
138
- #del self.modspec, self.mod
139
- self.modspec = importlib.util.spec_from_file_location(self._plugin_file[:-3], self._plugin_loc)
140
- self.mod = importlib.util.module_from_spec(self.modspec)
141
- self.modspec.loader.exec_module(self.mod)
142
-
143
- self._app_init = self.mod.__dict__["PLUGIN_PYMUD_START"]
144
- self._session_create = self.mod.__dict__["PLUGIN_SESSION_CREATE"]
145
- self._session_destroy = self.mod.__dict__["PLUGIN_SESSION_DESTROY"]
146
-
147
- @property
148
- def name(self):
149
- "插件名称,由插件文件中的 PLUGIN_NAME 常量定义"
150
- return self.mod.__dict__["PLUGIN_NAME"]
151
-
152
- @property
153
- def desc(self):
154
- "插件描述,由插件文件中的 PLUGIN_DESC 常量定义"
155
- return self.mod.__dict__["PLUGIN_DESC"]
156
-
157
- @property
158
- def help(self):
159
- "插件帮助,由插件文件中的文档字符串定义"
160
- return self.mod.__doc__
161
-
162
- def onAppInit(self, app):
163
- """
164
- PyMUD应用启动时对插件执行的操作,由插件文件中的 PLUGIN_PYMUD_START 函数定义
165
-
166
- :param app: 启动的 PyMudApp 对象实例
167
- """
168
- self._app_init(app)
169
-
170
- def onSessionCreate(self, session):
171
- """
172
- 新会话创建时对插件执行的操作,由插件文件中的 PLUGIN_SESSION_CREATE 函数定义
173
-
174
- :param session: 新创建的会话对象实例
175
- """
176
- self._session_create(session)
177
-
178
- def onSessionDestroy(self, session):
179
- """
180
- 会话关闭时(注意不是断开)对插件执行的操作,由插件文件中的 PLUGIN_SESSION_DESTROY 函数定义
181
-
182
- :param session: 所关闭的会话对象实例
183
- """
184
- self._session_destroy(session)
185
-
186
- def __getattr__(self, __name: str) -> Any:
187
- if hasattr(self.mod, __name):
1
+
2
+ import importlib, importlib.util
3
+ from abc import ABC, ABCMeta
4
+ from typing import Any
5
+ from .objects import BaseObject, Command
6
+
7
+ class ModuleInfo:
8
+ """
9
+ 模块管理类。对加载的模块文件进行管理。该类型由Session类进行管理,无需人工创建和干预。
10
+
11
+ 有关模块的分类和使用的详细信息,请参见 `脚本 <scripts.html>`_
12
+
13
+ :param module_name: 模块的名称, 应与 import xxx 语法中的 xxx 保持一致
14
+ :param session: 加载/创建本模块的会话
15
+
16
+ """
17
+ def __init__(self, module_name: str, session):
18
+ self.session = session
19
+ self._name = module_name
20
+ self._ismainmodule = False
21
+ self.load()
22
+
23
+ def _load(self, reload = False):
24
+ result = True
25
+ if reload:
26
+ self._module = importlib.reload(self._module)
27
+ else:
28
+ self._module = importlib.import_module(self.name)
29
+ self._config = {}
30
+ for attr_name in dir(self._module):
31
+ attr = getattr(self._module, attr_name)
32
+ if isinstance(attr, type) and attr.__module__ == self._module.__name__:
33
+ if (attr_name == "Configuration") or issubclass(attr, IConfig):
34
+ try:
35
+ self._config[f"{self.name}.{attr_name}"] = attr(self.session, reload = reload)
36
+ self.session.info(f"配置对象 {self.name}.{attr_name} {'重新' if reload else ''}创建成功.")
37
+ except Exception as e:
38
+ result = False
39
+ self.session.error(f"配置对象 {self.name}.{attr_name} 创建失败. 错误信息为: {e}")
40
+ self._ismainmodule = (self._config != {})
41
+ return result
42
+
43
+ def _unload(self):
44
+ for key, config in self._config.items():
45
+ if isinstance(config, Command):
46
+ # Command 对象在从会话中移除时,自动调用其 unload 系列方法,因此不能产生递归
47
+ self.session.delObject(config)
48
+
49
+ else:
50
+
51
+ if hasattr(config, "__unload__"):
52
+ unload = getattr(config, "__unload__", None)
53
+ if callable(unload): unload()
54
+
55
+ if hasattr(config, "unload"):
56
+ unload = getattr(config, "unload", None)
57
+ if callable(unload): unload()
58
+
59
+ if isinstance(config, BaseObject):
60
+ self.session.delObject(config)
61
+
62
+ del config
63
+ self._config.clear()
64
+
65
+ def load(self):
66
+ "加载模块内容"
67
+ if self._load():
68
+ self.session.info(f"{'主' if self.ismainmodule else '从'}配置模块 {self.name} 加载完成.")
69
+ else:
70
+ self.session.error(f"{'主' if self.ismainmodule else '从'}配置模块 {self.name} 加载失败.")
71
+
72
+ def unload(self):
73
+ "卸载模块内容"
74
+ self._unload()
75
+ self._loaded = False
76
+ self.session.info(f"{'主' if self.ismainmodule else '从'}配置模块 {self.name} 卸载完成.")
77
+
78
+ def reload(self):
79
+ "模块文件更新后调用,重新加载已加载的模块内容"
80
+ self._unload()
81
+ self._load(reload = True)
82
+ self.session.info(f"{'主' if self.ismainmodule else '从'}配置模块 {self.name} 重新加载完成.")
83
+
84
+ @property
85
+ def name(self):
86
+ "只读属性,模块名称"
87
+ return self._name
88
+
89
+ @property
90
+ def module(self):
91
+ "只读属性,模块文件的 ModuleType 对象"
92
+ return self._module
93
+
94
+ @property
95
+ def config(self):
96
+ "只读字典属性,根据模块文件 ModuleType 对象创建的其中名为 Configuration 的类型或继承自 IConfig 的子类型实例(若有)"
97
+ return self._config
98
+
99
+ @property
100
+ def ismainmodule(self):
101
+ "只读属性,区分是否主模块(即包含具体config的模块)"
102
+ return self._ismainmodule
103
+
104
+ class IConfig(metaclass = ABCMeta):
105
+ """
106
+ 用于提示PyMUD应用是否自动创建该配置类型的基础类(模拟接口)。
107
+
108
+ 继承 IConfig 类型让应用自动管理该类型,唯一需要的是,构造函数中,仅存在一个必须指定的参数 Session。
109
+
110
+ 在应用自动创建 IConfig 实例时,除 session 参数外,还会传递一个 reload 参数 (bool类型),表示是首次加载还是重新加载特性。
111
+ 可以从kwargs 中获取该参数,并针对性的设计相应代码。例如,重新加载相关联的其他模块等。
112
+ """
113
+ def __init__(self, session, *args, **kwargs):
114
+ self.session = session
115
+
116
+ def __unload__(self):
117
+ if self.session:
118
+ self.session.delObject(self)
119
+
120
+ class Plugin:
121
+ """
122
+ 插件管理类。对加载的插件文件进行管理。该类型由PyMudApp进行管理,无需人工创建。
123
+
124
+ 有关插件的详细信息,请参见 `插件 <plugins.html>`_
125
+
126
+ :param name: 插件的文件名, 如'myplugin.py'
127
+ :param location: 插件所在的目录。自动加载的插件包括PyMUD包目录下的plugins目录以及当前目录下的plugins目录
128
+
129
+ """
130
+ def __init__(self, name, location):
131
+ self._plugin_file = name
132
+ self._plugin_loc = location
133
+
134
+ self.reload()
135
+
136
+ def reload(self):
137
+ "加载/重新加载插件对象"
138
+ #del self.modspec, self.mod
139
+ self.modspec = importlib.util.spec_from_file_location(self._plugin_file[:-3], self._plugin_loc)
140
+ self.mod = importlib.util.module_from_spec(self.modspec)
141
+ self.modspec.loader.exec_module(self.mod)
142
+
143
+ self._app_init = self.mod.__dict__["PLUGIN_PYMUD_START"]
144
+ self._session_create = self.mod.__dict__["PLUGIN_SESSION_CREATE"]
145
+ self._session_destroy = self.mod.__dict__["PLUGIN_SESSION_DESTROY"]
146
+
147
+ @property
148
+ def name(self):
149
+ "插件名称,由插件文件中的 PLUGIN_NAME 常量定义"
150
+ return self.mod.__dict__["PLUGIN_NAME"]
151
+
152
+ @property
153
+ def desc(self):
154
+ "插件描述,由插件文件中的 PLUGIN_DESC 常量定义"
155
+ return self.mod.__dict__["PLUGIN_DESC"]
156
+
157
+ @property
158
+ def help(self):
159
+ "插件帮助,由插件文件中的文档字符串定义"
160
+ return self.mod.__doc__
161
+
162
+ def onAppInit(self, app):
163
+ """
164
+ PyMUD应用启动时对插件执行的操作,由插件文件中的 PLUGIN_PYMUD_START 函数定义
165
+
166
+ :param app: 启动的 PyMudApp 对象实例
167
+ """
168
+ self._app_init(app)
169
+
170
+ def onSessionCreate(self, session):
171
+ """
172
+ 新会话创建时对插件执行的操作,由插件文件中的 PLUGIN_SESSION_CREATE 函数定义
173
+
174
+ :param session: 新创建的会话对象实例
175
+ """
176
+ self._session_create(session)
177
+
178
+ def onSessionDestroy(self, session):
179
+ """
180
+ 会话关闭时(注意不是断开)对插件执行的操作,由插件文件中的 PLUGIN_SESSION_DESTROY 函数定义
181
+
182
+ :param session: 所关闭的会话对象实例
183
+ """
184
+ self._session_destroy(session)
185
+
186
+ def __getattr__(self, __name: str) -> Any:
187
+ if hasattr(self.mod, __name):
188
188
  return self.mod.__getattribute__(__name)