Python io.BufferedIOBase() Examples

The following are 30 code examples of io.BufferedIOBase(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module io , or try the search function .
Example #1
Source File: manifest.py    From commandment with MIT License 6 votes vote down vote up
def chunked_hash(stream: Union[io.RawIOBase, io.BufferedIOBase], chunk_size: int = DEFAULT_MD5_CHUNK_SIZE) -> List[bytes]:
    """Create a list of hashes of chunk_size size in bytes.

    Args:
          stream (Union[io.RawIOBase, io.BufferedIOBase]): The steam containing the bytes to be hashed.
          chunk_size (int): The md5 chunk size. Default is 10485760 (which is required for InstallApplication).

    Returns:
          List[str]: A list of md5 hashes calculated for each chunk
    """
    chunk = stream.read(chunk_size)
    hashes = []

    while chunk is not None:
        h = hashlib.md5()
        h.update(chunk)
        md5 = h.digest()
        hashes.append(md5)
        chunk = stream.read(chunk_size)

    return hashes 
Example #2
Source File: builder.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def _get_writable(stream_or_path, mode):
    """This method returns a tuple containing the stream and a flag to indicate
    if the stream should be automatically closed.

    The `stream_or_path` parameter is returned if it is an open writable stream.
    Otherwise, it treats the `stream_or_path` parameter as a file path and
    opens it with the given mode.

    It is used by the svg and png methods to interpret the file parameter.

    :type stream_or_path: str | io.BufferedIOBase
    :type mode: str | unicode
    :rtype: (io.BufferedIOBase, bool)
    """
    is_stream = hasattr(stream_or_path, 'write')
    if not is_stream:
        # No stream provided, treat "stream_or_path" as path
        stream_or_path = open(stream_or_path, mode)
    return stream_or_path, not is_stream 
Example #3
Source File: upload.py    From dwave-cloud-client with Apache License 2.0 6 votes vote down vote up
def __init__(self, fp, strict=True):
        if strict:
            valid = lambda f: (
                isinstance(f, (io.BufferedIOBase, io.RawIOBase))
                and f.seekable() and f.readable())
        else:
            valid = lambda f: all([hasattr(f, 'readinto'), hasattr(f, 'seek')])

        if not valid(fp):
            raise TypeError("expected file-like, seekable, readable object")

        # store file size, assuming it won't change
        self._size = fp.seek(0, os.SEEK_END)
        if self._size is None:
            # handle python2, when `fp.seek()` is `file.seek()`
            # note: not a thread-safe way
            self._size = fp.tell()

        self._fp = fp

        # multiple threads will be accessing the underlying file 
        self._lock = threading.RLock() 
Example #4
Source File: basic.py    From wechat-python-sdk with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _upload_media_py3(self, media_type, media_file, extension=''):
        if isinstance(media_file, io.IOBase) and hasattr(media_file, 'name'):
            extension = media_file.name.split('.')[-1].lower()
            if not is_allowed_extension(extension):
                raise ValueError('Invalid file type.')
            filename = media_file.name
        elif isinstance(media_file, io.BytesIO):
            extension = extension.lower()
            if not is_allowed_extension(extension):
                raise ValueError('Please provide \'extension\' parameters when the type of \'media_file\' is \'io.BytesIO\'.')
            filename = 'temp.' + extension
        else:
            raise ValueError('Parameter media_file must be io.BufferedIOBase(open a file with \'rb\') or io.BytesIO object.')

        return self.request.post(
            url='https://api.weixin.qq.com/cgi-bin/media/upload',
            params={
                'type': media_type,
            },
            files={
                'media': (filename, media_file, convert_ext_to_mime(extension))
            }
        ) 
Example #5
Source File: addressset.py    From btcrecover with GNU General Public License v2.0 6 votes vote down vote up
def tofile(self, dbfile):
        """Save the address set to a file

        :param dbfile: an open file object where the set is saved (overwriting it)
        :type dbfile: io.FileIO or file
        """
        if dbfile.tell() % mmap.ALLOCATIONGRANULARITY != 0:
            print("AddressSet: warning: if header position in file isn't a multiple of {}, it probably can't be loaded with fromfile()"
                  .format(mmap.ALLOCATIONGRANULARITY), file=sys.stderr)
        if "b" not in dbfile.mode:
            raise ValueError("must open file in binary mode")
        # Windows Python 2 file objects can't handle writes >= 4GiB. Objects returned
        # by io.open() work around this issue, see https://bugs.python.org/issue9611
        if not isinstance(dbfile, io.BufferedIOBase) and self._table_bytes >= 1 << 32:
            raise ValueError("must open file with io.open if size >= 4GiB")
        dbfile.truncate(dbfile.tell() + self.HEADER_LEN + self._table_bytes)
        dbfile.write(self._header())
        dbfile.write(self._data) 
Example #6
Source File: bz2.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def readline(self, size=-1):
        """Read a line of uncompressed bytes from the file.

        The terminating newline (if present) is retained. If size is
        non-negative, no more than size bytes will be read (in which
        case the line may be incomplete). Returns b'' if already at EOF.
        """
        if not isinstance(size, int):
            if not hasattr(size, "__index__"):
                raise TypeError("Integer argument expected")
            size = size.__index__()
        with self._lock:
            self._check_can_read()
            # Shortcut for the common case - the whole line is in the buffer.
            if size < 0:
                end = self._buffer.find(b"\n", self._buffer_offset) + 1
                if end > 0:
                    line = self._buffer[self._buffer_offset : end]
                    self._buffer_offset = end
                    self._pos += len(line)
                    return line
            return io.BufferedIOBase.readline(self, size) 
Example #7
Source File: http_server.py    From catt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def copy_byte_range(
    infile: io.BufferedIOBase,
    outfile: io.BufferedIOBase,
    start: int = None,
    stop: int = None,
    bufsize: int = 16 * 1024,
):
    """Like shutil.copyfileobj, but only copy a range of the streams.

    Both start and stop are inclusive.
    """
    if start is not None:
        infile.seek(start)
    while True:
        to_read = min(bufsize, stop + 1 - infile.tell() if stop else bufsize)
        buf = infile.read(to_read)
        if not buf:
            break
        outfile.write(buf) 
Example #8
Source File: saxutils.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def _gettextwriter(out, encoding):
    if out is None:
        import sys
        out = sys.stdout

    if isinstance(out, io.RawIOBase):
        buffer = io.BufferedIOBase(out)
        # Keep the original file open when the TextIOWrapper is
        # destroyed
        buffer.close = lambda: None
    else:
        # This is to handle passed objects that aren't in the
        # IOBase hierarchy, but just have a write method
        buffer = io.BufferedIOBase()
        buffer.writable = lambda: True
        buffer.write = out.write
        try:
            # TextIOWrapper uses this methods to determine
            # if BOM (for UTF-16, etc) should be added
            buffer.seekable = out.seekable
            buffer.tell = out.tell
        except AttributeError:
            pass
    # wrap a binary writer with TextIOWrapper
    return _UnbufferedTextIOWrapper(buffer, encoding=encoding,
                                   errors='xmlcharrefreplace',
                                   newline='\n') 
Example #9
Source File: update.py    From alertR with GNU Affero General Public License v3.0 5 votes vote down vote up
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str:
        """
        Internal function that calculates the sha256 hash of the file.

        :param fileHandle: file handle for which the hash should be created
        :return: sha256 hash digest
        """
        fileHandle.seek(0)
        sha256 = hashlib.sha256()
        while True:
            data = fileHandle.read(128)
            if not data:
                break
            sha256.update(data)
        return sha256.hexdigest() 
Example #10
Source File: alertRinstaller.py    From alertR with GNU Affero General Public License v3.0 5 votes vote down vote up
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str:
        """
        Internal function that calculates the sha256 hash of the file.

        :param fileHandle: file handle for which the hash should be created
        :return: sha256 hash digest
        """
        fileHandle.seek(0)
        sha256 = hashlib.sha256()
        while True:
            data = fileHandle.read(128)
            if not data:
                break
            sha256.update(data)
        return sha256.hexdigest() 
Example #11
Source File: numpy_pickle_utils.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, filename, mode="rb", compresslevel=9):
        # This lock must be recursive, so that BufferedIOBase's
        # readline(), readlines() and writelines() don't deadlock.
        self._lock = RLock()
        self._fp = None
        self._closefp = False
        self._mode = _MODE_CLOSED
        self._pos = 0
        self._size = -1

        if not isinstance(compresslevel, int) or not (1 <= compresslevel <= 9):
            raise ValueError("'compresslevel' must be an integer "
                             "between 1 and 9. You provided 'compresslevel={}'"
                             .format(compresslevel))

        if mode == "rb":
            mode_code = _MODE_READ
            self._decompressor = zlib.decompressobj(self.wbits)
            self._buffer = b""
            self._buffer_offset = 0
        elif mode == "wb":
            mode_code = _MODE_WRITE
            self._compressor = zlib.compressobj(compresslevel,
                                                zlib.DEFLATED,
                                                self.wbits,
                                                zlib.DEF_MEM_LEVEL,
                                                0)
        else:
            raise ValueError("Invalid mode: %r" % (mode,))

        if isinstance(filename, _basestring):
            self._fp = io.open(filename, mode)
            self._closefp = True
            self._mode = mode_code
        elif hasattr(filename, "read") or hasattr(filename, "write"):
            self._fp = filename
            self._mode = mode_code
        else:
            raise TypeError("filename must be a str or bytes object, "
                            "or a file") 
