class SFTPStorageFile(File): def __init__(self, name, storage, mode): self._name = name self._storage = storage self._mode = mode self._is_dirty = False self.file = BytesIO() self._is_read = False @property def size(self): if not hasattr(self, '_size'): self._size = self._storage.size(self._name) return self._size def read(self, num_bytes=None): if not self._is_read: self.file = self._storage._read(self._name) self._is_read = True return self.file.read(num_bytes) def write(self, content): if 'w' not in self._mode: raise AttributeError("File was opened for read-only access.") self.file = BytesIO(content) self._is_dirty = True self._is_read = True def close(self): if self._is_dirty: self._storage._save(self._name, self.file.getvalue()) self.file.close()
class CouchDBFile(File): """ CouchDBFile - a Django File-like class for CouchDB documents. """ def __init__(self, name, storage, mode): self._name = name self._storage = storage self._mode = mode self._is_dirty = False try: self._doc = self._storage.get_document(name) tmp, ext = os.path.split(name) if ext: filename = "content." + ext else: filename = "content" attachment = self._storage.db.get_attachment(self._doc, filename=filename) self.file = BytesIO(attachment) except couchdb.client.ResourceNotFound: if 'r' in self._mode: raise ValueError("The file cannot be reopened.") else: self.file = BytesIO() self._is_dirty = True @property def size(self): return self._doc['size'] def write(self, content): if 'w' not in self._mode: raise AttributeError("File was opened for read-only access.") self.file = BytesIO(content) self._is_dirty = True def close(self): if self._is_dirty: self._storage._put_file(self._name, self.file.getvalue()) self.file.close()
class LibCloudFile(File): """File inherited class for libcloud storage objects read and write""" def __init__(self, name, storage, mode): self.name = name self._storage = storage self._mode = mode self._is_dirty = False self._file = None def _get_file(self): if self._file is None: data = self._storage._read(self.name) self._file = BytesIO(data) return self._file def _set_file(self, value): self._file = value file = property(_get_file, _set_file) @property def size(self): if not hasattr(self, '_size'): self._size = self._storage.size(self.name) return self._size def read(self, num_bytes=None): return self.file.read(num_bytes) def write(self, content): if 'w' not in self._mode: raise AttributeError("File was opened for read-only access.") self.file = BytesIO(content) self._is_dirty = True def close(self): if self._is_dirty: self._storage._save(self.name, self.file) self.file.close()
class LibCloudFile(File): """File inherited class for libcloud storage objects read and write""" def __init__(self, name, storage, mode): self._name = name self._storage = storage self._mode = mode self._is_dirty = False self.file = BytesIO() self.start_range = 0 @property def size(self): if not hasattr(self, "_size"): self._size = self._storage.size(self._name) return self._size def read(self, num_bytes=None): if num_bytes is None: args = [] self.start_range = 0 else: args = [self.start_range, self.start_range + num_bytes - 1] data = self._storage._read(self._name, *args) self.file = BytesIO(data) return self.file.getvalue() def write(self, content): if "w" not in self._mode: raise AttributeError("File was opened for read-only access.") self.file = BytesIO(content) self._is_dirty = True def close(self): if self._is_dirty: self._storage._save(self._name, self.file) self.file.close()
class LibCloudFile(File): """File inherited class for libcloud storage objects read and write""" def __init__(self, name, storage, mode): self._name = name self._storage = storage self._mode = mode self._is_dirty = False self.file = BytesIO() self.start_range = 0 @property def size(self): if not hasattr(self, '_size'): self._size = self._storage.size(self._name) return self._size def read(self, num_bytes=None): if num_bytes is None: args = [] self.start_range = 0 else: args = [self.start_range, self.start_range + num_bytes - 1] data = self._storage._read(self._name, *args) self.file = BytesIO(data) return self.file.getvalue() def write(self, content): if 'w' not in self._mode: raise AttributeError("File was opened for read-only access.") self.file = BytesIO(content) self._is_dirty = True def close(self): if self._is_dirty: self._storage._save(self._name, self.file) self.file.close()