Python cStringIO.OutputType() Examples

The following are 11 code examples of cStringIO.OutputType(). 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 cStringIO , or try the search function .
Example #1
Source File: styles.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def unpickleStringO(val, sek):
    """
    Convert the output of L{pickleStringO} into an appropriate type for the
    current python version.  This may be called on Python 3 and will convert a
    cStringIO into an L{io.StringIO}.

    @param val: The content of the file.
    @type val: L{bytes}

    @param sek: The seek position of the file.
    @type sek: L{int}

    @return: a file-like object which you can write bytes to.
    @rtype: L{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3.
    """
    x = _cStringIO()
    x.write(val)
    x.seek(sek)
    return x 
Example #2
Source File: styles.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def unpickleStringI(val, sek):
    """
    Convert the output of L{pickleStringI} into an appropriate type for the
    current Python version.

    This may be called on Python 3 and will convert a cStringIO into an
    L{io.StringIO}.

    @param val: The content of the file.
    @type val: L{bytes}

    @param sek: The seek position of the file.
    @type sek: L{int}

    @return: a file-like object which you can read bytes from.
    @rtype: L{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3.
    """
    x = _cStringIO(val)
    x.seek(sek)
    return x 
Example #3
Source File: styles.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def unpickleStringO(val, sek):
    """
    Convert the output of L{pickleStringO} into an appropriate type for the
    current python version.  This may be called on Python 3 and will convert a
    cStringIO into an L{io.StringIO}.

    @param val: The content of the file.
    @type val: L{bytes}

    @param sek: The seek position of the file.
    @type sek: L{int}

    @return: a file-like object which you can write bytes to.
    @rtype: L{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3.
    """
    x = _cStringIO()
    x.write(val)
    x.seek(sek)
    return x 
Example #4
Source File: styles.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def unpickleStringI(val, sek):
    """
    Convert the output of L{pickleStringI} into an appropriate type for the
    current Python version.

    This may be called on Python 3 and will convert a cStringIO into an
    L{io.StringIO}.

    @param val: The content of the file.
    @type val: L{bytes}

    @param sek: The seek position of the file.
    @type sek: L{int}

    @return: a file-like object which you can read bytes from.
    @rtype: L{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3.
    """
    x = _cStringIO(val)
    x.seek(sek)
    return x 
Example #5
Source File: adb_commands.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def Pull(self, device_filename, dest_file=None, timeout_ms=None):
    """Pull a file from the device.

    Arguments:
      device_filename: The filename on the device to pull.
      dest_file: If set, a filename or writable file-like object.
      timeout_ms: Expected timeout for any part of the pull.

    Returns:
      The file data if dest_file is not set.
    """
    if isinstance(dest_file, basestring):
      dest_file = open(dest_file, 'w')
    elif not dest_file:
      dest_file = cStringIO.StringIO()
    connection = self.conn.Open(
        destination='sync:', timeout_ms=timeout_ms)
    filesync_protocol.FilesyncProtocol.Pull(
        connection, device_filename, dest_file)
    connection.Close()
    # An empty call to cStringIO.StringIO returns an instance of
    # cStringIO.OutputType.
    if isinstance(dest_file, cStringIO.OutputType):
      return dest_file.getvalue() 
Example #6
Source File: styles.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def pickleStringO(stringo):
    """
    Reduce the given cStringO.

    This is only called on Python 2, because the cStringIO module only exists
    on Python 2.

    @param stringo: The string output to pickle.
    @type stringo: L{cStringIO.OutputType}
    """
    'support function for copy_reg to pickle StringIO.OutputTypes'
    return unpickleStringO, (stringo.getvalue(), stringo.tell()) 
Example #7
Source File: sh.py    From scylla with Apache License 2.0 5 votes vote down vote up
def determine_how_to_feed_output(handler, encoding, decode_errors):
    if callable(handler):
        process, finish = get_callback_chunk_consumer(handler, encoding,
                decode_errors)

    # in py3, this is used for bytes
    elif isinstance(handler, (cStringIO, iocStringIO)):
        process, finish = get_cstringio_chunk_consumer(handler)

    # in py3, this is used for unicode
    elif isinstance(handler, (StringIO, ioStringIO)):
        process, finish = get_stringio_chunk_consumer(handler, encoding,
                decode_errors)

    elif hasattr(handler, "write"):
        process, finish = get_file_chunk_consumer(handler)

    else:
        try:
            handler = int(handler)
        except (ValueError, TypeError):
            process = lambda chunk: False
            finish = lambda: None
        else:
            process, finish = get_fd_chunk_consumer(handler)

    return process, finish 
Example #8
Source File: styles.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def pickleStringO(stringo):
    """
    Reduce the given cStringO.

    This is only called on Python 2, because the cStringIO module only exists
    on Python 2.

    @param stringo: The string output to pickle.
    @type stringo: L{cStringIO.OutputType}
    """
    'support function for copy_reg to pickle StringIO.OutputTypes'
    return unpickleStringO, (stringo.getvalue(), stringo.tell()) 
