Python io.IOBase() Examples

The following are 30 code examples of io.IOBase(). 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: connection.py    From plugin.video.netflix with MIT License 6 votes vote down vote up
def cmd_stmt_execute(self, statement_id, data=(), parameters=(), flags=0):
        """Execute a prepared MySQL statement"""
        parameters = list(parameters)
        long_data_used = {}

        if data:
            for param_id, _ in enumerate(parameters):
                if isinstance(data[param_id], IOBase):
                    binary = True
                    try:
                        binary = 'b' not in data[param_id].mode
                    except AttributeError:
                        pass
                    self.cmd_stmt_send_long_data(statement_id, param_id,
                                                 data[param_id])
                    long_data_used[param_id] = (binary,)

        execute_packet = self._protocol.make_stmt_execute(
            statement_id, data, tuple(parameters), flags,
            long_data_used, self.charset)
        packet = self._send_cmd(ServerCmd.STMT_EXECUTE, packet=execute_packet)
        result = self._handle_binary_result(packet)
        return result 
Example #2
Source File: base.py    From baseband with GNU General Public License v3.0 6 votes vote down vote up
def temporary_offset(self, offset=None, whence=0):
        """Context manager for temporarily seeking to another file position.

        To be used as part of a ``with`` statement::

            with fh_raw.temporary_offset() [as fh_raw]:
                with-block

        On exiting the ``with-block``, the file pointer is moved back to its
        original position.  As a convenience, one can pass on the offset
        to seek to when entering the context manager.  Parameters are as
        for :meth:`io.IOBase.seek`.
        """
        oldpos = self.tell()
        try:
            if offset is not None:
                self.seek(offset, whence)
            yield self
        finally:
            self.seek(oldpos) 
Example #3
Source File: base.py    From baseband with GNU General Public License v3.0 6 votes vote down vote up
def __getstate__(self):
        if self.writable():
            raise TypeError('cannot pickle file opened for writing')

        state = self.__dict__.copy()
        # IOBase instances cannot be pickled, but we can just reopen them
        # when we are unpickled.  Anything else may have internal state that
        # needs preserving (e.g., SequentialFile), so we will assume
        # it takes care of this itself.
        if isinstance(self.fh_raw, io.IOBase):
            fh = state.pop('fh_raw')
            state['fh_info'] = {
                'offset': 'closed' if fh.closed else fh.tell(),
                'filename': fh.name,
                'mode': fh.mode}

        return state 
Example #4
Source File: encoding.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def prepare_iter(value):
    """
    Ensure response body is iterable and resolves to False when empty.
    """
    if isinstance(value, text_or_bytes):
        # strings get wrapped in a list because iterating over a single
        # item list is much faster than iterating over every character
        # in a long string.
        if value:
            value = [value]
        else:
            # [''] doesn't evaluate to False, so replace it with [].
            value = []
    # Don't use isinstance here; io.IOBase which has an ABC takes
    # 1000 times as long as, say, isinstance(value, str)
    elif hasattr(value, 'read'):
        value = file_generator(value)
    elif value is None:
        value = []
    return value


# GZIP 
Example #5
Source File: project.py    From angr with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _store(self, container):
        # If container is a filename.
        if isinstance(container, str):
            with open(container, 'wb') as f:
                try:
                    pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)
                except RuntimeError as e: # maximum recursion depth can be reached here
                    l.error("Unable to store Project: '%s' during pickling", e)

        # If container is an open file.
        elif isinstance(container, IOBase):
            try:
                pickle.dump(self, container, pickle.HIGHEST_PROTOCOL)
            except RuntimeError as e: # maximum recursion depth can be reached here
                l.error("Unable to store Project: '%s' during pickling", e)

        # If container is just a variable.
        else:
            try:
                container = pickle.dumps(self, pickle.HIGHEST_PROTOCOL)
            except RuntimeError as e: # maximum recursion depth can be reached here
                l.error("Unable to store Project: '%s' during pickling", e) 
Example #6
Source File: base.py    From baseband with GNU General Public License v3.0 6 votes vote down vote up
def __getstate__(self):
        state = super().__getstate__()
        fh_raw = state.pop('fh_raw')
        if self.header0.mode == 'rawdump':
            fh_raw = [[fh_raw]]

        fh_info = []
        for fh_pair in fh_raw:
            pair_info = []
            for fh in fh_pair:
                if isinstance(fh, io.IOBase):
                    pair_info.append({
                        'offset': 'closed' if fh.closed else fh.tell(),
                        'filename': fh.name,
                        'mode': fh.mode})
                else:
                    pair_info.append(fh)
            fh_info.append(pair_info)

        state['fh_info'] = fh_info
        return state 
