def test_typesec_to_str(): assert (typecheck.typespec_to_str(str)) == "str" assert (typecheck.typespec_to_str( typing.Sequence[str])) == "sequence of str" assert (typecheck.typespec_to_str(typing.Optional[str])) == "optional str" with pytest.raises(NotImplementedError): typecheck.typespec_to_str(dict)
def dump_dicts(opts, keys: typing.List[str]=None): """ Dumps the options into a list of dict object. Return: A list like: { "anticache": { type: "bool", default: false, value: true, help: "help text"} } """ options_dict = {} keys = keys if keys else opts.keys() for k in sorted(keys): o = opts._options[k] t = typecheck.typespec_to_str(o.typespec) option = { 'type': t, 'default': o.default, 'value': o.current(), 'help': o.help, 'choices': o.choices } options_dict[k] = option return options_dict
def dump_defaults(opts): """ Dumps an annotated file with all options. """ # Sort data s = ruamel.yaml.comments.CommentedMap() for k in sorted(opts.keys()): o = opts._options[k] s[k] = o.default txt = o.help.strip() if o.choices: txt += " Valid values are %s." % ", ".join(repr(c) for c in o.choices) else: t = typecheck.typespec_to_str(o.typespec) txt += " Type %s." % t txt = "\n".join(textwrap.wrap(txt)) s.yaml_set_comment_before_after_key(k, before="\n" + txt) return ruamel.yaml.round_trip_dump(s)
def dump_dicts(opts): """ Dumps the options into a list of dict object. Return: A list like: [ { name: "anticache", type: "bool", default: false, value: true, help: "help text"} ] """ options_list = [] for k in sorted(opts.keys()): o = opts._options[k] t = typecheck.typespec_to_str(o.typespec) option = { 'name': k, 'type': t, 'default': o.default, 'value': o.current(), 'help': o.help, 'choices': o.choices } options_list.append(option) return options_list
def test_typesec_to_str(): assert(typecheck.typespec_to_str(str)) == "str" assert(typecheck.typespec_to_str(typing.Sequence[str])) == "sequence of str" assert(typecheck.typespec_to_str(typing.Optional[str])) == "optional str" with pytest.raises(NotImplementedError): typecheck.typespec_to_str(dict)