From: Brian Warner Date: Tue, 17 Mar 2009 07:01:17 +0000 (-0700) Subject: add utility function to parse durations, for lease-expiration config X-Git-Tag: allmydata-tahoe-1.4.0~48 X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/file/URI:LIT:krugkidfnzsc4/index.php?a=commitdiff_plain;h=406fdba61f03adeda7825b5cb1f59b8ddc6f533c;p=tahoe-lafs%2Ftahoe-lafs.git add utility function to parse durations, for lease-expiration config --- diff --git a/src/allmydata/test/test_storage.py b/src/allmydata/test/test_storage.py index d2dd5724..ef308a24 100644 --- a/src/allmydata/test/test_storage.py +++ b/src/allmydata/test/test_storage.py @@ -8,7 +8,7 @@ from twisted.application import service from foolscap import eventual import itertools from allmydata import interfaces -from allmydata.util import fileutil, hashutil, base32, pollmixin +from allmydata.util import fileutil, hashutil, base32, pollmixin, time_format from allmydata.storage.server import StorageServer from allmydata.storage.mutable import MutableShareFile from allmydata.storage.immutable import BucketWriter, BucketReader @@ -2144,6 +2144,21 @@ class LeaseCrawler(unittest.TestCase, pollmixin.PollMixin, WebRenderingMixin): self.failUnless("garbage-collection mode 'bogus'" " must be 'age' or 'date-cutoff'" in str(e), str(e)) + def test_parse_duration(self): + DAY = 24*60*60 + MONTH = 31*DAY + YEAR = 365*DAY + p = time_format.parse_duration + self.failUnlessEqual(p("7days"), 7*DAY) + self.failUnlessEqual(p("31day"), 31*DAY) + self.failUnlessEqual(p("60 days"), 60*DAY) + self.failUnlessEqual(p("2mo"), 2*MONTH) + self.failUnlessEqual(p("3 month"), 3*MONTH) + self.failUnlessEqual(p("2years"), 2*YEAR) + e = self.failUnlessRaises(ValueError, p, "2kumquats") + self.failUnless("no unit (like day, month, or year) in '2kumquats'" + in str(e), str(e)) + def test_limited_history(self): basedir = "storage/LeaseCrawler/limited_history" fileutil.make_dirs(basedir) diff --git a/src/allmydata/util/time_format.py b/src/allmydata/util/time_format.py index 98d32e49..0281b6d7 100644 --- a/src/allmydata/util/time_format.py +++ b/src/allmydata/util/time_format.py @@ -34,3 +34,27 @@ def iso_utc_time_to_localseconds(isotime, _conversion_re=re.compile(r"(?P\ localseconds += subsecfloat return localseconds +def parse_duration(s): + orig = s + unit = None + DAY = 24*60*60 + MONTH = 31*DAY + YEAR = 365*DAY + if s.endswith("s"): + s = s[:-1] + if s.endswith("day"): + unit = DAY + s = s[:-len("day")] + elif s.endswith("month"): + unit = MONTH + s = s[:-len("month")] + elif s.endswith("mo"): + unit = MONTH + s = s[:-len("mo")] + elif s.endswith("year"): + unit = YEAR + s = s[:-len("YEAR")] + else: + raise ValueError("no unit (like day, month, or year) in '%s'" % orig) + s = s.strip() + return int(s) * unit