comparison orpg/networking/server_plugins.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children 449a8900f9ac
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 import sys
2 import os
3 from orpg.dirpath import dir_struct
4
5 class _SingletonKey(object):
6 def __new__(cls, *args, **kwargs):
7 if not hasattr(cls, '_inst'):
8 cls._inst = super(_SingletonKey, cls).__new__(cls, *args, **kwargs)
9 return cls._inst
10
11 class PluginData(object):
12 """A holder class for inactive plugins"""
13 def __init__(self, Name, File, Author="", Help=""):
14 self.File = File
15 self.Name = Name
16 self.Author = Author
17 self.Help = Help
18 self.Activated = False
19
20 class BasePluginsClass(object):
21 def __init__(self, ptype):
22 self.__ptype = ptype
23 self.__plugins = {}
24
25 def initBase(self):
26 self._startPlugins()
27
28
29 #Methods
30 def _startPlugins(self):
31 autoload = []
32 #Fill autoload from some file with the Plugin Names
33
34 plugins = []
35 for root, dirs, files in os.walk(dir_struct['plugins'] + self.__ptype):
36 if '__init__.py' in files:
37 files.remove('__init__.py')
38 if 'base_plugin.py' in files:
39 files.remove('base_plugin.py')
40
41 for pfile in files:
42 if pfile[-3:] == '.py':
43 plugins.append(PluginData('New Plugin', root + os.sep + pfile))
44
45 for plugin in plugins:
46 pdata = self._load(plugin)
47 if pdata != None:
48 if pdata.Name not in autoload:
49 self._unload(plugin)
50
51 def activatePlugin(self, pluginName):
52 if not self.__plugins.has_key(pluginName):
53 #Print some error about invalid plugin
54 return
55 pluginData = self.__plugins[pluginName]
56 #Unload it first, just incase it is already loaded
57 self._unload(pluginData)
58
59 #Now Load it
60 self._load(pluginData)
61
62 #Write to the autoload file for this plugin
63
64 self.__plugins[pluginName].Activated = True
65 self.__plugins[pluginName].start()
66
67 def deactivatePugin(self, pluginData):
68 if not self.__plugins.has_key(pluginName):
69 #Print some error about invalid plugin
70 return
71 pluginData = self.__plugins[pluginName]
72
73 self.__plugins[pluginName].stop()
74
75 #Unload it
76 self._unload(pluginData)
77
78 #Remove this plugin from the autoload file
79
80 #Private Methods
81 def _findModule(self, pluginFile):
82 s1 = pluginFile.split(os.sep)
83 s2 = s1[-1].split('.')
84 return ('plugins.' + self.__ptype + '.' + s2[0], s2[0])
85
86 def _unload(self, pluginData):
87 self.__plugins[pluginData.Name] = PluginData(pluginData.Name, pluginData.File, pluginData.Author, pluginData.Help)
88 unload = []
89 mod = self._findModule(pluginData.File)[0]
90 for key, module in sys.modules.iteritems():
91 if str(module).find(mod) > -1:
92 unload.append(key)
93
94 for plugin in unload:
95 del sys.modules[plugin]
96
97 def _load(self, pluginData):
98 modinfo = self._findModule(pluginData.File)
99 try:
100 mod = __import__(modinfo[0])
101 mod = getattr(mod,self.__ptype)
102 tmp = getattr(mod, modinfo[1])
103 self.__plugins[pluginData.Name] = tmp.Plugin()
104 print "Loaded Module " + pluginData.File
105 return self.__plugins[pluginData.Name]
106 except Exception, e:
107 print e
108 print "Unable to load module" + pluginData.File
109
110 return None
111
112 #Property Methods
113 def _getPlugins(self):
114 return self.__plugins
115
116 def _getType(self):
117 return self.__ptype
118
119
120 #Properties
121 Plugins = property(_getPlugins, None)
122 Type = property(_getType, None)
123
124 class ServerPluginsClass(BasePluginsClass):
125 def __init__(self, singletonKey):
126 if not isinstance(singletonKey, _SingletonKey):
127 raise invalid_argument(_("Use ServerPlugins to get access to singleton"))
128
129 BasePluginsClass.__init__(self, 'server')
130 self.initBase()
131
132 def preParseIncoming(self, xml_dom, data):
133 sent = True
134 errmsg = ""
135
136 for pluginName, pluginData in self.Plugins.iteritems():
137 if pluginData.Activated:
138 xml_dom, data = pluginData.preParseIncoming(xml_dom, data)
139
140 return xml_dom, data
141
142 def postParseIncoming(self, data):
143 for pluginName, pluginData in self.Plugins.iteritems():
144 if pluginData.Activated:
145 data = pluginData.postParseIncoming(data)
146
147 return data
148
149 def getPlayer(self):
150 players = []
151 for pluginName, pluginData in self.Plugins.iteritems():
152 if pluginData.Activated:
153 playerName = pluginData.addPlayer(data)
154 players.append(playerName)
155
156 return players
157
158 def setPlayer(self, playerData):
159 players = []
160 for pluginName, pluginData in self.Plugins.iteritems():
161 if pluginData.Activated:
162 playerName = pluginData.addPlayer(data)
163 players.append(playerName)
164
165 return
166
167 def preParseOutgoing(self):
168 data = []
169 for pluginName, pluginData in self.Plugins.iteritems():
170 if pluginData.Activated:
171 xml = pluginData.preParseOutgoing()
172 for msg in xml:
173 data.append(msg)
174
175 return data
176
177 def postParseOutgoing(self):
178 data = []
179 for pluginName, pluginData in self.Plugins.iteritems():
180 if pluginData.Activated:
181 xml = pluginData.postParseOutgoing()
182 for msg in xml:
183 data.append(msg)
184
185 return data
186
187 __key = _SingletonKey()
188 ServerPlugins = ServerPluginsClass(__key)