示例#1
0
 def create_folder(self, filepath):
     try:
         os.makedirs(filepath)
     except OSError as e:
         # 'OSError' raised if the leaf directory already exists or cannot be
         # created. Check for case where 'filepath' has already been created and
         # silently ignore.
         if e.errno == errno.EEXIST:
             pass
         elif e.errno == errno.ENOENT and not filepath:
             raise exceptions.StorageError(
                 "Can't create a folder with an empty filepath!")
         else:
             raise exceptions.StorageError("Can't create folder at %s" %
                                           filepath)
示例#2
0
    def put(self, fileobj, filepath):
        # If we are passed an open file, seek to the beginning such that we are
        # copying the entire contents
        if not fileobj.closed:
            fileobj.seek(0)

        try:
            with open(filepath, 'wb') as destination_file:
                shutil.copyfileobj(fileobj, destination_file)
                # Force the destination file to be written to disk from Python's internal
                # and the operating system's buffers.  os.fsync() should follow flush().
                destination_file.flush()
                os.fsync(destination_file.fileno())
        except (OSError, IOError):
            raise exceptions.StorageError("Can't write file %s" % filepath)
示例#3
0
 def list_folder(self, filepath):
     try:
         return os.listdir(filepath)
     except FileNotFoundError:
         raise exceptions.StorageError("Can't list folder at %s" % filepath)
示例#4
0
 def getsize(self, filepath):
     try:
         return os.path.getsize(filepath)
     except OSError:
         raise exceptions.StorageError("Can't access file %s" % filepath)
示例#5
0
 def remove(self, filepath):
     try:
         os.remove(filepath)
     except (FileNotFoundError, PermissionError,
             OSError):  # pragma: no cover
         raise exceptions.StorageError("Can't remove file %s" % filepath)
示例#6
0
 def __enter__(self):
     try:
         self.file_object = open(self.filepath, 'rb')
         return self.file_object
     except (FileNotFoundError, IOError):
         raise exceptions.StorageError("Can't open %s" % self.filepath)