Example #1
0
def test_recipe():
    seq = CommentedSeq(["importlib-metadata >=0.12", "pytest"])
    seq.yaml_add_eol_comment("[py<38]", 0)
    item = RecipeItem(0, seq)
    assert item.value == "importlib-metadata >=0.12"
    assert item.selector == "py<38"
    assert str(item) == "importlib-metadata >=0.12   # [py<38]"

    item.value = "importlib-metadata"
    item.selector = "py35"
    assert item.value == "importlib-metadata"
    assert item.selector == "py35"
    assert seq.ca.items[0][0].value == " # [py35]"
    assert seq[0] == "importlib-metadata"
    assert str(item) == "importlib-metadata   # [py35]"
Example #2
0
def generate_example_input(
    inptype: Optional[CWLOutputType],
    default: Optional[CWLOutputType],
) -> Tuple[Any, str]:
    """Convert a single input schema into an example."""
    example = None
    comment = ""
    defaults = {
        "null":
        "null",
        "Any":
        "null",
        "boolean":
        False,
        "int":
        0,
        "long":
        0,
        "float":
        0.1,
        "double":
        0.1,
        "string":
        "a_string",
        "File":
        yaml.comments.CommentedMap([("class", "File"),
                                    ("path", "a/file/path")]),
        "Directory":
        yaml.comments.CommentedMap([("class", "Directory"),
                                    ("path", "a/directory/path")]),
    }  # type: CWLObjectType
    if isinstance(inptype, MutableSequence):
        optional = False
        if "null" in inptype:
            inptype.remove("null")
            optional = True
        if len(inptype) == 1:
            example, comment = generate_example_input(inptype[0], default)
            if optional:
                if comment:
                    comment = "{} (optional)".format(comment)
                else:
                    comment = "optional"
        else:
            example = CommentedSeq()
            for index, entry in enumerate(inptype):
                value, e_comment = generate_example_input(entry, default)
                example.append(value)
                example.yaml_add_eol_comment(e_comment, index)
            if optional:
                comment = "optional"
    elif isinstance(inptype, Mapping) and "type" in inptype:
        if inptype["type"] == "array":
            first_item = cast(MutableSequence[CWLObjectType],
                              inptype["items"])[0]
            items_len = len(cast(Sized, inptype["items"]))
            if items_len == 1 and "type" in first_item and first_item[
                    "type"] == "enum":
                # array of just an enum then list all the options
                example = first_item["symbols"]
                if "name" in first_item:
                    comment = u'array of type "{}".'.format(first_item["name"])
            else:
                value, comment = generate_example_input(inptype["items"], None)
                comment = "array of " + comment
                if items_len == 1:
                    example = [value]
                else:
                    example = value
            if default is not None:
                example = default
        elif inptype["type"] == "enum":
            symbols = cast(List[str], inptype["symbols"])
            if default is not None:
                example = default
            elif "default" in inptype:
                example = inptype["default"]
            elif len(cast(Sized, inptype["symbols"])) == 1:
                example = symbols[0]
            else:
                example = "{}_enum_value".format(inptype.get("name", "valid"))
            comment = u'enum; valid values: "{}"'.format('", "'.join(symbols))
        elif inptype["type"] == "record":
            example = yaml.comments.CommentedMap()
            if "name" in inptype:
                comment = u'"{}" record type.'.format(inptype["name"])
            for field in cast(List[CWLObjectType], inptype["fields"]):
                value, f_comment = generate_example_input(field["type"], None)
                example.insert(0, shortname(cast(str, field["name"])), value,
                               f_comment)
        elif "default" in inptype:
            example = inptype["default"]
            comment = u'default value of type "{}".'.format(inptype["type"])
        else:
            example = defaults.get(cast(str, inptype["type"]), str(inptype))
            comment = u'type "{}".'.format(inptype["type"])
    else:
        if not default:
            example = defaults.get(str(inptype), str(inptype))
            comment = u'type "{}"'.format(inptype)
        else:
            example = default
            comment = u'default value of type "{}".'.format(inptype)
    return example, comment