def metadata(path): def detail(value, unit): result = (" (%s)" % value if unit is None else " (%s %s)" % (value, unit)) return termstr.TermStr(result).fg_color(termstr.Color.grey_100) is_symlink = "yes" if os.path.islink(path) else "no" stat_result = os.stat(path) permissions = stat.filemode(stat_result.st_mode) hardlinks = str(stat_result.st_nlink) group = [pwd.getpwuid(stat_result.st_gid).pw_name, detail(stat_result.st_gid, "gid")] owner = [pwd.getpwuid(stat_result.st_uid).pw_name, detail(stat_result.st_uid, "uid")] modified, created, access = [ [time.asctime(time.gmtime(seconds)), detail(int(seconds), "secs")] for seconds in (stat_result.st_mtime, stat_result.st_ctime, stat_result.st_atime)] size = [_pretty_bytes(stat_result.st_size), detail(stat_result.st_size, "bytes")] stdout, *rest = _do_command( ["file", "--dereference", "--brief", "--uncompress", "--mime", path]) mime_type = stdout stdout, *rest = _do_command( ["file", "--dereference", "--brief", "--uncompress", path]) file_type = stdout md5sum = _md5(path) stdout, *rest = _do_command(["sha1sum", path]) sha1sum = stdout.split()[0] permissions_value = [permissions, detail(_permissions_in_octal(permissions), None)] text = [] for line in [ ("size", size), ("permissions", permissions_value), None, ("modified time", modified), ("creation time", created), ("access time", access), None, ("owner", owner), ("group", group), None, ("hardlinks", hardlinks), ("symlink", is_symlink), None, ("md5", md5sum), ("sha1", sha1sum), None, ("mime type", mime_type.strip()), ("file type", file_type.strip())]: if line is None: text.append("\n") else: name, value = line name = termstr.TermStr(name + ":").fg_color( termstr.Color.light_blue).ljust(16) text.append(name + fill3.join("", value) + "\n") return (Status.normal, fill3.Text(fill3.join("", text)))
def _syntax_highlight(text, lexer, style): def _parse_rgb(hex_rgb): if hex_rgb.startswith("#"): hex_rgb = hex_rgb[1:] return tuple(eval("0x"+hex_rgb[index:index+2]) for index in [0, 2, 4]) def _char_style_for_token_type(token_type, default_bg_color): token_style = style.style_for_token(token_type) fg_color = (None if token_style["color"] is None else _parse_rgb(token_style["color"])) bg_color = (default_bg_color if token_style["bgcolor"] is None else _parse_rgb(token_style["bgcolor"])) return termstr.CharStyle(fg_color, bg_color, token_style["bold"], token_style["italic"], token_style["underline"]) default_bg_color = _parse_rgb(style.background_color) text = fill3.join( "", [termstr.TermStr(text, _char_style_for_token_type( token_type, default_bg_color)) for token_type, text in pygments.lex(text, lexer)]) return fill3.Text(text, pad_char=termstr.TermStr(" ").bg_color( default_bg_color))
def _colorize_mccabe(text, python_version): return fill3.join("", [ termstr.TermStr(line).fg_color(termstr.Color.yellow) if _get_mccabe_line_score(line, python_version) > 10 else line for line in text.splitlines(keepends=True)])
def _colorize_coverage_report(text): line_color = {"> ": termstr.Color.green, "! ": termstr.Color.red, " ": None} return fill3.join("", [termstr.TermStr(line).fg_color(line_color[line[:2]]) for line in text.splitlines(keepends=True)])
def widget_to_string(widget): appearance = widget.appearance_min() return str(fill3.join("\n", appearance))
def assert_string(self, appearance, expected_string): self.assertEqual(str(fill3.join("\n", appearance)), expected_string)
def _widget_to_string(widget, dimensions=_DIMENSIONS): appearance = (widget.appearance_min() if dimensions is None else widget.appearance(dimensions)) return str(fill3.join("\n", appearance))