Ejemplo n.º 1
0
def test_creating_elements():

    atomic = factory.create_element(42)
    assert isinstance(atomic, AtomicElement)
    assert atomic.value == 42

    seq = factory.create_element(['a', 'p', 'p', 'l', 'e'])
    assert isinstance(seq, ArrayElement)
    assert seq.serialized() == '["a", "p", "p", "l", "e"]'
    assert ''.join(seq.primitive_value) == 'apple'

    mapping = factory.create_element(OrderedDict((('one', 1), ('two', 2))))
    assert isinstance(mapping, InlineTableElement)
    assert mapping.serialized() == '{one = 1, two = 2}'
Ejemplo n.º 2
0
    def __setitem__(self, key, value):

        new_element = value if isinstance(value, Element) else factory.create_element(value)

        try:

            key_i, value_i = self._find_key_and_value(key)
            # Found, then replace the value element with a new one
            self._sub_elements = self.sub_elements[:value_i] + [new_element] + self.sub_elements[value_i+1:]

        except KeyError:    # Key does not exist, adding anew!

            new_entry = [
                factory.create_string_element(key, bare_allowed=True),
                factory.create_whitespace_element(),
                factory.create_operator_element('='),
                factory.create_whitespace_element(),
                new_element,
            ]

            if self:    # If not empty
                new_entry = [
                    factory.create_operator_element(','),
                    factory.create_whitespace_element(),
                ] + new_entry

            insertion_index = self._find_closing_curly_bracket()
            self._sub_elements = self.sub_elements[:insertion_index] + new_entry + self.sub_elements[insertion_index:]
Ejemplo n.º 3
0
    def __setitem__(self, key, value):

        new_element = value if isinstance(
            value, Element) else factory.create_element(value)

        try:

            key_i, value_i = self._find_key_and_value(key)
            # Found, then replace the value element with a new one
            self._sub_elements = self.sub_elements[:value_i] + [
                new_element
            ] + self.sub_elements[value_i + 1:]

        except KeyError:  # Key does not exist, adding anew!

            new_entry = [
                factory.create_string_element(key, bare_allowed=True),
                factory.create_whitespace_element(),
                factory.create_operator_element('='),
                factory.create_whitespace_element(),
                new_element,
            ]

            if self:  # If not empty
                new_entry = [
                    factory.create_operator_element(','),
                    factory.create_whitespace_element(),
                ] + new_entry

            insertion_index = self._find_closing_curly_bracket()
            self._sub_elements = self.sub_elements[:
                                                   insertion_index] + new_entry + self.sub_elements[
                                                       insertion_index:]
Ejemplo n.º 4
0
    def append(self, v):
        new_entry = [create_element(v)]

        if self:    # If not empty, we need a comma and whitespace prefix!
            new_entry = [
                factory.create_operator_element(','),
                factory.create_whitespace_element(),
            ] + new_entry

        insertion_index = self._find_closing_square_bracket()
        self._sub_elements = self._sub_elements[:insertion_index] + new_entry + \
                             self._sub_elements[insertion_index:]
Ejemplo n.º 5
0
    def _insert(self, key, value):

        value_element = value if isinstance(value, Element) else factory.create_element(value)

        indentation_size = self._detect_indentation_size()
        indentation = [factory.create_whitespace_element(self._detect_indentation_size())] if indentation_size else []

        inserted_elements = indentation + [
            factory.create_string_element(key, bare_allowed=True),
            factory.create_whitespace_element(),
            factory.create_operator_element('='),
            factory.create_whitespace_element(),
            value_element,
            factory.create_newline_element(),
        ]

        insertion_index = self._find_insertion_index()

        self._sub_elements = \
            self.sub_elements[:insertion_index] + inserted_elements + self.sub_elements[insertion_index:]
Ejemplo n.º 6
0
    def _insert(self, key, value):

        value_element = value if isinstance(
            value, Element) else factory.create_element(value)

        indentation_size = self._detect_indentation_size()
        indentation = [
            factory.create_whitespace_element(self._detect_indentation_size())
        ] if indentation_size else []

        inserted_elements = indentation + [
            factory.create_string_element(key, bare_allowed=True),
            factory.create_whitespace_element(),
            factory.create_operator_element('='),
            factory.create_whitespace_element(),
            value_element,
            factory.create_newline_element(),
        ]

        insertion_index = self._find_insertion_index()

        self._sub_elements = \
            self.sub_elements[:insertion_index] + inserted_elements + self.sub_elements[insertion_index:]
Ejemplo n.º 7
0
 def _update(self, key, value):
     _, value_i = self._find_key_and_value(key)
     self._sub_elements[value_i] = value if isinstance(value, Element) else factory.create_element(value)
Ejemplo n.º 8
0
 def _update(self, key, value):
     _, value_i = self._find_key_and_value(key)
     self._sub_elements[value_i] = value if isinstance(
         value, Element) else factory.create_element(value)
Ejemplo n.º 9
0
 def __setitem__(self, i, value):
     value_i, _ = self._find_value(i)
     new_element = value if isinstance(value, Element) else factory.create_element(value)
     self._sub_elements = self.sub_elements[:value_i] + [new_element] + self.sub_elements[value_i+1:]