Example #7
Source File: project.py    From angr with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _load(container):
        if isinstance(container, str):
            # If container is a filename.
            if all(c in string.printable for c in container) and os.path.exists(container):
                with open(container, 'rb') as f:
                    return pickle.load(f)

            # If container is a pickle string.
            else:
                return pickle.loads(container)

        # If container is an open file
        elif isinstance(container, IOBase):
            return pickle.load(container)

        # What else could it be?
        else:
            l.error("Cannot unpickle container of type %s", type(container))
            return None 
Example #8
Source File: test_html.py    From ciftify with MIT License 6 votes vote down vote up
def test_title_changed_to_include_image_name_when_title_given(self,
            mock_open, mock_add_header, mock_add_img_subj, mock_index,
            mock_exists, mock_writable):
        mock_file = MagicMock(spec=io.IOBase)
        mock_open.return_value.__enter__.return_value = mock_file
        mock_exists.return_value = False
        mock_writable.return_value = True
        qc_config = self.get_config_stub()

        html.write_index_pages(self.qc_dir, qc_config, self.subject,
                title='QC mode for image {}')

        for image in qc_config.images:
            name = image.name
            found = False
            for call in mock_index.call_args_list:
                if name in call[1]['title']:
                    found = True
            assert found 
Example #9
Source File: pdfkit.py    From Remarkable with MIT License 6 votes vote down vote up
def _find_options_in_meta(self, content):
        """Reads 'content' and extracts options encoded in HTML meta tags

        :param content: str or file-like object - contains HTML to parse

        returns:
          dict: {config option: value}
        """
        if (isinstance(content, io.IOBase)
                or content.__class__.__name__ == 'StreamReaderWriter'):
            content = content.read()

        found = {}

        for x in re.findall('<meta [^>]*>', content):
            if re.search('name=["\']%s' % self.configuration.meta_tag_prefix, x):
                name = re.findall('name=["\']%s([^"\']*)' %
                                  self.configuration.meta_tag_prefix, x)[0]
                found[name] = re.findall('content=["\']([^"\']*)', x)[0]

        return found 
Example #10
Source File: common.py    From pwtools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def is_seq(seq):
    """Test if `seq` is some kind of sequence, based on calling iter(seq), i.e.
    if the object is iterable.

    Exclude cases which are iterable but that we still don't like (string, file
    object). In fact, we wish to catch list, tuple, numpy array.

    Parameters
    ----------
    seq : (nested) sequence of arbitrary objects
    """
    if isinstance(seq, str) or \
       isinstance(seq, io.IOBase):
       return False
    else:
        try:
            x=iter(seq)
            return True
        except:
            return False 
Example #11
Source File: tlm.py    From AIT-Core with MIT License 6 votes vote down vote up
def load(self, content):
        """Loads Packet Definitions from the given YAML content into this
        Telemetry Dictionary.  Content may be either a filename
        containing YAML content or a YAML string.

        Load has no effect if this Command Dictionary was already
        instantiated with a filename or YAML content.
        """
        if self.filename is None:
            if os.path.isfile(content):
                self.filename = content
                stream        = open(self.filename, 'rb')
            else:
                stream        = content

            pkts = yaml.load(stream, Loader=yaml.Loader)
            pkts = handle_includes(pkts)
            for pkt in pkts:
                self.add(pkt)

            if isinstance(stream, IOBase):
                stream.close() 
Example #12
Source File: slack_client.py    From botbuilder-python with MIT License 6 votes vote down vote up
def files_upload_ex(
        self,
        file: Union[str, IOBase] = None,
        content: str = None,
        channels: [str] = None,
        title: str = None,
        initial_comment: str = None,
        file_type: str = None,
    ):
        args = {}

        if channels:
            args["channels"] = ",".join(channels)

        if title:
            args["title"] = title

        if initial_comment:
            args["initial_comment"] = initial_comment

        if file_type:
            args["filetype"] = file_type

        return await self.files_upload(file=file, content=content, **args) 
