|
1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
|
2 # |
|
3 # This file is part of Iro. |
|
4 # |
|
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6 # this software and associated documentation files (the "Software"), to deal in |
|
7 # the Software without restriction, including without limitation the rights to use, |
|
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
9 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
10 # subject to the following conditions: |
|
11 # |
|
12 # The above copyright notice and this permission notice shall be included in |
|
13 # all copies or substantial portions of the Software. |
|
14 # |
|
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
21 |
|
22 from functools import partial |
|
23 try: |
|
24 from collections import OrderedDict |
|
25 except ImportError: |
|
26 from ordereddict import OrderedDict |
|
27 |
|
28 from ..error import NoRoute, NoTyp, ValidateException, NoProvider |
|
29 from ..config import Option, Config |
|
30 |
|
31 class Provider(Config): |
|
32 """Base class for Providerbackends.""" |
|
33 |
|
34 testmode = False |
|
35 """- **True** -- no message to external provider should be send. |
|
36 - **False** (default) -- message are send to external provider.""" |
|
37 def __init__(self, name, typs={}, options=[]): |
|
38 """Constructor for Provider class. |
|
39 |
|
40 :param string name: Name of the Provider. |
|
41 :param dict typs: A Dictonary with typs and routes. |
|
42 :param items options: [("name",Option(...)),...] |
|
43 |
|
44 >>> p = Provider("myProvider",{"sms":["route1","route2"]}) |
|
45 """ |
|
46 |
|
47 Config.__init__(self, name, OrderedDict([ |
|
48 ("typ",Option(vProvider, long="One available provider typ.", must=True, default=name)) |
|
49 ]+options) |
|
50 ) |
|
51 self.typs=typs |
|
52 self.testmode = False |
|
53 |
|
54 def send(self, typ, route, recipient, message): |
|
55 """Main send function, that is called, for every single message. |
|
56 |
|
57 .. note:: |
|
58 This function is not used directly. Normally :func:`~iro.offer.provider.Provider.getSendFunc` return this function with typ and route bound.""" |
|
59 pass |
|
60 |
|
61 def getSendFunc(self, typ, route): |
|
62 """Returns the actually send function for a given typ and route. |
|
63 |
|
64 Normally it returns the send function with typ and route bound. |
|
65 |
|
66 :raises: :exc:`~iro.error.NoRoute`, :exc:`~iro.error.NoTyp` |
|
67 """ |
|
68 |
|
69 try: |
|
70 if route not in self.typs[typ]: |
|
71 raise NoRoute(route) |
|
72 except KeyError: |
|
73 raise NoTyp(route) |
|
74 return partial(self.send, typ, route) |
|
75 |
|
76 def getProvider(name, typ, config): |
|
77 '''creates a provider object and init this with config. |
|
78 |
|
79 :param dict config: The Configuration dict. Normally you use ``configParser.items("section")``. |
|
80 :param string typ: A valid typ |
|
81 :raises: :exc:`~iro.error.NoProvider` |
|
82 ''' |
|
83 try: |
|
84 p = providers[typ](name) |
|
85 p.load(config) |
|
86 return p |
|
87 except KeyError: |
|
88 raise NoProvider(typ) |
|
89 |
|
90 def vProvider(typ, field): |
|
91 '''validator to test the existence of the typ. |
|
92 |
|
93 :param string typ: A typ |
|
94 :param string field: A field name used for the Exception. |
|
95 :return: |
|
96 - valid -- returns typ |
|
97 - invalid -- raises :class:`~iro.error.ValidateException` |
|
98 |
|
99 :raises: :exc:`~iro.error.ValidateException` |
|
100 ''' |
|
101 if typ not in providers.keys(): |
|
102 raise ValidateException(field=field) |
|
103 return typ |
|
104 |
|
105 providers={} |
|
106 """Avalable Providers. |
|
107 - **key** -- typ of provider (see configuration typ field). |
|
108 - **value** -- class to handle specific Providertyp. |
|
109 |
|
110 To extend provider typs, just add this new typ to this dict. |
|
111 see :doc:`provider` |
|
112 |
|
113 """ |