chiark / gitweb /
catacomb/pwsafe.py: Add a new ABRUPTP argument to `close' methods.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 24 May 2015 18:26:36 +0000 (19:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 31 May 2015 21:42:53 +0000 (22:42 +0100)
New backends might want to commit changes at close time, and an abrupt
close shouldn't do that.

catacomb/pwsafe.py

index 44382e76056b69a0dd6d9e577885331cbc0a5d86..8b68182e098b1df3231bfa03f435fb0829bea48e 100644 (file)
@@ -177,7 +177,8 @@ class StorageBackend (object):
   BE._open(FILE, WRITEP)
                         Open the existing database FILE; used by `open'.
 
-  BE._close()           Close the database, freeing up any resources.
+  BE._close(ABRUPTP)    Close the database, freeing up any resources.  If
+                        ABRUPTP then don't try to commit changes.
 
   BE._get_meta(NAME, DEFAULT)
                         Return the value of the metadata item with the given
@@ -283,7 +284,7 @@ class StorageBackend (object):
     me._writep = writep
     me._livep = True
 
-  def close(me):
+  def close(me, abruptp = False):
     """
     Close the database.
 
@@ -292,7 +293,7 @@ class StorageBackend (object):
     """
     if me._livep:
       me._livep = False
-      me._close()
+      me._close(abruptp)
 
   ## Utilities.
 
@@ -322,7 +323,7 @@ class StorageBackend (object):
     return me
   def __exit__(me, exctype, excvalue, exctb):
     """Context protocol: see `__enter__'."""
-    me.close()
+    me.close(excvalue is not None)
 
   ## Metadata.
 
@@ -440,7 +441,7 @@ class GDBMStorageBackend (StorageBackend):
   def _create(me, file):
     me._db = _G.open(file, 'n', 0600)
 
-  def _close(me):
+  def _close(me, abruptp):
     me._db.close()
     me._db = None
 
@@ -639,6 +640,6 @@ class PW (object):
   def __enter__(me):
     return me
   def __exit__(me, excty, excval, exctb):
-    me.db.close()
+    me.db.close(excval is not None)
 
 ###----- That's all, folks --------------------------------------------------