Python typing.BinaryIO() Examples

The following are 30 code examples of typing.BinaryIO(). 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 typing , or try the search function .
Example #1
Source File: datastructures.py    From quart with MIT License 6 votes vote down vote up
def save(self, destination: BinaryIO, buffer_size: int = 16384) -> None:
        """Save the file to the destination.

        Arguments:
            destination: A filename (str) or file object to write to.
            buffer_size: Buffer size as used as length in
                :func:`shutil.copyfileobj`.
        """
        close_destination = False
        if isinstance(destination, str):
            destination = open(destination, "wb")
            close_destination = True
        try:
            copyfileobj(self.stream, destination, buffer_size)  # type: ignore
        finally:
            if close_destination:
                destination.close() 
Example #2
Source File: datastructures.py    From quart with MIT License 6 votes vote down vote up
def __init__(
        self,
        stream: BinaryIO = None,
        filename: str = None,
        name: str = None,
        content_type: str = None,
        headers: Dict = None,
    ) -> None:
        self.name = name
        self.stream = stream or io.BytesIO()
        self.filename = filename
        if headers is None:
            headers = {}
        self.headers = headers
        if content_type is not None:
            headers["Content-Type"] = content_type 
Example #3
Source File: dash_utils.py    From dash-masternode-tool with MIT License 6 votes vote down vote up
def read_varint_from_file(fptr: typing.BinaryIO) -> int:
    buffer = fptr.read(1)
    if (buffer[0] < 0xfd):
        value_size = 1
        value = buffer[0]
    elif (buffer[0] == 0xfd):
        value_size = 2
        buffer = fptr.read(value_size)
        value = int.from_bytes(buffer[0: 2], byteorder='little')
    elif (buffer[0] == 0xfe):
        value_size = 4
        buffer = fptr.read(value_size)
        value = int.from_bytes(buffer[0: 4], byteorder='little')
    elif (buffer[0] == 0xff):
        value_size = 8
        buffer = fptr.read(value_size)
        value = int.from_bytes(buffer[0: 8], byteorder='little')
    else:
        raise Exception("Invalid varint size")
    if value_size != len(buffer):
        raise ValueError('File end before read completed.')
    return value 
Example #4
Source File: socket.py    From stem with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _write_to_socket(socket_file: BinaryIO, message: Union[str, bytes]) -> None:
  try:
    socket_file.write(stem.util.str_tools._to_bytes(message))
    socket_file.flush()
  except socket.error as exc:
    log.info('Failed to send: %s' % exc)

    # When sending there doesn't seem to be a reliable method for
    # distinguishing between failures from a disconnect verses other things.
    # Just accounting for known disconnection responses.

    if str(exc) == '[Errno 32] Broken pipe':
      raise stem.SocketClosed(exc)
    else:
      raise stem.SocketError(exc)
  except AttributeError:
    # if the control_file has been closed then flush will receive:
    # AttributeError: 'NoneType' object has no attribute 'sendall'

    log.info('Failed to send: file has been closed')
    raise stem.SocketClosed('file has been closed') 
Example #5
Source File: search_tools.py    From HoneyBot with MIT License 6 votes vote down vote up
def search_by_pcap(self, pcap_file_obj: typing.BinaryIO) -> requests.Response:
        """
        Search by a pcap/pcapng file, get list list of similar packet captures

        :param pcap_file_obj: A file like object that provides a .read() interface (E.G open('path_to_pcap.pcap, 'rb') )
        :return: A request.Response instance, containing a graph of similar pcaps with matched terms
        """
        response = super().analyze(pcap_file_obj)
        if response.status_code == 200:
            sim_response = super().pcap_similar(response.json()['pcap_metadata']['md5'])
        elif response.status_code == 202:
            pcap_id = response.json()['id']
            info_response = super().pcap_info(pcap_id)
            while info_response.status_code == 404:
                print('[{}] Waiting for {} to finish analyzing.'.format(datetime.utcnow(), pcap_id))
                info_response = super().pcap_info(response.json()['id'])
                time.sleep(10)
            print('[{}] Fetching results for {}.'.format(datetime.utcnow(), pcap_id))
            time.sleep(5)
            sim_response = super().pcap_similar(response.json()['id'])
        else:
            return response
        return sim_response 
Example #6
Source File: file_type.py    From gordo with GNU Affero General Public License v3.0 6 votes vote down vote up
def read_df(self, f: BinaryIO) -> pd.DataFrame:
        columns = self.time_series_columns.columns
        datetime_column = self.time_series_columns.datetime_column
        df = pd.read_parquet(f, engine="pyarrow", columns=columns).set_index(
            datetime_column
        )
        df.index = pd.to_datetime(df.index, utc=True)
        return df 
Example #7
Source File: networkstatus.py    From stem with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _parse_file_detached_sigs(detached_signature_file: BinaryIO, validate: bool = False) -> Iterator['stem.descriptor.networkstatus.DetachedSignature']:
  """
  Parses a file containing one or more detached signatures.

  :param detached_signature_file: file with detached signatures
  :param validate: checks the validity of the detached signature's
    contents if **True**, skips these checks otherwise

  :returns: iterator for :class:`stem.descriptor.networkstatus.DetachedSignature`
    instances in the file

  :raises:
    * **ValueError** if the detached signatures are invalid and validate is **True**
    * **IOError** if the file can't be read
  """

  while True:
    detached_sig_content = _read_until_keywords('consensus-digest', detached_signature_file, ignore_first = True)

    if detached_sig_content:
      yield stem.descriptor.networkstatus.DetachedSignature(bytes.join(b'', detached_sig_content), validate = validate)
    else:
      break  # done parsing file 
