| author | Sandro Knauß <knauss@netzguerilla.net> |
| Mon, 13 Feb 2012 15:38:50 +0100 | |
| branch | devel |
| changeset 144 | 1cc164bbb068 |
| parent 127 | 79966b937274 |
| child 200 | 01c34f22be42 |
| permissions | -rw-r--r-- |
| 92 | 1 |
# -*- coding: utf-8 -*- |
2 |
#Copyright (C) 2009 Sandro Knauß <bugs@sandroknauss.de> |
|
3 |
||
4 |
#This program is free software; you can redistribute it and/or modify it under the terms |
|
5 |
#of the GNU General Public License as published by the Free Software Foundation; |
|
6 |
#either version 3 of the License, or any later version. |
|
7 |
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|
8 |
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
9 |
#See the GNU General Public License for more details. |
|
10 |
||
11 |
#You should have received a copy of the GNU General Public License |
|
12 |
#along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
|
112
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
13 |
from twisted.web import soap, xmlrpc |
| 92 | 14 |
|
|
93
ee10253258ad
now right relative imports
Sandro Knauß <knauss@netzguerilla.net>
parents:
92
diff
changeset
|
15 |
from ..controller.viewinterface import Interface |
| 92 | 16 |
|
|
110
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
17 |
from ..error import InterfaceException, ValidateException |
| 92 | 18 |
|
19 |
class TwistedInterface(Interface): |
|
20 |
def __init__(self): |
|
21 |
Interface.__init__(self) |
|
22 |
||
23 |
def listMethods(self): |
|
24 |
"""Since we override lookupProcedure, its suggested to override |
|
25 |
listProcedures too. |
|
26 |
""" |
|
27 |
return self.listProcedures() |
|
28 |
||
29 |
||
30 |
def listProcedures(self): |
|
31 |
"""Since we override lookupProcedure, its suggested to override |
|
32 |
listProcedures too. |
|
33 |
""" |
|
|
127
79966b937274
made bill (aka statistic) function ready
Sandro Knauß <knauss@netzguerilla.net>
parents:
126
diff
changeset
|
34 |
return ['listMethods','status','stop','sms','fax','mail','routes','defaultRoute','bill','telnumber','email'] |
| 92 | 35 |
|
36 |
||
37 |
class XMLRPCInterface(TwistedInterface,xmlrpc.XMLRPC): |
|
38 |
def __init__(self): |
|
39 |
xmlrpc.XMLRPC.__init__(self) |
|
40 |
TwistedInterface.__init__(self) |
|
41 |
||
42 |
def lookupProcedure(self, procedurePath): |
|
43 |
if procedurePath not in self.listProcedures(): |
|
44 |
raise xmlrpc.NoSuchFunction(self.NOT_FOUND, |
|
45 |
"procedure %s not found" % procedurePath) |
|
46 |
try: |
|
47 |
return getattr(self,procedurePath) |
|
48 |
except KeyError: |
|
49 |
raise xmlrpc.NoSuchFunction(self.NOT_FOUND, |
|
50 |
"procedure %s not found" % procedurePath) |
|
51 |
||
|
110
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
52 |
def _ebRender(self, failure): |
|
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
53 |
if isinstance(failure.value, InterfaceException): |
|
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
54 |
return xmlrpc.Fault(failure.value.code, failure.value.msg) |
|
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
55 |
if isinstance(failure.value, ValidateException): |
|
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
56 |
return xmlrpc.Fault(failure.value.code, failure.value.msg) |
|
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
57 |
return xmlrpc.XMLRPC._ebRender(self, failure) |
|
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
58 |
|
|
601fc908d9f1
exception handling for xmprpc
Sandro Knauß <knauss@netzguerilla.net>
parents:
93
diff
changeset
|
59 |
|
| 92 | 60 |
class SOAPInterface(TwistedInterface,soap.SOAPPublisher): |
61 |
def __init__(self): |
|
62 |
soap.SOAPPublisher.__init__(self) |
|
63 |
TwistedInterface.__init__(self) |
|
64 |
||
65 |
def lookupFunction(self, functionName): |
|
66 |
"""Lookup published SOAP function. |
|
67 |
||
68 |
Override in subclasses. Default behaviour - publish methods |
|
69 |
starting with soap_, if they have true attribute useKeywords |
|
70 |
they are expected to accept keywords. |
|
71 |
|
|
72 |
@return: tuple (callable, useKeywords), or (None, None) if not found. |
|
73 |
""" |
|
74 |
if functionName in self.listProcedures(): |
|
75 |
function = getattr(self, functionName, None) |
|
76 |
if function: |
|
77 |
return function, getattr(function, "useKeywords", False) |
|
78 |
return None |
|
79 |
else: |
|
80 |
return None |
|
81 |
||
|
112
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
82 |
def appendResource(root): |
| 92 | 83 |
root.putChild('RPC2', XMLRPCInterface()) |
84 |
root.putChild('SOAP', SOAPInterface()) |
|
85 |
return root |
|
86 |
||
|
112
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
87 |
if __name__ == '__main__': |
|
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
88 |
from twisted.web import resource, server |
|
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
89 |
from twisted.internet import reactor |
|
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
90 |
|
|
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
91 |
root = resource.Resource() |
|
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
92 |
root = appendResource(root) |
|
ea437d1e7b65
reactor is now started my iro.main
Sandro Knauß <knauss@netzguerilla.net>
parents:
110
diff
changeset
|
93 |
reactor.listenTCP(7080, server.Site(root)) |
| 92 | 94 |
reactor.run() |