Example #12
Source File: update.py    From alertR with GNU Affero General Public License v3.0 5 votes vote down vote up
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str:
        """
        Internal function that calculates the sha256 hash of the file.

        :param fileHandle: file handle for which the hash should be created
        :return: sha256 hash digest
        """
        fileHandle.seek(0)
        sha256 = hashlib.sha256()
        while True:
            data = fileHandle.read(128)
            if not data:
                break
            sha256.update(data)
        return sha256.hexdigest() 
Example #13
Source File: test_subprocess.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_io_buffered_by_default(self):
        p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        try:
            self.assertIsInstance(p.stdin, io.BufferedIOBase)
            self.assertIsInstance(p.stdout, io.BufferedIOBase)
            self.assertIsInstance(p.stderr, io.BufferedIOBase)
        finally:
            p.stdin.close()
            p.stdout.close()
            p.stderr.close()
            p.wait() 
Example #14
Source File: remote.py    From pandaSDMX with Apache License 2.0 5 votes vote down vote up
def __init__(self, response, tee=None):
        self.response = response
        if tee is None:
            tee = BytesIO()
        # If tee is a file-like object or tempfile, then use it as cache
        if isinstance(tee, BufferedIOBase) or hasattr(tee, 'file'):
            self.tee = tee
        else:
            # So tee must be str or os.FilePath
            self.tee = open(tee, 'w+b')    
        self.tee.write(response.content)
        self.tee.seek(0) 