Example #8
Source File: graphml.py    From pybel with MIT License 6 votes vote down vote up
def to_graphml(graph: BELGraph, path: Union[str, BinaryIO], schema: Optional[str] = None) -> None:
    """Write a graph to a GraphML XML file using :func:`networkx.write_graphml`.

    :param graph: BEL Graph
    :param path: Path to the new exported file
    :param schema: Type of export. Currently supported: "simple" and "umbrella".

    The .graphml file extension is suggested so Cytoscape can recognize it.
    By default, this function exports using the PyBEL schema of including modifier information into the edges.
    As an alternative, this function can also distinguish between
    """
    if schema is None or schema == 'simple':
        rv = _to_graphml_simple(graph)
    elif schema == 'umbrella':
        rv = _to_graphml_umbrella(graph)
    else:
        raise ValueError('Unhandled schema: {}'.format(schema))

    nx.write_graphml(rv, path) 
Example #9
Source File: binary.py    From pywasm with MIT License 6 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = Expression()
        d = 1
        while True:
            i = Instruction.from_reader(r)
            if not i:
                break
            o.data.append(i)
            if i.opcode in [instruction.block, instruction.loop, instruction.if_]:
                d += 1
            if i.opcode == instruction.end:
                d -= 1
            if d == 0:
                break
        if o.data[-1].opcode != instruction.end:
            raise Exception('pywasm: expression did not end with 0xb')
        o.position = cls.mark(o.data)
        return o 
Example #10
Source File: socket.py    From stem with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _send(self, message: Union[bytes, str], handler: Callable[[Union[socket.socket, ssl.SSLSocket], BinaryIO, Union[bytes, str]], None]) -> None:
    """
    Send message in a thread safe manner. Handler is expected to be of the form...

    ::

      my_handler(socket, socket_file, message)
    """

    with self._send_lock:
      try:
        if not self.is_alive():
          raise stem.SocketClosed()

        handler(self._socket, self._socket_file, message)
      except stem.SocketClosed:
        # if send_message raises a SocketClosed then we should properly shut
        # everything down

        if self.is_alive():
          self.close()

        raise 
Example #11
Source File: bandwidth_file.py    From stem with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _parse_file(descriptor_file: BinaryIO, validate: bool = False, **kwargs: Any) -> Iterator['stem.descriptor.bandwidth_file.BandwidthFile']:
  """
  Iterates over the bandwidth authority metrics in a file.

  :param descriptor_file: file with descriptor content
  :param validate: checks the validity of the descriptor's content if
    **True**, skips these checks otherwise
  :param kwargs: additional arguments for the descriptor constructor

  :returns: :class:`stem.descriptor.bandwidth_file.BandwidthFile` object

  :raises:
    * **ValueError** if the contents is malformed and validate is **True**
    * **IOError** if the file can't be read
  """

  if kwargs:
    raise ValueError('BUG: keyword arguments unused by bandwidth files')

  yield BandwidthFile(descriptor_file.read(), validate) 
Example #12
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        return TypeIndex(leb128.u.decode_reader(r)[0]) 
Example #13
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = StartSection()
        o.start = StartFunction.from_reader(r)
        return o 
Example #14
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        return TableIndex(leb128.u.decode_reader(r)[0]) 
Example #15
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        return MemoryIndex(leb128.u.decode_reader(r)[0]) 
Example #16
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        return GlobalIndex(leb128.u.decode_reader(r)[0]) 
Example #17
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = Export()
        o.name = bytearray(r.read(leb128.u.decode_reader(r)[0])).decode()
        o.type = ord(r.read(1))
        o.desc = {
            convention.extern_function: FunctionIndex.from_reader,
            convention.extern_table: TableIndex.from_reader,
            convention.extern_memory: MemoryIndex.from_reader,
            convention.extern_global: GlobalIndex.from_reader,
        }[o.type](r)
        return o 
Example #18
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = StartFunction()
        o.function_idx = FunctionIndex.from_reader(r)
        return o 
Example #19
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = ExportSection()
        n = leb128.u.decode_reader(r)[0]
        o.data = [Export.from_reader(r) for _ in range(n)]
        return o 
Example #20
Source File: filesystem.py    From pex with Apache License 2.0 5 votes vote down vote up
def file(self):
            # type: () -> BinaryIO
            pass 
Example #21
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        return BlockType(ord(r.read(1))) 
Example #22
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = GlobalType()
        o.value_type = ValueType.from_reader(r)
        o.mut = Mut.from_reader(r)
        return o 
Example #23
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        return Mut(ord(r.read(1))) 
Example #24
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = TableType()
        o.element_type = ElementType.from_reader(r)
        o.limits = Limits.from_reader(r)
        return o 
Example #25
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = MemoryType()
        o.limits = Limits.from_reader(r)
        return o 
Example #26
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = Limits()
        flag = ord(r.read(1))
        o.n = leb128.u.decode_reader(r)[0]
        o.m = leb128.u.decode_reader(r)[0] if flag else 0x00
        return o 
Example #27
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = FunctionType()
        assert ord(r.read(1)) == convention.sign
        o.args = ResultType.from_reader(r)
        o.rets = ResultType.from_reader(r)
        return o 
Example #28
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = ResultType()
        n = leb128.u.decode_reader(r)[0]
        o.data = [ValueType.from_reader(r) for i in range(n)]
        return o 
Example #29
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        return ValueType(ord(r.read(1))) 
Example #30
Source File: binary.py    From pywasm with MIT License 5 votes vote down vote up
def from_reader(cls, r: typing.BinaryIO):
        o = CodeSection()
        n = leb128.u.decode_reader(r)[0]
        o.data = [Code.from_reader(r) for _ in range(n)]
        return o