Exemplo n.º 1
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:]
Exemplo 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:]
Exemplo n.º 3
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:]
Exemplo n.º 4
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:]
Exemplo 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:]