Пример #1
0
def pypsi_print(*args,
                sep=' ',
                end='\n',
                file=None,
                flush=True,
                width=None,
                wrap=True):
    '''
    Wraps the functionality of the Python builtin `print` function. The
    :meth:`pypsi.shell.Shell.bootstrap` overrides the Python :meth:`print`
    function with :meth:`pypsi_print`.

    :param str sep: string to print between arguments
    :param str end: string to print at the end of the output
    :param file file: output stream, if this is :const:`None`, the default is
        :data:`sys.stdout`
    :param bool flush: whether to flush the output stream
    :param int width: override the stream's width
    :param bool wrap: whether to word wrap the output
    '''
    file = file or sys.stdout
    last = len(args) - 1

    if wrap and hasattr(file, 'width') and file.width:
        width = width or file.width
        parts = []
        for arg in args:
            if isinstance(arg, str):
                parts.append(arg)
            elif arg is None:
                parts.append('')
            elif isinstance(arg, AnsiCode):
                if file.isatty():
                    parts.append(str(arg))
            else:
                parts.append(str(arg))

        txt = sep.join(parts)
        lineno = 0
        for (line, endl) in get_lines(txt):
            #file.write("Line: "+line+' ['+str(endl)+']\n')
            if line:
                first = True
                wrapno = 0
                for wrapped in wrap_line(line, width):
                    if not wrapped:
                        continue
                    #file.write("Wrapped: '" + wrapped+"'\n")
                    wrapno += 1
                    if not first:
                        file.write('\n')
                    else:
                        first = False
                    file.write(wrapped)

            if not line or endl:
                #file.write("NO Line\n")
                file.write('\n')
    else:
        last = len(args) - 1
        for (i, arg) in enumerate(args):
            file.write(str(arg))
            if sep and i != last:
                file.write(sep)

    if end:
        file.write(end)
    if flush:
        file.flush()
Пример #2
0
 def test_get_lines_trailing_endl(self):
     assert list(fmt.get_lines('hello\n')) == [('hello', True)]
Пример #3
0
 def test_get_lines_multi(self):
     assert list(fmt.get_lines("hello\nnew line")) == [('hello', True),
                                                       ('new line', False)]
Пример #4
0
 def test_get_lines_single(self):
     assert list(fmt.get_lines('hello')) == [('hello', False)]
Пример #5
0
 def test_get_lines_empty(self):
     assert list(fmt.get_lines('')) == []
Пример #6
0
def pypsi_print(*args, sep=' ', end='\n', file=None, flush=True, width=None,
                wrap=True, wrap_prefix=None, replace_errors=True):
    '''
    Wraps the functionality of the Python builtin `print` function. The
    :meth:`pypsi.shell.Shell.bootstrap` overrides the Python :meth:`print`
    function with :meth:`pypsi_print`.

    :param str sep: string to print between arguments
    :param str end: string to print at the end of the output
    :param file file: output stream, if this is :const:`None`, the default is
        :data:`sys.stdout`
    :param bool flush: whether to flush the output stream
    :param int width: override the stream's width
    :param bool wrap: whether to word wrap the output
    :param str wrap_prefix: prefix string to print prior to every new line that
        is wrapped
    :param bool replace_errors: replace invalid character points with the '?'
        character
    '''

    file = file or sys.stdout
    last = len(args) - 1

    def write_safe(data):
        '''
        Write the input str to the file and, if an encoding error occurs and
        replace_errors is ``True``, remove invalid code points and print again.
        '''

        try:
            file.write(data)
        except UnicodeEncodeError:
            if replace_errors:
                enc = getattr(file, 'encoding', sys.getdefaultencoding())
                file.write(data.encode(enc, errors='replace').decode(enc))
            else:
                raise

    if wrap and hasattr(file, 'width') and file.width:
        width = width or file.width
        parts = []
        for arg in args:
            if isinstance(arg, str):
                parts.append(arg)
            elif arg is None:
                parts.append('')
            elif isinstance(arg, AnsiCode):
                if file.isatty():
                    parts.append(str(arg))
                elif arg.s is not None:
                    parts.append(str(arg.s))
            else:
                parts.append(str(arg))

        txt = sep.join(parts)
        for (line, endl) in get_lines(txt):
            if line:
                first = True
                wrapno = 0
                for wrapped in wrap_line(line, width, wrap_prefix=wrap_prefix):
                    if not wrapped:
                        continue

                    wrapno += 1
                    if not first:
                        file.write('\n')
                    else:
                        first = False

                    write_safe(wrapped)

            if not line or endl:
                file.write('\n')
    else:
        last = len(args) - 1
        for (i, arg) in enumerate(args):
            write_safe(str(arg))

            if sep and i != last:
                write_safe(sep)

    if end:
        write_safe(end)
    if flush:
        file.flush()
