dan mackinlay avatar dan mackinlay committed 59c560a

add Gzip support by making handle factory generic

Comments (0)

Files changed (2)

 """
 A class providing dictionary access to a folder.
 
-My own bloated implementation of thsi has been replace by Ian Bicking's much
-more sleek one, documented with characteristic lucidity at
+My own original bloated implementation of this has been replaced by Ian
+Bicking's much more sleek one, documented with characteristic lucidity at
 http://blog.ianbicking.org/2007/08/17/dictmixin/
+
+For my purposes, automatic tmpdir creation and automatic GZipping are helpful, so I've added these features.
 """
 
-def get_tmp_dir():
+def _get_tmp_dir():
     import tempfile
     return tempfile.mkdtemp()
 
     backing, and it can, e.g. rsync.
     
     N.B. the keys ordering here is FS-dependent and thus unlike to be the same
-    as with a real dict. beware.
+    as with a real dict. Beware.
+    
+    If you pass "handle_factory=gzip.GzipFile", output will be gzipped. Or you
+    can pass any other kind of handle factory to access other exotic kinds of
+    FS-like storage.
     """
-    def __init__(self, path=None, mktmp=False):
+    def __init__(self, path=None, mktmp=False, handle_factory=open):
         if path is not None:
             self.path = path
         elif mktmp:
-            self.path = get_tmp_dir()
+            self.path = _get_tmp_dir()
         else:
             raise ValueError("no path supplied and no autmatic temp creation")
+        self._handle_factory = handle_factory
     
     def __getitem__(self, item):
         fn = self._get_path(item)
             raise KeyError("File %s does not exist" % fn)
         if os.path.isdir(fn):
             return self.__class__(fn)
-        f = self._get_handle(fn, 'rb')
+        f = self._handle_factory(fn, 'rb')
         c = f.read()
         f.close()
         return c
             del self[item]
         fn = self._get_path(item)
         if isinstance(value, str):
-            f = self._get_handle(fn, 'wb')
+            f = self._handle_factory(fn, 'wb')
             f.write(value)
             f.close()
         else:
     
     def delete(self):
         rmtree(self.path)
-        
+    
     def _get_path(self, file_name):
         return os.path.join(self.path, file_name)
-        
-    def _get_handle(self, file_path, mode='rb'):
-        return open(file_path, mode)
-        
+
 class FSPickleDict(FSDict):
     """
     Provide dictionary access to a temp dir, storing any picklable object.
             f = self[item]
             f.update(value)
         else: #pickle it!
-            with self._get_handle(fn, 'wb') as f:
+            with self._handle_factory(fn, 'wb') as f:
                 pickle.dump(value, f)
                 
     def __getitem__(self, item):
             raise KeyError("File %s does not exist" % fn)
         if os.path.isdir(fn):
             return self.__class__(fn)
-        with self._get_handle(fn, 'rb') as f:
+        with self._handle_factory(fn, 'rb') as f:
             return pickle.load(f)
 

tests/fsdict_tests.py

 from os.path import exists as pathexists
 
 def test_fsdict_pickles():
-    "occasionally something goes wrong with path in pickling"
+    "occasionally something goes wrong with pickling"
     d1 = FSPickleDict(mktmp=True)
     d1.update({'a':'b'})
     d2 = loads(dumps(d1))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.