import sys import colorama as color from settings import Settings OUTPUT = 0 DBG_ERROR = 1 DBG_INFO = 2 LOCKED = False CACHE = [] color.init(True) def output(text='', level=OUTPUT, end='\n'): if LOCKED: CACHE.append((text, level, end)) return if level <= Settings.Debuglvl: if level == 1: text = color.Fore.RED + '[ERROR] ' + text if level == 2: text = color.Fore.YELLOW + '[INFO] ' + text print(text, end=end) def ask_input(question, f=str, quit=False): output(color.Style.BRIGHT + color.Fore.CYAN + question, end=': ') _input = input().strip() if quit and _input == 'q': return None else: try: return f(_input) except: output('{0} cannot be casted to type {1}'.format(_input, type(f.__class__)), level=DBG_ERROR) def option_picker(question, options, objects=None, selected=None, quit=False, table=None): output(color.Style.BRIGHT + color.Fore.CYAN + question, end=':\n') width = None if type(table) is list: header = [] for column in table: _space = '[{:' + str(column[1]) + '}]' header.append(_space.format(column[0])) _header = ''.join(header) width = len(_header) output(color.Style.BRIGHT + _header) output(color.Style.BRIGHT + ('=' * width)) for i in range(0, len(options)): option = options[i] _row = '' if i == selected: _row += color.Style.BRIGHT + '>' if objects is not None and table is not None and len(objects) == len(table): for j in range(0, len(objects)): _row += '[{{{name}:{size}.{size}}}]'.format(name=str(objects[j]), size=str(table[j][1])) # _row += '[{' + str(objects[j]) + ':' + str(table[j][1]) + '.' + str(table[j][1]) + '}] ' output(_row.format(__id__=str(i), x=option)) # output(_row.format(__id__=str(i), x=dict(map(lambda s: s if s is not None else '', option.__dict__.items()))).strip()) else: if i == selected: output('>', end='') output('[{0:>2}]'.format(i), end=' ') output(option) if quit: if width is not None: output(color.Style.BRIGHT + ('=' * width)) else: output('==========') output(color.Fore.YELLOW + '[q] cancel') return ask_input('Pick option', int, quit=quit) def format_bytes(count): return '{:04.2f}MB'.format(count / 1048576) def lock(): global LOCKED LOCKED = True def unlock(): global LOCKED, CACHE for print in CACHE: output(print[0], print[1], print[2]) LOCKED = False CACHE = [] class ProgressBar: def __init__(self, total=None, size=100): self.size = size self.total = total self.current = 0 lock() def destroy(self): print() unlock() def report_progress(self, increment=1, progress=None): if progress is not None: self.current = progress else: self.current += increment self.draw() def draw(self): if self.total is None: # Marque pass else: percent = int((self.current / self.total) * 100) _draw = ('\r[{0:' + str(self.size) + '}] {1}%').format('=' * percent, percent) sys.stdout.write(_draw) sys.stdout.flush()