Пример #7
0
def pypsi_print(*args,
                sep=' ',
                end='\n',
                file=None,
                flush=True,
                width=None,
                wrap=True,
                wrap_prefix=None,
                replace_errors=True):
    '''
    Wraps the functionality of the Python builtin `print` function. The
    :meth:`pypsi.shell.Shell.bootstrap` overrides the Python :meth:`print`
    function with :meth:`pypsi_print`.

    :param str sep: string to print between arguments
    :param str end: string to print at the end of the output
    :param file file: output stream, if this is :const:`None`, the default is
        :data:`sys.stdout`
    :param bool flush: whether to flush the output stream
    :param int width: override the stream's width
    :param bool wrap: whether to word wrap the output
    :param str wrap_prefix: prefix string to print prior to every new line that
        is wrapped
    :param bool replace_errors: replace invalid character points with the '?'
        character
    '''

    file = file or sys.stdout
    last = len(args) - 1

    def write_safe(data):
        '''
        Write the input str to the file and, if an encoding error occurs and
        replace_errors is ``True``, remove invalid code points and print again.
        '''

        try:
            file.write(data)
        except UnicodeEncodeError:
            if replace_errors:
                enc = getattr(file, 'encoding', sys.getdefaultencoding())
                file.write(data.encode(enc, errors='replace').decode(enc))
            else:
                raise

    if wrap and hasattr(file, 'width') and file.width:
        width = width or file.width
        parts = []
        for arg in args:
            if isinstance(arg, str):
                parts.append(arg)
            elif arg is None:
                parts.append('')
            elif isinstance(arg, AnsiCode):
                if file.isatty():
                    parts.append(str(arg))
                elif arg.s is not None:
                    parts.append(str(arg.s))
            else:
                parts.append(str(arg))

        txt = sep.join(parts)
        for (line, endl) in get_lines(txt):
            if line:
                first = True
                wrapno = 0
                for wrapped in wrap_line(line, width, wrap_prefix=wrap_prefix):
                    if not wrapped:
                        continue

                    wrapno += 1
                    if not first:
                        file.write('\n')
                    else:
                        first = False

                    write_safe(wrapped)

            if not line or endl:
                file.write('\n')
    else:
        last = len(args) - 1
        for (i, arg) in enumerate(args):
            write_safe(str(arg))

            if sep and i != last:
                write_safe(sep)

    if end:
        write_safe(end)
    if flush:
        file.flush()
Пример #8
0
Файл: core.py Проект: cfm/pypsi
def pypsi_print(*args, sep=' ', end='\n', file=None, flush=True, width=None,
                wrap=True):
    '''
    Wraps the functionality of the Python builtin `print` function. The
    :meth:`pypsi.shell.Shell.bootstrap` overrides the Python :meth:`print`
    function with :meth:`pypsi_print`.

    :param str sep: string to print between arguments
    :param str end: string to print at the end of the output
    :param file file: output stream, if this is :const:`None`, the default is
        :data:`sys.stdout`
    :param bool flush: whether to flush the output stream
    :param int width: override the stream's width
    :param bool wrap: whether to word wrap the output
    '''

    file = file or sys.stdout
    last = len(args) - 1

    if wrap and hasattr(file, 'width') and file.width:
        width = width or file.width
        parts = []
        for arg in args:
            if isinstance(arg, str):
                parts.append(arg)
            elif arg is None:
                parts.append('')
            elif isinstance(arg, AnsiCode):
                if file.isatty():
                    parts.append(str(arg))
            else:
                parts.append(str(arg))

        txt = sep.join(parts)
        for (line, endl) in get_lines(txt):
            if line:
                first = True
                wrapno = 0
                for wrapped in wrap_line(line, width):
                    if not wrapped:
                        continue

                    wrapno += 1
                    if not first:
                        file.write('\n')
                    else:
                        first = False
                    file.write(wrapped)

            if not line or endl:
                file.write('\n')
    else:
        last = len(args) - 1
        for (i, arg) in enumerate(args):
            file.write(str(arg))
            if sep and i != last:
                file.write(sep)

    if end:
        file.write(end)
    if flush:
        file.flush()
Пример #9
0
 def test_get_lines_multi(self):
     assert list(fmt.get_lines("hello\nnew line")) == [('hello', True), ('new line', False)]
Пример #10
0
 def test_get_lines_trailing_endl(self):
     assert list(fmt.get_lines('hello\n')) == [('hello', True)]
Пример #11
0
 def test_get_lines_empty(self):
     assert list(fmt.get_lines('')) == []
Пример #12
0
 def test_get_lines_single(self):
     assert list(fmt.get_lines('hello')) == [('hello', False)]