Example #13
Source File: io.py    From cyaron with GNU Lesser General Public License v3.0 6 votes vote down vote up
def close(self):
        """Delete the IO object and close the input file and the output file"""
        if self.__closed:
            # avoid double close
            return
        deleted = False
        try:
            # on posix, one can remove a file while it's opend by a process
            # the file then will be not visable to others, but process still have the file descriptor
            # it is recommand to remove temp file before close it on posix to avoid race
            # on nt, it will just fail and raise OSError so that after closing remove it again
            self.__del_files()
            deleted = True
        except OSError:
            pass
        if isinstance(self.input_file, IOBase):
            self.input_file.close()
        if isinstance(self.output_file, IOBase):
            self.output_file.close()
        if not deleted:
            self.__del_files()
        self.__closed = True 
Example #14
Source File: tarwriter.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def copy_sparse_data(input_stream, output_stream, sparse_map):
    '''Copy data blocks from input to output according to sparse_map

    :param input_stream: io.IOBase input instance
    :param output_stream: io.IOBase output instance
    :param sparse_map: iterable of (offset, size)
    '''

    buf = bytearray(BUF_SIZE)

    for chunk in sparse_map:
        input_stream.seek(chunk[0])
        left = chunk[1]
        while left:
            if left > BUF_SIZE:
                read = input_stream.readinto(buf)
                output_stream.write(buf[:read])
            else:
                buf_trailer = input_stream.read(left)
                read = len(buf_trailer)
                output_stream.write(buf_trailer)
            left -= read
            if not read:
                raise Exception('premature EOF') 
Example #15
Source File: connection.py    From python-mysql-pool with MIT License 6 votes vote down vote up
def cmd_stmt_execute(self, statement_id, data=(), parameters=(), flags=0):
        """Execute a prepared MySQL statement"""
        parameters = list(parameters)
        long_data_used = {}

        if data:
            for param_id, _ in enumerate(parameters):
                if isinstance(data[param_id], IOBase):
                    binary = True
                    try:
                        binary = 'b' not in data[param_id].mode
                    except AttributeError:
                        pass
                    self.cmd_stmt_send_long_data(statement_id, param_id,
                                                 data[param_id])
                    long_data_used[param_id] = (binary,)

        execute_packet = self._protocol.make_stmt_execute(
            statement_id, data, tuple(parameters), flags,
            long_data_used, self.charset)
        packet = self._send_cmd(ServerCmd.STMT_EXECUTE, packet=execute_packet)
        result = self._handle_binary_result(packet)
        return result 
Example #16
Source File: cmd.py    From AIT-Core with MIT License 6 votes vote down vote up
def load(self, content):
        """Loads Command Definitions from the given YAML content into
        into this Command Dictionary.  Content may be either a
        filename containing YAML content or a YAML string.

        Load has no effect if this Command Dictionary was already
        instantiated with a filename or YAML content.
        """
        if self.filename is None:
            if os.path.isfile(content):
                self.filename = content
                stream        = open(self.filename, 'rb')
            else:
                stream        = content

            cmds = yaml.load(stream, Loader=yaml.Loader)
            cmds = handle_includes(cmds)
            for cmd in cmds:
                self.add(cmd)

            if isinstance(stream, IOBase):
                stream.close() 
Example #17
Source File: limits.py    From AIT-Core with MIT License 6 votes vote down vote up
def load(self, content):
        """Loads Limit Definitions from the given YAML content into this
        Telemetry Dictionary.  Content may be either a filename
        containing YAML content or a YAML string.

        Load has no effect if this Limits Dictionary was already
        instantiated with a filename or YAML content.
        """
        if self.filename is None:
            if os.path.isfile(content):
                self.filename = content
                stream        = open(self.filename, 'rb')
            else:
                stream        = content
            
            limits = yaml.load(stream, Loader=yaml.Loader)

            for lmt in limits:
                self.add(lmt)

            if isinstance(stream, IOBase):
                stream.close() 
Example #18
Source File: cfg.py    From AIT-Core with MIT License 6 votes vote down vote up
def loadYAML (filename=None, data=None):
    """Loads either the given YAML configuration file or YAML data.

    Returns None if there was an error reading from the configuration
    file and logs an error message via ait.core.log.error().
    """
    config = None

    try:
        if filename:
            data = open(filename, 'rt')

        config = yaml.load(data, Loader=yaml.Loader)

        if isinstance(data, IOBase):
            data.close()
    except IOError as e:
        msg = 'Could not read AIT configuration file "%s": %s'
        log.error(msg, filename, str(e))

    return config 
