Exemple #1
0
def getContacts(client):
    data = client.listFriends()
    table = Table(show_footer=False)
    table.add_column("Name", no_wrap=True)
    table.add_column("JID", no_wrap=True)
    table.add_column("Subscription", no_wrap=True)
    table.add_column("Status", no_wrap=True)
    table.add_column("Extra", no_wrap=True)
    table.title = (
        "[not italic]:girl_light_skin_tone: Users In Rooster [not italic]:boy_light_skin_tone:"
    )
    for row in data:
        table.add_row(*row)

    table.columns[4].header_style = "bold red"
    table.columns[3].header_style = "bold green"
    table.columns[2].header_style = "bold blue"
    table.columns[1].header_style = "red"
    table.columns[0].header_style = "cyan"
    table.row_styles = ["none", "dim"]
    table.border_style = "bright_yellow"
    for x in [
            box.SQUARE,
            box.MINIMAL,
            box.SIMPLE,
            box.SIMPLE_HEAD,
    ]:
        table.box = x
    console.print(table, justify="left")
Exemple #2
0
def search(query: str, amount: int, ignore_errors: bool, min_views, max_views, game) -> None:
    channels = API.search(query, live=True, amount=amount)
    table = Table()
    table_centerd = Align.center(table)

    table.add_column("channel")
    table.add_column("views")
    table.add_column("game")
    table.add_column("title")
    table.title = f"search results for [bold]{query}[/bold]"

    with Live(table_centerd, console=Console, refresh_per_second=20):
        for channel in channels:
            try:
                stream = API.get_stream_info(channel)
            except Exception as e:
                if not ignore_errors:
                    table.add_row(f"www.twitch.tv/{channel}", "[bold red]E[/bold red]", "[bold red]Error[/bold red]", f"[bold red]{e}[/bold red]")
            else:
                if max_views is not None and stream.views > max_views:
                    continue
                if min_views is not None and stream.views < min_views:
                    continue
                if game is not None and stream.game != game:
                    continue
                table.add_row(f"www.twitch.tv/{stream.channel}", str(stream.views), str(stream.game), str(stream.title))
Exemple #3
0
def getInfoUser(client):
    questions = [{
        'type': 'input',
        'message': 'Enter username to get info',
        'name': 'username'
    }]
    answers = prompt(questions, style=custom_style_2)
    data = client.getUserInfo(answers['username'])
    table = Table(show_footer=False)
    table.add_column("Email", no_wrap=True)
    table.add_column("JID", no_wrap=True)
    table.add_column("Username", no_wrap=True)
    table.add_column("Name", no_wrap=True)
    table.title = (
        "[not italic]:girl_light_skin_tone: Username Information [not italic]:boy_light_skin_tone:"
    )
    for row in data:
        table.add_row(*row)

    table.columns[3].header_style = "bold red"
    table.columns[2].header_style = "bold green"
    table.columns[1].header_style = "bold blue"
    table.columns[0].header_style = "cyan"
    table.row_styles = ["none", "dim"]
    table.border_style = "bright_yellow"
    for x in [
            box.SQUARE,
            box.MINIMAL,
            box.SIMPLE,
            box.SIMPLE_HEAD,
    ]:
        table.box = x
    console.print(table, justify="left")
Exemple #4
0
def generate_table_stats(stats) -> "Table":
    """Make a table for the global stats."""
    # print(stats)
    values = ["request_count", "average_size", "x200", "x300", "x400"]
    table = Table(show_header=True, header_style="bold magenta")
    table.title = ("[b] global statistics on request status : [/b]")
    table.add_column("status code")
    table.add_column("count")
    # table.add_column("Status")

    for index, row in enumerate(values):
        table.add_row(f"{row}", f"{stats[index]}")
    return table
Exemple #5
0
def generate_table_last10(last10) -> "Table":
    """Make a table for the last 10sec or just render a message no input"""
    # print(stats)
    if not last10:
        return Panel("No hits on the last 10s", style="on blue")
    values = ["Section", "hits count", "average byte sent"]
    table = Table(show_header=True, header_style="bold magenta")
    table.title = (
        "[b] Statistics on sections for the last 10 secondes : [/b]")
    for element in values:
        table.add_column(element)
    for row in last10:
        table.add_row(f"{row[0]}", f"{row[1]:d}", f"{row[2]:.2f}")
    return table
Exemple #6
0
def create_table(blocks, tps, max_tps) -> Table:
    table = Table()
    table.add_column("      Height", justify="right")
    table.add_column("Transactions", justify="right")
    table.title = 'TPS: {}(MAX) / {}(1m AVG)'.format(int(max_tps), int(tps))

    n = min(5, len(blocks))
    for i in range(n):
        block = blocks[len(blocks) - i - 1]
        txs = block['transactions']
        num_txs = 0
        if txs is not None:
            num_txs = len(txs)
        table.add_row(str(block['height']), str(num_txs))

    return table
Exemple #7
0
def output_entry(entry):
    properties = {}
    properties["Username"] = entry.username
    properties["Url"] = entry.url
    for key, value in entry.custom_properties.items():
        properties[key] = value
    properties["Notes"] = entry.notes

    table = Table()
    table.title = entry.title
    table.add_column("Field")
    table.add_column("Value")

    for key, value in properties.items():
        if value:
            table.add_row(key, value)

    CONSOLE.print(table)

    if entry.password:
        pyperclip.copy(entry.password)
        CONSOLE.print("Password copied to clipboard")
