def on_deleted(self, event): path = event.src_path if event.is_directory is True or not Utils.get_directory_from_path( path) == self.files_dir_path: return name = Utils.get_file_name_from_path(path) if name in self.info_container.get_list(): self.info_container.remove_element(name)
def on_modified(self, event): path = event.src_path if event.is_directory is True or not Utils.get_directory_from_path( path) == self.files_dir_path: return name = Utils.get_file_name_from_path(path) if name not in self.info_container.get_list(): state = self.json_file_manager.load_file_to_data_base(path) if state: self.info_container.append_list(name)
def on_moved(self, event): if event.is_directory is True: return path = event.src_path new_path = event.dest_path name = Utils.get_file_name_from_path(path) new_dir = Utils.get_directory_from_path(new_path) new_name = Utils.get_file_name_from_path(new_path) if name in self.info_container.get_list( ) and Utils.get_file_size(path) > 0: self.info_container.remove_element(name) if not new_dir == self.files_dir_path: return self.info_container.append_list(new_name)
def init_list(self): self.files_name_list.clear() if not Utils.check_path_exist(self.dump_path): dump_file = open(self.dump_path, "w") dump_file.close() dump_file = open(self.dump_path, "r") file_names = dump_file.read().split("\n") for file_name in file_names: if not file_name == "": self.append_list(file_name) dump_file.close()
def __init__(self): if not Utils.check_path_exist(self.CONFIG_FILE_PATH): print("Config file is missing! Creating config with default parameters...") set_config(self.CONFIG_FILE_PATH) config_dict = get_config_general_section(self.CONFIG_FILE_PATH) if not config_dict_validation(config_dict): print("Config file is incorrect! Exit...") return self.files_dir_path = config_dict["observer_directory_path"] if not Utils.check_path_exist(self.files_dir_path): print("Directory is missing! Please specify directory! Exit...") return db_login = config_dict["data_base_login"] db_password = config_dict["data_base_password"] db_port = int(config_dict["data_base_port"]) db_name = config_dict["data_base_name"] connection_result = cdbm.check_connection(db_login, db_password, db_port, db_name) if connection_result is not cdbm.DataBaseConnectionState.SUCCESS: if connection_result == cdbm.DataBaseConnectionState.DATABASE_FAILED: print("The database has been lost. The program will restore the database") try: cdbm.create_data_base(db_login, db_password, db_port, db_name) cdbm.fill_test_data(db_login, db_password, db_port, db_name) except Exception as exc: print(exc) return else: print("Error connecting to database! Recheck the connection settings in the ini file") return database_manager = cdbm.CallsDataBaseManager(db_login, db_password, db_port, db_name) self.calls_get_handler = CallsGetRequestHandler(database_manager) self.calls_get_handler.start() self.shutdown_handler = ShutdownGetRequestHandler(self) self.shutdown_handler.start() self.json_file_manager = JsonFilesManager(database_manager) self.info_container = FilesStateInfoContainer(self.FILES_STATE_INFO_DUMP_PATH) self.initial_check() self.observer_handler = CallsFilesRealtimeHandler(self.info_container, self.json_file_manager, self.files_dir_path) self.observer_obj = Observer() self.observer_obj.schedule(self.observer_handler, self.files_dir_path, True)
def initial_check(self): if self.files_dir_path is None: return current_files_name_set = set() for file_name in Utils.get_all_file_names(self.files_dir_path): current_files_name_set.add(file_name) dumped_files_name_set = self.info_container.get_list() set_to_upd = current_files_name_set.difference(dumped_files_name_set) set_to_delete = dumped_files_name_set.difference(current_files_name_set) for file_name in set_to_delete: self.info_container.remove_element(file_name) for file_name in set_to_upd: new_file_path = self.files_dir_path + "\\" + file_name state = self.json_file_manager.load_file_to_data_base(new_file_path) if state: self.info_container.append_list(file_name) self.info_container.dump_list()
def load_file_to_data_base(self, file_path): self.file_loading_locker.acquire() if not Utils.check_file_exist(file_path): return False file = open(file_path, "r") try: data = json.load(file) if not data_validation(data): return False self.database_manager.insert_call(data["caller_number"], data["destination_number"], data["date_begin"], data["date_ending"], data["rate"], data["cost"]) return True except Exception as exc: print(exc) return False finally: file.close() self.file_loading_locker.release()