Exemplo n.º 1
0
def test_bedtools_window_sm():
    """
    These two flags have almost the same name, and almost the same description
    """
    flags = [
        Flag(
            synonyms=["-sm"],
            description=
            "Only report hits in B that overlap A on the _same_ strand.",
            args=EmptyFlagArg(),
        ),
        Flag(
            synonyms=["-sm"],
            description=
            "Only report hits in B that overlap A on the _opposite_ strand.",
            args=EmptyFlagArg(),
        ),
        Flag(
            synonyms=["-c"],
            description=
            "For each entry in A, report the number of overlaps with B.",
            args=EmptyFlagArg(),
        ),
    ]
    args = WrapperGenerator().choose_variable_names(flags)
    assert len(set([arg.name for arg in args])) == 3
Exemplo n.º 2
0
def test_same_arg():
    """
    Normally we ignore one-character flag names, and instead try to read their descriptions for a more informative name.
    However, if the descriptions are identical to each other, we have to fall back to the description
    """
    flags = [
        Flag(synonyms=["-a"], description="", args=SimpleFlagArg("SomeThing")),
        Flag(synonyms=["-b"], description="", args=SimpleFlagArg("SomeThing")),
    ]
    names = WrapperGenerator().choose_variable_names(flags)
    assert names[0].name == "a"
    assert names[1].name == "b"
Exemplo n.º 3
0
def test_snake_short(snake_gen):
    flag = Flag(synonyms=["-t"],
                description="number of threads [1]",
                args=EmptyFlagArg())
    names = snake_gen.choose_variable_names([flag], length=2)
    assert "number" in names[0].name
    assert "threads" in names[0].name
Exemplo n.º 4
0
def parse_help(cmd: typing.Collection[str], text: str, max_length=1000) -> Command:
    """
    Parse a string of help text into a Command. Use this if you already have run the executable and extracted the
    help text yourself

    :param cmd: List of arguments used to generate this help text, e.g. ['bwa', 'mem']
    :param text: The help text to parse
    :param max_length: If the input text has more than this many lines, no attempt will be made to parse the file (as
        it's too large, will likely take a long time, and there's probably an underlying problem if this has happened).
        In this case, an empty Command will be returned
    """
    if len(text.splitlines()) > max_length:
        return Command(list(cmd))

    help_command = CliParser().parse_command(name=cmd, cmd=text)
    usage_command = UsageParser().parse_usage(list(cmd), text)

    # Combine the two commands by picking from the help_command where possible, otherwise falling back on the usage
    fields = dict(
        help_text=text,
        # Use the help command's positionals preferentially, but fall back to usage
        positional=help_command.positional or usage_command.positional,
        # Combine the flags from both help and usage
        named=list(Flag.combine([help_command.named, usage_command.named])),
    )
    for field in attr.fields(Command):
        fields[field.name] = (
            fields.get(field.name)
            or getattr(help_command, field.name)
            or getattr(usage_command, field.name)
        )

    return Command(**fields)
Exemplo n.º 5
0
def test_snake_long(snake_gen):
    flag = Flag(
        synonyms=["-g", "--genomepaths", "--genomefolders"],
        description="number of threads [1]",
        args=EmptyFlagArg(),
    )
    names = snake_gen.choose_variable_names([flag], length=2)
    assert names[0].name == "genome_folders"
Exemplo n.º 6
0
def test_name_to_words_symbol(gen):
    """
    Check that we can get an argument name even if the argument's flag is a symbol
    """
    arg = Flag(
        synonyms=["-@"],
        description="Number of additional threads to use",
        args=EmptyFlagArg(),
    )

    name = gen.choose_variable_names([arg])[0].name
    assert name == "at"
Exemplo n.º 7
0
def test_samtools_dict_output():
    gen = WdlGenerator()
    arg = Flag(
        synonyms=["-o", "--output"],
        description="file to write out dict file [stdout]",
        args=SimpleFlagArg(name="str"),
    )
    name = gen.choose_variable_names([arg])[0].name
    # The WDL converter should avoid naming a variable "output" since that's a WDL keyword
    assert name != "output"

    # Also, since we have a description, the generator shouldn't choose the lazy option of var_output
    assert name != "var_output"