Example #15
Source File: update.py    From alertR with GNU Affero General Public License v3.0 5 votes vote down vote up
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str:
        """
        Internal function that calculates the sha256 hash of the file.

        :param fileHandle: file handle for which the hash should be created
        :return: sha256 hash digest
        """
        fileHandle.seek(0)
        sha256 = hashlib.sha256()
        while True:
            data = fileHandle.read(128)
            if not data:
                break
            sha256.update(data)
        return sha256.hexdigest() 
Example #16
Source File: update.py    From alertR with GNU Affero General Public License v3.0 5 votes vote down vote up
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str:
        """
        Internal function that calculates the sha256 hash of the file.

        :param fileHandle: file handle for which the hash should be created
        :return: sha256 hash digest
        """
        fileHandle.seek(0)
        sha256 = hashlib.sha256()
        while True:
            data = fileHandle.read(128)
            if not data:
                break
            sha256.update(data)
        return sha256.hexdigest() 
Example #17
Source File: update.py    From alertR with GNU Affero General Public License v3.0 5 votes vote down vote up
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str:
        """
        Internal function that calculates the sha256 hash of the file.

        :param fileHandle: file handle for which the hash should be created
        :return: sha256 hash digest
        """
        fileHandle.seek(0)
        sha256 = hashlib.sha256()
        while True:
            data = fileHandle.read(128)
            if not data:
                break
            sha256.update(data)
        return sha256.hexdigest() 
