def test_datatype_is_CommentedSeq(self): c = CommentedSeq() c.insert(0, "key") c.insert(1, "to") c2 = CommentedMap() c2.insert(0, "to", "from") c2.insert(1, "__from__", "to") c.insert(2, c2) result = CommentedSeq() result.append("key") result.append("to") result.append("to") self.assertEqual(result, parse_for_variable_hierarchies(c, "__from__"))
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
def test_parse_for_variable_hierarchies(self): data = 'testStr' keyword = 'Key' self.assertEqual(data, parse_for_variable_hierarchies(data, keyword)) data = CommentedSeq() data.insert(0, 'First') data.insert(1, 'Second') data.insert(2, 'Third') data.insert(3, 'Key') self.assertEqual('Key', parse_for_variable_hierarchies(data, keyword)) data = CommentedMap() data.insert(0, 'First', 'test1') data.insert(1, 'Second', 'test2') data.insert(2, 'Third', 'test3') data.insert(3, 'Key', 'test4') self.assertEqual('test4', parse_for_variable_hierarchies(data, keyword))