Exemplo n.º 8
0
def test_name_to_words(gen):
    """
    Check that we can get an argument name even if the argument's flag is a symbol
    """
    arg = Flag(
        synonyms=["--genomepaths"],
        description="",
        args=EmptyFlagArg(),
    )

    name = gen.choose_variable_names([arg])[0].name
    assert "genome" in name
    assert "paths" in name
Exemplo n.º 9
0
def test_camel_short(camel_gen):
    flag = Flag(synonyms=["-t"],
                description="number of threads [1]",
                args=EmptyFlagArg())
    names = camel_gen.choose_variable_names([flag], length=3)
    assert names[0].name == "numberOfThreads"
Exemplo n.º 10
0
            hint["class"] == "DockerRequirement"
            and hint["dockerPull"] == container for hint in parsed_cwl["hints"]
        ])

    wdl = WdlGenerator().save_to_string(intersect)
    parsed_wdl = parse_document(wdl).tasks[0]
    assert parsed_wdl.runtime["docker"].literal.value == container


@pytest.mark.parametrize(
    "flag,cwltype,wdltype",
    [
        [
            Flag(
                synonyms=["--some-flag"],
                optional=True,
                args=SimpleFlagArg("string"),
                description="",
            ),
            "string?",
            "String?",
        ],
        [
            Flag(
                synonyms=["--some-flag"],
                optional=False,
                args=SimpleFlagArg("string"),
                description="",
            ),
            "string",
            "String",
        ],
Exemplo n.º 11
0
            ),
        ),
    ],
)
def test_type_inference(string, typ):
    inferred_type = infer_type(string)
    assert inferred_type == typ


@pytest.mark.parametrize(
    "flag,typ",
    [
        [
            Flag(
                description=
                "Filename to output the counts to instead of stdout.",
                synonyms=["-c", "--counts_output"],
                args=SimpleFlagArg("OUTPUT_FILENAME"),
            ),
            CliFile(output=True),
        ],
        pytest.param(
            Flag(
                description=
                "redirect output to specified file\ndefault: undefined",
                synonyms=["-o"],
                args=EmptyFlagArg(),
            ),
            CliFile(output=True),
            marks=pytest.mark.xfail(
                reason=
                "Because the help doesn't indicate an argument, we can't know that this is an output file"
Exemplo n.º 12
0
def test_bwt2sa_i(gen):
    arg = Flag(synonyms=["-i"], description="", args=SimpleFlagArg(name="32"))

    name = gen.choose_variable_names([arg])[0].name
    # 32 isn't a valid variable name, so the only option here is to use the letter i
    assert name == "i"
Exemplo n.º 13
0
def visit_short_flag_list(s, loc, toks):
    return [
        Flag.from_synonyms(
            [FlagSynonym(name="-" + flag, argtype=EmptyFlagArg())],
            description=None) for flag in toks[1:]
    ]
Exemplo n.º 14
0

options_placeholder = (Regex(
    "options?", flags=re.IGNORECASE).suppress().setName("OptionsPlaceholder"))

list_element = (
    (OneOrMore(options_placeholder ^ mandatory_element ^ variable_element) +
     Literal(".")[2, 3] +
     Optional(options_placeholder ^ mandatory_element ^ variable_element)
     ).setParseAction(visit_list_element).setName("list_element"))
"""
When one or more arguments are allowed, e.g. "<in2.bam> ... <inN.bam>"
"""

usage_flag = (And([flag_with_arg
                   ]).setParseAction(lambda s, loc, toks: Flag.from_synonyms(
                       toks, description="")).setName("usage_flag"))

usage_element <<= Or([
    optional_section,
    list_element,
    # short_flag_list,
    usage_flag,
    variable_element,
    options_placeholder,
    mandatory_element,
]).setName("usage_element")

stack = [1]


def visit_usage(s, loc, toks):