Exemplo n.º 1
0
def _encode_function(flagdef, val):
    assert flagdef.min is not None and flagdef.max is not None
    func_name = flagdef.distribution or "uniform"
    low = flag_util.encode_flag_val(flagdef.min)
    high = flag_util.encode_flag_val(flagdef.max)
    args = [low, high]
    if val is not None:
        initial = flag_util.encode_flag_val(val)
        args.append(initial)
    return "%s[%s]" % (func_name, ":".join(args))
Exemplo n.º 2
0
def _print_run_info_dict(name, val):
    cli.out("%s:" % name)
    for item_name, item_val in _sort_run_info_attr(name, val):
        if isinstance(item_val, list):
            cli.out("  %s:" % item_name)
            for item_item in item_val:
                cli.out("    - %s" % flag_util.encode_flag_val(item_item))
        elif isinstance(item_val, dict):
            cli.out("  %s:" % item_name)
            # Use full YAML formatting for config blocks.
            cli.out(_indent(yaml_util.encode_yaml(item_val), 4))
        else:
            cli.out("  %s: %s" %
                    (item_name, flag_util.encode_flag_val(item_val)))
Exemplo n.º 3
0
def _print_run_info_list(name, val):
    cli.out("%s:" % name)
    for item in val:
        if isinstance(item, dict):
            cli.out("  -")
            for item_name, item_val in sorted(item.items()):
                encoded = _fix_quoted_string(
                    flag_util.encode_flag_val(item_val))
                if "\n" in encoded:
                    cli.out(_indent("%s: |" % item_name, 4))
                    cli.out(_indent(_unindent(encoded), 6))
                else:
                    cli.out(_indent("%s: %s" % (item_name, encoded), 4))
        else:
            cli.out("  - %s" % flag_util.encode_flag_val(item))
Exemplo n.º 4
0
def flags_hash(flags):
    flag_parts = [
        "%s:%s" % (name, flag_util.encode_flag_val(val))
        for name, val in sorted(flags.items())
    ]
    to_hash = "\n".join(flag_parts).encode()
    return hashlib.md5(to_hash).hexdigest()
Exemplo n.º 5
0
def _format_flag_choices_dl(choices, out):
    out.write_heading("Choices")
    out.indent()
    out.write_dl([(flag_util.encode_flag_val(choice.value), "\n\n".join(
        choice.description.split("\n"))) for choice in choices],
                 preserve_paragraphs=True)
    out.dedent()
Exemplo n.º 6
0
def _args_for_flag(name, val, cmd_flag, flag_dest, cmd_args):
    cmd_flag = cmd_flag or CmdFlag()
    if cmd_flag.arg_skip:
        return []
    arg_name = cmd_flag.arg_name or name
    if "--%s" % arg_name in cmd_args:
        log.warning(
            "ignoring flag '%s=%s' because it's shadowed "
            "in the operation cmd as --%s",
            name,
            flag_util.encode_flag_val(val),
            arg_name,
        )
        return []
    elif cmd_flag.arg_switch is not None:
        if cmd_flag.arg_switch == val:
            return ["--%s" % arg_name]
        else:
            return []
    elif val is not None:
        if _splittable(val, cmd_flag):
            encoded = _encode_split_args(val, flag_dest, cmd_flag.arg_split)
            return ["--%s" % arg_name] + encoded if encoded else []
        else:
            return [
                "--%s" % arg_name,
                _encode_flag_arg(val, flag_dest, cmd_flag.arg_split),
            ]
    else:
        return []
Exemplo n.º 7
0
Arquivo: op.py Projeto: yuanbw/guildai
def _cmd_option_args(name, val):
    if val is None:
        return []
    opt = "--%s" % name
    if val is op_util.NO_ARG_VALUE:
        return [opt]
    else:
        return [opt, flag_util.encode_flag_val(val)]
Exemplo n.º 8
0
def _render_label_template(label_template, default_label, flag_vals):
    resolve_vals = {
        "default_label": default_label,
    }
    resolve_vals.update({
        name: flag_util.encode_flag_val(val)
        for name, val in flag_vals.items() if val is not None
    })
    return util.resolve_refs(label_template, resolve_vals, "")