Exemple #8
0
def to_build_tree(ydoc, variants, config, selected_features):
    for k in variants:
        table = Table(show_header=True, header_style="bold")
        table.title = f"Output: [bold white]{k}[/bold white]"
        table.add_column("Package")
        table.add_column("Variant versions")
        for pkg, var in variants[k].items():
            table.add_row(pkg, "\n".join(var))
        console.print(table)

    # first we need to perform a topological sort taking into account all the outputs
    if ydoc.get("outputs"):
        outputs = [
            Output(o, config, parent=ydoc, selected_features=selected_features)
            for o in ydoc["outputs"]
        ]
        outputs = {o.name: o for o in outputs}
    else:
        outputs = [Output(ydoc, config, selected_features=selected_features)]
        outputs = {o.name: o for o in outputs}

    if len(outputs) > 1:
        sort_dict = {
            k: [x.name for x in o.all_requirements()] for k, o in outputs.items()
        }
        tsorted = toposort.toposort(sort_dict)
        tsorted = [o for o in tsorted if o in sort_dict.keys()]
    else:
        tsorted = [o for o in outputs.keys()]

    final_outputs = []

    for name in tsorted:
        output = outputs[name]
        if variants.get(output.name):
            v = variants[output.name]
            combos = []

            differentiating_keys = []
            for k in v:
                if len(v[k]) > 1:
                    differentiating_keys.append(k)
                combos.append([(k, x) for x in v[k]])

            all_combinations = tuple(itertools.product(*combos))
            all_combinations = [dict(x) for x in all_combinations]
            for c in all_combinations:
                x = output.apply_variant(c, differentiating_keys)
                final_outputs.append(x)
        else:
            x = output.apply_variant({})
            final_outputs.append(x)

    temp = final_outputs
    final_outputs = []
    has_intermediate = False
    for o in temp:
        if o.sections["build"].get("intermediate"):
            if has_intermediate:
                raise RuntimeError(
                    "Already found an intermediate build. There can be only one!"
                )
            final_outputs.insert(0, o)
            has_intermediate = True
        else:
            final_outputs.append(o)

    # Note: maybe this should happen _before_ apply variant?!
    if has_intermediate:
        # inherit dependencies
        def merge_requirements(a, b):
            b_names = [x.name for x in b]
            for r in a:
                if r.name in b_names:
                    continue
                else:
                    b.append(r)

        intermediate = final_outputs[0]
        for o in final_outputs[1:]:
            merge_requirements(
                intermediate.requirements["host"], o.requirements["host"]
            )
            merge_requirements(
                intermediate.requirements["build"], o.requirements["build"]
            )
            merged_variant = {}
            merged_variant.update(intermediate.config.variant)
            merged_variant.update(o.config.variant)
            o.config.variant = merged_variant

    return final_outputs
