]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/helpers.py
Adds a date option to save, split and bup.git.PackWriter._new_commit()
[bup.git] / lib / bup / helpers.py
index f911b046151123c8c31d85c6b41d9dae7dd97e7e..bf431455921de2b0d558de67516e2f2201b77198 100644 (file)
@@ -16,6 +16,14 @@ def atoi(s):
         return 0
 
 
+def atof(s):
+    """Convert the string 's' to a float. Return 0 if s is not a number."""
+    try:
+        return float(s or '0')
+    except ValueError:
+        return 0
+
+
 buglvl = atoi(os.environ.get('BUP_DEBUG', 0))
 
 
@@ -388,6 +396,16 @@ def columnate(l, prefix):
         out += prefix + ''.join(('%-*s' % (clen+2, s)) for s in row) + '\n'
     return out
 
+def parse_date_or_fatal(str, fatal):
+    """Parses the given date or calls Option.fatal().
+    For now we expect a string that contains a float."""
+    try:
+        date = atof(str)
+    except ValueError, e:
+        raise fatal('invalid date format (should be a float): %r' % e)
+    else:
+        return date
+
 
 # hashlib is only available in python 2.5 or higher, but the 'sha' module
 # produces a DeprecationWarning in python 2.6 or higher.  We want to support