forked from xuming/micolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
micolog_plugin.py
303 lines (259 loc) · 8.17 KB
/
micolog_plugin.py
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import os,logging,re
from model import OptionSet
from google.appengine.ext.webapp import template
from google.appengine.ext import zipserve
RE_FIND_GROUPS = re.compile('\(.*?\)')
class PluginIterator:
def __init__(self, plugins_path='plugins'):
self.iterating = False
self.plugins_path = plugins_path
self.list = []
self.cursor = 0
def __iter__(self):
return self
def next(self):
if not self.iterating:
self.iterating = True
self.list = os.listdir(self.plugins_path)
self.cursor = 0
if self.cursor >= len(self.list):
self.iterating = False
raise StopIteration
else:
value = self.list[self.cursor]
self.cursor += 1
if os.path.isdir(os.path.join(self.plugins_path, value)):
return value,'%s.%s.%s'%(self.plugins_path,value,value)
elif value.endswith('.py') and not value=='__init__.py':
value=value[:-3]
return value,'%s.%s'%(self.plugins_path,value)
else:
return self.next()
class Plugins:
def __init__(self,blog=None):
self.blog=blog
self.list={}
self._filter_plugins={}
self._action_plugins={}
self._urlmap={}
self._handlerlist={}
self._setupmenu=[]
pi=PluginIterator()
self.active_list=OptionSet.getValue("PluginActive",[])
for v,m in pi:
try:
#import plugins modules
mod=__import__(m,globals(),locals(),[v])
plugin=getattr(mod,v)()
#internal name
plugin.iname=v
plugin.active=v in self.active_list
plugin.blog=self.blog
self.list[v]=plugin
except:
pass
def add_urlhandler(self,plugin,application):
for regexp,handler in plugin._handlerlist.items():
try:
application._handler_map[handler.__name__] = handler
if not regexp.startswith('^'):
regexp = '^' + regexp
if not regexp.endswith('$'):
regexp += '$'
compiled = re.compile(regexp)
application._url_mapping.insert(-2,(compiled, handler))
num_groups = len(RE_FIND_GROUPS.findall(regexp))
handler_patterns = application._pattern_map.setdefault(handler, [])
handler_patterns.insert(-2,(compiled, num_groups))
except:
pass
def remove_urlhandler(self,plugin,application):
for regexp,handler in plugin._handlerlist.items():
try:
if application._handler_map.has_key(handler.__name__):
del application._handler_map[handler.__name__]
for um in application._url_mapping:
if um[1].__name__==handler.__name__:
del um
break
for pm in application._pattern_map:
if pm.__name__==handler.__name__:
del pm
break
except:
pass
def register_handlerlist(self,application):
for name,item in self.list.items():
if item.active and item._handlerlist:
self.add_urlhandler(item,application)
def reload(self):
pass
def __getitem__(self,index):
return self.list.values()[index]
def getPluginByName(self,iname):
if self.list.has_key(iname):
return self.list[iname]
else:
return None
def activate(self,iname,active):
if active:
plugin=self.getPluginByName(iname)
if plugin:
if iname not in self.active_list:
self.active_list.append(iname)
OptionSet.setValue("PluginActive",self.active_list)
plugin.active=active
#add filter
for k,v in plugin._filter.items():
if self._filter_plugins.has_key(k):
if not v in self._filter_plugins[k]:
self._filter_plugins[k].append(v)
#add action
for k,v in plugin._action.items():
if self._action_plugins.has_key(k):
if not v in self._action_plugins[k]:
self._action_plugins[k].append(v)
if self.blog.application:
self.add_urlhandler(plugin,self.blog.application)
else:
plugin=self.getPluginByName(iname)
if plugin:
if iname in self.active_list:
self.active_list.remove(iname)
OptionSet.setValue("PluginActive",self.active_list)
plugin.active=active
#remove filter
for k,v in plugin._filter.items():
if self._filter_plugins.has_key(k):
if v in self._filter_plugins[k]:
self._filter_plugins[k].remove(v)
#remove action
for k,v in plugin._action.items():
if self._action_plugins.has_key(k):
if v in self._action_plugins[k]:
self._action_plugins[k].remove(v)
if self.blog.application:
self.remove_urlhandler(plugin,self.blog.application)
self._urlmap={}
self._setupmenu=[]
def filter(self,attr,value):
rlist=[]
for item in self:
if item.active and hasattr(item,attr) and getattr(item,attr)==value:
rlist.append(item)
return rlist
def get_filter_plugins(self,name):
if not self._filter_plugins.has_key(name) :
for item in self:
if item.active and hasattr(item,"_filter") :
if item._filter.has_key(name):
if self._filter_plugins.has_key(name):
self._filter_plugins[name].append(item._filter[name])
else:
self._filter_plugins[name]=[item._filter[name]]
if self._filter_plugins.has_key(name):
return tuple(self._filter_plugins[name])
else:
return ()
def get_action_plugins(self,name):
if not self._action_plugins.has_key(name) :
for item in self:
if item.active and hasattr(item,"_action") :
if item._action.has_key(name):
if self._action_plugins.has_key(name):
self._action_plugins[name].append(item._action[name])
else:
self._action_plugins[name]=[item._action[name]]
if self._action_plugins.has_key(name):
return tuple(self._action_plugins[name])
else:
return ()
def get_urlmap_func(self,url):
if not self._urlmap:
for item in self:
if item.active:
self._urlmap.update(item._urlmap)
if self._urlmap.has_key(url):
return self._urlmap[url]
else:
return None
def get_setupmenu(self):
#Get menu list for admin setup page
if not self._setupmenu:
for item in self:
if item.active:
self._setupmenu+=item._setupmenu
return self._setupmenu
def get_handlerlist(self,url):
if not self._handlerlist:
for item in self:
if item.active:
self._handlerlist.update(item._handlerlist)
if self._handlerlist.has_key(url):
return self._handlerlist[url]
else:
return {}
def tigger_filter(self,name,content,*arg1,**arg2):
for func in self.get_filter_plugins(name):
content=func(content,*arg1,**arg2)
return content
def tigger_action(self,name,*arg1,**arg2):
for func in self.get_action_plugins(name):
func(*arg1,**arg2)
def tigger_urlmap(self,url,*arg1,**arg2):
func=self.get_urlmap_func(url)
if func:
func(*arg1,**arg2)
return True
else:
return None
class Plugin:
def __init__(self,pfile=__file__):
self.name="Unnamed"
self.author=""
self.description=""
self.uri=""
self.version=""
self.authoruri=""
self.template_vals={}
self.dir=os.path.dirname(pfile)
self._filter={}
self._action={}
self._urlmap={}
self._handlerlist={}
self._urlhandler={}
self._setupmenu=[]
def get(self,page):
return "<h3>%s</h3><p>%s</p>"%(self.name,self.description)
def render_content(self,template_file,template_vals={}):
"""
Helper method to render the appropriate template
"""
self.template_vals.update(template_vals)
path = os.path.join(self.dir, template_file)
return template.render(path, self.template_vals)
def error(self,msg=""):
return "<h3>Error:%s</h3>"%msg
def register_filter(self,name,func):
self._filter[name]=func
def register_action(self,name,func):
self._action[name]=func
def register_urlmap(self,url,func):
self._urlmap[url]=func
def register_urlhandler(self,url,handler):
self._handlerlist[url]=handler
def register_urlzip(self,name,zipfile):
zipfile=os.path.join(self.dir,zipfile)
self._handlerlist[name]=zipserve.make_zip_handler(zipfile)
def register_setupmenu(self,m_id,title,url):
#Add menu to admin setup page.
#m_id is a flag to check current page
self._setupmenu.append({'m_id':m_id,'title':title,'url':url})
class Plugin_importbase(Plugin):
def __init__(self,pfile,name,description=""):
Plugin.__init__(self,pfile)
self.is_import_plugin=True
self.import_name=name
self.import_description=description
def post(self):
pass