Exemple #9
0
    def output(
        self,
        outdata: Union[List[str], Dict[str, Any]],
        tablefmt: str = "rich",
        title: str = None,
        caption: str = None,
        account: str = None,
        config=None,
        ok_status: Union[int, List[int], Tuple[int, str],
                         List[Tuple[int, str]]] = None,
    ) -> str:
        # log.debugv(f"data passed to output():\n{pprint(outdata, indent=4)}")
        def _do_subtables(data: list, tablefmt: str = "rich"):
            out = []
            for inner_dict in data:  # the object: switch/vlan etc dict
                for key, val in inner_dict.items():
                    if not isinstance(val, (list, dict, tuple)):
                        if val is None:
                            inner_dict[key] = ''
                        elif isinstance(val,
                                        str) and val.lower() in ['up', 'down']:
                            color = 'red' if val.lower() == 'down' else 'green'
                            if tablefmt == 'rich':
                                inner_dict[
                                    key] = f'[b {color}]{val.title()}[/b {color}]'
                            else:
                                inner_dict[key] = typer.style(val.title(),
                                                              fg=color)
                        else:
                            if tablefmt == 'rich':
                                inner_dict[key] = Text(str(val), style=None)
                            else:
                                inner_dict[key] = str(val)
                    else:
                        val = self.listify(val)
                        if val and tablefmt == "rich" and hasattr(
                                val[0], 'keys'):
                            inner_table = Table(
                                *(k for k in val[0].keys()),
                                show_header=True,
                                # padding=(0, 0),
                                pad_edge=False,
                                collapse_padding=True,
                                show_edge=False,
                                header_style="bold cyan",
                                box=SIMPLE)
                            _ = [
                                inner_table.add_row(*[
                                    self.do_pretty(kk, str(vv))
                                    for kk, vv in v.items()
                                ]) for v in val
                            ]
                            with console.capture():
                                console.print(inner_table)
                            inner_dict[key] = console.export_text()
                        elif val and tablefmt == "tabulate" and hasattr(
                                val[0], 'keys'):
                            inner_table = tabulate(val,
                                                   headers="keys",
                                                   tablefmt=tablefmt)
                            inner_dict[key] = inner_table
                        else:
                            if all(isinstance(v, str) for v in val):
                                inner_dict[key] = ", ".join(val)
                out.append(inner_dict)
            return out

        raw_data = outdata
        _lexer = table_data = None

        if config and config.sanitize and raw_data and all(
                isinstance(x, dict) for x in raw_data):
            redact = [
                "mac", "serial", "neighborMac", "neighborSerial",
                "neighborPortMac", "longitude", "latitude"
            ]
            outdata = [{
                k: d[k] if k not in redact else "--redacted--"
                for k in d
            } for d in raw_data]

        # -- // List[str, ...] \\ --  Bypass all formatters, (config file output, etc...)
        if outdata and all(isinstance(x, str) for x in outdata):
            tablefmt = "strings"

        # -- convert List[dict] --> Dict[dev_name: dict] for yaml/json outputs
        if tablefmt in ['json', 'yaml', 'yml']:
            outdata = self.listify(outdata)
            if outdata and 'name' in outdata[0]:
                outdata: Dict[str, Dict[str, Any]] = {
                    item['name']:
                    {k: v
                     for k, v in item.items() if k != 'name'}
                    for item in outdata
                }

        if tablefmt == "json":
            raw_data = json.dumps(outdata, indent=4)
            _lexer = lexers.JsonLexer

        elif tablefmt in ["yml", "yaml"]:
            raw_data = yaml.dump(outdata, sort_keys=False)
            _lexer = lexers.YamlLexer

        elif tablefmt == "csv":
            raw_data = table_data = "\n".join([
                ",".join([
                    k if outdata.index(d) == 0 else str(v)
                    for k, v in d.items() if k not in CUST_KEYS
                ]) for d in outdata
            ])

        elif tablefmt == "rich":
            from rich.console import Console
            from rich.table import Table
            from rich.box import HORIZONTALS, SIMPLE
            from rich.text import Text
            from centralcli import constants
            console = Console(record=True)

            customer_id, customer_name = "", ""
            # outdata = self.listify(outdata)

            # -- // List[dict, ...] \\ --
            if outdata and all(isinstance(x, dict) for x in outdata):
                customer_id = outdata[0].get("customer_id", "")
                customer_name = outdata[0].get("customer_name", "")
                outdata = [{k: v
                            for k, v in d.items() if k not in CUST_KEYS}
                           for d in outdata]

                table = Table(
                    # show_edge=False,
                    show_header=True,
                    title=title,
                    header_style='magenta',
                    show_lines=False,
                    box=HORIZONTALS,
                    row_styles=['none', 'dark_sea_green'])

                fold_cols = ['description']
                _min_max = {'min': 10, 'max': 30}
                set_width_cols = {'name': _min_max, 'model': _min_max}
                full_cols = [
                    'mac', 'serial', 'ip', 'public ip', 'version', 'radio',
                    'id'
                ]

                for k in outdata[0].keys():
                    if k in fold_cols:
                        table.add_column(k,
                                         overflow='fold',
                                         max_width=115,
                                         justify='left')
                    elif k in set_width_cols:
                        table.add_column(k,
                                         min_width=set_width_cols[k]['min'],
                                         max_width=set_width_cols[k]['max'],
                                         justify='left')
                    elif k in full_cols:
                        table.add_column(k, no_wrap=True, justify='left')
                    else:
                        table.add_column(k, justify='left')

                formatted = _do_subtables(outdata)
                [
                    table.add_row(*list(in_dict.values()))
                    for in_dict in formatted
                ]

                if title:
                    table.title = f'[italic cornflower_blue]{constants.what_to_pretty(title)}'
                if account or caption:
                    table.caption_justify = 'left'
                    table.caption = '' if not account else f'[italic dark_olive_green2] Account: {account}'
                    if caption:
                        table.caption = f"[italic dark_olive_green2]{table.caption}  {caption}"

                data_header = f"--\n{'Customer ID:':15}{customer_id}\n{'Customer Name:':15} {customer_name}\n--\n"

                with console.capture():
                    console.print(table)

                raw_data = console.export_text(clear=False)
                table_data = console.export_text(styles=True)

                raw_data = f"{data_header}{raw_data}" if customer_id else f"{raw_data}"
                table_data = f"{data_header}{table_data}" if customer_id else f"{table_data}"

        elif tablefmt == "tabulate":
            customer_id = customer_name = ""
            outdata = self.listify(outdata)

            # -- // List[dict, ...] \\ --
            if outdata and all(isinstance(x, dict) for x in outdata):
                customer_id = outdata[0].get("customer_id", "")
                customer_name = outdata[0].get("customer_name", "")
                outdata = [{k: v
                            for k, v in d.items() if k not in CUST_KEYS}
                           for d in outdata]
                raw_data = outdata

                outdata = _do_subtables(outdata, tablefmt=tablefmt)
                # outdata = [dict((k, v) for k, v in zip(outdata[0].keys(), val)) for val in outdata]

                table_data = tabulate(outdata,
                                      headers="keys",
                                      tablefmt=tablefmt)
                td = table_data.splitlines(keepends=True)
                table_data = f"{typer.style(td[0], fg='cyan')}{''.join(td[1:])}"

                data_header = f"--\n{'Customer ID:':15}{customer_id}\n" \
                              f"{'Customer Name:':15} {customer_name}\n--\n"
                table_data = f"{data_header}{table_data}" if customer_id else f"{table_data}"
                raw_data = f"{data_header}{raw_data}" if customer_id else f"{raw_data}"

        else:  # strings output No formatting
            # -- // List[str, ...] \\ --
            if len(outdata) == 1:
                if "\n" not in outdata[0]:
                    # we can format green as only success output is sent through formatter.
                    table_data = typer.style(f"  {outdata[0]}", fg="green")
                    raw_data = outdata[0]
                else:  # template / config file output
                    # get rid of double nl @ EoF (configs)
                    raw_data = table_data = "{}\n".format(
                        '\n'.join(outdata).rstrip('\n'))
            else:
                raw_data = table_data = '\n'.join(outdata)
                # Not sure what hit's this, but it was created so something must
                log.debug("List[str] else hit")

        if _lexer and raw_data:
            table_data = highlight(
                bytes(raw_data, 'UTF-8'), _lexer(),
                formatters.Terminal256Formatter(style='solarized-dark'))

        return self.Output(rawdata=raw_data,
                           prettydata=table_data,
                           config=config)
