def dump_element (cls, element, fallback=None): if is_entity(element): return cls.dump_entity(element, fallback=fallback) elif isinstance(element,Comment): return cls.dump_comment(element) else: return element
def entity_diff (self, entities): ediff = EntityDiff(self.id) if is_entity(entities): entities = (self, entities) else: try: entities = [self,] + entities except TypeError: entities = (self,) + entities for key, test in test_params.items(): last = None value = [] is_value_different = False for entity in entities: getter = test[0] if len(test)>0 else key param = getattr(entity, getter) if hasattr(param, '__call__'): nval = param.__call__() else: nval = param if not is_value_different and last: is_value_different = last!=nval value.append(nval) last = nval if is_value_different: ediff[key] = tuple(value) return ediff
def entity(self, id): """returns an entity for a given id """ for item in self: if is_entity(item) and item.id == id: return item raise KeyError('No such entity')
def entity_pos(self, id): """returns the position of an entity in the Structure """ for i, item in enumerate(self): if is_entity(item) and item.id == id: return i raise KeyError('No such entity')
def __contains__(self, id): """returns True if an entity with given id exists """ for item in self: if is_entity(item) and item.id == id: return True return False
def entities_with_path(self, path_prefix): """Returns a dict of all entities from the Structure in a form of d[entity.id] = (entity, path) """ spath = '%s/%s' % (path_prefix, self.id) if path_prefix else self.id return dict([(item.id, (item, spath)) for item in self if is_entity(item)])
def remove_entity(self, id): """removes an entity for the given id or raises KeyError """ for i, item in enumerate(self): if is_entity(item) and item.id == id: del self[i] return True raise KeyError('[%s] No such entity: %s' % (self.id, id))
def dump_element(cls, element): if is_entity(element): return cls.dump_entity(element) elif isinstance(element, Comment): return cls.dump_comment(element) elif isinstance(element, Section): return cls.dump_section(element) else: return element
def modify_entity(self, id, value): """modifies an entity value """ found = False for item in self: if is_entity(item) and item.id == id: item.value = value found = True if found: return True else: raise KeyError('No such entity')
def add(self, item, pos=None): """adds an element (string, entity or comment) to the Structure pos - if given addes an element at given position returns a number representing how many new elements have been added Usually one, but if a new string gets added and is concatanated to previous/next one the value will be 0. """ if is_string(item): # string return self.add_string(item, pos) elif is_entity(item): # Entity return self.add_entity(item, pos) elif isinstance(item, Comment): # Comment return self.add_comment(item, pos) elif item is None: return 0 else: raise Exception('Cannot add element of type "' + type(item).__name__ + '" to the Structure')
def entitylist(self): """Returns an EntityList object with entities from the Structure. """ return EntityList(self.id, *[item for item in self if is_entity(item)])
def entities(self): """ Returns a list of entities from the structure """ return [item for item in self if is_entity(item)]
def __contains__(self, id): """returns True if an entity with given id exists""" for item in self: if is_entity(item) and item.id == id: return True return False
def entities_with_path(self, path_prefix): """Returns a dict of all entities from the Structure in a form of d[entity.id] = (entity, path) """ spath = f"{path_prefix}/{self.id}" if path_prefix else self.id return {item.id: (item, spath) for item in self if is_entity(item)}
def entitylist(self): """Returns an EntityList object with entities from the Structure.""" return EntityList(self.id, *[item for item in self if is_entity(item)])