Commits

dan mackinlay committed 62d12fd

don't deserialise large files when checking for keys

Comments (0)

Files changed (1)

 # −*− coding: UTF−8 −*−
 import os
-import pickle
+try:
+    import cPickle as pickle
+except ImportError:
+    import pickle
 from UserDict import DictMixin
 from shutil import rmtree
 
 """
 A class providing dictionary access to a folder.
 
-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/
+My own original bloated implementation has been largely replaced by a tweaked
+version of 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.
+
+TODO: propagate init args into children.
 """
 
 def _get_tmp_dir():
     """
     def __init__(self, path=None, mktmp=False, handle_factory=open):
         if path is not None:
-            self.path = path
+            self.path = unicode(path)
         elif mktmp:
             self.path = _get_tmp_dir()
         else:
         if not os.path.exists(fn):
             raise KeyError("File %s does not exist" % fn)
         if os.path.isdir(fn):
-            return self.__class__(fn)
+            return self.__class__(fn, handle_factory=handle_factory)
         f = self._handle_factory(fn, 'rb')
         c = f.read()
         f.close()
     
     def _get_path(self, file_name):
         return os.path.join(self.path, file_name)
+    
+    def has_key(self, item):
+        """implement this here to avoid UserDict enthusastically reading in
+        the potentially large file"""
+        return os.path.exists(self._get_path(item))
 
 class FSPickleDict(FSDict):
     """
         if not os.path.exists(fn):
             raise KeyError("File %s does not exist" % fn)
         if os.path.isdir(fn):
-            return self.__class__(fn)
+            return self.__class__(fn, handle_factory=handle_factory)
         with self._handle_factory(fn, 'rb') as f:
             return pickle.load(f)