Example #19
Source File: bgzf.py    From bamnostic with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, filepath_or_object, mode="wb", compresslevel=6):
        """Initialize the class."""
        if isinstance(filepath_or_object, io.IOBase):
            handle = filepath_or_object
        else:
            handle = open(filepath_or_object, mode=mode)
        """
        if fileobj:
            assert filename is None
            handle = fileobj
        else:
            if "w" not in mode.lower() and "a" not in mode.lower():
                raise ValueError("Must use write or append mode, not %r" % mode)
            if "a" in mode.lower():
                handle = open(filename, "ab")
            else:
                handle = open(filename, "wb")
        """
        self._text = "b" not in mode.lower()
        self._handle = handle
        self._buffer = b""
        self.compresslevel = compresslevel 
Example #20
Source File: listeners.py    From molotov with Apache License 2.0 6 votes vote down vote up
def _body2str(self, body):
        try:
            from aiohttp.payload import Payload
        except ImportError:
            Payload = None

        if Payload is not None and isinstance(body, Payload):
            body = body._value

        if isinstance(body, io.IOBase):
            return _FILE

        if not isinstance(body, str):
            try:
                body = str(body, "utf8")
            except UnicodeDecodeError:
                return _UNREADABLE

        return body 
Example #21
Source File: response.py    From chinese-support-redux with GNU General Public License v3.0 5 votes vote down vote up
def closed(self):
        if not self.auto_close:
            return io.IOBase.closed.__get__(self)
        elif self._fp is None:
            return True
        elif hasattr(self._fp, "isclosed"):
            return self._fp.isclosed()
        elif hasattr(self._fp, "closed"):
            return self._fp.closed
        else:
            return True 
Example #22
Source File: _compression.py    From ms_deisotope with Apache License 2.0 5 votes vote down vote up
def test_if_file_has_fast_random_access(file_obj):
    """Determine whether or not the passed file-like
    object supports fast random access.

    Parameters
    ----------
    file_obj : :class:`io.IOBase`
        The file-like object to test

    Returns
    -------
    bool
    """
    # If we have a normal gzip.GzipFile, then we definitely don't have fast random access
    if isinstance(file_obj, tuple(slow_random_access_file_types)):
        return DefinitelyNotFastRandomAccess
    # If we have an idzip.IdzipFile, then we need to query its _impl attribute (QUESTION: Should this be made
    # a part of the IdzipFile API and be pushed upstream?)
    elif has_idzip and isinstance(file_obj, GzipFile):
        # If the _impl attribute is an ordinary gzip.GzipFile, then it is just a normal gzip file without
        # an index.
        if isinstance(file_obj._impl, _GzipFile):
            return DefinitelyNotFastRandomAccess
        # Otherwise it's an idzip file and we can use fast random access
        return DefinitelyFastRandomAccess
    else:
        # We're looking at a file-like object of some sort. It could be a compressed file not caught
        # by the earlier checks. The only good test would be to examine the file's raw contents, but
        # this is not an option here. Assume that we're looking at an uncompressed stream.
        if isinstance(file_obj, file_like_object_bases):
            return DefinitelyFastRandomAccess
        # It's not a file of any sort. It could be a regular file, it could be something else entirely.
        return MaybeFastRandomAccess 
Example #23
Source File: test_streaming_client_encryption_stream.py    From aws-encryption-sdk-python with Apache License 2.0 5 votes vote down vote up
def _mock_source_stream(self):
        mock_source_stream = MagicMock()
        mock_source_stream.__class__ = io.IOBase
        mock_source_stream.tell.side_effect = (10, 500)
        return mock_source_stream 
Example #24
Source File: featureseq.py    From pySCENIC with GNU General Public License v3.0 5 votes vote down vote up
def from_bed_file(file: Union[str, Type[io.IOBase]], transform=lambda x: x):
        if isinstance(file, io.IOBase):
            def _feature_iterator():
                for line in file:
                    yield Feature.from_string(line, transform)
        else:
            def _feature_iterator():
                with open(file, 'r') as f:
                    for line in f:
                        yield Feature.from_string(line, transform)

        return FeatureSeq(_feature_iterator()) 
Example #25
Source File: target.py    From colin with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, target, **_):
        super().__init__()
        self.target_name = target
        logger.debug("Target is a dockerfile.")
        if isinstance(target, io.IOBase):
            logger.debug("Target is a dockerfile loaded from the file-like object.")
            self.instance = DockerfileParser(fileobj=target)
        else:
            self.instance = DockerfileParser(fileobj=open(target)) 
