iro/offer/provider.py
branchdevel
changeset 253 e8d56537c9cc
parent 220 602720314930
child 254 de301b8c58d9
equal deleted inserted replaced
252:d0de2ca7201a 253:e8d56537c9cc
     2 
     2 
     3 from ..error import NoRoute, NoTyp, ValidateException, NoProvider
     3 from ..error import NoRoute, NoTyp, ValidateException, NoProvider
     4 from ..config import Option, Config
     4 from ..config import Option, Config
     5 
     5 
     6 class Provider(Config):
     6 class Provider(Config):
       
     7     """Base class for Providerbackends."""
       
     8 
       
     9     testmode = False
       
    10     """- **True** -- no message to external provider should be send.
       
    11     - **False** (default) -- message are send to external provider."""
     7     def __init__(self, name, typs={}):
    12     def __init__(self, name, typs={}):
       
    13         """Constructor for Provider class.
       
    14         
       
    15         :param string name: Name of the Provider.
       
    16         :param dict typs: A Dictonary with typs and routes.
       
    17 
       
    18         >>> p = Provider("myProvider",{"sms":["route1","route2"]})
       
    19         """
       
    20 
     8         Config.__init__(self, name)
    21         Config.__init__(self, name)
     9         self.typs=typs
    22         self.typs=typs
    10         self.testmode = False
    23         self.testmode = False
    11 
    24 
    12         self.options = {
    25         self.options = {
    13                 "typ":Option(vProvider, long="One available provider typ.", must=True, default=name)
    26                 "typ":Option(vProvider, long="One available provider typ.", must=True, default=name)
    14                 }
    27                 }
       
    28         """Options dict for Options used in configuration file (see :class:`iro.config.Option`). Ordering of configuration fields are done by :attr:`order`. 
       
    29         
       
    30         Sample::
       
    31         
       
    32                {"typ":Option(vProvider, long="One available provider typ.", must=True, default="default")}
       
    33         
       
    34         A child class typically use update to add more options (see code of :class:`iro.offer.smtp.SMTP`).
       
    35         """
    15 
    36 
    16         self.order = ["typ"]
    37         self.order = ["typ"]
       
    38         """ A list for ordering the options dict (:attr:`options`). """
    17 
    39 
    18     def send(self, typ, route, recipient, message):
    40     def send(self, typ, route, recipient, message):
       
    41         """Main send function, that is called, for every single message.
       
    42 
       
    43         .. note::
       
    44             This function is not used directly. Normally :func:`~iro.offer.provider.Provider.getSendFunc` return this function with typ and route bound."""
    19         pass
    45         pass
    20 
    46 
    21     def getSendFunc(self, typ, route):
    47     def getSendFunc(self, typ, route):
       
    48         """Returns the actually send Functionfor a given typ and route.
       
    49         
       
    50         Normally it returns the send function with typ and route bound.
       
    51 
       
    52         :raises: :exc:`~iro.error.NoRoute`, :exc:`~iro.error.NoTyp`
       
    53         """
       
    54 
    22         try:
    55         try:
    23             if route not in self.typs[typ]:
    56             if route not in self.typs[typ]:
    24                 raise NoRoute(route)
    57                 raise NoRoute(route)
    25         except KeyError:
    58         except KeyError:
    26             raise NoTyp(route)
    59             raise NoTyp(route)
    27         return partial(self.send, typ, route)
    60         return partial(self.send, typ, route)
    28 
    61 
    29 def getProvider(name, typ, config):
    62 def getProvider(name, typ, config):
       
    63     '''creates a provider object and init this with config.
       
    64    
       
    65     :param dict config: The Configuration dict. Normally you use ``configParser.items("section")``.
       
    66     :param string typ: A valid typ
       
    67     :raises: :exc:`~iro.error.NoProvider`
       
    68     '''
    30     try:
    69     try:
    31         p = providers[typ](name)
    70         p = providers[typ](name)
    32         p.load(config)
    71         p.load(config)
    33         return p
    72         return p
    34     except KeyError:
    73     except KeyError:
    35         raise NoProvider(typ)
    74         raise NoProvider(typ)
    36 
    75 
    37 def vProvider(typ, field):
    76 def vProvider(typ, field):
       
    77     '''validator to test the existence of the typ.
       
    78 
       
    79     :param string typ: A typ
       
    80     :param string field: A field name used for the Exception.
       
    81     :return:
       
    82         - valid -- returns typ
       
    83         - invalid -- raises :class:`~iro.error.ValidateException`
       
    84 
       
    85     :raises: :exc:`~iro.error.ValidateException`
       
    86     '''
    38     if typ not in providers.keys():
    87     if typ not in providers.keys():
    39         raise ValidateException()
    88         raise ValidateException()
    40     return typ
    89     return typ
    41 
    90 
    42 providers={}
    91 providers={}
       
    92 """Avalable Providers.
       
    93     - **key** -- typ of provider (see configuration typ field).
       
    94     - **value** -- class to handle specific Providertyp.
       
    95 
       
    96 To extend provider typs, just add this new typ to this dict.
       
    97 see :doc:`provider`
       
    98    
       
    99 """