Exemple #10
0
def to_build_tree(ydoc, variants, config, cbc, selected_features):
    for k in variants:
        table = Table(show_header=True, header_style="bold")
        table.title = f"Output: [bold white]{k}[/bold white]"
        table.add_column("Package")
        table.add_column("Variant versions")
        for pkg, var in variants[k].items():
            table.add_row(pkg, "\n".join(var))
        console.print(table)

    # first we need to perform a topological sort taking into account all the outputs
    if ydoc.get("outputs"):
        outputs = [
            Output(
                o,
                config,
                parent=ydoc,
                conda_build_config=cbc,
                selected_features=selected_features,
            )
            for o in ydoc["outputs"]
        ]
        outputs = {o.name: o for o in outputs}
    else:
        outputs = [
            Output(
                ydoc,
                config,
                conda_build_config=cbc,
                selected_features=selected_features,
            )
        ]
        outputs = {o.name: o for o in outputs}

    if len(outputs) > 1:
        sort_dict = {
            k: [x.name for x in o.all_requirements()] for k, o in outputs.items()
        }
        tsorted = toposort.toposort(sort_dict)
        tsorted = [o for o in tsorted if o in sort_dict.keys()]
    else:
        tsorted = [o for o in outputs.keys()]

    final_outputs = []

    # need to strip static away from output name... :/
    static_feature = selected_features.get("static", False)

    for name in tsorted:
        output = outputs[name]

        # this is all a bit hacky ... will have to clean that up eventually
        variant_name = name
        if static_feature and name.endswith("-static"):
            variant_name = name[: -len("-static")]

        # zip keys need to be contracted
        zipped_keys = cbc.get("zip_keys", [])

        if variants.get(variant_name):
            v = variants[variant_name]
            import copy

            vzipped = copy.copy(v)
            zippers = {}
            for zkeys in zipped_keys:
                # we check if our variant contains keys that need to be zipped
                if sum(k in v for k in zkeys) > 1:
                    filtered_zip_keys = [k for k in v if k in zkeys]
                    print("Filtered zip keys: ", filtered_zip_keys)

                    zkname = "__zip_" + "_".join(filtered_zip_keys)

                    zklen = None
                    for zk in filtered_zip_keys:
                        if zk not in cbc:
                            raise RuntimeError(
                                f"Trying to zip keys, but not all zip keys found on conda-build-config {zk}"
                            )

                        zkl = len(cbc[zk])
                        if not zklen:
                            zklen = zkl

                        if zklen and zkl != zklen:
                            raise RuntimeError(
                                f"Trying to zip keys, but not all zip keys have the same length {zkeys}"
                            )

                    vzipped[zkname] = [str(i) for i in range(zklen)]
                    zippers[zkname] = {zk: cbc[zk] for zk in filtered_zip_keys}

                    for zk in filtered_zip_keys:
                        del vzipped[zk]

            combos = []
            differentiating_keys = []
            for k, vz in vzipped.items():
                if len(vz) > 1:
                    differentiating_keys.append(k)
                combos.append([(k, x) for x in vz])

            all_combinations = tuple(itertools.product(*combos))
            all_combinations = [dict(x) for x in all_combinations]

            # unzip the zipped keys
            unzipped_combinations = []
            for c in all_combinations:
                unz_combo = {}
                for vc in c:
                    if vc.startswith("__zip_"):
                        ziptask = zippers[vc]
                        zipindex = int(c[vc])
                        for zippkg in ziptask:
                            unz_combo[zippkg] = ziptask[zippkg][zipindex]
                        if vc in differentiating_keys:
                            differentiating_keys.remove(vc)
                            differentiating_keys.extend(zippers[vc].keys())
                    else:
                        unz_combo[vc] = c[vc]

                unzipped_combinations.append(unz_combo)

            for c in unzipped_combinations:
                x = output.apply_variant(c, differentiating_keys)
                final_outputs.append(x)
        else:
            x = output.apply_variant({})
            final_outputs.append(x)

    temp = final_outputs
    final_outputs = []
    has_intermediate = False
    for o in temp:
        if o.sections["build"].get("intermediate"):
            if has_intermediate:
                raise RuntimeError(
                    "Already found an intermediate build. There can be only one!"
                )
            final_outputs.insert(0, o)
            has_intermediate = True
        else:
            final_outputs.append(o)

    # Note: maybe this should happen _before_ apply variant?!
    if has_intermediate:
        # inherit dependencies
        def merge_requirements(a, b):
            b_names = [x.name for x in b]
            for r in a:
                if r.name in b_names:
                    continue
                else:
                    b.append(r)

        intermediate = final_outputs[0]
        for o in final_outputs[1:]:
            merge_requirements(
                intermediate.requirements["host"], o.requirements["host"]
            )
            merge_requirements(
                intermediate.requirements["build"], o.requirements["build"]
            )
            merged_variant = {}
            merged_variant.update(intermediate.config.variant)
            merged_variant.update(o.config.variant)
            o.config.variant = merged_variant

    return final_outputs
