def search_note(self) -> None: """ Function that displays a tree with Panels as child nodes with the notes searched """ root = Tree('📒[bold #964B00] List of Notes Found') query = self.sql_query() if query: notes_sorted = sorted(query, key=sorter) actual_note = notes_sorted[0] actual_category = actual_note[2] child_node = root.add(f':file_folder:[#d898ed]{actual_category}') for actual_note in notes_sorted: if actual_note[2] != actual_category: actual_category = actual_note[2] child_node = root.add(f':file_folder: [#d898ed]{actual_category}') if actual_note[3] == 0: child_node.add( Panel( actual_note[1] if actual_note[1] else '[red bold]Empty note[/red bold]', title=f'{actual_note[0]} {actual_note[4]}' ) ) else: # actual_note[3] == 1 markdown = Markdown( actual_note[1] if actual_note[1] else '# Note Empty') child_node.add( Panel(markdown, title=f'{actual_note[0]} {actual_note[4]}') ) else: root.add('[red]❌ No Note Found') self.console.print(root)
def test_tree_measure(): tree = Tree("foo") tree.add("bar") tree.add("musroom risotto") console = Console() measurement = Measurement.get(console, tree) assert measurement == Measurement(11, 19)
def print_impacts_list(self, impacts): tree = Tree(_('[bold]Impacts')) for impact in impacts: tree.add(f'[bold]#{impact.id}[/bold] - {impact.name}') self.console.print(tree)
def __construct_rich_tree( file_tree: FileTree, max_str: int, max_size: int, depth: int ) -> Tuple[Tree, int]: """Construct the rich tree from the file tree.""" tree = Tree(file_tree.name) tree_length = len(file_tree.subtrees) for node in file_tree.subtrees: if isinstance(node, FileTree): subtree, length = __construct_rich_tree(node, max_str, max_size, depth + 1) tree.add(subtree) tree_length += length else: line = node[0] if show_size and node[1] is not None: tab = th.TextHandler.format_tabs( string_len=len(node[0]), max_string_len=max_str - 4 * depth, ) line += f"{tab}{node[1].split()[0]}" # Define space between number and size format tabs_bf_format = th.TextHandler.format_tabs( string_len=len(node[1].split()[1]), max_string_len=max_size, tab_len=2, ) line += f"{tabs_bf_format}{node[1].split()[1]}" tree.add(line) return tree, tree_length
def walk_directory(directory: pathlib.Path, tree: Tree) -> None: """Recursively build a Tree with directory contents.""" # Sort dirs first then by filename paths = sorted( pathlib.Path(directory).iterdir(), key=lambda path: (path.is_file(), path.name.lower()), ) for path in paths: # Remove hidden files if path.name.startswith("."): continue if path.is_dir(): style = "dim" if path.name.startswith("__") else "" branch = tree.add( f"[bold magenta]:open_file_folder: [link file://{path}]{escape(path.name)}", style=style, guide_style=style, ) walk_directory(path, branch) else: text_filename = Text(path.name, "green") text_filename.highlight_regex(r"\..*$", "bold red") text_filename.stylize(f"link file://{path}") file_size = path.stat().st_size text_filename.append(f" ({decimal(file_size)})", "blue") icon = "🐍 " if path.suffix == ".py" else "📄 " tree.add(Text(icon) + text_filename)
def list(storage_prefix): """lists all available assets and their versions.""" manager = StorageProvider(prefix=storage_prefix, ) console = Console() tree = Tree("[bold]Assets store[/bold]") tree.add( f"[dim]storage provider[/dim] {manager.driver.__class__.__name__}") tree.add(f"[dim]prefix[/dim] {storage_prefix}") console.print(tree) table = Table(show_header=True, header_style="bold") table.add_column("Asset name") table.add_column("Versions", style="dim") n = 0 n_versions = 0 with Progress(SpinnerColumn(), "[progress.description]{task.description}", transient=True) as progress: progress.add_task("Listing remote assets", start=False) for asset_name, versions_list in manager.iterate_assets(): table.add_row(asset_name, " ".join(versions_list)) n += 1 n_versions += len(versions_list) console.print(table) console.print(f"Found {n} assets ({n_versions} different versions)")
def add_fixture_usages_by_tests_to_tree(node: Tree, used_by: Iterable[Test]) -> None: grouped_used_by = group_by(used_by, key=lambda t: t.description) for description, tests in grouped_used_by.items(): test = tests[0] loc = format_test_location(test) sep = f" [{len(tests)}]" if len(tests) > 1 else "" node.add(f"[muted]{loc}{sep}[/muted] {description}")
def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: if not spans: return SpanExportResult.SUCCESS tree = Tree( label= f"Trace {opentelemetry.trace.format_trace_id(spans[0].context.trace_id)}" ) parents = {} for span in spans: child = tree.add(label=Text.from_markup( f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}" )) parents[span.context.span_id] = child _child_to_tree(child, span) for span in spans: if span.parent and span.parent.span_id in parents: child = parents[span.parent.span_id].add(label=Text.from_markup( f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}" )) else: child = tree.add(label=Text.from_markup( f"[blue][{_ns_to_time(span.start_time)}][/blue] [bold]{span.name}[/bold], span {opentelemetry.trace.format_span_id(span.context.span_id)}" )) parents[span.context.span_id] = child _child_to_tree(child, span) self.console.print(tree) return SpanExportResult.SUCCESS
def format_strings_as_flat_tree(strings: Iterable[str], title: str, icon: str) -> str: """Format list of strings as flat tree.""" tree = Tree(title) for name in strings: tree.add(icon + name) text = render_to_string(tree, console=console) return text
def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult: tree = Tree( f"[b]CorrectionSet[/b] ([i]schema v{self.schema_version}[/i])\n" + (self.description or "[i]No description[/i]") + "\n" + ":open_file_folder:") for corr in self.corrections: tree.add(corr) yield tree
def rich_tree(cls, tree=None): if not inspect.isclass(cls): cls = type(cls) tree = Tree(f"{cls.__name__}") bases = [c for c in cls.__bases__ if c is not object] if bases: for base in bases: tree.add(rich_tree(base, tree)) return tree
def build_workspace_tree(workspace: Dict[str, str]) -> "Tree": from rich.tree import Tree tree = Tree( "DVC assumes the following workspace structure:", highlight=True, ) for value in sorted(workspace.values()): tree.add(f"[green]{value}[/green]") return tree
def _search_gadget(self, title, search_strs): title = f"[bright_yellow]{title}[/bright_yellow] gadgets" tree = Tree(title) for search_str in search_strs: for gadget in self.get_gadgets(search_str): pretty = Gadgetizer.prettify(gadget) tree.add(pretty) return tree
def resource_tree(self): """Show list of resources""" tree = Tree(f"{self.herb.id}") for r in self.herb.resources: tree.add(f'{r.descriptor.get("path")}') pl = Panel(tree, title=f"Resources of {self.herb.id}") return pl
def test_render_single_branch(): tree = Tree("foo") tree.add("bar") console = Console(color_system=None, width=20) console.begin_capture() console.print(tree) result = console.end_capture() print(repr(result)) expected = "foo \n└── bar \n" assert result == expected
def pretty_print_data(data): """ Users rich to generate panels for individual API response fields for better readability! :param data: the response from ipdata.lookup """ # we print single value panels first then multiple value panels for better organization single_value_panels = [] multiple_value_panels = [] # if data is empty do nothing if not data: return # push the blocklists field up a level, it's usually nested under the threat data if data.get("threat", {}).get("blocklists"): data["blocklists"] = data.get("threat", {}).pop("blocklists") # generate panels! for key, value in data.items(): # simple case if type(value) in [str, bool]: single_value_panels.append( Panel(f"[b]{key}[/b]\n[yellow]{value}", expand=True)) # if the value is a dictionary we generate a tree inside a panel if type(value) is dict: tree = Tree(key) for k, v in value.items(): if key == "threat": if v: sub_tree = tree.add(f"[b]{k}[/b]\n[bright_red]{v}") else: sub_tree = tree.add(f"[b]{k}[/b]\n[green]{v}") else: sub_tree = tree.add(f"[b]{k}[/b]\n[yellow]{v}") multiple_value_panels.append(Panel(tree, expand=False)) # if value if a list we generate nested trees if type(value) is list: tree = Tree(key) for item in value: branch = tree.add("") for k, v in item.items(): _ = branch.add(f"[b]{k}[/b]\n[yellow]{v}") multiple_value_panels.append(Panel(tree, expand=False)) # print the single value panels to the console console.print(Columns(single_value_panels), overflow="ignore", crop=False) # print the multiple value panels to the console console.print(Columns(multiple_value_panels), overflow="ignore", crop=False)
def describe(self, t=None): if not t: t = Tree("") if self.configuration_key: sub_t = t.add( f"[deep_sky_blue1]configuration[/deep_sky_blue1]: " f"[orange3]{self.configuration_key}" ) if self.__doc__: t.add(f"[deep_sky_blue1]doc[/deep_sky_blue1]: {self.__doc__.strip()}") if self._item_type and self._return_type: sub_t = t.add( f"[deep_sky_blue1]signature[/deep_sky_blue1]: " f"{pretty_print_type(self._item_type)} ->" f" {pretty_print_type(self._return_type)}" ) if self._load_time: sub_t = t.add( "[deep_sky_blue1]load time[/deep_sky_blue1]: [orange3]" + humanize.naturaldelta(self._load_time, minimum_unit="seconds") ) if self._load_memory_increment is not None: sub_t = t.add( f"[deep_sky_blue1]load memory[/deep_sky_blue1]: " f"[orange3]{humanize.naturalsize(self._load_memory_increment)}" ) if self.model_dependencies.models: dep_t = t.add("[deep_sky_blue1]dependencies") for m in self.model_dependencies.models: dep_t.add("[orange3]" + escape(m)) if self.asset_path: sub_t = t.add( f"[deep_sky_blue1]asset path[/deep_sky_blue1]: " f"[orange3]{self.asset_path}" ) if self.batch_size: sub_t = t.add( f"[deep_sky_blue1]batch size[/deep_sky_blue1]: " f"[orange3]{self.batch_size}" ) if self.model_settings: sub_t = t.add("[deep_sky_blue1]model settings[/deep_sky_blue1]") describe(self.model_settings, t=sub_t) return t
def set_attrs(field_def: Dict, tree: Tree) -> None: """ adds the field definitions that match the keys in SETTINGS_FIELD_STYLES to the given tree instance """ for field, style in SETTINGS_FIELD_STYLES.items(): value = field_def.get(field, "") if not value: continue value_str = f"{style}{value}" tree_str = f"{field}: {value_str}" tree.add(tree_str)
def profile(self, content: Optional[str] = '') -> Dict[str, float]: """Profiling a single query's roundtrip including network and compuation latency. Results is summarized in a table. :param content: the content to be sent for profiling. By default it sends an empty Document that helps you understand the network latency. :return: the latency report in a dict. """ st = time.perf_counter() r = self._client.post('/', self._iter_doc([content]), return_responses=True) ed = (time.perf_counter() - st) * 1000 route = r[0].routes gateway_time = (route[0].end_time.ToMilliseconds() - route[0].start_time.ToMilliseconds()) clip_time = (route[1].end_time.ToMilliseconds() - route[1].start_time.ToMilliseconds()) network_time = ed - gateway_time server_network = gateway_time - clip_time from rich.table import Table def make_table(_title, _time, _percent): table = Table(show_header=False, box=None) table.add_row(_title, f'[b]{_time:.0f}[/b]ms', f'[dim]{_percent * 100:.0f}%[/dim]') return table from rich.tree import Tree t = Tree(make_table('Roundtrip', ed, 1)) t.add( make_table('Client-server network', network_time, network_time / ed)) t2 = t.add(make_table('Server', gateway_time, gateway_time / ed)) t2.add( make_table('Gateway-CLIP network', server_network, server_network / gateway_time)) t2.add(make_table('CLIP model', clip_time, clip_time / gateway_time)) from rich import print print(t) return { 'Roundtrip': ed, 'Client-server network': network_time, 'Server': gateway_time, 'Gateway-CLIP network': server_network, 'CLIP model': clip_time, }
def test_render(): tree = Tree("foo") tree.add("bar", style="italic") baz_tree = tree.add("baz", guide_style="bold red", style="on blue") baz_tree.add("1") baz_tree.add("2") tree.add("egg") console = Console(width=20, force_terminal=True, color_system="standard") console.begin_capture() console.print(tree) result = console.end_capture() print(repr(result)) expected = "foo \n├── \x1b[3mbar\x1b[0m\x1b[3m \x1b[0m\n\x1b[44m├── \x1b[0m\x1b[44mbaz\x1b[0m\x1b[44m \x1b[0m\n\x1b[44m│ \x1b[0m\x1b[1;31;44m┣━━ \x1b[0m\x1b[44m1\x1b[0m\x1b[44m \x1b[0m\n\x1b[44m│ \x1b[0m\x1b[1;31;44m┗━━ \x1b[0m\x1b[44m2\x1b[0m\x1b[44m \x1b[0m\n└── egg \n" assert result == expected
def test_render_ascii(): tree = Tree("foo") tree.add("bar") tree.add("baz") class AsciiConsole(Console): @property def encoding(self): return "ascii" console = AsciiConsole(color_system=None, width=20) console.begin_capture() console.print(tree) result = console.end_capture() expected = "foo \n+-- bar \n`-- baz \n" assert result == expected
def parse_tree(tree: Tree, routes: List[CompiledRouterNode], *, prefix: str = None): for node in routes: resource = type(node.resource).__name__ endpoint = node.uri_template if prefix: endpoint = "/" + endpoint.lstrip(prefix) method_map = node.method_map if method_map is None: continue child = tree.add(f":bookmark_tabs: {endpoint} ({resource})", highlight=True) table = Table(show_lines=True) table.add_column("Method") table.add_column("Description") for method, responder in method_map.items(): doc = (responder.__doc__ or "").strip("\n") name = responder.__name__ if name.startswith("on_"): table.add_row(method, doc) child.add(table)
def list(self): r""" Lists wallets. """ wallets = next(os.walk(os.path.expanduser(self.config.wallet.path)))[1] root = Tree("Wallets") for w_name in wallets: wallet_for_name = bittensor.wallet( path = self.config.wallet.path, name = w_name) try: if wallet_for_name.coldkeypub_file.exists_on_device() and not wallet_for_name.coldkeypub_file.is_encrypted(): coldkeypub_str = wallet_for_name.coldkeypub.ss58_address else: coldkeypub_str = '?' except: coldkeypub_str = '?' wallet_tree = root.add("\n[bold white]{} ({})".format(w_name, coldkeypub_str)) hotkeys_path = self.config.wallet.path + w_name + '/hotkeys' try: hotkeys = next(os.walk(os.path.expanduser(hotkeys_path))) if len( hotkeys ) > 1: for h_name in hotkeys[2]: hotkey_for_name = bittensor.wallet( path = self.config.wallet.path, name = w_name, hotkey = h_name) try: if hotkey_for_name.hotkey_file.exists_on_device() and not hotkey_for_name.hotkey_file.is_encrypted(): hotkey_str = hotkey_for_name.hotkey.ss58_address else: hotkey_str = '?' except: hotkey_str = '?' wallet_tree.add("[bold grey]{} ({})".format(h_name, hotkey_str)) except: pass print(root)
def find(where, exclude, include, output): """ Find plugins by scanning the given path for PluginSpecs. It starts from the current directory if --where is not specified. This is what a setup.py method would run as a build step, i.e., discovering entry points. """ with console.status(f"Scanning path {where}"): plugins = find_plugins(where, exclude, include) if output == "tree": tree = Tree("Entrypoints") for namespace, entry_points in plugins.items(): node = tree.add(f"[bold]{namespace}") t = Table() t.add_column("Name") t.add_column("Location") for ep in entry_points: key, value = ep.split("=") t.add_row(key, value) node.add(t) rprint(tree) elif output == "dict": rprint(dict(plugins)) else: raise click.ClickException("unknown output format %s" % output)
def _search_gadget(self, title, search_strs): title = f"[bright_yellow]{title}[/bright_yellow] gadgets" tree = Tree(title) gadget_filter = re.compile( r'ret 0x[0-9a-fA-F]{3,};') # filter out rets larger than 255 for search_str in search_strs: for file, gadget in self.get_gadgets(search_str): if gadget_filter.search(gadget.simpleString()): # not sure how to filter large ret sizes within ropper's search functionality, so doing it here continue tree.add( f"{escape(str(gadget)).replace(':', ' #', 1)} :: {file}") self.addresses.add(hex(gadget.address)) return tree
def cache(namespace): """ Outputs the stevedore entrypoints cache from which plugins are loaded. """ from stevedore._cache import _c data = _c._get_data_for_path(None) tree = Tree("Entrypoints") for group, entry_points in data.get("groups").items(): if namespace and group != namespace: continue node = tree.add(f"[bold]{group}") t = Table() t.add_column("Name") t.add_column("Value") for key, value, _ in entry_points: t.add_row(key, value) node.add(t) if namespace: rprint(t) return rprint(tree)
def add_fixture_dependencies_to_tree( parent: Tree, fixture: Fixture, fixtures_to_parents_or_children: FixtureHierarchyMapping, show_scopes: bool, max_depth: Optional[int], depth: int = 0, ) -> None: if max_depth is not None and depth >= max_depth: return this_layer = fixtures_to_parents_or_children[fixture] if not this_layer: return for dep in this_layer: node = parent.add(make_text_for_fixture(fixture=dep, show_scope=show_scopes)) add_fixture_dependencies_to_tree( parent=node, fixture=dep, fixtures_to_parents_or_children=fixtures_to_parents_or_children, show_scopes=show_scopes, max_depth=max_depth, depth=depth + 1, )
def info(self): tree = Tree("Supermark Extensions") for extension_point in self.extension_points.values(): ep_tree = tree.add(extension_point.name) for extension in extension_point.extensions.values(): ep_tree.add(str(extension)) rich.print(tree)
def generate_tree_from_schema(schema): tree = Tree("Topology") for s_dev, d_dev_params in schema.items(): s_dev_tree = tree.add(s_dev) for d_dev, params in d_dev_params.items(): s_dev_tree.add(d_dev) return Panel(Padding(tree, 4), title="Current topology")
def print_config( config: DictConfig, fields: Sequence[str] = ( "data", "models", ), resolve: bool = True, ) -> None: """Prints content of DictConfig using Rich library and its tree structure. Args: config (DictConfig): Config. fields (Sequence[str], optional): Determines which main fields from config will be printed and in what order. resolve (bool, optional): Whether to resolve reference fields of DictConfig. """ style = "dim" tree = Tree(f":gear: CONFIG", style=style, guide_style=style) for field in fields: branch = tree.add(field, style=style, guide_style=style) config_section = config.get(field) branch_content = str(config_section) if isinstance(config_section, DictConfig): branch_content = OmegaConf.to_yaml(config_section, resolve=resolve) branch.add(Syntax(branch_content, "yaml")) printr(tree)