Example #18
Source File: image.py    From pyrobud with MIT License 5 votes vote down vote up
def img_to_png(src: FileLike, dest: Optional[FileLike] = None) -> FileLike:
    """Coverts the given image to a PNG using Pillow."""

    if dest is None:
        dest = src

    def _img_to_png() -> None:
        im = Image.open(src).convert("RGBA")
        if isinstance(dest, io.BufferedIOBase):
            dest.seek(0)
        im.save(dest, "png")

    await run_sync(_img_to_png)
    return dest 
Example #19
Source File: gbxparser.py    From PyPlanet with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, buffer):
		"""
		:param buffer: Buffer
		:type buffer: io.BufferedIOBase
		"""
		self.buffer = buffer 
Example #20
Source File: saxutils.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _gettextwriter(out, encoding):
    if out is None:
        import sys
        out = sys.stdout

    if isinstance(out, io.RawIOBase):
        buffer = io.BufferedIOBase(out)
        # Keep the original file open when the TextIOWrapper is
        # destroyed
        buffer.close = lambda: None
    else:
        # This is to handle passed objects that aren't in the
        # IOBase hierarchy, but just have a write method
        buffer = io.BufferedIOBase()
        buffer.writable = lambda: True
        buffer.write = out.write
        try:
            # TextIOWrapper uses this methods to determine
            # if BOM (for UTF-16, etc) should be added
            buffer.seekable = out.seekable
            buffer.tell = out.tell
        except AttributeError:
            pass
    # wrap a binary writer with TextIOWrapper
    return _UnbufferedTextIOWrapper(buffer, encoding=encoding,
                                   errors='xmlcharrefreplace',
                                   newline='\n') 
Example #21
Source File: numpy_pickle_utils.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def readinto(self, b):
        """Read up to len(b) bytes into b.

        Returns the number of bytes read (0 for EOF).
        """
        with self._lock:
            return io.BufferedIOBase.readinto(self, b) 
Example #22
Source File: saxutils.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def _gettextwriter(out, encoding):
    if out is None:
        import sys
        return sys.stdout

    if isinstance(out, io.TextIOBase):
        # use a text writer as is
        return out

    if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)):
        # use a codecs stream writer as is
        return out

    # wrap a binary writer with TextIOWrapper
    if isinstance(out, io.RawIOBase):
        # Keep the original file open when the TextIOWrapper is
        # destroyed
        class _wrapper:
            __class__ = out.__class__
            def __getattr__(self, name):
                return getattr(out, name)
        buffer = _wrapper()
        buffer.close = lambda: None
    else:
        # This is to handle passed objects that aren't in the
        # IOBase hierarchy, but just have a write method
        buffer = io.BufferedIOBase()
        buffer.writable = lambda: True
        buffer.write = out.write
        try:
            # TextIOWrapper uses this methods to determine
            # if BOM (for UTF-16, etc) should be added
            buffer.seekable = out.seekable
            buffer.tell = out.tell
        except AttributeError:
            pass
    return io.TextIOWrapper(buffer, encoding=encoding,
                            errors='xmlcharrefreplace',
                            newline='\n',
                            write_through=True) 
Example #23
Source File: myzipfile.py    From fb2mobi with MIT License 5 votes vote down vote up
def readline(self, limit=-1):
        """Read and return a line from the stream.

        If limit is specified, at most limit bytes will be read.
        """

        if limit < 0:
            # Shortcut common case - newline found in buffer.
            i = self._readbuffer.find(b'\n', self._offset) + 1
            if i > 0:
                line = self._readbuffer[self._offset: i]
                self._offset = i
                return line

        return io.BufferedIOBase.readline(self, limit) 