Exemplo n.º 9
0
def _args_for_flag(name, val, cmd_flag, cmd_args):
    cmd_flag = cmd_flag or CmdFlag()
    if cmd_flag.arg_skip:
        return []
    arg_name = cmd_flag.arg_name or name
    if "--%s" % arg_name in cmd_args:
        log.warning(
            "ignoring flag '%s=%s' because it's shadowed "
            "in the operation cmd as --%s", name,
            flag_util.encode_flag_val(val), arg_name)
        return []
    if cmd_flag.arg_switch is not None:
        if cmd_flag.arg_switch == val:
            return ["--%s" % arg_name]
        else:
            return []
    elif val is not None:
        return ["--%s" % arg_name, flag_util.encode_flag_val(val)]
    else:
        return []
Exemplo n.º 10
0
def _trials_table_data(trials):
    names = set()
    data = []
    for i, flags in enumerate(trials):
        row = {"_trial": i + 1}
        data.append(row)
        if flags:
            row.update({name: flag_util.encode_flag_val(flags[name]) for name in flags})
            names.update(flags)
    heading = {name: name for name in names}
    heading["_trial"] = "#"
    return [heading] + data, ["_trial"] + sorted(names)
Exemplo n.º 11
0
def format_attr(val):
    if val is None:
        return ""
    elif isinstance(val, six.string_types):
        return val
    elif isinstance(val, (bool, int, float)):
        return flag_util.encode_flag_val(val)
    elif isinstance(val, list):
        return _format_attr_list(val)
    elif isinstance(val, dict):
        return _format_attr_dict(val)
    else:
        return _format_yaml_block(val)
Exemplo n.º 12
0
def _format_flag_desc(flag, max_flag_len):
    lines = flag.description.split("\n")
    if flag.default is not None:
        fmt_default = flag_util.encode_flag_val(flag.default)
        line1_suffix = "(default is %s)" % fmt_default
    elif flag.required:
        line1_suffix = "(required)"
    elif flag.null_label:
        line1_suffix = "(default is %s)" % flag.null_label
    else:
        line1_suffix = ""
    if lines[0]:
        lines[0] += " "
    lines[0] += line1_suffix
    if flag.choices:
        lines.append(_format_flag_choices(flag.choices, max_flag_len))
    if len(lines) > 1:
        return "\n\n".join(lines) + "\n\b\n"
    else:
        return lines[0]
Exemplo n.º 13
0
def _encode_params(params):
    return {
        name: flag_util.encode_flag_val(val)
        for name, val in params.items()
    }
Exemplo n.º 14
0
def _encode_env_val(val):
    if isinstance(val, six.string_types):
        return val
    return flag_util.encode_flag_val(val)
Exemplo n.º 15
0
def _format_flag_default(flag):
    return " (default is %s)" % flag_util.encode_flag_val(flag.default)
Exemplo n.º 16
0
def _null_label(name, null_labels):
    null_label = null_labels.get(name, "default")
    return flag_util.encode_flag_val(null_label)
Exemplo n.º 17
0
def _null_label(name, opdef):
    flag = opdef.get_flagdef(name)
    if flag and flag.null_label is not None:
        return flag_util.encode_flag_val(flag.null_label)
    return "default"
Exemplo n.º 18
0
def _format_flag_choices_value_list(choices, out):
    vals = [c.value for c in choices]
    fmt_vals = flag_util.encode_flag_val(vals)
    out.write_dl([("Choices:", _strip_list_brackets(fmt_vals))])
Exemplo n.º 19
0
def _format_flag_choices_value_list(choices, out):
    vals = [c.value for c in choices]
    out.write_dl([("Choices:", flag_util.encode_flag_val(vals))])
Exemplo n.º 20
0
def _format_choice_list(choices):
    vals = [c.alias or c.value for c in choices]
    fmt_vals = flag_util.encode_flag_val(vals)
    return _strip_list_brackets(fmt_vals)
Exemplo n.º 21
0
def _render_label_template(label_template, flag_vals):
    resolve_vals = {
        name: flag_util.encode_flag_val(val)
        for name, val in flag_vals.items() if val is not None
    }
    return util.resolve_refs(label_template, resolve_vals, "")