Exemple #11
0
    with beat(10):
        console.print(table, justify="center")

    table.add_column("Budget", "[u]$412,000,000", no_wrap=True)
    with beat(10):
        console.print(table, justify="center")

    table.add_column("Opening Weekend", "[u]$577,703,455", no_wrap=True)
    with beat(10):
        console.print(table, justify="center")

    table.add_column("Box Office", "[u]$4,331,212,357", no_wrap=True)
    with beat(10):
        console.print(table, justify="center")

    table.title = "Star Wars Box Office"
    with beat(10):
        console.print(table, justify="center")

    table.title = (
        "[not italic]:popcorn:[/] Star Wars Box Office [not italic]:popcorn:[/]"
    )
    with beat(10):
        console.print(table, justify="center")

    table.caption = "Made with Rich"
    with beat(10):
        console.print(table, justify="center")

    table.caption = "Made with [b]Rich[/b]"
    with beat(10):
Exemple #12
0
    def __rich__(self):
        from rich import box

        table = Table(box=box.MINIMAL_DOUBLE_HEAD)
        s = f"Output: {self.name} {self.version} BN: {self.build_number}\n"
        if hasattr(self, "differentiating_variant"):
            short_v = " ".join([val for val in self.differentiating_variant])
            s += f"Variant: {short_v}\n"
        s += "Build:\n"
        table.title = s
        table.add_column("Dependency")
        table.add_column("Version requirement")
        table.add_column("Selected")
        table.add_column("Build")
        table.add_column("Channel")

        def spec_format(x):
            version, fv = " ", " "
            channel = CondaChannel.from_url(x.channel).name

            if (x.channel.startswith("file://")
                    and context.local_build_root in x.channel):
                channel = "local"

            if len(x.final.split(" ")) > 1:
                version = " ".join(r.final.split(" ")[1:])
            if hasattr(x, "final_version"):
                fv = x.final_version
            color = "white"
            if x.from_run_export:
                color = "blue"
            if x.from_pinnings:
                color = "green"
            if x.is_transitive_dependency:
                table.add_row(f"{r.final_name}", "", f"{fv[0]}", f"{fv[1]}",
                              f"{channel}")
                return

            if x.is_pin:
                if x.is_pin_compatible:
                    version = "PC " + version
                else:
                    version = "PS " + version
                color = "cyan"

            if len(fv) >= 2:
                table.add_row(
                    f"[bold white]{r.final_name}[/bold white]",
                    f"[{color}]{version}[/{color}]",
                    f"{fv[0]}",
                    f"{fv[1]}",
                    f"{channel}",
                )
            else:
                table.add_row(
                    f"[bold white]{r.final_name}[/bold white]",
                    f"[{color}]{version}[/{color}]",
                    f"{fv[0]}",
                    "",
                    f"{channel}",
                )

        def add_header(header, head=False):
            p = Padding("", (0, 0), style="black")
            if head:
                pns = Padding("", (0, 0), style="black")
                table.add_row(pns, pns, pns, pns, pns)
            table.add_row(Padding(header, (0, 0), style="bold yellow"), p, p,
                          p, p)

        if self.requirements["build"]:
            add_header("Build")
            for r in self.requirements["build"]:
                spec_format(r)
        if self.requirements["host"]:
            add_header("Host", True)
            for r in self.requirements["host"]:
                spec_format(r)
        if self.requirements["run"]:
            add_header("Run", True)
            for r in self.requirements["run"]:
                spec_format(r)
        return table
Exemple #13
0
    def __init__(self, d, config, parent=None, selected_features=None):
        if parent is None:
            parent = {}
        if selected_features is None:
            selected_features = {}
        self.data = d
        self.data["source"] = d.get("source", parent.get("source", {}))
        self.config = config

        self.name = d["package"]["name"]
        self.version = d["package"]["version"]
        self.build_string = d["package"].get("build_string")
        self.build_number = d["build"].get("number", 0)
        self.is_first = False
        self.sections = {}

        def set_section(sname):
            self.sections[sname] = {}
            self.sections[sname].update(parent.get(sname, {}))
            self.sections[sname].update(d.get(sname, {}))

        set_section("build")
        set_section("package")
        set_section("app")
        set_section("extra")

        self.sections["files"] = d.get("files")
        self.sections["source"] = self.data.get("source", {})
        if hasattr(self.sections["source"], "keys"):
            self.sections["source"] = [self.sections["source"]]

        self.sections["features"] = parent.get("features", [])

        self.feature_map = {
            f["name"]: f
            for f in self.sections.get("features", [])
        }
        for fname, feat in self.feature_map.items():
            activated = feat.get("default", False)
            if fname in selected_features:
                activated = selected_features[fname]

            feat["activated"] = activated

        if self.feature_map.get(
                "static") and self.feature_map["static"]["activated"]:
            self.name += "-static"

        if len(self.feature_map):
            table = Table()
            table.title = "Activated Features"
            table.add_column("Feature")
            table.add_column("State")
            for feature in self.feature_map:
                if self.feature_map[feature]["activated"]:
                    table.add_row(feature, "[green]ON[/green]")
                else:
                    table.add_row(feature, "[red]OFF[/red]")

            console.print(table)

        self.requirements = copy.copy(d.get("requirements", {}))
        for f in self.feature_map.values():
            if f["activated"]:
                if not f.get("requirements"):
                    continue
                for i in ["build", "host", "run", "run_constrained"]:
                    base_req = self.requirements.get(i, [])
                    feat_req = f["requirements"].get(i, [])
                    base_req += feat_req
                    if len(base_req):
                        self.requirements[i] = base_req

        self.transactions = {}

        self.parent = parent

        for section in ("build", "host", "run", "run_constrained"):
            self.requirements[section] = [
                CondaBuildSpec(r)
                for r in (self.requirements.get(section) or [])
            ]

        # handle strong and weak run exports
        run_exports = []
        for el in self.sections["build"].get("run_exports", []):
            if type(el) is str:
                run_exports.append(CondaBuildSpec(el))
            else:
                raise RuntimeError(
                    "no strong run exports supported as of now.")
                # sub_run_exports = []
                # for key, val in el:
                #     for x in val:
                #         sub_run_exports.append(CondaBuildSpec(x))
                #     run_exports.append({})
        if run_exports:
            self.sections["build"]["run_exports"] = run_exports
