X-Git-Url: http://git.vrable.net/?p=cumulus.git;a=blobdiff_plain;f=python%2Fcumulus%2Fstore%2Ffile.py;h=833a5f0e0d9888cf1c512863df2a87128b5c6e77;hp=0998448720d66dfac1786b448221b4468dc76b37;hb=ee98274cfd9e9383214a9792c01fdfe4f22ef677;hpb=64bff41cb3ccdd60e767a5bb9ed8525d2dda1966 diff --git a/python/cumulus/store/file.py b/python/cumulus/store/file.py index 0998448..833a5f0 100644 --- a/python/cumulus/store/file.py +++ b/python/cumulus/store/file.py @@ -16,47 +16,43 @@ # 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("/") +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 _get_path(self, type, name): - return "%s/%s" % (self.prefix, name) - - def list(self, type): - files = os.listdir(self.prefix) - return (f for f in files if type_patterns[type].match(f)) + def list(self, subdir): + try: + return os.listdir(os.path.join(self.prefix, subdir)) + except OSError: + raise cumulus.store.NotFoundError(subdir) - def get(self, type, name): - k = self._get_path(type, name) - return open(k, 'rb') + def get(self, path): + try: + return open(os.path.join(self.prefix, path), "rb") + except IOError: + raise cumulus.store.NotFoundError(path) - def put(self, type, name, fp): - k = self._get_path(type, name) - out = open(k, 'wb') - buf = fp.read(4096) - while len(buf) > 0: - out.write(buf) + def put(self, path, fp): + 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, type, name): - k = self._get_path(type, name) - os.unlink(k) + def delete(self, path): + os.unlink(os.path.join(self.prefix, path)) - def stat(self, type, name): + def stat(self, path): try: - stat = os.stat(self._get_path(type, name)) + stat = os.stat(os.path.join(self.prefix, path)) return {'size': stat.st_size} except OSError: - raise cumulus.store.NotFoundError, (type, name) - -Store = FileStore + raise cumulus.store.NotFoundError(path)