def test_progress(self): def progress(cp, col_num, lu=False, l=101): cb = cp.printf('progress', '?') for i in range(0, l): cb.progress_cb(col_num, i if not lu else None) sleep(0.05) cb.progress_cp(col_num) cp.printf('done', progress=False) cp = ColumnPrinter(cols=[Column(width=20), ProgressColumn(pwidth=10)]) progress(cp, 1) cp = ColumnPrinter(cols=[Column(width=20), ProgressColumn(pwidth=10)]) progress(cp, 1, lu=True) cp = ColumnPrinter( cols=[Column(width=20), ProgressColumn(pwidth=5, char='*')]) progress(cp, 1, lu=True) cp = ColumnPrinter(cols=[Column(width=20), ProgressColumn(pwidth=10)]) progress(cp, 1, l=70) cp = ColumnPrinter(cols=[Column(width=20), ProgressColumn(pwidth=1)]) progress(cp, 1, lu=True)
def test_3_col_fill_col_2_wrap(self): cp = ColumnPrinter(cols=[ Column(width=20), Column(fill=True, max=90, wrap=True), Column(width=10) ]) s = self.gen_string(100) cp.printf('test', s, '1234')
def test_margin(self): cp = ColumnPrinter(cols=[ Column(width=20, lmargin=3), Column(width=20, rmargin=3, wrap=True), Column(width=1, rmargin=0) ]) s = self.gen_string(50) cp.printf('margin3l', s, '$')
def printf(self, msg=None, data=None, progress=False, col_updt=False, verbosity=1): if verbosity > self._verbosity or sys.stdout is None or not sys.stdout.isatty( ) or self._cp is None: if progress or col_updt: return self.empty_callbacks() else: return msg = msg or '' data = data or '' try: if not terminal_utf8(): msg = filter_unicode_chars(msg) data = filter_unicode_chars(data) if not progress and not col_updt: self._cp.printf(msg, data) elif progress: try: progress_cp = ColumnPrinter(cols=[ Column(width=12), ProgressColumn(pwidth=12), Column(width=30) ], row_width=55) self._cp.printf(msg, progress_cp) except ColumnPrinterError as e: pass return self.empty_callbacks() cb = progress_cp.printf('?', '?', '', col_updt=True) pcb = cb.progress_cb pcp = cb.progress_cp cb.progress_cb = lambda p: pcb(1, p) def pcp2(): pcp(1) progress_cp.done() cb.progress_cp = pcp2 return cb else: return self._cp.printf(msg, data, col_updt=True) except (UnicodeDecodeError, UnicodeEncodeError) as e: log.error(e)
def test_with_column_printer(self): cp = ColumnPrinter(cols=[15, 15, 10]) cb = cp.printf('getting image', progress_col=2, col_cb=True) hr = HttpRequest() clc = lambda l: cb.col_cb(1, format_size(l)) pcb = lambda p: cb.progress_cb('%.2f %%' % p) ro = RequestOptions(progress_cb=pcb, progress_cp=cb.progress_cp, content_length_cb=clc) hr.get('http://i.imgur.com/eFglueb.jpg', ro)
def __init__(self, verbosity=3): self._cp = None try: self._cp = ColumnPrinter(cols=[ Column(width=30), SepColumn(), Column(min=30, fill=True, wrap=True) ]) except ColumnPrinterError as e: pass self._verbosity = Config().eget('output.verbosity', default=1)
def test_exceptions(self): with self.assertRaises(ColumnPrinterError) as e: cp = ColumnPrinter(cols=[ Column(ratio=0.4, fill=True), Column(ratio=1.2, fill=True, wrap=True) ]) cp = ColumnPrinter(cols=[ Column(ratio=0.0, fill=True), Column(ratio=1.2, fill=True, wrap=True) ]) cp = ColumnPrinter(cols=[ Column(width=10), Column(ratio=1.2, fill=True, wrap=True) ]) cp = ColumnPrinter(cols=[ Column(width=1000), Column(ratio=0.2, fill=True, wrap=True) ]) cp = ColumnPrinter(cols=[ Column(width=-1), Column(ratio=0.2, fill=True, wrap=True) ]) ColumnPrinter.get_terminal_width = lambda s: 100 cp = ColumnPrinter(cols=[Column(width=99), Column(width=2)]) cp = ColumnPrinter( cols=[Column(width=99), Column(fill=True), Column(fill=True)]) cp = ColumnPrinter( cols=[Column( width=99), Column(fill=True, ratio=0.1)])
def test_cb(self): cp = ColumnPrinter(cols=[Column(width=20), Column(width=20)]) cb = cp.printf('test', '?', col_updt=True) for i in range(0, 100): cb.col_updt_cb(1, str(i)) sleep(0.1) if i == 70: cp.printf('interrupt') cb.col_updt_cp() cp.printf('done')
def test_align(self): cp = ColumnPrinter(cols=[ Column(width=20, align='l'), Column(width=20, align='r'), Column(width=20, align='c'), Column(width=20) ]) cp.printf('left', 'right', 'center', 'left') print('-') cp = ColumnPrinter(cols=[ Column(width=10, lmargin=1), Column(width=10, rmargin=0), Column(width=10, align='r'), Column(width=10, rmargin=0), Column(width=10, align='c'), Column(width=10, rmargin=0) ]) cp.printf('left', '0' * 10, 'right', '0' * 10, 'center12', '0' * 10)
def list(self): '''List the commands registered for autocomplete. Key: [user]: installed for current user [all]: installed for everyone [user, all]: installed for current user as well as everyone''' try: dstore = DataStore() autocomp_list = dstore.list_autocomp_commands() printer = ColumnPrinter(cols=[Column(width=20), Column(width=10), Column(width=10)]) for i in autocomp_list: c = len(i.access) printer.printf(i.command, i.access[0], i.version[0] or 'n/a') if c > 1: printer.printf('', i.access[1], i.version[1] or 'n/a') except DataStoreError as e: print(e) raise CommandError()
def test_default(self): cp = ColumnPrinter() cp.printf('test123') cp.printf('really long string ' * 10)
class Printer: def __init__(self, verbosity=3): self._cp = None try: self._cp = ColumnPrinter(cols=[ Column(width=30), SepColumn(), Column(min=30, fill=True, wrap=True) ]) except ColumnPrinterError as e: pass self._verbosity = Config().eget('output.verbosity', default=1) def printf(self, msg=None, data=None, progress=False, col_updt=False, verbosity=1): if verbosity > self._verbosity or sys.stdout is None or not sys.stdout.isatty( ) or self._cp is None: if progress or col_updt: return self.empty_callbacks() else: return msg = msg or '' data = data or '' try: if not terminal_utf8(): msg = filter_unicode_chars(msg) data = filter_unicode_chars(data) if not progress and not col_updt: self._cp.printf(msg, data) elif progress: try: progress_cp = ColumnPrinter(cols=[ Column(width=12), ProgressColumn(pwidth=12), Column(width=30) ], row_width=55) self._cp.printf(msg, progress_cp) except ColumnPrinterError as e: pass return self.empty_callbacks() cb = progress_cp.printf('?', '?', '', col_updt=True) pcb = cb.progress_cb pcp = cb.progress_cp cb.progress_cb = lambda p: pcb(1, p) def pcp2(): pcp(1) progress_cp.done() cb.progress_cp = pcp2 return cb else: return self._cp.printf(msg, data, col_updt=True) except (UnicodeDecodeError, UnicodeEncodeError) as e: log.error(e) def empty_callbacks(self): cb = Callbacks() cb.col_updt_cb = lambda x, y: None cb.col_updt_cp = lambda: None cb.progress_cb = lambda x: None cb.progress_cp = lambda: None return cb
def test_2_col_auto_fill(self): cp = ColumnPrinter(cols=[Column(fill=True), Column(fill=True)]) s = self.gen_string(100) cp.printf(s, s)
def test_sep_column(self): cp = ColumnPrinter(cols=[ Column(width=10, wrap=True), SepColumn(), Column(width=10, wrap=True) ]) cp.printf('one', '1') cp.printf('two') cp.printf('three', '3') cp.printf('wrap text', 'test ' * 4) cp.printf('wrap this column', 'text') cp.printf('wrap both columns', 'text ' * 3) # need to fix cp.printf() cp.printf('done', '', 'extra')
def test_progress_in_inner_cp(self): cp = ColumnPrinter(cols=[Column(width=20), Column(width=50)]) incp = ColumnPrinter( cols=[Column(width=20), ProgressColumn(pwidth=10)], row_width=50) cp.printf('start') cp.printf('progress', incp) cb = incp.printf('downloading', '?') for i in range(0, 101): cb.progress_cb(1, i) sleep(0.05) cb.progress_cp(1) incp.done() cp.printf('done')
def stats(self): 'Print wallpaper statistics.' st = Statistics() cp = ColumnPrinter( cols=[Column( width=30), SepColumn(), Column(fill=True)]) cp.printf('wallpaper count', str(st.wallpaper_count)) cp.printf('usage time', str(st.usage_time)) cp.printf('avg life of a wallpaper', st.avg_wallpaper_life) cp.printf('avg image size', format_size(st.avg_downloaded_image_size)) cp.printf() incp = ColumnPrinter( cols=[Column(width=30), SepColumn(), Column(width=6, align='r')], row_width=38) cp.printf('top domains', incp) for domain, count in st.top_domains: incp.printf(domain, str(count)) incp.done()
def test_2_cp_in_cp(self): cp = ColumnPrinter( cols=[Column(width=20), Column(width=40), Column(width=40)]) incp = ColumnPrinter(cols=[Column(width=20), Column(width=20)], row_width=40) incp2 = ColumnPrinter(cols=[Column(width=20), Column(fill=True)], row_width=40) cp.printf('test', incp, incp2) incp.printf('first-1', '1') incp2.printf('first-2', '1') incp.printf('second-1', '2') incp2.printf('second-2', '2') incp.printf('third-1', '3') incp.printf('fourth-1', '4') incp2.printf('third-2', '3') incp2.done() incp.printf('more-1', 'm') incp.printf('more-1', 'm') incp.printf('more-1', 'm') incp.done() cp.printf('done')
def test_cp_in_cp(self): cp = ColumnPrinter(cols=[Column(width=20), Column(width=40)]) incp = ColumnPrinter(cols=[Column(width=20), Column(width=20)], row_width=40) cp.printf('test', incp) incp.printf('first', '1') incp.printf('second', '2') incp.printf('third', '3') cp.printf('done') incp.done() incp.printf('extra') # should not print
def test_2_col_fixed_width(self): cp = ColumnPrinter(cols=[Column(width=20), Column(width=40)]) cp.printf('test', '123') cp.printf('test2', 'really long string ' * 10)
def test_ratio_2_col(self): cp = ColumnPrinter( cols=[Column(ratio=0.4, fill=True), Column(ratio=0.6, fill=True)]) s = self.gen_string(100) cp.printf(s, s) print('-') cp = ColumnPrinter(cols=[ Column(ratio=0.4, fill=True), Column(ratio=0.6, fill=True, wrap=True) ]) cp.printf(s, s + s) print('-') cp = ColumnPrinter(cols=[ Column(ratio=0.4, fill=True), Column(ratio=0.6, fill=True, wrap=True, max=70) ]) cp.printf(s, s + s) print('-') cp = ColumnPrinter( cols=[Column(ratio=0.4, fill=True), Column(fill=True, wrap=True)]) cp.printf(s, s + s)
def printf(self, album): printer = ColumnPrinter(cols=[Column(width=15), Column(fill=True)]) for field, title in self.fields: printer.printf(title, ignore_u(str_if_not(getattr(album, field, ''))))
def test_2_col_wrap_both(self): cp = ColumnPrinter( cols=[Column(width=20, wrap=True), Column(width=40, wrap=True)]) cp.printf('test', '123') cp.printf('test2 ' * 5, 'really long string ' * 10)