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:]
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:]
def _do_table(table_element): elements = table_element.sub_elements # Our iterator index i = float('-inf') def next_key(): return t.find_following(elements, t.predicates.non_metadata, i) def next_assignment(): return t.find_following(elements, t.predicates.op_assignment, next_key()) def next_value(): return t.find_following(elements, t.predicates.non_metadata, next_assignment()) while next_key() >= 0: del elements[next_key() + 1:next_assignment()] del elements[next_assignment() + 1:next_value()] elements.insert(next_assignment(), element_factory.create_whitespace_element(1)) elements.insert(next_value(), element_factory.create_whitespace_element(1)) i = t.find_following(elements, t.predicates.newline, i)
def _do_table(table_element): elements = table_element.sub_elements # Our iterator index i = float('-inf') def next_key(): return t.find_following(elements, t.predicates.non_metadata, i) def next_assignment(): return t.find_following(elements, t.predicates.op_assignment, next_key()) def next_value(): return t.find_following(elements, t.predicates.non_metadata, next_assignment()) while next_key() >= 0: del elements[next_key()+1:next_assignment()] del elements[next_assignment()+1:next_value()] elements.insert(next_assignment(), element_factory.create_whitespace_element(1)) elements.insert(next_value(), element_factory.create_whitespace_element(1)) i = t.find_following(elements, t.predicates.newline, i)
def _do_table(table_element, table_level): elements = table_element.sub_elements # Iterator index i = float('-inf') def first_indent(): return t.find_following(elements, t.predicates.whitespace, i) def next_non_metadata(): return t.find_following(elements, t.predicates.non_metadata, i) def next_newline(): return t.find_following(elements, t.predicates.newline, next_non_metadata()) while next_non_metadata() >= 0: if first_indent() >= 0: del elements[first_indent():next_non_metadata()] elements.insert( next_non_metadata(), element_factory.create_whitespace_element((table_level - 1) * 2)) i = next_newline()
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:]
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:]
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:]
def _do_table(table_elements): # Iterator index i = float('-inf') def next_newline(): return t.find_following(table_elements, t.predicates.newline, i) def next_comment(): return t.find_following(table_elements, t.predicates.comment, i) def last_non_metadata(): return t.find_previous(table_elements, t.predicates.non_metadata, next_comment()) while next_comment() >= 0: if i < last_non_metadata() < next_comment() < next_newline(): del table_elements[last_non_metadata()+1:next_comment()] table_elements.insert(next_comment(), element_factory.create_whitespace_element(char='\t', length=1)) i = next_newline()
def _do_table(table_elements): # Iterator index i = float('-inf') def next_newline(): return t.find_following(table_elements, t.predicates.newline, i) def next_comment(): return t.find_following(table_elements, t.predicates.comment, i) def last_non_metadata(): return t.find_previous(table_elements, t.predicates.non_metadata, next_comment()) while next_comment() >= 0: if i < last_non_metadata() < next_comment() < next_newline(): del table_elements[last_non_metadata() + 1:next_comment()] table_elements.insert( next_comment(), element_factory.create_whitespace_element(char='\t', length=1)) i = next_newline()
def _do_table(table_element, table_level): elements = table_element.sub_elements # Iterator index i = float("-inf") def first_indent(): return t.find_following(elements, t.predicates.whitespace, i) def next_non_metadata(): return t.find_following(elements, t.predicates.non_metadata, i) def next_newline(): return t.find_following(elements, t.predicates.newline, next_non_metadata()) while next_non_metadata() >= 0: if first_indent() >= 0: del elements[first_indent() : next_non_metadata()] elements.insert(next_non_metadata(), element_factory.create_whitespace_element((table_level - 1) * 2)) i = next_newline()