def add_element(self, element): """ Function that lets a handler add an element to a repository :param element: The element to be added """ if self.__has_id and not hasattr(element, 'id'): raise RepositoryException("Trying to add an id-less element to a repo with id") self._contents.append(element)
def __flush_changes_to_file(self): try: dict_to_save = self.__convert_to_json() with open(self.__file_name, 'w') as file: converted_contents = json.dumps(dict_to_save) file.write(converted_contents) except FileNotFoundError as fnferror: raise RepositoryException(fnferror)
def access_index(self, index): """ Function that accesses the repository at a given index (twin to get_by_id) :param index: The index to return """ if self.__has_id: raise RepositoryException('Trying to access an index in a dictionary repository') else: return self._contents[index]
def id_exists(self, id): """ Check if a certain ID exists in the repository :param id: The ID to look for """ if self.__has_id: if id in self.get_all_ids(): return True return False else: raise RepositoryException('Checking if an ID exists in an ID-less repository')
def get_all_ids(self): """ Function that returns all possible IDs that can be accessed :return: """ if self.__has_id: ids = [] for object in self._contents: ids.append(object.id) return ids else: raise RepositoryException('Trying to get all IDs but this repository doesn\'t keep a dictionary')
def __load_data(self): try: with open(self.__file_name) as file: json_data = file.read() file_contents = json.loads(json_data) for object_data in file_contents: self._contents.append( self.__class_constructor(*object_data.values())) except FileNotFoundError as fnferror: raise RepositoryException(fnferror) except: pass
def get_by_id(self, id): """ Function that returns an element with the corresponding ID :param id: The ID of the element to return :return: The element found """ if self.__has_id: for object in self._contents: if object.id == id: return object raise KeyError("The given ID does not exist") else: raise RepositoryException('Trying to access by ID when there are no ids stored in this repository')
def remove_id(self, id): """ Function that removes an element with a certain ID :param id: The ID to remove """ if self.__has_id: for index, object in enumerate(self._contents[:]): if object.id == id: removed = object del self._contents[index] return removed raise KeyError("The given ID does not exist") else: raise RepositoryException('Trying to delete by ID when there is none in the repository')
def update_attribute(self, id, attribute, value): """ For the element with the given ID, change its given attribute to a given value :param id: The ID of the element :param attribute: The attribute to look for :param value: The new value to assign """ if self.__has_id: try: object = self.get_by_id(id) if hasattr(object, attribute): setattr(object, attribute, value) except KeyError: print('The given ID does not exist') else: raise RepositoryException('This repository has no ID feature')
def search_name(self, target_name): matches = [] elements = self._contents.values() for element in elements: try: if target_name.lower() in element.name.lower(): matches.append(element) except AttributeError: print('Element {} has no attribute \'name\''.format(element)) if len(matches) is 0: raise RepositoryException('No matches found for name {}'.format(target_name)) return matches