Exemple #14
0
    def generate_table():

        nonlocal config
        nonlocal manager
        nonlocal subtensor
        nonlocal dendrite
        nonlocal meta

        total_stake = 0.0
        total_incentive = 0.0
        total_rank = 0.0
        total_success = 0
        total_time = 0.0

        # Fill row.
        def get_row(droplet):

            nonlocal total_stake
            nonlocal total_incentive
            nonlocal total_rank
            nonlocal total_time
            nonlocal total_success
            nonlocal config

            if droplet.name not in config.names:
                return

            # Setup asyncio loop.
            connection = connection_for_droplet(droplet)

            # Get connection string
            can_connect_bool = can_connect(connection)
            connect_str = '[bold green] YES' if can_connect(
                connection) else '[bold red] NO'

            # get hotkey
            if can_connect_bool:
                try:
                    hotkey = get_hotkey(connection)
                    hotkey_str = hotkey if hotkey != None else '[yellow] None'
                except Exception as e:
                    hotkey_str = '[yellow] None'
                    logger.error('{}: Failed to pull hotkey error = {}',
                                 droplet.name, e)
            else:
                hotkey_str = '[yellow] None'

            # get coldkey
            if can_connect_bool:
                try:
                    coldkeypub = get_coldkeypub(connection)
                    coldkeypub_str = coldkeypub if coldkeypub != None else '[yellow] None'
                except Exception as e:
                    coldkeypub_str = '[yellow] None'
                    logger.error('{}: Failed to pull coldkey error = {}',
                                 droplet.name, e)
            else:
                coldkeypub_str = '[yellow] None'

            # get branch
            if can_connect_bool:
                try:
                    branch = get_branch(connection)
                    branch_str = branch if branch != None else '[yellow] None'
                except Exception as e:
                    branch_str = '[yellow] None'
                    logger.error('{}: Failed to pull branch error = {}',
                                 droplet.name, e)
            else:
                branch_str = '[yellow] None'

            # get install status
            if can_connect_bool and branch != None:
                try:
                    installed = is_installed(connection)
                    is_installed_str = '[bold green] Yes' if installed else '[bold red] No'
                except Exception as e:
                    installed = False
                    is_installed_str = '[bold red] No'
                    logger.error(
                        '{}: Failed to pull install status error = {}',
                        droplet.name, e)
            else:
                installed = False
                is_installed_str = '[bold red] No'

            # get miner status
            if can_connect_bool and installed:
                try:
                    is_running = is_miner_running(connection)
                    is_running_str = '[bold green] Yes' if is_running else '[bold red] No'
                except Exception as e:
                    is_running = False
                    is_running_str = '[bold red] No'
                    logger.error(
                        '{}: Failed to pull running status: error = {}',
                        droplet.name, e)
            else:
                is_running = False
                is_running_str = '[bold red] No'

            # get is_subscribed
            try:
                uid = meta.hotkeys.index(hotkey)
                is_subscribed = True
                is_subscribed_str = '[bold green] Yes'
            except:
                is_subscribed = False
                is_subscribed_str = '[bold red] No'

            # get subscription status.
            if is_subscribed:
                stake = meta.S[uid].item()
                rank = meta.R[uid].item()
                incentive = meta.I[uid].item()
                lastemit = int(meta.block - meta.lastemit[uid])
                lastemit = "[bold green]" + str(
                    lastemit) if lastemit < 3000 else "[bold red]" + str(
                        lastemit)
                address = str(meta.addresses[uid])
                neuron = meta.neuron_endpoints[uid]

                total_stake += stake
                total_rank += rank
                total_incentive += incentive * 14400

                uid_str = str(uid)
                stake_str = '[green]\u03C4{:.5}'.format(stake)
                rank_str = '[green]\u03C4{:.5}'.format(rank)
                incentive_str = '[green]\u03C4{:.5}'.format(incentive * 14400)
                lastemit_str = str(lastemit)
                address_str = str(address)

            else:
                uid_str = '[dim yellow] None'
                stake_str = '[dim yellow] None'
                rank_str = '[dim yellow] None'
                incentive_str = '[dim yellow] None'
                lastemit_str = '[dim yellow] None'
                address_str = '[dim yellow] None'

            # Make query and get response.
            if installed and is_running and is_subscribed and wallet.has_hotkey and neuron != None:
                start_time = time.time()
                result, code = dendrite.forward_text(
                    neurons=[neuron],
                    x=[torch.zeros((1, 1), dtype=torch.int64)])
                end_time = time.time()
                code_to_string = bittensor.utils.codes.code_to_string(
                    code.item())
                code_color = bittensor.utils.codes.code_to_color(code.item())
                code_str = '[' + str(code_color) + ']' + code_to_string
                query_time_str = '[' + str(
                    code_color) + ']' + "" + '{:.3}'.format(end_time -
                                                            start_time) + "s"

                if code.item() == 0:
                    total_success += 1
                    total_time += end_time - start_time
            else:
                code_str = '[dim yellow] N/A'
                query_time_str = '[dim yellow] N/A'

            row = [
                str(droplet.name),
                str(droplet.ip_address),
                str(droplet.region['name']),
                str(droplet.size_slug),
                str(connect_str), branch_str, is_installed_str, is_running_str,
                is_subscribed_str, address_str, uid_str, stake_str, rank_str,
                incentive_str, lastemit_str, query_time_str, code_str,
                hotkey_str, coldkeypub_str
            ]
            return row

        # Get latest droplets.
        droplets = manager.get_all_droplets(tag_name=[TAG])
        if config.names == None:
            config.names = [droplet.name for droplet in droplets]

        subtensor.connect()
        meta.load()
        meta.sync(subtensor=subtensor, force=config.force)
        meta.save()

        TABLE_DATA = []
        with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
            if config.live:
                TABLE_DATA = list(executor.map(get_row, droplets))
            else:
                TABLE_DATA = list(
                    tqdm(executor.map(get_row, droplets), total=len(droplets)))
        TABLE_DATA = [row for row in TABLE_DATA if row != None]
        TABLE_DATA.sort(key=lambda TABLE_DATA: TABLE_DATA[0])

        total_stake_str = '\u03C4{:.7}'.format(total_stake)
        total_rank_str = '\u03C4{:.7}'.format(total_rank)
        total_incentive_str = '\u03C4{:.7}'.format(total_incentive)
        total_time_str = '{:.3}s'.format(
            total_time / len(config.names)) if total_time != 0 else '0.0s'
        total_success_str = '[bold green]' + str(
            total_success) + '/[bold red]' + str(
                len(config.names) - total_success)

        console = Console()
        table = Table(show_footer=False)
        table.title = ("[bold white]Miners")
        table.add_column("[overline white]NAME",
                         str(len(config.names)),
                         footer_style="overline white",
                         style='white')
        table.add_column("[overline white]IP", style='blue')
        table.add_column("[overline white]LOC", style='yellow')
        table.add_column("[overline white]SIZE", style='green')
        table.add_column("[overline white]CONN", style='green')
        table.add_column("[overline white]BRNCH", style='bold purple')
        table.add_column("[overline white]INSTL")
        table.add_column("[overline white]RNG")
        table.add_column("[overline white]SUBD")
        table.add_column("[overline white]ADDR", style='blue')
        table.add_column("[overline white]UID", style='yellow')
        table.add_column("[overline white]STAKE(\u03C4)",
                         total_stake_str,
                         footer_style="overline white",
                         justify='right',
                         style='green',
                         no_wrap=True)
        table.add_column("[overline white]RANK(\u03C4)",
                         total_rank_str,
                         footer_style="overline white",
                         justify='right',
                         style='green',
                         no_wrap=True)
        table.add_column("[overline white]INCN(\u03C4/d)",
                         total_incentive_str,
                         footer_style="overline white",
                         justify='right',
                         style='green',
                         no_wrap=True)
        table.add_column("[overline white]LEmit",
                         justify='right',
                         no_wrap=True)
        table.add_column("[overline white]Qry(sec)",
                         total_time_str,
                         footer_style="overline white",
                         justify='right',
                         no_wrap=True)
        table.add_column("[overline white]Qry(code)",
                         total_success_str,
                         footer_style="overline white",
                         justify='right',
                         no_wrap=True)
        table.add_column("[overline white]HOT",
                         style='bold blue',
                         no_wrap=False)
        table.add_column("[overline white]COLD", style='blue', no_wrap=False)
        table.show_footer = True

        console.clear()
        for row in TABLE_DATA:
            table.add_row(*row)
        table.box = None
        table.pad_edge = False
        table.width = None
        table = Align.center(table)
        return table
