How to use the pyfakefs.fake_filesystem.FakeFile function in pyfakefs

To help you get started, we’ve selected a few pyfakefs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jmcgeheeiv / pyfakefs / pyfakefs / fake_filesystem.py View on Github external
raise OSError(errno.EINVAL,
                          'Fake os mknod implementation only supports '
                          'regular files.')

        head, tail = self.path.split(filename)
        if not tail:
            if self.filesystem.Exists(head):
                raise OSError(errno.EEXIST, 'Fake filesystem: %s: %s' % (
                    os.strerror(errno.EEXIST), filename))
            raise OSError(errno.ENOENT, 'Fake filesystem: %s: %s' % (
                os.strerror(errno.ENOENT), filename))
        if tail == '.' or tail == '..' or self.filesystem.Exists(filename):
            raise OSError(errno.EEXIST, 'Fake fileystem: %s: %s' % (
                os.strerror(errno.EEXIST), filename))
        try:
            self.filesystem.AddObject(head, FakeFile(tail,
                                                     mode & ~self.filesystem.umask,
                                                     filesystem=self.filesystem))
        except IOError:
            raise OSError(errno.ENOTDIR, 'Fake filesystem: %s: %s' % (
                os.strerror(errno.ENOTDIR), filename))
github jmcgeheeiv / pyfakefs / pyfakefs / fake_filesystem.py View on Github external
raise IOError(errno.EEXIST,
                          'File already exists in fake filesystem',
                          file_path)
        parent_directory, new_file = self.SplitPath(file_path)
        if not parent_directory:
            parent_directory = self.cwd
        self._AutoMountDriveIfNeeded(parent_directory)
        if not self.Exists(parent_directory):
            if not create_missing_dirs:
                raise IOError(errno.ENOENT, 'No such fake directory', parent_directory)
            self.CreateDirectory(parent_directory)
        else:
            parent_directory = self.NormalizeCase(parent_directory)
        if apply_umask:
            st_mode &= ~self.umask
        file_object = FakeFile(new_file, st_mode, filesystem=self)
        self.last_ino += 1
        file_object.SetIno(self.last_ino)
        self.AddObject(parent_directory, file_object)

        if contents is not None or st_size is not None:
            try:
                if st_size is not None:
                    file_object.SetLargeFileSize(st_size)
                else:
                    file_object._SetInitialContents(contents, encoding)
            except IOError:
                self.RemoveObject(file_path)
                raise

        return file_object
github jmcgeheeiv / pyfakefs / pyfakefs / fake_filesystem.py View on Github external
"""
        self.st_mtime = st_mtime

    def __str__(self):
        return '%s(%o)' % (self.name, self.st_mode)

    def SetIno(self, st_ino):
        """Set the self.st_ino attribute.

        Args:
          st_ino: The desired inode.
        """
        self.st_ino = st_ino


class FakeDirectory(FakeFile):
    """Provides the appearance of a real dir."""

    def __init__(self, name, perm_bits=PERM_DEF, filesystem=None):
        """init.

        Args:
          name:  name of the file/directory, without parent path information
          perm_bits: permission bits. defaults to 0o777.
          filesystem: if set, the fake filesystem where the directory is created
        """
        FakeFile.__init__(self, name, stat.S_IFDIR | perm_bits, {}, filesystem=filesystem)

    @property
    def contents(self):
        return self.byte_contents
github kobayashi / s3monkey / s3monkey / core.py View on Github external
return

        self.upload()

        # for raw io, all writes are flushed immediately
        if self.allow_update and not self.raw_io:
            self.flush()
        if self._closefd:
            self._filesystem._close_open_file(self.filedes)
        else:
            self._filesystem.open_files[self.filedes].remove(self)
        if self.delete_on_close:
            self._filesystem.remove_object(self.get_object().path)


class S3File(fake_fs.FakeFile):
    """docstring for S3File"""

    def __init__(self, key, filesystem):
        self.key = key
        super(S3File, self).__init__(self.key.name, filesystem=filesystem)

    def IsLargeFile(self):
        return False

    @property
    def byte_contents(self):
        data = self.key.get()

        self.set_contents(data, encoding=chardet.detect(data))
        return data
github jmcgeheeiv / pyfakefs / pyfakefs / fake_filesystem.py View on Github external
def __init__(self, name, perm_bits=PERM_DEF, filesystem=None):
        """init.

        Args:
          name:  name of the file/directory, without parent path information
          perm_bits: permission bits. defaults to 0o777.
          filesystem: if set, the fake filesystem where the directory is created
        """
        FakeFile.__init__(self, name, stat.S_IFDIR | perm_bits, {}, filesystem=filesystem)