def yatiml_sweeten(self, node: yatiml.Node) -> None: namespace_nodes = node.get_attribute('namespaces').seq_items() namespaces = list(map(yatiml.Node.get_value, namespace_nodes)) namespace_str = '.'.join(namespaces) name = node.get_attribute('name').get_value() node.set_value('{}.{}'.format(namespace_str, name))
def test_is_scalar(class_node: yatiml.Node) -> None: assert class_node.get_attribute('attr1').is_scalar() assert class_node.get_attribute('attr1').is_scalar(int) assert class_node.get_attribute('null_attr').is_scalar(None) assert not class_node.is_scalar() with pytest.raises(ValueError): class_node.get_attribute('attr1').is_scalar(dict)
def test_seq_attribute_to_map(class_node: yatiml.Node, class_node_dup_key: yatiml.Node) -> None: assert class_node.has_attribute('list1') assert class_node.get_attribute('list1').is_sequence() class_node.seq_attribute_to_map('list1', 'item_id', 'price') assert class_node.has_attribute_type('list1', dict) attr_node = class_node.get_attribute('list1') assert attr_node.is_mapping() assert attr_node.has_attribute_type('item1', float) item1_node = attr_node.get_attribute('item1') assert item1_node.get_value() == 100.0 assert attr_node.has_attribute_type('item2', dict) item2_node = attr_node.get_attribute('item2') assert not item2_node.has_attribute('item_id') assert item2_node.has_attribute('price') assert item2_node.has_attribute('on_sale') # check that it fails silently if the attribute is missing or not a list class_node.seq_attribute_to_map('non_existent_attribute', 'item_id') class_node.seq_attribute_to_map('attr1', 'item_id') # check that it raises with strict=True and duplicate keys with pytest.raises(yatiml.SeasoningError): class_node_dup_key.seq_attribute_to_map('dup_list', 'item_id', strict=True)
def test_rename_attribute(class_node: yatiml.Node) -> None: assert class_node.has_attribute('attr1') assert not class_node.has_attribute('attr2') attr1_value = class_node.get_attribute('attr1').get_value() class_node.rename_attribute('attr1', 'attr2') assert not class_node.has_attribute('attr1') assert class_node.has_attribute('attr2') assert class_node.get_attribute('attr2').get_value() == attr1_value # make sure that this does not raise class_node.rename_attribute('non_existent_attribute', 'attr3')
def test_map_attribute_to_seq(class_node: yatiml.Node) -> None: assert class_node.has_attribute_type('dict1', dict) class_node.map_attribute_to_seq('dict1', 'item_id', 'price') assert class_node.has_attribute_type('dict1', list) attr_node = class_node.get_attribute('dict1') assert len(attr_node.seq_items()) == 3 first_item_node = attr_node.seq_items()[0] assert first_item_node.has_attribute('item_id') assert first_item_node.has_attribute('price') first_item_id = first_item_node.get_attribute('item_id').get_value() second_item_node = attr_node.seq_items()[1] assert second_item_node.has_attribute('item_id') assert second_item_node.has_attribute('price') second_item_id = second_item_node.get_attribute('item_id').get_value() third_item_node = attr_node.seq_items()[2] assert third_item_node.has_attribute('item_id') assert third_item_node.has_attribute('price') third_item_id = third_item_node.get_attribute('item_id').get_value() assert first_item_id == 'item1' assert second_item_id == 'item2' assert third_item_id == 'item3' assert ((first_item_id == 'item1' and second_item_id == 'item2') or (first_item_id == 'item2' and second_item_id == 'item1'))
def test_map_attribute_to_index(class_node: yatiml.Node) -> None: class_node.map_attribute_to_index('dict1', 'item_id', 'price') assert class_node.has_attribute_type('dict1', dict) attr_node = class_node.get_attribute('dict1') assert attr_node.is_mapping() assert attr_node.has_attribute('item1') item1_node = attr_node.get_attribute('item1') assert len(item1_node.yaml_node.value) == 2 assert item1_node.has_attribute('item_id') assert item1_node.get_attribute('item_id').get_value() == 'item1' assert item1_node.has_attribute('price') assert item1_node.get_attribute('price').get_value() == 100.0 assert attr_node.has_attribute('item2') item2_node = attr_node.get_attribute('item2') assert len(item2_node.yaml_node.value) == 2 assert item2_node.has_attribute('item_id') assert item2_node.get_attribute('item_id').get_value() == 'item2' assert item2_node.has_attribute('price') assert item2_node.get_attribute('price').get_value() == 200.0 assert attr_node.has_attribute('item3') item3_node = attr_node.get_attribute('item3') assert len(item3_node.yaml_node.value) == 2 assert item3_node.has_attribute('item_id') assert item3_node.get_attribute('item_id').get_value() == 'item3' assert item3_node.has_attribute('price') assert item3_node.get_attribute('price').get_value() == 150.0
def _yatiml_sweeten(cls, node: yatiml.Node) -> None: multiplicity = node.get_attribute('multiplicity') items = multiplicity.seq_items() if len(items) == 0: node.remove_attribute('multiplicity') elif len(items) == 1: node.set_attribute('multiplicity', items[0].get_value())
def test_index_attribute_to_map1(class_node: yatiml.Node) -> None: class_node.index_attribute_to_map('index1', 'item_id') assert class_node.has_attribute('index1') new_mapping = class_node.get_attribute('index1') assert new_mapping.is_mapping() assert new_mapping.has_attribute('item1') item1_mapping = new_mapping.get_attribute('item1') assert item1_mapping.is_mapping() assert not item1_mapping.has_attribute('item_id') assert item1_mapping.has_attribute('price') assert new_mapping.has_attribute('item2') item2_mapping = new_mapping.get_attribute('item2') assert item2_mapping.is_mapping() assert not item2_mapping.has_attribute('item_id') assert item2_mapping.has_attribute('price') assert new_mapping.has_attribute('item3') item3_mapping = new_mapping.get_attribute('item3') assert item3_mapping.is_mapping() assert not item3_mapping.has_attribute('item_id') assert item3_mapping.has_attribute('price') assert item3_mapping.has_attribute('on_sale') # check that it fails silently if the attribute is missing or not a mapping class_node.index_attribute_to_map('non_existent_attribute', 'item_id') class_node.index_attribute_to_map('attr1', 'item_id')
def _yatiml_sweeten(cls, node: yatiml.Node) -> None: int_to_str = { 5: 'five', 6: 'six', 7: 'seven' } int_val = int(node.get_attribute('age').get_value()) if int_val in int_to_str: node.set_attribute('age', int_to_str[int_val])
def _yatiml_savorize(cls, node: yatiml.Node) -> None: if node.has_attribute('multiplicity'): if node.has_attribute_type('multiplicity', int): attr = node.get_attribute('multiplicity') start_mark = attr.yaml_node.start_mark end_mark = attr.yaml_node.end_mark new_seq = yaml.nodes.SequenceNode( 'tag:yaml.org,2002:seq', [attr.yaml_node], start_mark, end_mark) node.set_attribute('multiplicity', new_seq)
def _yatiml_sweeten(cls, node: yatiml.Node) -> None: if node.get_attribute('model').is_scalar(type(None)): node.remove_attribute('model') if node.get_attribute('settings').is_scalar(type(None)): node.remove_attribute('settings') if len(node.get_attribute('settings').yaml_node.value) == 0: node.remove_attribute('settings') impl = node.get_attribute('implementations') if (impl.is_scalar(type(None)) or impl.is_mapping() and impl.is_empty()): node.remove_attribute('implementations') node.index_attribute_to_map('implementations', 'name', 'script') res = node.get_attribute('resources') if (res.is_scalar(type(None)) or res.is_mapping() and res.is_empty()): node.remove_attribute('resources') node.index_attribute_to_map('resources', 'name', 'num_cores')
def test_set_attribute(class_node: yatiml.Node) -> None: assert class_node.get_attribute('attr1').get_value() == 42 class_node.set_attribute('attr1', 43) attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:int' assert attr1.yaml_node.value == '43' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None class_node.set_attribute('attr1', 'test') attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:str' assert attr1.yaml_node.value == 'test' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None class_node.set_attribute('attr1', 3.14) attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:float' assert attr1.yaml_node.value == '3.14' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None class_node.set_attribute('attr1', True) attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:bool' assert attr1.yaml_node.value == 'true' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None class_node.set_attribute('attr1', None) attr1 = class_node.get_attribute('attr1') assert attr1.yaml_node.tag == 'tag:yaml.org,2002:null' assert attr1.yaml_node.value == '' assert attr1.yaml_node.start_mark is not None assert attr1.yaml_node.end_mark is not None assert not class_node.has_attribute('attr2') class_node.set_attribute('attr2', 'testing') attr2 = class_node.get_attribute('attr2') assert attr2.yaml_node.value == 'testing' assert attr2.yaml_node.start_mark is not None assert attr2.yaml_node.end_mark is not None node = yaml.ScalarNode('tag:yaml.org,2002:str', 'testnode') class_node.set_attribute('attr3', node) assert class_node.get_attribute('attr3').yaml_node == node with pytest.raises(TypeError): class_node.set_attribute('attr4', class_node) # type: ignore
def _yatiml_savorize(cls, node: yatiml.Node) -> None: str_to_int = { 'five': 5, 'six': 6, 'seven': 7, } if node.has_attribute_type('age', str): str_val = node.get_attribute('age').get_value() if str_val in str_to_int: node.set_attribute('age', str_to_int[str_val]) else: raise yatiml.SeasoningError('Invalid age string')
def _yatiml_sweeten(cls, node: yatiml.Node) -> None: script_node = node.get_attribute('script') if script_node.is_scalar(str): text = cast(str, script_node.get_value()) if '\n' in text: # make a sequence of lines lines = text.split('\n') start_mark = node.yaml_node.start_mark end_mark = node.yaml_node.end_mark lines_nodes = [ yaml.ScalarNode( 'tag:yaml.org,2002:str', line, start_mark, end_mark) for line in lines] seq_node = yaml.SequenceNode( 'tag:yaml.org,2002:seq', lines_nodes, start_mark, end_mark) node.set_attribute('script', seq_node)
def test_index_attribute_to_map2(class_node: yatiml.Node) -> None: class_node.index_attribute_to_map('index1', 'item_id', 'price') assert class_node.has_attribute('index1') new_mapping = class_node.get_attribute('index1') assert new_mapping.is_mapping() assert new_mapping.has_attribute('item1') item1 = new_mapping.get_attribute('item1') print(item1.yaml_node) assert item1.is_scalar(float) assert item1.get_value() == 100.0 assert new_mapping.has_attribute('item2') item2 = new_mapping.get_attribute('item2') assert item2.is_scalar(float) assert item2.get_value() == 200.0 assert new_mapping.has_attribute('item3') item3_mapping = new_mapping.get_attribute('item3') assert item3_mapping.is_mapping() assert not item3_mapping.has_attribute('item_id') assert item3_mapping.has_attribute('price') assert item3_mapping.has_attribute('on_sale')
def _yatiml_sweeten(cls, node: yatiml.Node) -> None: node.seq_attribute_to_map('components', 'name', 'implementation') if len(node.get_attribute('conduits').seq_items()) == 0: node.remove_attribute('conduits') node.seq_attribute_to_map('conduits', 'sender', 'receiver')
def test_get_attribute(class_node: yatiml.Node) -> None: assert class_node.get_attribute('attr1').yaml_node.value == '42' with pytest.raises(yatiml.SeasoningError): class_node.get_attribute('non_existent_attribute')
def yatiml_sweeten(self, node: yatiml.Node) -> None: digits = node.get_attribute('digits').get_value() letters = node.get_attribute('letters').get_value() node.set_value('{} {}'.format(digits, letters))
def test_is_mapping(class_node: yatiml.Node) -> None: assert class_node.is_mapping() assert not class_node.get_attribute('attr1').is_mapping() assert not class_node.get_attribute('list1').is_mapping()
def test_get_value(class_node: yatiml.Node) -> None: assert class_node.get_attribute('attr1').get_value() == 42
def test_is_sequence(class_node: yatiml.Node) -> None: assert not class_node.is_sequence() assert class_node.get_attribute('list1').is_sequence() assert not class_node.get_attribute('attr1').is_sequence()