]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
add utility function to parse durations, for lease-expiration config
authorBrian Warner <warner@lothar.com>
Tue, 17 Mar 2009 07:01:17 +0000 (00:01 -0700)
committerBrian Warner <warner@lothar.com>
Tue, 17 Mar 2009 07:01:17 +0000 (00:01 -0700)
src/allmydata/test/test_storage.py
src/allmydata/util/time_format.py

index d2dd5724a97f786c70c6ead7c621098883e133e3..ef308a24a5855e92c901ff2644fb8d3cb225deee 100644 (file)
@@ -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)
index 98d32e491a0e4adbaec156a0da0d250655bdbfa0..0281b6d7c27c07765ddb287eb8c82012d19a740f 100644 (file)
@@ -34,3 +34,27 @@ def iso_utc_time_to_localseconds(isotime, _conversion_re=re.compile(r"(?P<year>\
         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