Exemple #15
0
    def overview(self):
        r""" Prints an overview for the wallet's colkey.
        """
        console = bittensor.__console__
        wallet = bittensor.wallet( config = self.config )
        subtensor = bittensor.subtensor( config = self.config )
        all_hotkeys = CLI._get_hotkey_wallets_for_wallet( wallet )
        neurons = []
        block = subtensor.block
        with console.status(":satellite: Syncing with chain: [white]{}[/white] ...".format(self.config.subtensor.network)):
            for wallet in tqdm(all_hotkeys):
                nn = subtensor.neuron_for_pubkey( wallet.hotkey.ss58_address )
                if not nn.is_null:
                    neurons.append( nn )
            balance = subtensor.get_balance( wallet.coldkeypub.ss58_address )

        TABLE_DATA = []  
        total_stake = 0.0
        total_rank = 0.0
        total_trust = 0.0
        total_consensus = 0.0
        total_incentive = 0.0
        total_dividends = 0.0
        total_emission = 0     
        for nn, hotwallet in tqdm(list(zip(neurons,all_hotkeys))):
            uid = nn.uid
            active = nn.active
            stake = nn.stake
            rank = nn.rank
            trust = nn.trust
            consensus = nn.consensus
            incentive = nn.incentive
            dividends = nn.dividends
            emission = int(nn.emission * 1000000000)
            last_update = int(block -  nn.last_update)
            row = [
                hotwallet.hotkey_str,
                str(uid), 
                str(active), 
                '{:.5f}'.format(stake),
                '{:.5f}'.format(rank), 
                '{:.5f}'.format(trust), 
                '{:.5f}'.format(consensus), 
                '{:.5f}'.format(incentive),
                '{:.5f}'.format(dividends),
                '{}'.format(emission),
                str(last_update),
                bittensor.utils.networking.int_to_ip( nn.ip) + ':' + str(nn.port) if nn.port != 0 else '[yellow]none[/yellow]', 
                nn.hotkey
            ]
            total_stake += stake
            total_rank += rank
            total_trust += trust
            total_consensus += consensus
            total_incentive += incentive
            total_dividends += dividends
            total_emission += emission
            TABLE_DATA.append(row)
            
        total_neurons = len(neurons)                
        table = Table(show_footer=False)
        table.title = (
            "[white]Wallet - {}:{}".format(self.config.wallet.name, wallet.coldkeypub.ss58_address)
        )
        table.add_column("[overline white]HOTKEY",  str(total_neurons), footer_style = "overline white", style='bold white')
        table.add_column("[overline white]UID",  str(total_neurons), footer_style = "overline white", style='yellow')
        table.add_column("[overline white]ACTIVE", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]STAKE(\u03C4)", '\u03C4{:.5f}'.format(total_stake), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]RANK", '{:.5f}'.format(total_rank), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]TRUST", '{:.5f}'.format(total_trust), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]CONSENSUS", '{:.5f}'.format(total_consensus), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]INCENTIVE", '{:.5f}'.format(total_incentive), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]DIVIDENDS", '{:.5f}'.format(total_dividends), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]EMISSION(\u03C1)", '\u03C1{}'.format(int(total_emission)), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]UPDATED", justify='right', no_wrap=True)
        table.add_column("[overline white]AXON", justify='left', style='dim blue', no_wrap=True) 
        table.add_column("[overline white]HOTKEY", style='dim blue', no_wrap=False)
        table.show_footer = True
        table.caption = "[white]Wallet balance: [green]\u03C4" + str(balance.tao)

        console.clear()
        for row in TABLE_DATA:
            table.add_row(*row)
        table.box = None
        table.pad_edge = False
        table.width = None
        console.print(table)
