-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplemod.py
More file actions
273 lines (218 loc) · 6.96 KB
/
Copy pathsimplemod.py
File metadata and controls
273 lines (218 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import logging
import logging.config
import sys
import yaml
import datetime
import host
import importlib
import os
import imp
import simplefuncs
class Config():
defaultOptions = {}
options = {}
filename = "simplemod.yml"
def implement(self):
file = open(self.filename, "a")
file.close()
self.read()
for key, value, in self.defaultOptions.items():
self.options[key] = self.options.get(key, value)
self.write()
def read(self):
file = open(self.filename, "r")
data = yaml.safe_load(file)
file.close()
if data is None: data = {}
self.options = data
def write(self):
file = open(self.filename, "w")
yaml.dump(self.options, file)
file.close()
return True
def addDefOptions(self, **kwargs):
self.defaultOptions.update(kwargs)
return True
def get(self, option, default_value=None):
keys = option.split('.')
value = self.options
for key in keys:
value = value.get(key, default_value)
if value is None:
break
return value
def set(self, option, value):
self.options[option] = value
return self.write()
class Event(object):
def __init__(self, name, method, args):
self.name = name
self.method = method
self.args = args
class Plugin(object):
def __init__(self, name, module, plugin):
self.module = module
self.name = name
self.plugin = plugin
PL_ATTR_NAME = 'name'
PL_ATTR_VERSION = 'version'
PL_ATTR_DEVELOPERS = 'developers'
PL_ATTR_REQUIRED_PLUGINS = 'required_plugins'
PL_EVENT_ENABLE = 'onEnable'
PL_EVENT_DISABLE = 'onDisable'
class SimpleMod(object):
name = "Simple Mod"
version = "0.0.2"
developer = ""
def __init__(self):
self.config = Config()
self.config.addDefOptions(
sm_logs_filename='simplemod.log',
sm_logs_dir='logs',
sm_logs_path='%Y_%m_%d.log',
sm_logs_encoding='utf-8',
sm_logs_format='%(asctime)s: %(name)s: %(levelname)s| %(message)s',
sm_plugin_dir='admin/plugins',
sm_plugins=[],
)
self.config.implement()
if not os.path.exists(self.config.get('sm_plugin_dir')):
os.makedirs(self.config.get('sm_plugin_dir'))
sys.path.append( self.config.get('sm_plugin_dir') )
logging.basicConfig(filename=self.config.get('sm_logs_filename'), level=logging.DEBUG, format=self.config.get('sm_logs_format'))
fileHandlerHis = logging.FileHandler(datetime.datetime.now().strftime("%s/%s"%(self.config.get('sm_logs_dir'), self.config.get('sm_logs_path'))), mode="a", encoding=self.config.get('sm_logs_encoding'))
fileHandlerHis.setLevel(logging.DEBUG)
formatter = logging.Formatter(self.config.get('sm_logs_format'))
fileHandlerHis.setFormatter(formatter)
logging.getLogger().addHandler(fileHandlerHis)
self.__plugins = {}
self.__events = {}
def add_plugin(self, name, module, plugin):
if name in self.__plugins: raise Exception("Plugin %s with the name has already been added" % name)
self.__plugins[name] = Plugin(name, module, plugin)
return True
def plugin(self, name):
if name not in self.__plugins: raise Exception("Plugin %s named no" % name)
return self.__plugins.get(name)
def plugins(self):
return self.__plugins.keys()
# plugin: Plugin, events: List((event, args))
def add_plugin_events(self, plugin, events):
plugin.events = {}
for event in events:
plugin.events[event[0]] = Event(event[0], getattr(plugin.plugin, event[0]), event[1])
return True
def import_plugin(self, name):
module = imp.load_source(name, '%s/%s.py'%(self.config.get('sm_plugin_dir'), name))
plugin = module.setup(self)
self.output("debug", str(plugin))
return self.add_plugin(name, module, plugin)
# plugin: Plugin
def __req_plugins(self, plugin):
req_plugins = getattr(plugin, PL_ATTR_REQUIRED_PLUGINS, [])
plugins = self.plugins()
for req_plugin in req_plugins:
if req_plugin not in plugins: raise Exception("Plugins required for operation are required: %s" % ", ".join(req_plugins))
def initialize_plugin(self, name):
plugin = self.plugin(name)
try:
self.__req_plugins(plugin.plugin)
events = simplefuncs.plugin_events(name)
self.add_plugin_events(plugin, events)
eEnable = plugin.events.get(PL_EVENT_ENABLE)
if eEnable is not None:
eEnable.method(*eEnable.args)
except Exception, e:
raise Exception("Plugin %s is not initialized: %s" % (name, e))
def disable_plugin(self, name):
plugin = self.plugin(name)
try:
eDisable = plugin.events.get(PL_EVENT_DISABLE)
if eDisable is not None:
eDisable.method(*eDisable.args)
except Exception, e:
raise Exception("Plugin %s is not shutdown: %s" % (name, e))
def remove_plugin(self, name):
plugin = self.plugin(name)
del self.__plugins[name]
return True
def reload_plugin(self, name):
self.disable_plugin(name)
if self.remove_plugin(name):
self.import_plugin(name)
self.initialize_plugin(name)
self.config.implement()
def import_plugins(self):
plugins = self.config.get('sm_plugins')
logging.info("Importing plugins (%s)" % len(plugins))
for plugin in plugins:
try:
self.import_plugin(plugin)
logging.info("Plugin %s is imported" % (plugin))
except Exception, e:
self.output("error", "Plugin %s is not imported: %s", plugin, e)
logging.exception(e)
def initialize_plugins(self):
plugins = self.plugins()
logging.info("Initializing plugins (%s)" % len(plugins))
for plugin in plugins:
try:
self.initialize_plugin(plugin)
self.output("info", "Plugin %s is initialized", plugin)
except Exception, e:
logging.error(e, exc_info=True)
def shutdown_plugins(self):
plugins = self.plugins()
logging.info("Shutdowning plugins (%s)" % len(plugins))
for plugin in plugins:
try:
if self.disable_plugin(plugin): continue
logging.info("Plugin %s is shutdown" % (plugin))
except Exception, e:
self.output("error", "Plugin %s is not shutdown: %s", plugin, e)
logging.exception(e)
def reload_plugins(self):
plugins = self.plugins()
logging.info("Reloading plugins (%s)" % len(plugins))
for plugin in plugins:
try:
if self.reload_plugin(plugin): continue
logging.info("Plugin %s is reloaded" % (plugin))
except Exception, e:
self.output("error", "Plugin %s is not reloaded: %s", plugin, e)
logging.exception(e)
def init(self):
self.output("info", "\nA %s v%s by %s has started its work", self.name, self.version, self.developer)
simplefuncs.add_event("onEnable")
simplefuncs.add_event("onDisable")
self.import_plugins()
self.config.implement()
self.initialize_plugins()
def output(self, level, text, *args):
text = text % args
getattr(logging, level)(text)
host.rcon_invoke("echo \"%s\"" % text.replace("\n", ""))
def shutdown(self):
self.output("info","\nA %s has completed its work", self.name)
def update(self):
pass
def logger(func):
def wrapper(*argv, **kwargs):
try:
return func(*argv, **kwargs)
except Exception, e:
logging.critical(e, exc_info=True)
return wrapper
simpleMod = SimpleMod()
@logger
def getInstance():
return simpleMod
@logger
def init():
simpleMod.init();
@logger
def shutdown():
simpleMod.shutdown()
@logger
def update():
simpleMod.update()