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))
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)))
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))
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()
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()
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 []
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)]
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, "")
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 []
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)
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)
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]
def _encode_params(params): return { name: flag_util.encode_flag_val(val) for name, val in params.items() }
def _encode_env_val(val): if isinstance(val, six.string_types): return val return flag_util.encode_flag_val(val)
def _format_flag_default(flag): return " (default is %s)" % flag_util.encode_flag_val(flag.default)
def _null_label(name, null_labels): null_label = null_labels.get(name, "default") return flag_util.encode_flag_val(null_label)
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"
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))])
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))])
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)
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, "")