aboutsummaryrefslogtreecommitdiff
path: root/rpkid
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2008-06-21 19:35:54 +0000
committerRob Austein <sra@hactrn.net>2008-06-21 19:35:54 +0000
commit14cb05e6a8ad35619e87c0566e5a04aadb6853b9 (patch)
treeeedc4ca43b3be09cde95e4518b97236dc6f92f1f /rpkid
parent8d9b72659cf10d81aead8a8915aca370647081a2 (diff)
Tighten up timedelta.parse() error handling
svn path=/rpkid/rpki/sundial.py; revision=1915
Diffstat (limited to 'rpkid')
-rw-r--r--rpkid/rpki/sundial.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/rpkid/rpki/sundial.py b/rpkid/rpki/sundial.py
index 009dc12e..1d7ff8bf 100644
--- a/rpkid/rpki/sundial.py
+++ b/rpkid/rpki/sundial.py
@@ -150,10 +150,13 @@ class timedelta(pydatetime.timedelta):
# Tags are intended for use with re.MatchObject.groupdict() and map
# directly to the keywords expected by the timedelta constructor.
- regexp = re.compile("\\s*(?:(?P<days>\\d+)D)?" +
- "\\s*(?:(?P<hours>\\d+)H)?" +
- "\\s*(?:(?P<minutes>\\d+)M)?" +
- "\\s*(?:(?P<seconds>\\d+)S)?\\s*", re.I)
+ regexp = re.compile("\\s*".join(("^",
+ "(?:(?P<days>\\d+)D)?",
+ "(?:(?P<hours>\\d+)H)?",
+ "(?:(?P<minutes>\\d+)M)?",
+ "(?:(?P<seconds>\\d+)S)?",
+ "$")),
+ re.I)
@classmethod
def parse(cls, arg):
@@ -163,7 +166,12 @@ class timedelta(pydatetime.timedelta):
elif arg.isdigit():
return cls(seconds = int(arg))
else:
- return cls(**dict((k, int(v)) for (k, v) in cls.regexp.match(arg).groupdict().items() if v is not None))
+ match = cls.regexp.match(arg)
+ if match:
+ return cls(**dict((k, int(v)) for (k, v) in match.groupdict().items() if v is not None))
+ else:
+ raise RuntimeError, "Couldn't parse timedelta %s" % repr(arg)
+
def convert_to_seconds(self):
"""Convert a timedelta interval to seconds."""
@@ -186,3 +194,5 @@ if __name__ == "__main__":
print "Testing time conversion routines"
test(now())
test(now() + timedelta(days = 30))
+ test(now() + timedelta.parse("3d5s"))
+ timedelta.parse(" 3d 5s ")