# HG changeset patch # User Sandro Knauß # Date 1330012677 -3600 # Node ID 07ee5543751b1ece1626196ed359fe54916d3fdc # Parent e6af45edff5a5adbf755c296bef313f631ea065b offer.provider now handles the options dict and loadConfig is only in Provider class diff -r e6af45edff5a -r 07ee5543751b iro/offer/provider.py --- a/iro/offer/provider.py Thu Feb 23 16:56:25 2012 +0100 +++ b/iro/offer/provider.py Thu Feb 23 16:57:57 2012 +0100 @@ -1,6 +1,8 @@ from functools import partial -from iro.error import NoRoute, NoTyp +from ..error import NoRoute, NoTyp, ValidateException, NoProvider, NeededOption +from ..config import Option + class Provider(): def __init__(self, name, config, typs={}): self.name = name @@ -8,9 +10,22 @@ self.typs=typs self.testmode = False - self.loadConfig() + self.options = { + "typ":Option(vProvider, long="One available provider typ.", must=True) + } def loadConfig(self): + c=dict(self.config) + for o in self.options: + try: + value = self.options[o].validate(c[o],o) + setattr(self, o, value) + except (KeyError): + if self.options[o].must: + raise NeededOption(self.name, o) + elif self.options[o].default is not None: + setattr(self,o,self.options[o].default) + for (n, v) in self.config: if n == "typ": self.typ = v diff -r e6af45edff5a -r 07ee5543751b iro/offer/smstrade.py --- a/iro/offer/smstrade.py Thu Feb 23 16:56:25 2012 +0100 +++ b/iro/offer/smstrade.py Thu Feb 23 16:57:57 2012 +0100 @@ -16,7 +16,7 @@ import copy from functools import partial -from ..error import UnknownOption, NeededOption +from ..config import Option from ..model.status import Status from .provider import Provider, providers @@ -82,25 +82,10 @@ def __init__(self, name, config): self.url = "https://gateway.smstrade.de" Provider.__init__(self,name,config,{"sms":["basic","economy","gold","direct"]}) - - def loadConfig(self): - """Read options from config""" - needed=["key"] - - for n in needed: - setattr(self,n,None) - - for (n, v) in self.config: - if n in needed: - setattr(self,n,v) - else: - raise UnknownOption(self.name, n) - - for n in needed: - if getattr(self,n) is None: - raise NeededOption(self.name, n) - - + self.options.update({ + "key":Option(lambda x,y:x,long="smstrade Gateway Key https://login.smstrade.de/index.php?gateway", must=True) + }) + self.loadConfig() def send(self, route, sms, recipient): """send SMS with $sms to $recipients""" diff -r e6af45edff5a -r 07ee5543751b iro/offer/smtp.py --- a/iro/offer/smtp.py Thu Feb 23 16:56:25 2012 +0100 +++ b/iro/offer/smtp.py Thu Feb 23 16:57:57 2012 +0100 @@ -13,48 +13,35 @@ import smtplib import copy +from functools import partial +from ..validate import vInteger, vEmail,vBool from ..model.status import Status +from ..config import Option from .provider import Provider, providers class SMTP(Provider): def __init__(self, name, config): Provider.__init__(self,name,config,{"mail":[None]}) - - - def loadConfig(self): - """Read options from config""" - needed=["send_from","host", "port", "user", "password"] - - for n in needed: - setattr(self,n,None) - - Provider.loadConfig(self) - self.bTLS = False - self.bSSL = False - - for (n, v) in self.config: - if n in needed: - setattr(self,n,v) - elif n == "TLS": - self.bTLS = bool(v) - elif n == "SSL": - self.bSSL = bool(v) - else: - raise UnknownOption(self.name, n) - - for n in needed: - if getattr(self,n) is None: - raise NeededOption(self.name, n) + self.options.update({ + "send_from":Option(vEmail,long="Emailaddress from which mail will be sended.",must=True), + "host":Option(lambda x,y:x, long="Hostname of MTA", must=True), + "port":Option(partial(vInteger,minv=0),long="Port of the MTA", default=25), + "user":Option(lambda x,y:x, long="username to login into MTA.",default=""), + "password":Option(lambda x,y:x, long="password to login into MTA.",default=""), + "TLS":Option(vBool,long="use TLS for connection to MTA", default=False), + "SSL":Option(vBool,long="use SSL for connection to MTA", default=False), + }) + self.loadConfig() def send(self,mail,recipient): if not self.testmode: - if self.bSSL: + if self.SSL: smtp = smtplib.SMTP_SSL(self.host,self.port) else: smtp = smtplib.SMTP(self.host,self.port) - if self.bTLS: + if self.TLS: smtp.starttls() if not self.user == "": diff -r e6af45edff5a -r 07ee5543751b tests/smstrade.py --- a/tests/smstrade.py Thu Feb 23 16:56:25 2012 +0100 +++ b/tests/smstrade.py Thu Feb 23 16:57:57 2012 +0100 @@ -2,7 +2,7 @@ from mock import patch, Mock -from iro.error import NoRoute, NoTyp, NeededOption, UnknownOption +from iro.error import NoRoute, NoTyp, NeededOption from iro.telnumber import Telnumber from iro.model.message import SMS from iro.offer import Smstrade @@ -14,6 +14,7 @@ def getProvider(self, c=None): ret={"key":"XXXXXX", + "typ":"smstrade", } if c: @@ -48,15 +49,12 @@ testStatusCode.todo = "to implement" def testNeededOption(self): - c={"key":"XXXXXXXX",} + c={"key":"XXXXXXXX","typ":"smstrade"} s=Smstrade("test",c.items()) self.assertEqual(s.key, "XXXXXXXX") self.assertRaises(NeededOption,Smstrade,"test",[]) - c = {"unknown":""} - self.assertRaises(UnknownOption,Smstrade,"test",c.items()) - def testSendFunc(self): s = self.getProvider() p = s.getSendFunc("sms","basic") diff -r e6af45edff5a -r 07ee5543751b tests/smtp.py --- a/tests/smtp.py Thu Feb 23 16:56:25 2012 +0100 +++ b/tests/smtp.py Thu Feb 23 16:57:57 2012 +0100 @@ -8,7 +8,7 @@ from mock import patch, Mock import smtplib -from iro.error import NoRoute, NoTyp, NeededOption, UnknownOption +from iro.error import NoRoute, NoTyp, NeededOption from iro.model.message import Mail from iro.offer.smtp import SMTP @@ -27,8 +27,7 @@ ret={"send_from":"send@t.de", "host":HOST, "port":PORT, - "user":"", - "password":"", + "typ":"smtp", } if c: @@ -119,6 +118,7 @@ "port":PORT, "user":"u", "password":"p", + "typ":"smtp", } s=SMTP("test",c.items()) self.assertEqual(s.send_from, "send@t.de") @@ -126,20 +126,17 @@ self.assertEqual(s.port, PORT) self.assertEqual(s.user, "u") self.assertEqual(s.password, "p") - self.assertEqual(s.bSSL,False) - self.assertEqual(s.bTLS,False) + self.assertEqual(s.SSL,False) + self.assertEqual(s.TLS,False) c.update({"TLS":True, "SSL":True}) s=SMTP("test", c.items()) - self.assertEqual(s.bSSL,True) - self.assertEqual(s.bTLS,True) + self.assertEqual(s.SSL,True) + self.assertEqual(s.TLS,True) del c["host"] self.assertRaises(NeededOption,SMTP,"test",c.items()) - c = {"unknown":""} - self.assertRaises(UnknownOption,SMTP,"test",c.items()) - def testSendFunc(self): s = self.getSMTP() self.assertEqual(s.getSendFunc("mail",None), s.send)