iro/newuser.py
branchdevel
changeset 80 265124610789
parent 79 17a00b99b9fd
child 81 fea4c6760ca5
equal deleted inserted replaced
79:17a00b99b9fd 80:265124610789
     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/>.
       
    13 from twisted.web import soap, xmlrpc, resource, server
       
    14 import logging
       
    15 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s')
       
    16 
       
    17 class User(object):
       
    18     '''class for a xmlrpc user
       
    19     '''
       
    20     
       
    21     def status(self, apikey, id=None, detailed=False):
       
    22         '''Gibt den aktuellen Status eines Auftrages zurück.
       
    23 
       
    24         Keywords:
       
    25         apikey[string]: Der API Key
       
    26         id[hash]: Eine Auftragsnummer
       
    27         detailed[boolean]: Details ausgeben
       
    28 
       
    29         Return:
       
    30         jobs[list]: Eine Liste der Aufträge.
       
    31         job.name[string]: Angebener Name
       
    32         job.status[string]: Status des Auftrages
       
    33 
       
    34 
       
    35         '''
       
    36         return ""
       
    37 
       
    38     def stop(self, apikey,id):
       
    39         '''Stoppt den angegeben Auftrag.
       
    40 
       
    41         Keywords:
       
    42         apikey[string]: Der API Key
       
    43         id[hash]: Eine Auftragsnummer
       
    44 
       
    45         Return:
       
    46 
       
    47         '''
       
    48         return ""
       
    49     
       
    50     def sms(self, apikey, message, recipients, route="default"):
       
    51         '''Versendet eine SMS.
       
    52 
       
    53         Keywords:
       
    54         apikey[string]: Der API Key
       
    55         message[string]: Nachricht
       
    56         recipients[list]: eine Liste von Emfänger-Nummern (gemäß ITU-T E.123)
       
    57         route[string|list]: Route über den geschickt werden soll, 
       
    58                             oder eine Liste von Routen, um Fallbacks anzugeben
       
    59 
       
    60         Return:
       
    61         id[hash]: Die ID des Auftrages
       
    62 
       
    63         '''
       
    64         return ""
       
    65    
       
    66     
       
    67     def fax(self, apikey, subject, fax, recipients, route="default"):
       
    68         '''Versendet ein FAX.
       
    69 
       
    70         Keywords:
       
    71         apikey[string]: Der API Key
       
    72         subject[string]: Der Betreff
       
    73         fax[string]: Das PDF base64 kodiert
       
    74         recipients[list]: Eine Liste von Emfänger-Nummern (gemäß ITU-T E.123)
       
    75         route[string|list]: Route über den geschickt werden soll, 
       
    76                             oder eine Liste von Routen, um Fallbacks anzugeben
       
    77 
       
    78         Return:
       
    79         id[hash]: Die ID des Auftrages
       
    80 
       
    81         '''
       
    82         return ""
       
    83 
       
    84     def mail(self, apikey, subject,  body, recipients, frm, route="default"):
       
    85         '''Versendet eine Email.
       
    86 
       
    87         Keywords:
       
    88         apikey[string]: Der API Key
       
    89         subject[string]: Der Betreff
       
    90         body[string]: Der Email Body
       
    91         recipients[list]: Eine Liste von Emailadressen
       
    92         frm[string]: Die Absender Emailadresse
       
    93         route[string|list]: Route über den geschickt werden soll, 
       
    94                             oder eine Liste von Routen, um Fallbacks anzugeben
       
    95 
       
    96         Return:
       
    97         id[hash]: Die ID des Auftrages
       
    98 
       
    99         '''
       
   100         return ""
       
   101        
       
   102     def routes(self, apikey, typ):
       
   103         '''Gibt eine Liste aller verfügbaren Provider zurück.
       
   104 
       
   105         Keywords:
       
   106         apikey[string]: Der API Key
       
   107         typ[string]: Der Typ zu dem die Providerloste ausgeben werden soll
       
   108                      Einer der Liste ["sms","fax","mail"]
       
   109 
       
   110         Return:
       
   111         providerlist[list]: Eine Liste aller möglichen Provider
       
   112 
       
   113         '''
       
   114         return ""
       
   115         
       
   116     def defaultRoute(self, apikey, typ):
       
   117         '''Gibt den Standardprovider zurück.
       
   118  
       
   119         Keywords:
       
   120         apikey[string]: Der API Key
       
   121         typ[string]: Der Typ zu dem die Providerloste ausgeben werden soll
       
   122                      Einer der Liste ["sms","fax","mail"]
       
   123 
       
   124         Return:
       
   125         provider[string]: Der Standardprovider für den angeben Typ
       
   126 
       
   127 
       
   128         '''
       
   129         return ""
       
   130 
       
   131     def statistic(self,apikey):
       
   132         '''Gibt eine Statik zurück über die versendendeten Nachrichten und des Preises.
       
   133 
       
   134         Keywords:
       
   135         apikey[string]: Der API Key
       
   136 
       
   137         Return:
       
   138         statistic[list]: Eine Liste nach Nachrichtentypen
       
   139         '''
       
   140         return ""
       
   141 
       
   142     def listMethods(self):
       
   143         """Since we override lookupProcedure, its suggested to override
       
   144         listProcedures too.
       
   145         """
       
   146         return self.listProcedures()
       
   147   
       
   148 
       
   149     def listProcedures(self):
       
   150         """Since we override lookupProcedure, its suggested to override
       
   151         listProcedures too.
       
   152         """
       
   153         return ['listMethods','status','stop','sms','fax','mail','routes','defaultRoute','statistic']
       
   154 
       
   155 
       
   156 class XMLRPCUser(User,xmlrpc.XMLRPC): 
       
   157     def __init__(self):
       
   158         xmlrpc.XMLRPC.__init__(self) 
       
   159         User.__init__(self)
       
   160         self.allowNone = True
       
   161     
       
   162     def lookupProcedure(self, procedurePath):
       
   163         logging.debug("lookupProcedure('%s')"%procedurePath)
       
   164         if procedurePath not in self.listProcedures():
       
   165             raise xmlrpc.NoSuchFunction(self.NOT_FOUND,
       
   166                         "procedure %s not found" % procedurePath)
       
   167         try:
       
   168             return getattr(self,procedurePath)
       
   169         except KeyError:
       
   170             raise xmlrpc.NoSuchFunction(self.NOT_FOUND,
       
   171                         "procedure %s not found" % procedurePath)
       
   172 
       
   173 class SOAPUser(User,soap.SOAPPublisher): 
       
   174     def __init__(self):
       
   175         soap.SOAPPublisher.__init__(self) 
       
   176         User.__init__(self)
       
   177         
       
   178     def lookupFunction(self, functionName):
       
   179         """Lookup published SOAP function.
       
   180 
       
   181         Override in subclasses. Default behaviour - publish methods
       
   182         starting with soap_, if they have true attribute useKeywords
       
   183         they are expected to accept keywords.
       
   184         
       
   185         @return: tuple (callable, useKeywords), or (None, None) if not found.
       
   186         """
       
   187         if functionName in self.listProcedures():
       
   188             function = getattr(self, functionName, None)
       
   189             if function:
       
   190                 return function, getattr(function, "useKeywords", False)
       
   191             return None
       
   192         else:
       
   193             return None
       
   194 
       
   195 
       
   196 def main():
       
   197     from twisted.internet import reactor
       
   198     root = resource.Resource()
       
   199     root.putChild('RPC2', XMLRPCUser())
       
   200     root.putChild('SOAP', SOAPUser())
       
   201     reactor.listenTCP(7080, server.Site(root))
       
   202     reactor.run()
       
   203 
       
   204 if __name__ == '__main__':
       
   205     main()