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: encoding.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #2
Source File: slack_client.py From botbuilder-python with MIT License | 6 votes |
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 #3
Source File: test_html.py From ciftify with MIT License | 6 votes |
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 #4
Source File: base.py From baseband with GNU General Public License v3.0 | 6 votes |
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 #5
Source File: base.py From baseband with GNU General Public License v3.0 | 6 votes |
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 #6
Source File: base.py From baseband with GNU General Public License v3.0 | 6 votes |
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: listeners.py From molotov with Apache License 2.0 | 6 votes |
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 #8
Source File: bgzf.py From bamnostic with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #9
Source File: connection.py From python-mysql-pool with MIT License | 6 votes |
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 #10
Source File: io.py From cyaron with GNU Lesser General Public License v3.0 | 6 votes |
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 #11
Source File: connection.py From plugin.video.netflix with MIT License | 6 votes |
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 #12
Source File: tarwriter.py From qubes-core-admin with GNU Lesser General Public License v2.1 | 6 votes |
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 #13
Source File: cmd.py From AIT-Core with MIT License | 6 votes |
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 #14
Source File: limits.py From AIT-Core with MIT License | 6 votes |
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 #15
Source File: cfg.py From AIT-Core with MIT License | 6 votes |
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 #16
Source File: tlm.py From AIT-Core with MIT License | 6 votes |
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 #17
Source File: common.py From pwtools with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #18
Source File: project.py From angr with BSD 2-Clause "Simplified" License | 6 votes |
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 #19
Source File: project.py From angr with BSD 2-Clause "Simplified" License | 6 votes |
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 #20
Source File: pdfkit.py From Remarkable with MIT License | 6 votes |
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 #21
Source File: test_html.py From ciftify with MIT License | 5 votes |
def test_writes_images_to_index(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) expected_writes = len(qc_config.images) assert mock_index.call_count == expected_writes
Example #22
Source File: test_html.py From ciftify with MIT License | 5 votes |
def test_doesnt_write_images_if_make_index_is_false(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 qc_config = self.get_config_stub(make_all=False) mock_exists.return_value = False mock_writable.return_value = True html.write_index_pages(self.qc_dir, qc_config, self.subject) # One image in the list should have 'make_index' = False expected_writes = len(qc_config.images) - 1 assert mock_index.call_count == expected_writes
Example #23
Source File: Requester.py From gist-alfred with MIT License | 5 votes |
def __requestRaw(self, cnx, verb, url, requestHeaders, input): original_cnx = cnx if cnx is None: cnx = self.__createConnection() cnx.request( verb, url, input, requestHeaders ) response = cnx.getresponse() status = response.status responseHeaders = dict((k.lower(), v) for k, v in response.getheaders()) output = response.read() cnx.close() if input: if isinstance(input, IOBase): input.close() self.__log(verb, url, requestHeaders, input, status, responseHeaders, output) if status == 202 and (verb == 'GET' or verb == 'HEAD'): # only for requests that are considered 'safe' in RFC 2616 time.sleep(Consts.PROCESSING_202_WAIT_TIME) return self.__requestRaw(original_cnx, verb, url, requestHeaders, input) if status == 301 and 'location' in responseHeaders: o = urlparse.urlparse(responseHeaders['location']) return self.__requestRaw(original_cnx, verb, o.path, requestHeaders, input) return status, responseHeaders, output
Example #24
Source File: utils.py From Telethon with MIT License | 5 votes |
def _get_extension(file): """ Gets the extension for the given file, which can be either a str or an ``open()``'ed file (which has a ``.name`` attribute). """ if isinstance(file, str): return os.path.splitext(file)[-1] elif isinstance(file, pathlib.Path): return file.suffix elif isinstance(file, bytes): kind = imghdr.what(io.BytesIO(file)) return ('.' + kind) if kind else '' elif isinstance(file, io.IOBase) and not isinstance(file, io.TextIOBase) and file.seekable(): kind = imghdr.what(file) return ('.' + kind) if kind is not None else '' elif getattr(file, 'name', None): # Note: ``file.name`` works for :tl:`InputFile` and some `IOBase` return _get_extension(file.name) else: # Maybe it's a Telegram media return get_extension(file)
Example #25
Source File: sequentialfile.py From baseband with GNU General Public License v3.0 | 5 votes |
def __getstate__(self): 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., another SequentialFile), so we will assume # it takes care of this itself. if isinstance(self.fh, io.IOBase): fh = state.pop('fh') state['fh_info'] = { 'file_nr': state.pop('file_nr'), 'offset': 'closed' if fh.closed else fh.tell()} return state
Example #26
Source File: response.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def close(self): if not self.closed: self._fp.close() if self._connection: self._connection.close() if not self.auto_close: io.IOBase.close(self)
Example #27
Source File: response.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
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 #28
Source File: response.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def close(self): if not self.closed: self._fp.close() if self._connection: self._connection.close() if not self.auto_close: io.IOBase.close(self)
Example #29
Source File: response.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
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 #30
Source File: test_upload.py From md2notion with MIT License | 5 votes |
def test_filesFromPathUrl_with_file(): '''it can get a file name, path, and file object from a file''' #arrange/act filePath, fileName, file = next(filesFromPathsUrls(['tests/TEST.md'])) #assert assert fileName == 'TEST.md' assert filePath == 'tests/TEST.md' assert isinstance(file, IOBase)