Commits

Matt Chaput  committed 0af154e

Added context manager interface to Storage base class. See issue #277.

  • Participants
  • Parent commits 78e9856

Comments (0)

Files changed (1)

File src/whoosh/filedb/filestore.py

     def __iter__(self):
         return iter(self.list())
 
+    def __enter__(self):
+        self.create()
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self.close()
+
     def create(self):
         """Creates any required implementation-specific resources. For example,
         a filesystem-based implementation might create a directory, while a
 
             st = FileStorage("indexdir").create()
 
+        Storage implementations should be written so that calling create() a
+        second time on the same storage
+
         :return: a :class:`Storage` instance.
         """
 
         del self.files
         del self.locks
 
+    def close(self):
+        self.destroy()
+
     def list(self):
         return list(self.files.keys())