Ejemplo n.º 1
0
def test_table():
    table = get_test_table()
    r1 = dtu.format_table_plus(table)

    table[1] = dtu.make_row_red(table[1])
    r2 = dtu.format_table_plus(table)

    # print r1
    # print r2
    r2e = dtu.remove_escapes(r2)
    # print r2e
    assert r1 == r2e
Ejemplo n.º 2
0
def format_logs(logs):
    if not logs:
        s = "No logs found."
        return s
    else:
        s = f"Found {len(logs)} logs.\n"

        table = get_logs_description_table(logs)
        dtu.remove_table_field(table, "filename")
        dtu.remove_table_field(table, "topics")
        dtu.remove_table_field(table, "description")
        dtu.remove_table_field(table, "hash bag")
        s += dtu.indent(dtu.format_table_plus(table, colspacing=4), "| ")

        counts = defaultdict(lambda: set())
        for l in list(logs.values()):
            for rname, dtr_yaml in list(l.resources.items()):
                counts[rname].add(dtr_yaml["name"])

        s += "\n\nCount of resources: "
        rsort = sorted(counts, key=lambda _: -len(counts[_]))
        for rname in rsort:
            rcount = len(counts[rname])
            s += f"\n {rcount:3d} {rname}"
            if rcount <= 3:
                s += "  " + " ".join(counts[rname])
        return s
Ejemplo n.º 3
0
def format_enc_publishers(enc, descriptions):
    table = [[
        "name",
        "type",
        "topic",
        "options",
        "description",
    ]]

    for p in list(enc.publishers.values()):
        if p.desc:
            desc = dtu.wrap_line_length(p.desc, 80)
        else:
            desc = "(none)"
        options = []
        if p.queue_size is not None:
            options.append(f"queue_size = {p.queue_size} ")
        if p.latch is not None:
            options.append(f"latch = {p.latch}")

        options = "\n".join(options)
        table.append([p.name, p.type.__name__, p.topic, options, desc])
    if not descriptions:
        dtu.remove_table_field(table, "description")
    return dtu.format_table_plus(table, 2)
Ejemplo n.º 4
0
def format_enc_subscriptions(enc: EasyNodeConfig, descriptions: bool) -> str:
    table = [[
        "name",
        "type",
        "topic",
        "options",
        "process",
        "description",
    ]]

    for p in list(enc.subscriptions.values()):
        if p.desc:
            desc = dtu.wrap_line_length(p.desc, 80)
        else:
            desc = "(none)"
        options = []
        if p.queue_size is not None:
            options.append(f"queue_size = {p.queue_size}")
        if p.latch is not None:
            options.append(f"latch = {p.latch} ")
        if p.timeout is not None:
            options.append(f"timeout = {p.timeout} ")

        options = "\n".join(options)
        table.append(
            [p.name, p.type.__name__, p.topic, options, p.process, desc])
    if not descriptions:
        dtu.remove_table_field(table, "description")
    return dtu.format_table_plus(table, 2)
Ejemplo n.º 5
0
def format_enc_parameters(enc: EasyNodeConfig, descriptions: bool) -> str:
    table = [[
        "name",
        "type",
        "default",
        "description",
    ]]

    for p in list(enc.parameters.values()):
        if p.desc:
            desc = dtu.wrap_line_length(p.desc, 80)
        else:
            desc = "(none)"
        if p.has_default:
            default = p.default
        else:
            default = "(none)"
        if p.type is None:
            t = "(n/a)"
        else:
            t = p.type.__name__
        table.append([p.name, t, default, desc])
    if not descriptions:
        dtu.remove_table_field(table, "description")
    return dtu.format_table_plus(table, 2)
Ejemplo n.º 6
0
def format_families(families: List[EasyAlgoFamily],
                    colorize: bool = True,
                    verbose: bool = True):
    if not families:
        s = "No algorithm families found."
        return s
    else:

        table = [[
            "Family name",
            "interface",
            "pattern",
            "# found",
            "valid",
            "filename",
            "description",
        ]]
        for family in families:
            assert isinstance(family, EasyAlgoFamily)
            row = [
                family.family_name, family.interface, family.instances_pattern
            ]
            if not family.instances:
                row.append("\n(none)")
            else:
                n_valid = len(
                    [_ for _ in list(family.instances.values()) if _.valid])
                n_invalid = len(family.instances) - n_valid
                ss = f"{len(family.instances)}"
                if n_invalid:
                    ss += dtu.make_red(f" ({n_invalid} invalid)")
                row.append(ss)

            if family.valid:
                ss = "yes"
            else:
                ss = "no: " + family.error_if_invalid
            row.append(ss)
            row.append(dtu.friendly_path(family.filename))

            if (not family.valid) and colorize:
                row = dtu.make_row_red(row)

            row.append(family.description.strip())
            table.append(row)

        if not verbose:
            dtu.remove_table_field(table, "filename")

        s = f"Found {len(families)} algorithm families:\n\n"
        s += dtu.indent(dtu.format_table_plus(table, colspacing=4), "   ")

        return s
