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 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 imp_lib(self, filename): """ Import an external library to the default directory :param filename: str - path of the library directory :return: """ try: library_name = os.path.basename(filename) # e = InputManager.import_library(filename, library_name) e = InputManager.imp_new_library(filename) OutputManager2.create_library(self.dir_path, library_name) OutputManager.save_library(e) return e except Exception: return FileNotFoundError(filename)
def remove_collection_from_library(self, library_name, collection_name): """ Deletes a collection :param library_name: str - library name :param collection_name: str - collection name :return: None """ try: lib = self.libraries[library_name] except Exception: return LibraryNotOpenedError(library_name) e = lib.remove_collection(collection_name) if isinstance(e, Error): return e OutputManager2.delete_collection(self.dir_path + "/" + library_name, library_name, collection_name)
def create_library(self, library_name): """ Creates a new library :param library_name: str - library name :return: None """ e = OutputManager2.create_library(self.dir_path, library_name) # If library exists OutputManager returns an error if isinstance(e, Error): return e
def add_object(self, library_name, collection_name, arr): """ Adds an object to a collection :param library_name: str - library name :param collection_name: str - collection name :param arr: str[] - object values :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 data_types = col.get_obj_def().get_obj_data_types() values = [] for i in range(len(arr)): try: if arr[i] == 'None': values.append(None) elif data_types[i] is bool: if arr[i] == 'True': values.append(True) else: values.append(False) elif data_types[i] is str: values.append(arr[i]) elif data_types[i] is int: values.append(int(arr[i])) elif data_types[i] is float: values.append(float(arr[i])) else: values.append(None) except Exception: return ObjectNotCompatibleError() col.add_obj(values) OutputManager2.add_object_to_collection( "{}/{}/Collections/{}.csv".format(self.dir_path, library_name, collection_name), arr)
def remove_library(self, library_name): """ Deletes a library :param library_name: str - library name :return: None """ e = OutputManager2.delete_library(self.dir_path, library_name) if isinstance(e, Error): return e try: del self.libraries[library_name] except Exception: pass
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)