def setUp(self) -> None: h_clear_init_all_folders() self._server_process = h_start_server_process() self.folder1_abs_local_path = client_paths.normalize_path( client_paths.LOCAL_CLIENT_DATA, "folder1") h_create_empty(self.folder1_abs_local_path) main.MIN_UPDATE_PAUSE_TIME = 1
def create_file(self, ignore=False, is_folder=False): ignore_patterns = [] if ignore: ignore_patterns = [".*\\.txt"] file_changes.add_folder(self.abs_folder_path, exclude_regexes=ignore_patterns) if is_folder: rel_file_path = "dummy" os.mkdir(os.path.join(self.abs_folder_path, rel_file_path)) else: rel_file_path = "test.txt" abs_file_path = os.path.join(self.abs_folder_path, rel_file_path) with open(abs_file_path, "w") as f: f.write("Hello" * 10) time.sleep(1) expected_changes = {} if is_folder: action = gen_json.ACTION_MKDIR else: action = gen_json.ACTION_PULL gen_json._add_new_change_entry(expected_changes, paths.normalize_path(rel_file_path), action, is_folder) data = gen_json._get_json_data() actual_changes = data[self.abs_folder_path]["changes"] if ignore: self.assertEqual({}, actual_changes) else: expected_changes[rel_file_path]["timestamp"] = actual_changes[rel_file_path]["timestamp"] self.assertEqual(expected_changes, actual_changes)
def start_tray(background_function, shutdown_function): """Add an tray icon and is blocking the program. Must be called from the main thread.""" global tray def background_wrapper(icon_): background_function() def _stop(): """ Stops synchronization. Tray keeps open """ shutdown_function() def _close(): """ Stops synchronization and stops the tray. """ shutdown_function() stop_tray() image = PIL.Image.open(c_paths.normalize_path(c_paths.ASSETS, "Logo.png")) menu = pystray.Menu(pystray.MenuItem("Open", _open_gui), pystray.MenuItem("Stop", _stop), pystray.MenuItem("Close", _close)) tray = pystray.Icon("OpenDrive", image, "OpenDrive", menu) tray.icon = image tray.visible = True tray.run(background_wrapper)
def simulate_explorer(): h_create_empty( client_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA, "folder1")) h_register_dummy_user_device_client() file_changes_json.init_file(empty=True) h_add_dummy_server_folder("folder1") h_add_dummy_server_folder("folder2") gui.main.main()
def test_add_single_ignores(self): file_changes.start_observing() file_changes.add_folder(self.path) rel_path = paths.normalize_path("test.txt") time.sleep(0.3) file_changes.add_single_ignores(self.path, [rel_path]) with open(os.path.join(self.path, rel_path), "w+") as f: f.write("Hello" * 100) folder = file_changes_json.get_folder_entry(self.path) self.assertEqual(0, len(folder["changes"]))
def test_add_change_entry(self): file_changes_json.init_file() path, include, exclude = h_get_dummy_folder_data() file_changes_json.add_folder(path, include, exclude) rel_file_path = client_paths.normalize_path("test.txt") file_changes_json.add_change_entry(path, rel_file_path, gen_json.ACTION_PULL) folder_entry = file_changes_json.get_folder_entry(path) changes = folder_entry["changes"] self.assertEqual(1, len(changes))
def on_moved(self, event): if self._ignore: return old_file_path = self._rel_path new_file_path = paths.normalize_path( os.path.relpath(event.dest_path, self.folder_path)) file_changes_json.add_change_entry(self.folder_path, old_file_path, gen_json.ACTION_MOVE, is_directory=self._is_dir, new_file_path=new_file_path)
def setUp(self) -> None: time.sleep(0.2) if file_changes.observer.is_alive(): file_changes.stop_observing() # file_changes.observer.unschedule_all() h_clear_init_all_folders() self._server_process = h_start_server_process() file_changes_json.init_file(empty=True) file_changes.start_observing() self.folder1_abs_local_path = client_paths.normalize_path( client_paths.LOCAL_CLIENT_DATA, "folder1") h_create_empty(self.folder1_abs_local_path)
def _calculate_remote_actions(local_folder: dict, remote_folder: dict, local_folder_path: client_paths.NormalizedPath, remote_folder_path: client_paths.NormalizedPath): needed_remote_actions = [] conflicts = [] for l_file_path, l_file in local_folder["changes"].items(): if l_file_path in remote_folder["changes"].keys(): conflicts.append({"folders": [local_folder_path, remote_folder_path], "rel_file_path": l_file_path, "local_file": l_file, "remote_file": remote_folder["changes"][l_file_path]}) remote_folder["changes"].pop(l_file_path) else: if l_file["action"] == gen_json.ACTION_PULL[0]: remote_abs_path = client_paths.normalize_path(local_folder_path, l_file_path) else: remote_abs_path = None action = create_action(remote_folder_path, l_file_path, gen_json.ActionType((l_file["action"], 0)), l_file["is_directory"], l_file["rel_old_file_path"], remote_abs_path) needed_remote_actions.append(action) return needed_remote_actions, conflicts
def h_create_expected_change(rel_file_path: str, action: gen_json.ActionType, is_folder: bool = False, new_file_path: str = None): expected_changes = {} gen_json._add_new_change_entry(expected_changes, paths.normalize_path(rel_file_path), action, is_folder, new_file_path) return expected_changes
def h_create_empty_dummy_folder(id_: int = 1) -> paths.NormalizedPath: path = os.path.join(paths.LOCAL_CLIENT_DATA, f"dummy_folder_{id_}") h_create_empty(path) return paths.normalize_path(path)
def get_image_path(image: str) -> str: return c_paths.normalize_path(c_paths.ASSETS, image)
def h_get_dummy_folder_data(): path = client_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA) include = [".*"] exclude = [] return path, include, exclude
def pull_file(rel_server_path: str, abs_client_path: str) -> None: """Pulls a file from the server and saves it at the client""" os.makedirs(os.path.split(abs_client_path)[0], exist_ok=True) with ignore_on_synchronize(paths.normalize_path(abs_client_path)): file = net_interface.server.get_file(rel_server_path, abs_client_path)
def get_file(abs_src_path: str, abs_dest_path: str) -> net.File: """Call to send a file from the client to the server.""" with ignore_on_synchronize(paths.normalize_path(abs_src_path)): return gen_file_exchanges.get_file(abs_src_path, abs_dest_path)