Ejemplo n.º 7
0
def user_config_summary() -> str:
    db = get_config_db()

    # def apply_to_lines(f, x):
    #     return "\n".join(f(_) for _ in x.split('\n'))
    #
    # def red(s):
    #     red_ = lambda _: termcolor.colored(_, 'red')
    #     return apply_to_lines(red_, s)

    red = lambda x: x

    table = []

    table.append([
        "package name",
        "node name",
        "config_name",
        "effective",
        "extends",
        "valid",
        "error",
        "description",
        "filename",
    ])
    for c in db.configs:
        assert isinstance(c, ConfigInfo)
        d = dtu.truncate_string_right(c.description.replace("\n", " "), 40)
        date = c.date_effective.strftime("%Y-%m-%d")
        if c.valid is None:
            valid = "?"
            valid_error = ""
        else:
            valid = "yes" if c.valid else red("no")
            valid_error = "" if c.valid else red(c.error_if_invalid)

        table.append([
            c.package_name,
            c.node_name,
            c.config_name,
            date,
            c.extends,
            valid,
            valid_error,
            d,
            dtu.friendly_path(c.filename),
        ])

    dtu.remove_table_field(table, "filename")

    s = dtu.format_table_plus(table, colspacing=4)
    return s
Ejemplo n.º 8
0
def config_summary(all_keys: Sequence[str], values: dict,
                   origin: Dict[str, str]) -> str:
    table = []
    table.append(["-" * len(_) for _ in table[0]])
    for k in all_keys:
        if k in values:
            v = yaml.dump(values[k])
            v = v.strip()
            if v.endswith("..."):
                v = v[:-3]
            v = v.strip()
            table.append([k, v, dtu.friendly_path(origin[k])])
        else:
            table.append([k, "(unset)", "(not found)"])
    return dtu.format_table_plus(table, 4)
Ejemplo n.º 9
0
def print_results(analyzers: List[str], results_all, out: str):
    base = os.path.join(out, "statistics")
    yaml_data = dtu.yaml_dump_pretty(results_all)
    dtu.write_str_to_file(yaml_data, os.path.join(base, "statistics.yaml"))
    print((dtu.indent(yaml_data, "print_results ")))

    for a in analyzers:
        dtu.write_str_to_file(dtu.yaml_dump_pretty(results_all[a]),
                              os.path.join(base, f"{a}.table.yaml"))
        s = ""
        s += "\n" + "-" * 10 + f" Results for {a} " + "-" * 10
        table = table_for_analyzer(results_all[a])
        s += "\n" + dtu.indent(dtu.format_table_plus(table, colspacing=3),
                               "  ")
        s += "\n"
        dtu.write_str_to_file(s, os.path.join(base, f"{a}.table.txt"))
Ejemplo n.º 10
0
def format_instances(family, colorize, verbose=False):
    if not family.instances:
        s = (
            f'No instances files found for family "{family.family_name}" (pattern = '
            f"{family.instances_pattern}).\n\n")
        return s
    else:
        s = f'Found {len(family.instances)} instances of algorithm family "{family.family_name}":\n'
        table = []
        table.append([
            "Instance name", "constructor", "parameters", "description",
            "filename"
        ])
        for _ in list(family.instances.values()):
            row = []
            name = _.instance_name
            if (not _.valid) and colorize:
                name = dtu.make_red(name)

            row.append(name)

            row.append(_.constructor)
            row.append(dtu.yaml_dump_pretty(_.parameters))

            row.append(_.description)
            row.append(dtu.friendly_path(_.filename))

            table.append(row)

        if not verbose:
            dtu.remove_table_field(table, "filename")
            dtu.remove_table_field(table, "description")
        s += dtu.indent(dtu.format_table_plus(table, colspacing=4), "| ")

        for _ in list(family.instances.values()):
            if not _.valid:
                msg = _.error_if_invalid
                s += dtu.make_red("\n" +
                                  dtu.indent(msg, "", _.instance_name + ": "))

        return s