Example #26
Source File: test_helpers.py    From farcy with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_get_session__from_credentials_file__handled_exception(
            self, mock_path, mock_open, mock_is_starred, mock_stderr,
            mock_prompt, mock_getpass, mock_authorize):
        mock_path.expanduser.return_value = 'mock_path'
        mock_path.isfile.return_value = True
        mock_open.return_value = MagicMock(spec=IOBase)

        mock_response = MockResponse(content='', status_code=401)
        mock_is_starred.side_effect = GitHubError(mock_response)
        self.assertTrue(isinstance(helpers.get_session(), GitHub))
        self.assertTrue(mock_stderr.write.called)
        self.assertTrue(mock_prompt.called)
        self.assertTrue(mock_getpass.called)
        self.assertTrue(mock_open.called) 
Example #27
Source File: config.py    From KL-Loss with Apache License 2.0 5 votes vote down vote up
def load_cfg(cfg_to_load):
    """Wrapper around yaml.load used for maintaining backward compatibility"""
    file_types = [file, io.IOBase] if six.PY2 else [io.IOBase]  # noqa false positive
    expected_types = tuple(file_types + list(six.string_types))
    assert isinstance(cfg_to_load, expected_types), \
        'Expected one of {}, got {}'.format(expected_types, type(cfg_to_load))
    if isinstance(cfg_to_load, tuple(file_types)):
        cfg_to_load = ''.join(cfg_to_load.readlines())
    for old_module, new_module in iteritems(_RENAMED_MODULES):
        # yaml object encoding: !!python/object/new:<module>.<object>
        old_module, new_module = 'new:' + old_module, 'new:' + new_module
        cfg_to_load = cfg_to_load.replace(old_module, new_module)
    # Import inline due to a circular dependency between env.py and config.py
    import detectron.utils.env as envu
    return envu.yaml_load(cfg_to_load) 
Example #28
Source File: test_data.py    From marvin-python-toolbox with Apache License 2.0 5 votes vote down vote up
def test_load_data_from_filesystem(data_path_key, data_path):
    data = 'return value'

    # If the data was not found try to load from filesystem
    with mock.patch('marvin_python_toolbox.common.data.open', create=True) as mock_open:
        mock_open.return_value = mock.MagicMock(spec=IOBase)
        mocked_fp = mock_open.return_value.__enter__.return_value
        mocked_fp.read.return_value = data
        content = MarvinData.load_data(os.path.join('named_features', 'brands.json'))

    mocked_fp.read.assert_called_once()
    assert content == data 
Example #29
Source File: vocabulary.py    From fastNLP with Apache License 2.0 5 votes vote down vote up
def save(self, filepath):
        r"""

        :param str,io.StringIO filepath: Vocabulary的储存路径
        :return:
        """
        if isinstance(filepath, io.IOBase):
            assert filepath.writable()
            f = filepath
        elif isinstance(filepath, str):
            try:
                f = open(filepath, 'w', encoding='utf-8')
            except Exception as e:
                raise e
        else:
            raise TypeError("Illegal `filepath`.")

        f.write(f'max_size\t{self.max_size}\n')
        f.write(f'min_freq\t{self.min_freq}\n')
        f.write(f'unknown\t{self.unknown}\n')
        f.write(f'padding\t{self.padding}\n')
        f.write(f'rebuild\t{self.rebuild}\n')
        f.write('\n')
        # idx: 如果idx为-2, 说明还没有进行build; 如果idx为-1,说明该词未编入
        # no_create_entry: 如果为1,说明该词是no_create_entry; 0 otherwise
        # word \t count \t idx \t no_create_entry \n
        idx = -2
        for word, count in self.word_count.items():
            if self._word2idx is not None:
                idx = self._word2idx.get(word, -1)
            is_no_create_entry = int(self._is_word_no_create_entry(word))
            f.write(f'{word}\t{count}\t{idx}\t{is_no_create_entry}\n')
        if isinstance(filepath, str):  # 如果是file的话就关闭
            f.close() 
Example #30
Source File: core.py    From pass-import with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, prefix=None):
        """Asset manager initialisation.

        :param prefix: (optional) Path, identifiant of the pm. It can also
            be a file object. Therefore, the passwords are read from it.
        :param dict settings: (optional) Additional settings for the pm.

        """
        self.prefix = None
        self.file = None
        if isinstance(prefix, io.IOBase):
            self.file = prefix
        else:
            self.prefix = prefix
        super(Asset, self).__init__()