iro/view/xmlrpc.py
author Sandro Knauß <knauss@netzguerilla.net>
Tue, 28 Feb 2012 01:47:19 +0100
branchdevel
changeset 200 01c34f22be42
parent 127 79966b937274
child 265 f6c906b40cce
permissions -rw-r--r--
deleting stop function from iro.controller.viewinterface

# -*- coding: utf-8 -*-
#Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>

#This program is free software; you can redistribute it and/or modify it under the terms
#of the GNU General Public License as published by the Free Software Foundation;
#either version 3 of the License, or any later version.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#See the GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, see <http://www.gnu.org/licenses/>.
from twisted.web import soap, xmlrpc

from ..controller.viewinterface import Interface

from ..error import InterfaceException, ValidateException

class TwistedInterface(Interface):
    def __init__(self):
        Interface.__init__(self)

    def listMethods(self):
        """Since we override lookupProcedure, its suggested to override
        listProcedures too.
        """
        return self.listProcedures()
  

    def listProcedures(self):
        """Since we override lookupProcedure, its suggested to override
        listProcedures too.
        """
        return ['listMethods','status','sms','fax','mail','routes','defaultRoute','bill','telnumber','email']


class XMLRPCInterface(TwistedInterface,xmlrpc.XMLRPC): 
    def __init__(self):
        xmlrpc.XMLRPC.__init__(self) 
        TwistedInterface.__init__(self)
    
    def lookupProcedure(self, procedurePath):
        if procedurePath not in self.listProcedures():
            raise xmlrpc.NoSuchFunction(self.NOT_FOUND,
                        "procedure %s not found" % procedurePath)
        try:
            return getattr(self,procedurePath)
        except KeyError:
            raise xmlrpc.NoSuchFunction(self.NOT_FOUND,
                        "procedure %s not found" % procedurePath)

    def _ebRender(self, failure):
        if isinstance(failure.value, InterfaceException):
            return xmlrpc.Fault(failure.value.code, failure.value.msg)
        if isinstance(failure.value, ValidateException):
            return xmlrpc.Fault(failure.value.code, failure.value.msg)
        return xmlrpc.XMLRPC._ebRender(self, failure)


class SOAPInterface(TwistedInterface,soap.SOAPPublisher): 
    def __init__(self):
        soap.SOAPPublisher.__init__(self) 
        TwistedInterface.__init__(self)
        
    def lookupFunction(self, functionName):
        """Lookup published SOAP function.

        Override in subclasses. Default behaviour - publish methods
        starting with soap_, if they have true attribute useKeywords
        they are expected to accept keywords.
        
        @return: tuple (callable, useKeywords), or (None, None) if not found.
        """
        if functionName in self.listProcedures():
            function = getattr(self, functionName, None)
            if function:
                return function, getattr(function, "useKeywords", False)
            return None
        else:
            return None

def appendResource(root):
    root.putChild('RPC2', XMLRPCInterface())
    root.putChild('SOAP', SOAPInterface())
    return root

if __name__ == '__main__':
    from twisted.web import resource, server
    from twisted.internet import reactor
    
    root = resource.Resource()
    root = appendResource(root)
    reactor.listenTCP(7080, server.Site(root))
    reactor.run()