Ejemplo n.º 1
0
def test_blank_comment():
    blank = ""
    assert argutils.format_comment(blank) == ""
    assert argutils.format_comment(None) == ""


    
Ejemplo n.º 2
0
def to_config(cmd_name, argsdict, desc=None):
    """Create an INI-style config file from a given dictionary of arguments.

    :param cmd_name: name of the command (used in the config section header)
    :param argsdict: a dictionary of arguments, as provided by argutils.read.*
    :param desc: (optional) a description of the command, if one is not provided
    by the argsdict
    :returns: a string representation of the config file, which can be written 
    to a file
    """ 

    if not isinstance(argsdict, OrderedDict):
        raise ValueError(
            "Arguments dictionary is unordered: output order will be random."
        )

    CFG_LINE_STR = "{key} = {value}\n"
    CFG_SECTION_STR = "[{header}]\n"

    # The string that will hold the final config file contents
    out = ""

    # Use the description provided if it's missing from the argsdict
    if META_KEY in argsdict:
        section_desc = argsdict[META_KEY].get(DESC_KEY, desc)
    else:
        section_desc = desc

    out += format_comment(section_desc, quote="## ")
    out += CFG_SECTION_STR.format(header=cmd_name)

    for argname, argvals in six.iteritems(argsdict):
        # Skip the metadata section (since we handled already)
        if argname == META_KEY:
            continue

        if EXCLUDE_FLAG in argvals:
            continue

        description = format_comment(argvals.get(DESC_KEY, ""))
        default = argvals.get("default", "")
        line = CFG_LINE_STR.format(key=argname, value=default)
        out += description + line

    return out