示例#1
0
文件: stream.py 项目: Grahack/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)
        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_wrap_line_nowrap(self):
     assert list(fmt.wrap_line('hello adam', 0)) == ['hello adam']
示例#3
0
 def test_wrap_line_long_word(self):
     assert list(fmt.wrap_line('areallylongword',
                               4)) == ['areal', 'lylon', 'gword']
示例#4
0
 def test_wrap_line_ansi_multi(self):
     ANSI = '\x1b[1;32mhello\x1b[0m \x1b[1;30madam\x1b[0m'
     assert list(fmt.wrap_line(ANSI, 5)) == ANSI.split(' ', 1)
示例#5
0
 def test_wrap_line_prefix(self):
     TXT = 'hello, adam'
     assert list(fmt.wrap_line(TXT, 6, '>> ')) == ['hello,', '>> adam']
示例#6
0
 def test_wrap_line_ascii(self):
     assert list(fmt.wrap_line('hello adam', 5)) == ['hello', 'adam']
示例#7
0
 def test_wrap_line_ansi_single(self):
     ANSI = '\x1b[1;32mhello\x1b[0m \x1b[1;30madam\x1b[0m'
     assert list(fmt.wrap_line(ANSI, 10)) == [ANSI]
示例#8
0
文件: core.py 项目: ameily/pypsi
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()
示例#9
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()
示例#10
0
 def test_wrap_line_long_word(self):
     assert list(fmt.wrap_line('areallylongword', 4)) == ['areal', 'lylon', 'gword']
示例#11
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()
示例#12
0
 def test_wrap_line_nowrap(self):
     assert list(fmt.wrap_line('hello adam', 0)) == ['hello adam']
示例#13
0
 def test_wrap_line_prefix(self):
     TXT = 'hello, adam'
     assert list(fmt.wrap_line(TXT, 6, '>> ')) == ['hello,', '>> adam']
示例#14
0
 def test_wrap_line_ansi_multi(self):
     ANSI = '\x1b[1;32mhello\x1b[0m \x1b[1;30madam\x1b[0m'
     assert list(fmt.wrap_line(ANSI, 5)) == ANSI.split(' ', 1)
示例#15
0
 def test_wrap_line_ansi_single(self):
     ANSI = '\x1b[1;32mhello\x1b[0m \x1b[1;30madam\x1b[0m'
     assert list(fmt.wrap_line(ANSI, 10)) == [ANSI]
示例#16
0
 def test_wrap_line_ascii(self):
     assert list(fmt.wrap_line('hello adam', 5)) == ['hello', 'adam']