dan mackinlay avatar dan mackinlay committed 0e30d2b

factor out path names fetching and opening.

Comments (0)

Files changed (1)

             raise ValueError("no path supplied and no autmatic temp creation")
     
     def __getitem__(self, item):
-        fn = os.path.join(self.path, item)
+        fn = self._get_path(item)
         if not os.path.exists(fn):
             raise KeyError("File %s does not exist" % fn)
         if os.path.isdir(fn):
             return self.__class__(fn)
-        f = open(fn, 'rb')
+        f = self._get_handle(fn, 'rb')
         c = f.read()
         f.close()
         return c
     def __setitem__(self, item, value):
         if item in self:
             del self[item]
-        fn = os.path.join(self.path, item)
+        fn = self._get_path(item)
         if isinstance(value, str):
-            f = open(fn, 'wb')
+            f = self._get_handle(fn, 'wb')
             f.write(value)
             f.close()
         else:
             f.update(value)
 
     def __delitem__(self, item):
-        fn = os.path.join(self.path, item)
+        fn = self._get_path(item)
         if not os.path.exists(fn):
             raise KeyError("File %s does not exist" % fn)
         if os.path.isdir(fn):
     
     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.
     def __setitem__(self, item, value):
         if item in self:
             del self[item]
-        fn = os.path.join(self.path, item)
+        fn = self._get_path(item)
         if isinstance(value, FSDict):
             os.mkdir(fn)
             f = self[item]
             f.update(value)
         else: #pickle it!
-            with open(fn, 'wb') as f:
+            with self._get_handle(fn, 'wb') as f:
                 pickle.dump(value, f)
                 
     def __getitem__(self, item):
-        fn = os.path.join(self.path, item)
+        fn = self._get_path(item)
         if not os.path.exists(fn):
             raise KeyError("File %s does not exist" % fn)
         if os.path.isdir(fn):
             return self.__class__(fn)
-        with open(fn, 'rb') as f:
+        with self._get_handle(fn, 'rb') as f:
             return pickle.load(f)
 
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.