def start_observing() -> None: """Start watching at all folders in a new thread. Calling this is enough and no further function calls inside this module are needed.""" file_changes_json.init_file() all_folders = file_changes_json.get_all_data() logger_sync.info(f"Watching at folders for changes: {all_folders}") for folder_path, folder in all_folders.items(): _add_watcher(folder_path, folder["include_regexes"], folder["exclude_regexes"]) observer.start() program_state.watching.add_on_stop(sync_waiter.waiter.set)
def full_synchronize() -> None: """Starts a sync process, where changes from server and client are merged.""" with program_state.sync_lock: server_changes = _get_server_changes() client_changes = _get_client_changes() start_synchronization = gen_json.get_current_timestamp() server_actions, client_actions, conflicts = _merge_changes(server_changes, client_changes) logger_sync.info(f"Execute server actions {str(server_actions)[:1000]}") _execute_server_actions(server_actions) logger_sync.info(f"Execute client actions {str(client_actions)[:1000]}") execute_client_actions(client_actions) gen_json.remove_handled_changes(start_synchronization) net_interface.server.remove_handled_changes(start_synchronization) if len(conflicts) > 0: logger_sync.error(f"Unhandled conflicts: {conflicts}")
def add_folder(abs_folder_path: str, include_regexes: List[str] = (".*", ), exclude_regexes: List[str] = (), remote_name: Optional[str] = None) -> bool: """If possible add folder to file and start watching. Returns True, if the folder was added.""" assert isinstance(include_regexes, list) or isinstance( include_regexes, tuple) assert isinstance(exclude_regexes, list) or isinstance( exclude_regexes, tuple) abs_folder_path = normalize_path(abs_folder_path) added = file_changes_json.add_folder(abs_folder_path, include_regexes, exclude_regexes, remote_name) if not added: return False _add_watcher(abs_folder_path, include_regexes, exclude_regexes) logger_sync.info( f"Start watching at new folder: {abs_folder_path}, include_regexes={include_regexes}, " f"exclude_regexes={exclude_regexes}") return True
def trigger_server_synchronization(): """Called from the server, when there were changes at the server.""" logger_sync.info("Triggered server synchronization") c_file_changes.sync_waiter.sync()
def remove_folder_from_watching(abs_folder_path: str) -> None: """Stops watching at the folder and removes it permanently from the json file""" norm_folder_path = normalize_path(abs_folder_path) _remove_watcher(norm_folder_path) file_changes_json.remove_folder(norm_folder_path) logger_sync.info(f"Stop watching at folder: {abs_folder_path}")