contrib/po: announce my meta-po integration branch
[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             self._debug_fn('ikiwiki is going down, and so are we...')
122             raise _IkiWikiExtPluginXMLRPCHandler._GoingDown
123
124         data = xmlrpclib.loads(xml)[0][0]
125         self._debug_fn('parsed data from response to procedure %s: [%s]' % (cmd, data))
126         return data
127
128     def handle_rpc(self, in_fd, out_fd):
129         self._debug_fn('waiting for procedure calls from ikiwiki...')
130         xml = _IkiWikiExtPluginXMLRPCHandler._read(in_fd)
131         if xml is None:
132             # ikiwiki is going down
133             self._debug_fn('ikiwiki is going down, and so are we...')
134             raise _IkiWikiExtPluginXMLRPCHandler._GoingDown
135
136         self._debug_fn('received procedure call from ikiwiki: [%s]' % xml)
137         params, method = xmlrpclib.loads(xml)
138         ret = self._dispatcher.dispatch(method, params)
139         xml = xmlrpclib.dumps((ret,), methodresponse=True)
140         self._debug_fn('sending procedure response to ikiwiki: [%s]' % xml)
141         _IkiWikiExtPluginXMLRPCHandler._write(out_fd, xml)
142         return ret
143
144     class _GoingDown:
145         pass
146
147 class IkiWikiProcedureProxy(object):
148
149     # how to communicate None to ikiwiki
150     _IKIWIKI_NIL_SENTINEL = {'null':''}
151
152     # sleep during each iteration
153     _LOOP_DELAY = 0.1
154
155     def __init__(self, id, in_fd=sys.stdin, out_fd=sys.stdout, debug_fn=None):
156         self._id = id
157         self._in_fd = in_fd
158         self._out_fd = out_fd
159         self._hooks = list()
160         self._functions = list()
161         self._imported = False
162         if debug_fn is not None:
163             self._debug_fn = debug_fn
164         else:
165             self._debug_fn = lambda s: None
166         self._xmlrpc_handler = _IkiWikiExtPluginXMLRPCHandler(self._debug_fn)
167         self._xmlrpc_handler.register_function(self._importme, name='import')
168
169     def rpc(self, cmd, *args, **kwargs):
170         def subst_none(seq):
171             for i in seq:
172                 if i is None:
173                     yield IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL
174                 else:
175                     yield i
176
177         args = list(subst_none(args))
178         kwargs = dict(zip(kwargs.keys(), list(subst_none(kwargs.itervalues()))))
179         ret = self._xmlrpc_handler.send_rpc(cmd, self._in_fd, self._out_fd,
180                                             *args, **kwargs)
181         if ret == IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL:
182             ret = None
183         return ret
184
185     def hook(self, type, function, name=None, id=None, last=False):
186         if self._imported:
187             raise IkiWikiProcedureProxy.AlreadyImported
188
189         if name is None:
190             name = function.__name__
191
192         if id is None:
193             id = self._id
194
195         def hook_proxy(*args):
196 #            curpage = args[0]
197 #            kwargs = dict([args[i:i+2] for i in xrange(1, len(args), 2)])
198             ret = function(self, *args)
199             self._debug_fn("%s hook `%s' returned: [%s]" % (type, name, ret))
200             if ret == IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL:
201                 raise IkiWikiProcedureProxy.InvalidReturnValue, \
202                         'hook functions are not allowed to return %s' \
203                         % IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL
204             if ret is None:
205                 ret = IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL
206             return ret
207
208         self._hooks.append((id, type, name, last))
209         self._xmlrpc_handler.register_function(hook_proxy, name=name)
210
211     def inject(self, rname, function, name=None, memoize=True):
212         if self._imported:
213             raise IkiWikiProcedureProxy.AlreadyImported
214
215         if name is None:
216             name = function.__name__
217
218         self._functions.append((rname, name, memoize))
219         self._xmlrpc_handler.register_function(function, name=name)
220
221     def getargv(self):
222         return self.rpc('getargv')
223
224     def setargv(self, argv):
225         return self.rpc('setargv', argv)
226
227     def getvar(self, hash, key):
228         return self.rpc('getvar', hash, key)
229
230     def setvar(self, hash, key, value):
231         return self.rpc('setvar', hash, key, value)
232
233     def getstate(self, page, id, key):
234         return self.rpc('getstate', page, id, key)
235
236     def setstate(self, page, id, key, value):
237         return self.rpc('setstate', page, id, key, value)
238
239     def pagespec_match(self, spec):
240         return self.rpc('pagespec_match', spec)
241
242     def error(self, msg):
243         try:
244             self.rpc('error', msg)
245         except IOError, e:
246             if e.errno != 32:
247                 raise
248         import posix
249         sys.exit(posix.EX_SOFTWARE)
250
251     def run(self):
252         try:
253             while True:
254                 ret = self._xmlrpc_handler.handle_rpc(self._in_fd, self._out_fd)
255                 time.sleep(IkiWikiProcedureProxy._LOOP_DELAY)
256         except _IkiWikiExtPluginXMLRPCHandler._GoingDown:
257             return
258
259         except Exception, e:
260             import traceback
261             self.error('uncaught exception: %s\n%s' \
262                        % (e, traceback.format_exc(sys.exc_info()[2])))
263             return
264
265     def _importme(self):
266         self._debug_fn('importing...')
267         for id, type, function, last in self._hooks:
268             self._debug_fn('hooking %s/%s into %s chain...' % (id, function, type))
269             self.rpc('hook', id=id, type=type, call=function, last=last)
270         for rname, function, memoize in self._functions:
271             self._debug_fn('injecting %s as %s...' % (function, rname))
272             self.rpc('inject', name=rname, call=function, memoize=memoize)
273         self._imported = True
274         return IkiWikiProcedureProxy._IKIWIKI_NIL_SENTINEL
275
276     class InvalidReturnValue(Exception):
277         pass
278
279     class AlreadyImported(Exception):
280         pass