def mamage_version(self, value): """Version of the output""" term.pos(16, 3) term.write(value["system"]) term.pos(16, 10) term.write(value["version"])
async def exec_command(*args): debug("[command] start run cmd") buffer = bytearray() debug("[command] ebook-meta ", *args) cmd_process = asyncio.create_subprocess_exec( "ebook-meta", *args, stdout=asyncio.subprocess.PIPE) proc = await cmd_process while True: line = await proc.stdout.readline() # debug("[command] r: ", line[0:-1].decode("gbk")) if not line: break buffer.extend(line) await proc.wait() return_code = proc.returncode debug("[command] return code: ", return_code) if not return_code: cmd_output = bytes(buffer).decode("gbk") term.writeLine("\n书籍元数据信息如下:", term.green, term.bold) print("====" * 30) term.write(cmd_output, term.bgwhite, term.bold, term.black) print("====" * 30) return return_code
def append(char): chars.append(char) term.save_cursor() term.move_cursor(self.puzzle.displayed_width + 60, self.puzzle.displayed_height + 3) term.write(''.join(chars[-8:])) term.restore_cursor() term.flush()
def print_objects(state): grid = [[0 for i in range(state.width)] for j in range(state.height)] for item in state.items: for i in range(item.y, item.y + item.height): for j in range(item.x, item.x + item.width): if grid[i][j] == 0: grid[i][j] = item.id + 1 else: if type(grid[i][j]) is list or grid[i][j] == None: grid[i][j] = None else: grid[i][j] = [grid[i][j], item.id + 1] print_bundle = '' n_to_center = center_element(len(grid[0]) * 2) for line in grid: print_bundle += (' ' * n_to_center) for i in range(len(line)): if line[i] == 0: print_bundle += term.black + term.bgyellow + '0 ' + term.off elif type(line[i]) is list: print_bundle += term.black + term.bgred + '%c%c' % ( chr(64 + line[i][0]), chr(64 + line[i][1])) + term.off elif line[i] == None: print_bundle += term.black + term.bgred + '--' + term.off else: print_bundle += term.black + term.bggreen + '%c ' % ( chr(64 + line[i])) + term.off if i < len(line): print_bundle += '\n' term.write(print_bundle)
def print_stats(self): """ Display statistics of current request """ if not self.queryset_stats: return term.writeLine("\n\t ERASERHEAD STATS \n", term.bold, term.reverse) for queryset_storage in self.queryset_stats: queryset_storage.print_stats() print() term.write("\t TOTAL WASTED MEMORY: ", term.bold, term.reverse) term.write( " {}".format(humanfriendly.format_size(self.total_wasted_memory)), term.red) print()
def create_folders(folders_list, base=''): for folder in folders_list: folder_path = folder['path'] full_folder_path = os.path.join(base, folder_path) term.write('%s ' % full_folder_path) if not os.path.exists(folder_path): if folder.get('create'): os.makedirs(full_folder_path) term.writeLine('CREATED', term.yellow) else: term.writeLine('MISSING', term.red) else: term.writeLine('OK', term.green)
def output(item): author = re.search(r'<h2>(.*?)</h2>', item[0], re.S) #text = re.search(r'(.*)', item[1], re.S) text = item[1] other_info = re.findall(r'<i.+?number">(\d+)</i>\s(\S+?)\s?<', item[2], re.S) term.writeLine(author.group(1), term.green, term.blink) term.writeLine(text.replace('<br/>', '\n').strip(), term.yellow) # do like this in case of no fun or comment for info in other_info: term.write(info[0]+info[1]+'\t', term.blue) else: print '\n'
def output(item): author = re.search(r'<h2>(.*?)</h2>', item[0], re.S) #text = re.search(r'(.*)', item[1], re.S) text = item[1] other_info = re.findall(r'<i.+?number">(\d+)</i>\s(\S+?)\s?<', item[2], re.S) term.writeLine(author.group(1), term.green, term.blink) term.writeLine(text.replace('<br/>', '\n').strip(), term.yellow) # do like this in case of no fun or comment for info in other_info: term.write(info[0] + info[1] + '\t', term.blue) else: print '\n'
def draw(self): startLine = self.scroll[1] endLine = min(self.scroll[1] + self.size[1], len(self.linesi) + 1) # cap endLine to the end of the buffer for i in range(startLine, endLine): starti = ([0] + self.linesi)[i] # start and end index of this line endi = (self.linesi + [len(self.buffer)])[i] term.move(self.pos[0], self.pos[1] - startLine + i) starti += self.scroll[0] endi = max(min(endi, starti + self.size[0]), starti) # ensure starti <= endi and endi fits on screen for c in self.buffer[starti:endi]: if c != "\n": term.write(c) else: term.write(" ") term.write( " " * (self.size[0] - endi + starti)) # erase till end of line complete = endLine - startLine for i in range(self.size[1] - complete): term.move(self.pos[0], self.pos[1] + i + complete) term.write(" " * self.size[0]) c = self.getPos(self.cursori) term.move(self.pos[0] + c[0] - self.scroll[0], self.pos[1] + c[1] - self.scroll[1])
class BeautyCLI(Cursor): def __init__(self, clear=True): if clear: tm.sleep(.25) self.cls() cls = lambda self: trm.clear() cls_ln = lambda self: trm.clearLine() fmt = lambda self, *args, **kwargs: trm.format(*args, **kwargs) wrt = lambda self, *args, **kwargs: trm.write(*args, **kwargs) wln = lambda self, *args, **kwargs: trm.writeLine(*args, **kwargs) right = lambda self, t: trm.right(t) center = lambda self, t: trm.center(t) def echo(self, text, row=None, col=None, align='left', *args): formated_text = text if align in 'right': formated_text = self.right(formated_text) elif align in 'center': formated_text = self.center(formated_text) if row is not None and col is not None: self.pos(row, col) elif row is not None and row is None: self.pos(row, 1) elif col is not None and row is None: self.pos(1, col) self.wln(formated_text, *args)
def print_grid(grid): print_bundle = '' n_to_center = center_element(len(grid[0]) * 2) for line in grid: print_bundle += (' ' * n_to_center) for i in range(len(line)): if line[i] == 0: print_bundle += term.black + term.bgyellow + '%d ' % line[ i] + term.off elif line[i] == 1: print_bundle += term.black + term.bggreen + '%d ' % line[ i] + term.off elif line[i] > 1: print_bundle += term.black + term.bgred + '%d ' % line[ i] + term.off if i < len(line): print_bundle += '\n' term.write(print_bundle)
def render(self) -> None: term.clear_screen() term.move_cursor(0, 0) term.write(term.bold(self.data.title)) term.move_cursor(0, 1) term.write(self.data.author) for y, line in enumerate(self.puzzle.render_grid(self.cursor), start=2): term.move_cursor(0, y) term.write(line) for direction, x_offset in zip(Direction, (2, 36)): for y, line in enumerate(self.puzzle.render_clues(self.cursor, direction), start=2): term.move_cursor(self.puzzle.displayed_width + x_offset, y) term.write(line) if self.message is not None: term.move_cursor(0, self.puzzle.displayed_height + 3) term.write(self.message) x, y = self.cursor.displayed_coords() term.move_cursor(x, y + 2) term.flush()
def show_images_statistics(): """ Iterate through all articles and find img tags in them """ def find_missing(files): missing = [] for filename in files: path_to_file = os.path.expanduser( os.path.join(config.STATIC_FOLDER, filename.lstrip(os.path.sep))) if not os.path.exists(path_to_file): missing.append(filename) return missing all_posts = Post.select() parser = ImgParser() posts_affected = 0 images_to_restore = 0 for post in all_posts: parser.feed(post.post_text) if parser._captured_tags: images_in_post = len(parser._captured_tags) missing_images = len(find_missing(parser._captured_tags)) term.write('Post #{post_id} has {images_count} images '.format( post_id=post.post_id, images_count=images_in_post )) if missing_images: term.writeLine('/ {missing_count} missing'.format( missing_count=missing_images), term.yellow) else: term.writeLine('OK', term.green) parser.clear() posts_affected += 1 images_to_restore += missing_images term.writeLine('Total posts with images %d' % posts_affected, term.bold) if images_to_restore: term.writeLine('Total images to restore %d' % images_to_restore, term.red) else: term.writeLine('All posts are in good shape!', term.green)
def show_images_statistics(): """ Iterate through all articles and find img tags in them """ def find_missing(files): missing = [] for filename in files: path_to_file = os.path.expanduser( os.path.join(config.STATIC_FOLDER, filename.lstrip(os.path.sep))) if not os.path.exists(path_to_file): missing.append(filename) return missing all_posts = Post.select() parser = ImgParser() posts_affected = 0 images_to_restore = 0 for post in all_posts: parser.feed(post.post_text) if parser._captured_tags: images_in_post = len(parser._captured_tags) missing_images = len(find_missing(parser._captured_tags)) term.write('Post #{post_id} has {images_count} images '.format( post_id=post.post_id, images_count=images_in_post)) if missing_images: term.writeLine( '/ {missing_count} missing'.format( missing_count=missing_images), term.yellow) else: term.writeLine('OK', term.green) parser.clear() posts_affected += 1 images_to_restore += missing_images term.writeLine('Total posts with images %d' % posts_affected, term.bold) if images_to_restore: term.writeLine('Total images to restore %d' % images_to_restore, term.red) else: term.writeLine('All posts are in good shape!', term.green)
def mamage_disk(self, value): """Disk output format""" term.pos(13, 3) term.write("Disk", term.bold) term.pos(13, 8) total = (value["used"]/value["total"]) * 100 percent = ("%s%%" % (int(total))) term.write("[%-10s]%s" % ("|" * int(total/10), percent)) term.pos(13, 30) term.write("-Disk-", term.bold) term.pos(13, 39) term.write("total: %s GB" % (int(value["total"]))) term.pos(13, 55) term.write("used: %s GB" % (int(value["used"]))) term.pos(13, 70) term.write("free: %s GB" % (int(value["free"]))) term.pos(13, 3)
def paint(paint_map): term.clear() for pos, value in paint_map.items(): term.pos(pos[0] + 20, pos[1] + 20) term.write('#' if value == 1 else ' ')
def header(self): """Frame structure format output""" # upper part bold = term.format("#", term.bold) frame_up = bold.ljust(100, "=") frame_up_format = term.format(frame_up) term.write(frame_up_format+bold) # center section 1 term.pos(3, 0) frame_three = term.format("+".ljust(87, "-")) term.write(frame_three+"+") # center section 2 term.pos(5, 0) frame_five = term.format("+".ljust(87, "-")) term.write(frame_five+"+") # center section 3 frame_centre1 = "+".ljust(87, "=") term.pos(15, 0) term.write(frame_centre1+"+") # center section 4 frame_centre2 = "+".ljust(59, "=") term.pos(17, 0) term.write(frame_centre2) term.pos(17, 88) term.write("|", term.bold) # Producer information kevin = "Kevin.Xiang" term.pos(16, 47) term.write(kevin) # next part frame_down = bold.ljust(100, "=") term.pos(39, 0) term.write(frame_down+bold) # border style for i1 in range(7, 14): term.pos(i1, 26) term.write("|") for i2 in range(16, 39): term.pos(i2, 60) term.write("|", term.bold) # Border output style IO term.pos(16, 61) frame_back = term.format("".rjust(27, " "), term.bgwhite) term.write(frame_back) term.pos(16, 71) frame_network = term.format("NETWORK", term.black, term.bgwhite) term.write(frame_network) term.pos(27, 61) term.write(frame_back) term.pos(27, 73) frame_disk = term.format("DISK", term.black, term.bgwhite) term.write(frame_disk) # Process output style term.pos(18, 0) space = "".center(4, " ") frame = term.format(" USER" + space + "PID" + space + "CPU%" + space + "MEM%" + space + "THRE" + space + "NAME", term.bold) term.write(frame) # Border output style list_down = [2, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38] for i3 in list_down: term.pos(i3, 0) term.write("|", term.bold) term.pos(i3, 88) term.write("|", term.bold)
def mamage_process(self, value): """Process output format""" value = value["key"] count = 19 # Loop outputs each process for v in value: term.pos(count, 3) term.write(v["user"]) term.pos(count, 11) term.write(str(v["pid"])) term.pos(count, 18) term.write(str(v["cpu"])) term.pos(count, 26) term.write(str(round(v["memory"], 2))) term.pos(count, 34) term.write(str(v["threads"])) term.pos(count, 42) term.write(str(v["name"])) count = count + 1 term.pos(18, 4)
def mamage_diskio(self, value): """Disk IO output format""" term.pos(31, 68) term.clearLineFromPos() term.write("Read: " + str(round(value["read"], 2)) + " KB") term.pos(31, 88) term.write("|", term.bold) term.pos(32, 68) term.clearLineFromPos() term.write("Wrtn: " + str(round(value["write"], 2)) + " KB") term.pos(32, 88) term.write("|", term.bold) term.pos(33, 68) term.write("Rsec: " + str(value["read_count"])) term.pos(34, 68) term.write("Wsec: " + str(value["write_count"])) term.pos(35, 69) term.write("Tps: " + str(value["tps"])) term.pos(32, 68)
def mamage_network(self, value): """Network output format""" term.pos(20, 68) term.clearLineFromPos() term.write("TX: " + str(round(value["send"], 2)) + " KB") term.pos(20, 88) term.write("|", term.bold) term.pos(21, 68) term.clearLineFromPos() term.write("RX: " + str(round(value["recv"], 2)) + " KB") term.pos(21, 88) term.write("|", term.bold) term.pos(22, 67) term.write("TXP: " + str(value["packets_sent"])) term.pos(23, 67) term.write("TXP: " + str(value["packets_recv"])) term.pos(20, 68)
def mamage_system(self, value): """System output format""" day, now = time.strftime("%Y-%m-%d %H:%M:%S").split() course_pid = psutil.pids() course_count = 0 for i in course_pid: course_count = course_count + 1 term.pos(2, 4) term.write("USER: "******"username"])) term.pos(2, 16) term.write("T: " + str(value["terminal"])) term.pos(2, 28) term.write("-") term.pos(2, 33) term.write("Process: " + str(course_count)) term.pos(2, 48) term.write("-") term.pos(2, 53) term.write("BootTime: " + str(value["BootTime"])) term.pos(4, 4) term.write("HOST: " + str(value["hostname"])) term.pos(4, 28) term.write("-") term.pos(4, 32) term.write("Sec: " + str(now)) term.pos(4, 48) term.write("-") term.pos(4, 53) term.write("LogiTime: " + str(value["started"])) term.pos(2, 3)
def print_tooltip(string): term.write(term.bgblue + string + term.off + '\n')
def mamage_swap(self, value): """Swap output format""" term.pos(11, 3) term.write("Swap", term.bold) term.pos(11, 8) # Determine if the value is 0 and the exception is caught try: total = (int(value["used"])/int(value["total"])) * 100 except ZeroDivisionError: total = 0 percent = ("%s%%" % (int(total))) term.write("[%-10s]%s" % ("|" * int(total/10), percent)) term.pos(11, 30) term.write("-Swap-", term.bold) term.pos(11, 39) term.write("total: %s MB" % (int(value["total"]))) term.pos(11, 55) term.write("used: %s MB" % (int(value["used"]))) term.pos(11, 70) term.write("free: %s MB" % (int(value["free"]))) term.pos(12, 41) term.write("sin: %s MB" % (int(value["sin"]))) term.pos(12, 55) term.write("sout: %s MB" % (int(value["sout"]))) term.pos(12, 70) term.write("perc: %s %%" % (str(value["percent"]))) term.pos(11, 3)
def mamage_memory(self, value): """Memory output format""" term.pos(9, 4) term.write("Mem", term.bold) term.pos(9, 8) total = (value["used"]/value["total"]) * 100 percent = ("%s%%" % (int(total))) term.write("[%-10s]%s" % ("|" * int(total/10), percent)) term.pos(9, 30) term.write("-MEM-", term.bold) term.pos(9, 39) term.write("total: %s MB" % (int(value["total"]))) term.pos(9, 55) term.write("used: %s MB" % (int(value["used"]))) term.pos(9, 70) term.write("free: %s MB" % (int(value["free"]))) term.pos(10, 39) term.write("activ: %s MB" % (int(value["active"]))) term.pos(10, 55) term.write("buff: %s MB" % (int(value["buffers"]))) term.pos(10, 70) term.write("cach: %s MB" % (int(value["cached"]))) term.pos(9, 4)
def mamage_cpu(self, value): """CPU output format""" term.pos(7, 4) term.write("CPU", term.bold) term.pos(7, 19) term.write(" ") term.pos(7, 8) # Output progress bar percent = ("%s%%" % (int(value["total"]))) term.write("[%-10s]%s" % ("|" * int(value["total"]/10), percent)) term.pos(7, 30) term.write("-CPU-", term.bold) term.pos(7, 45) term.write(" ") term.pos(7, 39) term.write("total: %s %%" % (round(value["total"], 1))) term.pos(7, 60) term.write(" ") term.pos(7, 55) term.write("used: %s %%" % (round(value["user"], 1))) term.pos(7, 75) term.write(" ") term.pos(7, 70) term.write("syst: %s %%" % (round(value["system"], 1))) term.pos(8, 45) term.write(" ") term.pos(8, 39) term.write("iowai: %s %%" % (round(value["iowait"], 1))) term.pos(8, 60) term.write(" ") term.pos(8, 55) term.write("nice: %s %%" % (round(value["nice"], 1))) term.pos(8, 75) term.write(" ") term.pos(8, 70) term.write("nice: %s %%" % (round(value["idle"], 1))) term.pos(7, 4)
def _print_named_value(self, label, value): term.write('\t') term.write(label + ':', term.underscore) term.writeLine(" {}".format(value))
def print_div(string): n_to_center = center_element(len(string)) term.write(term.bgblue + '\n' + (' ' * n_to_center) + string + (' ' * n_to_center) + term.off + '\n')