Byte/string handling fixes for Python 3.
[cumulus.git] / python / cumulus / store / file.py
index 8304401..833a5f0 100644 (file)
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
+from __future__ import division, print_function, unicode_literals
+
 import os, sys, tempfile
 
 import cumulus.store
 
-type_patterns = cumulus.store.type_patterns
-
-class FileStore(cumulus.store.Store):
-    def __init__(self, url, **kw):
-        # if constructor isn't called via factory interpret url as filename
-        if not hasattr (self, 'path'):
-            self.path = url
-        self.prefix = self.path.rstrip("/")
-
-    def _get_path(self, type, name):
-        return os.path.join(self.prefix, type, name)
+class Store(cumulus.store.Store):
+    """Storage backend that accesses the local file system."""
+    def __init__(self, url):
+        super(Store, self).__init__(url)
+        self.prefix = cumulus.store.unquote(url.path)
 
     def list(self, subdir):
         try:
@@ -40,16 +36,16 @@ class FileStore(cumulus.store.Store):
 
     def get(self, path):
         try:
-            return open(os.path.join(self.prefix, path), 'rb')
+            return open(os.path.join(self.prefix, path), "rb")
         except IOError:
             raise cumulus.store.NotFoundError(path)
 
     def put(self, path, fp):
-        out = open(os.path.join(self.prefix, path), 'wb')
-        buf = fp.read(4096)
-        while len(buf) > 0:
-            out.write(buf)
+        with open(os.path.join(self.prefix, path), "wb") as out:
             buf = fp.read(4096)
+            while len(buf) > 0:
+                out.write(buf)
+                buf = fp.read(4096)
 
     def delete(self, path):
         os.unlink(os.path.join(self.prefix, path))
@@ -59,6 +55,4 @@ class FileStore(cumulus.store.Store):
             stat = os.stat(os.path.join(self.prefix, path))
             return {'size': stat.st_size}
         except OSError:
-            raise cumulus.store.NotFoundError, path
-
-Store = FileStore
+            raise cumulus.store.NotFoundError(path)