예제 #1
0
    def listdir(self, dirname):
        """Returns a list of entries contained within a directory."""
        if not self.isdir(dirname):
            raise errors.NotFoundError(None, None, "Could not find directory")

        entries = os.listdir(compat.as_str_any(dirname))
        entries = [compat.as_str_any(item) for item in entries]
        return entries
예제 #2
0
 def stat(self, filename):
     """Returns file statistics for a given path."""
     # NOTE: Size of the file is given by .st_size as returned from
     # os.stat(), but we convert to .length
     try:
         file_length = os.stat(compat.as_bytes(filename)).st_size
     except OSError:
         raise errors.NotFoundError(None, None, "Could not find file")
     return StatData(file_length)
예제 #3
0
 def stat(self, filename):
     """Returns file statistics for a given path."""
     # NOTE: Size of the file is given by ContentLength from S3,
     # but we convert to .length
     client = boto3.client("s3", endpoint_url=self._s3_endpoint)
     bucket, path = self.bucket_and_path(filename)
     try:
         obj = client.head_object(Bucket=bucket, Key=path)
         return StatData(obj["ContentLength"])
     except botocore.exceptions.ClientError as exc:
         if exc.response["Error"]["Code"] == "404":
             raise errors.NotFoundError(None, None, "Could not find file")
         else:
             raise
예제 #4
0
    def read(self, filename, binary_mode=False, size=None, continue_from=None):
        """Reads contents of a file to a string.

        Args:
            filename: string, a path
            binary_mode: bool, read as binary if True, otherwise text
            size: int, number of bytes or characters to read, otherwise
                read all the contents of the file (from the continuation
                marker, if present).
            continue_from: An opaque value returned from a prior invocation of
                `read(...)` marking the last read position, so that reading
                may continue from there.  Otherwise read from the beginning.

        Returns:
            A tuple of `(data, continuation_token)` where `data' provides either
            bytes read from the file (if `binary_mode == true`) or the decoded
            string representation thereof (otherwise), and `continuation_token`
            is an opaque value that can be passed to the next invocation of
            `read(...) ' in order to continue from the last read position.
        """
        fs, path = self._fs_path(filename)

        mode = "rb" if binary_mode else "r"
        encoding = None if binary_mode else "utf8"
        if not exists(filename):
            raise errors.NotFoundError(
                None, None, "Not Found: " + compat.as_text(filename))
        with fs.open(path, mode, encoding=encoding) as f:
            if continue_from is not None:
                if not f.seekable():
                    raise errors.InvalidArgumentError(
                        None,
                        None,
                        "{} is not seekable".format(filename),
                    )
                offset = continue_from.get("opaque_offset", None)
                if offset is not None:
                    f.seek(offset)

            data = f.read(size)
            # The new offset may not be `offset + len(data)`, due to decoding
            # and newline translation.
            # So, just measure it in whatever terms the underlying stream uses.
            continuation_token = ({
                "opaque_offset": f.tell()
            } if f.seekable() else {})
            return (data, continuation_token)
예제 #5
0
 def func_wrapper(self, *args, **kwargs):
     try:
         return func(self, *args, **kwargs)
     except FileNotFoundError as e:
         raise errors.NotFoundError(None, None, str(e))