add a default value for Time/Duration

This commit is contained in:
Romain Bignon 2014-03-19 20:45:39 +01:00
commit e217b24427
2 changed files with 11 additions and 1 deletions

View file

@ -260,6 +260,10 @@ class Time(Filter):
regexp = re.compile(ur'(?P<hh>\d+):?(?P<mm>\d+)(:(?P<ss>\d+))?')
kwargs = {'hour': 'hh', 'minute': 'mm', 'second': 'ss'}
def __init__(self, selector, default=_NO_DEFAULT):
super(Time, self).__init__(selector)
self.default = default
def filter(self, txt):
m = self.regexp.search(txt)
if m:
@ -268,6 +272,11 @@ class Time(Filter):
kwargs[key] = int(m.groupdict()[index] or 0)
return self.klass(**kwargs)
if self.default is not _NO_DEFAULT:
return self.default
else:
raise ValueError('Unable to find time in %r' % txt)
class Duration(Time):
klass = datetime.timedelta