Example #24
Source File: recorder.py    From pyrdp with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, fileName: Union[str, Path]):
        super().__init__()

        # Buffer data until we have enough bytes for a meaningful replay.
        self.pending = b''

        self.filename = fileName
        self.fd: BufferedIOBase = None 
Example #25
Source File: lint_target.py    From vint with MIT License 5 votes vote down vote up
def __init__(self, alternate_path, buffered_io):
        # type: (Path, BufferedIOBase) -> None
        super(LintTargetBufferedStream, self).__init__(alternate_path)
        self._buffered_io = buffered_io 
Example #26
Source File: strtools.py    From extratools with MIT License 5 votes vote down vote up
def __checksum(f: Any, func: Callable[[bytes], Any]) -> str:
    content: bytes

    if isinstance(f, str):
        content = f.encode("utf-8")
    elif isinstance(f, bytes):
        content = f
    elif isinstance(f, TextIOBase):
        content = f.read().encode("utf-8")
    elif isinstance(f, BufferedIOBase):
        content = f.read()

    return func(content).hexdigest() 
Example #27
Source File: strtools.py    From extratools with MIT License 5 votes vote down vote up
def __checksum(f: Any, func: Callable[[bytes], Any]) -> str:
    content: bytes

    if isinstance(f, str):
        content = f.encode("utf-8")
    elif isinstance(f, bytes):
        content = f
    elif isinstance(f, TextIOBase):
        content = f.read().encode("utf-8")
    elif isinstance(f, BufferedIOBase):
        content = f.read()

    return func(content).hexdigest() 
Example #28
Source File: test_subprocess.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_io_buffered_by_default(self):
        p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        try:
            self.assertIsInstance(p.stdin, io.BufferedIOBase)
            self.assertIsInstance(p.stdout, io.BufferedIOBase)
            self.assertIsInstance(p.stderr, io.BufferedIOBase)
        finally:
            p.stdin.close()
            p.stdout.close()
            p.stderr.close()
            p.wait() 
Example #29
Source File: test_pathlib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_open_common(self):
        p = self.cls(BASE)
        with (p / 'fileA').open('r') as f:
            self.assertIsInstance(f, io.TextIOBase)
            self.assertEqual(f.read(), "this is file A\n")
        with (p / 'fileA').open('rb') as f:
            self.assertIsInstance(f, io.BufferedIOBase)
            self.assertEqual(f.read().strip(), b"this is file A")
        with (p / 'fileA').open('rb', buffering=0) as f:
            self.assertIsInstance(f, io.RawIOBase)
            self.assertEqual(f.read().strip(), b"this is file A") 
Example #30
Source File: saxutils.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def _gettextwriter(out, encoding):
    if out is None:
        import sys
        return sys.stdout

    if isinstance(out, io.TextIOBase):
        # use a text writer as is
        return out

    if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)):
        # use a codecs stream writer as is
        return out

    # wrap a binary writer with TextIOWrapper
    if isinstance(out, io.RawIOBase):
        # Keep the original file open when the TextIOWrapper is
        # destroyed
        class _wrapper:
            __class__ = out.__class__
            def __getattr__(self, name):
                return getattr(out, name)
        buffer = _wrapper()
        buffer.close = lambda: None
    else:
        # This is to handle passed objects that aren't in the
        # IOBase hierarchy, but just have a write method
        buffer = io.BufferedIOBase()
        buffer.writable = lambda: True
        buffer.write = out.write
        try:
            # TextIOWrapper uses this methods to determine
            # if BOM (for UTF-16, etc) should be added
            buffer.seekable = out.seekable
            buffer.tell = out.tell
        except AttributeError:
            pass
    return io.TextIOWrapper(buffer, encoding=encoding,
                            errors='xmlcharrefreplace',
                            newline='\n',
                            write_through=True)