Exemple #16
0
    def metagraph(self):
        r""" Prints an entire metagraph.
        """
        console = bittensor.__console__
        subtensor = bittensor.subtensor( config = self.config )
        metagraph = bittensor.metagraph( subtensor = subtensor )
        console.print(":satellite: Syncing with chain: [white]{}[/white] ...".format(self.config.subtensor.network))
        metagraph.sync()
        metagraph.save()
        issuance = subtensor.total_issuance
        difficulty = subtensor.difficulty

        TABLE_DATA = [] 
        total_stake = 0.0
        total_rank = 0.0
        total_trust = 0.0
        total_consensus = 0.0
        total_incentive = 0.0
        total_dividends = 0.0
        total_emission = 0  
        for uid in metagraph.uids:
            ep = metagraph.endpoint_objs[uid]
            row = [
                str(ep.uid), 
                '{:.5f}'.format( metagraph.stake[uid]),
                '{:.5f}'.format( metagraph.ranks[uid]), 
                '{:.5f}'.format( metagraph.trust[uid]), 
                '{:.5f}'.format( metagraph.consensus[uid]), 
                '{:.5f}'.format( metagraph.incentive[uid]),
                '{:.5f}'.format( metagraph.dividends[uid]),
                '{}'.format( int(metagraph.emission[uid] * 1000000000)),
                str((metagraph.block.item() - metagraph.last_update[uid].item())),
                str( metagraph.active[uid].item() ), 
                ep.ip + ':' + str(ep.port) if ep.is_serving else '[yellow]none[/yellow]', 
                ep.hotkey[:10],
                ep.coldkey[:10]
            ]
            total_stake += metagraph.stake[uid]
            total_rank += metagraph.ranks[uid]
            total_trust += metagraph.trust[uid]
            total_consensus += metagraph.consensus[uid]
            total_incentive += metagraph.incentive[uid]
            total_dividends += metagraph.dividends[uid]
            total_emission += int(metagraph.emission[uid] * 1000000000)
            TABLE_DATA.append(row)
        total_neurons = len(metagraph.uids)                
        table = Table(show_footer=False)
        table.title = (
            "[white]Metagraph: name: {}, block: {}, N: {}/{}, tau: {}/block, stake: {}, issuance: {}, difficulty: {}".format(subtensor.network, metagraph.block.item(), sum(metagraph.active.tolist()), metagraph.n.item(), bittensor.Balance.from_tao(metagraph.tau.item()), bittensor.Balance.from_tao(total_stake), issuance, difficulty )
        )
        table.add_column("[overline white]UID",  str(total_neurons), footer_style = "overline white", style='yellow')
        table.add_column("[overline white]STAKE(\u03C4)", '\u03C4{:.5f}'.format(total_stake), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]RANK", '{:.5f}'.format(total_rank), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]TRUST", '{:.5f}'.format(total_trust), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]CONSENSUS", '{:.5f}'.format(total_consensus), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]INCENTIVE", '{:.5f}'.format(total_incentive), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]DIVIDENDS", '{:.5f}'.format(total_dividends), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]EMISSION(\u03C1)", '\u03C1{}'.format(int(total_emission)), footer_style = "overline white", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]UPDATED", justify='right', no_wrap=True)
        table.add_column("[overline white]ACTIVE", justify='right', style='green', no_wrap=True)
        table.add_column("[overline white]AXON", justify='left', style='dim blue', no_wrap=True) 
        table.add_column("[overline white]HOTKEY", style='dim blue', no_wrap=False)
        table.add_column("[overline white]COLDKEY", style='dim purple', no_wrap=False)
        table.show_footer = True

        for row in TABLE_DATA:
            table.add_row(*row)
        table.box = None
        table.pad_edge = False
        table.width = None
        console.print(table)