1from prettytoml.elements.table import TableElement 2 3 4class FreshTable(TableElement): 5 """ 6 A fresh TableElement that appended itself to each of parents when it first gets written to at most once. 7 8 parents is a sequence of objects providing an append_fresh_table(TableElement) method 9 """ 10 11 def __init__(self, parent, name, is_array=False): 12 TableElement.__init__(self, sub_elements=[]) 13 14 self._parent = parent 15 self._name = name 16 self._is_array = is_array 17 18 # As long as this flag is false, setitem() operations will append the table header and this table 19 # to the toml_file's elements 20 self.__appended = False 21 22 @property 23 def name(self): 24 return self._name 25 26 @property 27 def is_array(self): 28 return self._is_array 29 30 def _append_to_parent(self): 31 """ 32 Causes this ephemeral table to be persisted on the TOMLFile. 33 """ 34 35 if self.__appended: 36 return 37 38 if self._parent is not None: 39 self._parent.append_fresh_table(self) 40 41 self.__appended = True 42 43 def __setitem__(self, key, value): 44 TableElement.__setitem__(self, key, value) 45 self._append_to_parent() 46