]> sipb.mit.edu Git - ikiwiki.git/blob - plugins/proxy.py
Refactor remote procedure calls in the proxy
[ikiwiki.git] / plugins / proxy.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # proxy.py — helper for Python-based external (xml-rpc) ikiwiki plugins
5 #
6 # Copyright © martin f. krafft <madduck@madduck.net>
7 # Released under the terms of the GNU GPL version 2
8 #
9 __name__ = 'proxy.py'
10 __description__ = 'helper for Python-based external (xml-rpc) ikiwiki plugins'
11 __version__ = '0.1'
12 __author__ = 'martin f. krafft <madduck@madduck.net>'
13 __copyright__ = 'Copyright © ' + __author__
14 __licence__ = 'GPLv2'
15
16 import sys
17 import time
18 import xmlrpclib
19 import xml.parsers.expat
20 from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
21
22 class _IkiWikiExtPluginXMLRPCDispatcher(SimpleXMLRPCDispatcher):
23
24     def __init__(self, allow_none=False, encoding=None):
25         try:
26             SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
27         except TypeError:
28             # see http://bugs.debian.org/470645
29             # python2.4 and before only took one argument
30             SimpleXMLRPCDispatcher.__init__(self)
31
32     def dispatch(self, method, params):
33         return self._dispatch(method, params)
34
35 class _XMLStreamParser(object):
36
37     def __init__(self):
38         self._parser = xml.parsers.expat.ParserCreate()
39         self._parser.StartElementHandler = self._push_tag
40         self._parser.EndElementHandler = self._pop_tag
41         self._parser.XmlDeclHandler = self._check_pipelining
42         self._reset()
43
44     def _reset(self):
45         self._stack = list()
46         self._acc = r''
47         self._first_tag_received = False
48
49     def _push_tag(self, tag, attrs):
50         self._stack.append(tag)
51         self._first_tag_received = True
52
53     def _pop_tag(self, tag):
54         top = self._stack.pop()
55         if top != tag:
56             raise ParseError, 'expected %s closing tag, got %s' % (top, tag)
57
58     def _request_complete(self):
59         return self._first_tag_received and len(self._stack) == 0
60
61     def _check_pipelining(self, *args):
62         if self._first_tag_received:
63             raise PipeliningDetected, 'need a new line between XML documents'
64
65     def parse(self, data):
66         self._parser.Parse(data, False)
67         self._acc += data
68         if self._request_complete():
69             ret = self._acc
70             self._reset()
71             return ret
72
73     class ParseError(Exception):
74         pass
75
76     class PipeliningDetected(Exception):
77         pass
78
79 class _IkiWikiExtPluginXMLRPCHandler(object):
80
81     def __init__(self, debug_fn):
82         self._dispatcher = _IkiWikiExtPluginXMLRPCDispatcher()
83         self.register_function = self._dispatcher.register_function
84         self._debug_fn = debug_fn
85
86     def register_function(self, function, name=None):
87         # will be overwritten by __init__
88         pass
89
90     @staticmethod
91     def _write(out_fd, data):
92         out_fd.write(str(data))
93         out_fd.flush()
94
95     @staticmethod
96     def _read(in_fd):
97         ret = None
98         parser = _XMLStreamParser()
99         while True:
100             line = in_fd.readline()
101             if len(line) == 0:
102                 # ikiwiki exited, EOF received
103                 return None
104
105             ret = parser.parse(line)
106             # unless this returns non-None, we need to loop again
107             if ret is not None:
108                 return ret
109
110     def send_rpc(self, cmd, in_fd, out_fd, *args, **kwargs):
111         xml = xmlrpclib.dumps(sum(kwargs.iteritems(), args), cmd)
112         self._debug_fn("calling ikiwiki procedure `%s': [%s]" % (cmd, xml))
113         _IkiWikiExtPluginXMLRPCHandler._write(out_fd, xml)
114
115         self._debug_fn('reading response from ikiwiki...')
116
117         xml = _IkiWikiExtPluginXMLRPCHandler._read(in_fd)
118         self._debug_fn('read response to procedure %s from ikiwiki: [%s]' % (cmd, xml))
119         if xml is None:
120             # ikiwiki is going down
121             return None
122
123         data = xmlrpclib.loads(xml)[0][0]
124         self._debug_fn('parsed data from response to procedure %s: [%s]' % (cmd, data))
125         return data
126
127     def handle_rpc(self, in_fd, out_fd):
128         self._debug_fn('waiting for procedure calls from ikiwiki...')
129         xml = _IkiWikiExtPluginXMLRPCHandler._read(in_fd)
130         if xml is None:
131             # ikiwiki is going down
132             self._debug_fn('ikiwiki is going down, and so are we...')
133             return
134
135         self._debug_fn('received procedure call from ikiwiki: [%s]' % xml)
136         params, method = xmlrpclib.loads(xml)
137         ret = self._dispatcher.dispatch(method, params)
138         xml = xmlrpclib.dumps((ret,), methodresponse=True)
139         self._debug_fn('sending procedure response to ikiwiki: [%s]' % xml)
140         _IkiWikiExtPluginXMLRPCHandler._write(out_fd, xml)
141         return ret
142
143 class IkiWikiProcedureProxy(object):
144
145     # how to communicate None to ikiwiki
146     _IKIWIKI_NIL_SENTINEL = {'null':''}
147
148     # sleep during each iteration
149     _LOOP_DELAY = 0.1
150
151     def __init__(self, id, in_fd=sys.stdin, out_fd=sys.stdout, debug_fn=None):
152         self._id = id
153         self._in_fd = in_fd
154         self._out_fd = out_fd
155         self._hooks = list()
156         self._functions = list()
157         self._imported = False
158         if debug_fn is not None:
159             self._debug_fn = debug_fn
160         else:
161             self._debug_fn = lambda s: None
162         self._xmlrpc_handler = _IkiWikiExtPluginXMLRPCHandler(self._debug_fn)
163         self._xmlrpc_handler.register_function(self._importme, name='import')
164
165     def rpc(self, cmd, *args, **kwargs):
166         def subst_none(seq):
167             for i in seq:
168                 if i is None:
169                     yield IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL
170                 else:
171                     yield i
172
173         args = list(subst_none(args))
174         kwargs = dict(zip(kwargs.keys(), list(subst_none(kwargs.itervalues()))))
175         ret = self._xmlrpc_handler.send_rpc(cmd, self._in_fd, self._out_fd,
176                                             *args, **kwargs)
177         if ret == IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL:
178             ret = None
179         return ret
180
181     def hook(self, type, function, name=None, last=False):
182         if self._imported:
183             raise IkiWikiProcedureProxy.AlreadyImported
184
185         if name is None:
186             name = function.__name__
187
188         def hook_proxy(*args):
189 #            curpage = args[0]
190 #            kwargs = dict([args[i:i+2] for i in xrange(1, len(args), 2)])
191             ret = function(self, *args)
192             self._debug_fn("%s hook `%s' returned: [%s]" % (type, name, ret))
193             if ret == IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL:
194                 raise IkiWikiProcedureProxy.InvalidReturnValue, \
195                         'hook functions are not allowed to return %s' \
196                         % IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL
197             if ret is None:
198                 ret = IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL
199             return ret
200
201         self._hooks.append((type, name, last))
202         self._xmlrpc_handler.register_function(hook_proxy, name=name)
203
204     def inject(self, rname, function, name=None, memoize=True):
205         if self._imported:
206             raise IkiWikiProcedureProxy.AlreadyImported
207
208         if name is None:
209             name = function.__name__
210
211         self._functions.append((rname, name, memoize))
212         self._xmlrpc_handler.register_function(function, name=name)
213
214     def getargv(self):
215         return self.rpc('getargv')
216
217     def setargv(self, argv):
218         return self.rpc('setargv', argv)
219
220     def getvar(self, hash, key):
221         return self.rpc('getvar', hash, key)
222
223     def setvar(self, hash, key, value):
224         return self.rpc('setvar', hash, key, value)
225
226     def getstate(self, page, id, key):
227         return self.rpc('getstate', page, id, key)
228
229     def setstate(self, page, id, key, value):
230         return self.rpc('setstate', page, id, key, value)
231
232     def pagespec_match(self, spec):
233         return self.rpc('pagespec_match', spec)
234
235     def error(self, msg):
236         try:
237             self.rpc('error', msg)
238         except IOError, e:
239             if e.errno != 32:
240                 raise
241
242         import posix
243         sys.exit(posix.EX_SOFTWARE)
244
245     def run(self):
246         try:
247             while True:
248                 ret = self._xmlrpc_handler.handle_rpc(self._in_fd, self._out_fd)
249                 if ret is None:
250                     return
251                 time.sleep(IkiWikiProcedureProxy._LOOP_DELAY)
252         except Exception, e:
253             import traceback
254             self.error('uncaught exception: %s\n%s' \
255                        % (e, traceback.format_exc(sys.exc_info()[2])))
256             import posix
257             sys.exit(posix.EX_SOFTWARE)
258
259     def _importme(self):
260         self._debug_fn('importing...')
261         for type, function, last in self._hooks:
262             self._debug_fn('hooking %s into %s chain...' % (function, type))
263             self.rpc('hook', id=self._id, type=type, call=function, last=last)
264         for rname, function, memoize in self._functions:
265             self._debug_fn('injecting %s as %s...' % (function, rname))
266             self.rpc('inject', name=rname, call=function, memoize=memoize)
267         self._imported = True
268         return IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL
269
270     class InvalidReturnValue(Exception):
271         pass
272
273     class AlreadyImported(Exception):
274         pass