Пример #1
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())
Пример #2
0
 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])
Пример #3
0
 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)
Пример #4
0
 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')
Пример #5
0
def test_remove_attribute(class_node: yatiml.Node) -> None:
    assert class_node.has_attribute('attr1')
    class_node.remove_attribute('attr1')
    assert not class_node.has_attribute('attr1')

    class_node.set_attribute('attr1', 10)
    class_node.set_attribute('attr2', 11)
    assert class_node.has_attribute('attr2')
    class_node.remove_attribute('attr2')
    assert not class_node.has_attribute('attr2')

    class_node.remove_attribute('attr2')
    assert not class_node.has_attribute('attr2')
Пример #6
0
    def yatiml_savorize(cls, node: yatiml.Node) -> None:
        text = str(node.get_value())
        parts = text.split('.')
        node.make_mapping()

        # We need to make a yaml.SequenceNode by hand here, since
        # set_attribute doesn't take lists as an argument.
        start_mark = yaml.error.StreamMark('generated node', 0, 0, 0)
        end_mark = yaml.error.StreamMark('generated node', 0, 0, 0)
        item_nodes = list()
        for part in parts[:-1]:
            item_nodes.append(
                yaml.ScalarNode('tag:yaml.org,2002:str', part, start_mark,
                                end_mark))
        ynode = yaml.SequenceNode('tag:yaml.org,2002:seq', item_nodes,
                                  start_mark, end_mark)
        node.set_attribute('namespaces', ynode)
        node.set_attribute('name', parts[-1])
Пример #7
0
 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)
Пример #8
0
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
Пример #9
0
 def yatiml_savorize(cls, node: yatiml.Node) -> None:
     text = str(node.get_value())
     node.make_mapping()
     node.set_attribute('digits', int(text[0:4]))
     node.set_attribute('letters', text[5:7])
Пример #10
0
 def yatiml_sweeten(cls, node: yatiml.Node) -> None:
     node.set_attribute('subclass', 'B2')
Пример #11
0
 def _yatiml_savorize(cls, node: yatiml.Node) -> None:
     if not node.has_attribute('settings'):
         node.set_attribute('settings', None)
     node.map_attribute_to_index('implementations', 'name', 'script')
     node.map_attribute_to_index('resources', 'name', 'num_cores')
Пример #12
0
 def _yatiml_savorize(cls, node: yatiml.Node) -> None:
     # wrap the existing mapping into a new mapping with attribute settings
     setting_values = node.yaml_node
     node.make_mapping()
     node.set_attribute('settings', setting_values)
Пример #13
0
 def _yatiml_sweeten(cls, node: yatiml.Node) -> None:
     node.set_attribute('ymmsl_version', 'v0.1')
     # The above adds the attribute to the end, but we want it at
     # the top; this moves it there.
     node.yaml_node.value.insert(0, node.yaml_node.value[-1])
     del node.yaml_node.value[-1]