def remove_object_from_collection(self, library_name, collection_name, index): """ Removes an object from a collection :param library_name: str - library name :param collection_name: str - collection name :param index: int - index of the object to be removed :return: None """ try: lib = self.libraries[library_name] except Exception: return LibraryNotOpenedError(library_name) col = lib.get_collection(collection_name) if isinstance(col, Error): return col index = int(index) if index >= col.col_size(): return ObjectIndexOutOfBound(str(index)) col.remove_obj(index) OutputManager2.delete_collection(self.dir_path + "/" + library_name, library_name, collection_name) OutputManager2.create_collection(self.dir_path, library_name, col)
def create_collection(self, library_name, collection_name, object_type): """ Creates a new collection and adds it to a library :param library_name: str - library name :param collection_name: str - collection name :param object_type: str - name of the object type :return: """ try: lib = self.libraries[library_name] except Exception: return LibraryNotOpenedError(library_name) try: obj_type = self.objects[object_type] except Exception: return ObjectDoesNotExistError(object_type) col = Collection(collection_name, obj_type) e = lib.add_collection(col) # If collection already exists Library returns an error if isinstance(e, Error): return e else: OutputManager2.create_collection(self.dir_path, library_name, col)
def imp_col(self, file_path, library_name): try: lib = self.libraries[library_name] except Exception: return LibraryNotOpenedError(library_name) try: filereader = open(file_path, 'r') except Exception: return FileNotFoundError(file_path) file = filereader.read().split("\n") # Path of file with data col_path = file[0] # Name of collection col_name = file[1] # Name of object obj_name = file[2] # Object attributes obj_attr = file[3] # Convert the object attributes to a string att = obj_attr.split(",") att_dict = {} for a in att: split = a.split(":") att_dict[split[0]] = split[1] obj = ObjectType(obj_name, att_dict) data_types = obj.get_obj_data_types() try: col = InputManager.import_collection(col_path, Collection(col_name, obj), data_types) except Exception: return FileNotFoundError(col_path) lib.add_collection(col) OutputManager2.create_collection(self.dir_path, library_name, col)