Example #9
Source File: mailbox.py    From bitmask-dev with GNU General Public License v3.0 4 votes vote down vote up
def addMessage(self, message, flags, date=None):
        """
        Adds a message to this mailbox.

        :param message: the raw message
        :type message: str

        :param flags: flag list
        :type flags: list of str

        :param date: timestamp
        :type date: str, or None

        :return: a deferred that will be triggered with the UID of the added
                 message.
        """
        # TODO should raise ReadOnlyMailbox if not rw.
        # TODO have a look at the cases for internal date in the rfc
        # XXX we could treat the message as an IMessage from here

        # TODO -- fast appends should be definitely solved by Blobs.
        # A better solution will probably involve implementing MULTIAPPEND
        # extension or patching imap server to support pipelining.

        if isinstance(message,
                      (cStringIO.OutputType, StringIO.StringIO, io.BytesIO)):
            message = message.getvalue()

        leap_assert_type(message, basestring)

        if flags is None:
            flags = tuple()
        else:
            flags = tuple(str(flag) for flag in flags)

        if date is None:
            date = formatdate(time.time())

        d = self.collection.add_msg(message, flags, date=date)
        d.addCallback(lambda message: message.get_uid())
        d.addErrback(
            lambda failure: self.log.failure('Error while adding msg'))
        return d 
Example #10
Source File: compat.py    From Safejumper-for-Desktop with GNU General Public License v2.0 4 votes vote down vote up
def ioType(fileIshObject, default=unicode):
    """
    Determine the type which will be returned from the given file object's
    read() and accepted by its write() method as an argument.

    In other words, determine whether the given file is 'opened in text mode'.

    @param fileIshObject: Any object, but ideally one which resembles a file.
    @type fileIshObject: L{object}

    @param default: A default value to return when the type of C{fileIshObject}
        cannot be determined.
    @type default: L{type}

    @return: There are 3 possible return values:

            1. L{unicode}, if the file is unambiguously opened in text mode.

            2. L{bytes}, if the file is unambiguously opened in binary mode.

            3. L{basestring}, if we are on python 2 (the L{basestring} type
               does not exist on python 3) and the file is opened in binary
               mode, but has an encoding and can therefore accept both bytes
               and text reliably for writing, but will return L{bytes} from
               read methods.

            4. The C{default} parameter, if the given type is not understood.

    @rtype: L{type}
    """
    if isinstance(fileIshObject, TextIOBase):
        # If it's for text I/O, then it's for text I/O.
        return unicode
    if isinstance(fileIshObject, IOBase):
        # If it's for I/O but it's _not_ for text I/O, it's for bytes I/O.
        return bytes
    encoding = getattr(fileIshObject, 'encoding', None)
    import codecs
    if isinstance(fileIshObject, (codecs.StreamReader, codecs.StreamWriter)):
        # On StreamReaderWriter, the 'encoding' attribute has special meaning;
        # it is unambiguously unicode.
        if encoding:
            return unicode
        else:
            return bytes
    if not _PY3:
        # Special case: if we have an encoding file, we can *give* it unicode,
        # but we can't expect to *get* unicode.
        if isinstance(fileIshObject, file):
            if encoding is not None:
                return basestring
            else:
                return bytes
        from cStringIO import InputType, OutputType
        from StringIO import StringIO
        if isinstance(fileIshObject, (StringIO, InputType, OutputType)):
            return bytes
    return default 
Example #11
Source File: compat.py    From learn_python3_spider with MIT License 4 votes vote down vote up
def ioType(fileIshObject, default=unicode):
    """
    Determine the type which will be returned from the given file object's
    read() and accepted by its write() method as an argument.

    In other words, determine whether the given file is 'opened in text mode'.

    @param fileIshObject: Any object, but ideally one which resembles a file.
    @type fileIshObject: L{object}

    @param default: A default value to return when the type of C{fileIshObject}
        cannot be determined.
    @type default: L{type}

    @return: There are 3 possible return values:

            1. L{unicode}, if the file is unambiguously opened in text mode.

            2. L{bytes}, if the file is unambiguously opened in binary mode.

            3. L{basestring}, if we are on python 2 (the L{basestring} type
               does not exist on python 3) and the file is opened in binary
               mode, but has an encoding and can therefore accept both bytes
               and text reliably for writing, but will return L{bytes} from
               read methods.

            4. The C{default} parameter, if the given type is not understood.

    @rtype: L{type}
    """
    if isinstance(fileIshObject, TextIOBase):
        # If it's for text I/O, then it's for text I/O.
        return unicode
    if isinstance(fileIshObject, IOBase):
        # If it's for I/O but it's _not_ for text I/O, it's for bytes I/O.
        return bytes
    encoding = getattr(fileIshObject, 'encoding', None)
    import codecs
    if isinstance(fileIshObject, (codecs.StreamReader, codecs.StreamWriter)):
        # On StreamReaderWriter, the 'encoding' attribute has special meaning;
        # it is unambiguously unicode.
        if encoding:
            return unicode
        else:
            return bytes
    if not _PY3:
        # Special case: if we have an encoding file, we can *give* it unicode,
        # but we can't expect to *get* unicode.
        if isinstance(fileIshObject, file):
            if encoding is not None:
                return basestring
            else:
                return bytes
        from cStringIO import InputType, OutputType
        from StringIO import StringIO
        if isinstance(fileIshObject, (StringIO, InputType, OutputType)):
            return bytes
    return default