Skip to content

Commit

Permalink
Fix bug when replicating events
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsand97 committed Oct 15, 2022
1 parent c11c974 commit 1d4eb43
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,12 +1705,14 @@ def on_any_event(self, event: we.FileSystemEvent) -> None:
self.target_location.external_path = self.target_location.base_path.joinpath(*new_parts)

def on_moved(self, event: Union[we.DirMovedEvent, we.FileMovedEvent]) -> None:
if not self.include_hidden_files and any(
path.startswith(".") for path in self.source_location.internal_path.parts
):
return
info_textview: Dict[str, str] = {"Event": f"{'Folder' if event.is_directory else 'File'} movement"}
try:
# Ignore hidden files if the option is disabled
if not self.include_hidden_files and any(
path.startswith(".") for path in self.source_location.internal_path.parts
):
return

# Destination of movement in the source folder
internal_target_source: Path = Path(event.dest_path)
new_parts_source = internal_target_source.parts[len(self.source_location.base_path.resolve().parts) :]
Expand All @@ -1728,7 +1730,8 @@ def on_moved(self, event: Union[we.DirMovedEvent, we.FileMovedEvent]) -> None:
)

## Event Replication
self.target_location.internal_path.rename(target=internal_target_target)
if self.target_location.internal_path.exists():
self.target_location.internal_path.rename(target=internal_target_target)

info_textview.update({"Result": f"{'Folder' if event.is_directory else 'File'} moved correctly"})
except Exception as e:
Expand All @@ -1740,12 +1743,14 @@ def on_moved(self, event: Union[we.DirMovedEvent, we.FileMovedEvent]) -> None:
self.emit_event_to_textview(info=info_textview)

def on_created(self, event: Union[we.DirCreatedEvent, we.FileCreatedEvent]) -> None:
if not self.include_hidden_files and any(
path.startswith(".") for path in self.source_location.internal_path.parts
):
return
info_textview: Dict[str, str] = {"Event": f"{'Folder' if event.is_directory else 'File'} creation"}
try:
# Ignore hidden files if the option is disabled
if not self.include_hidden_files and any(
path.startswith(".") for path in self.source_location.internal_path.parts
):
return

info_textview.update(
{
"Source": str(self.source_location.external_path),
Expand All @@ -1754,10 +1759,11 @@ def on_created(self, event: Union[we.DirCreatedEvent, we.FileCreatedEvent]) -> N
)

## Event Replication
if event.is_directory:
self.target_location.internal_path.mkdir(exist_ok=True)
else:
shutil.copy2(src=self.source_location.internal_path, dst=self.target_location.internal_path)
if self.source_location.internal_path.exists():
if event.is_directory:
self.target_location.internal_path.mkdir(exist_ok=True)
else:
shutil.copy2(src=self.source_location.internal_path, dst=self.target_location.internal_path)

info_textview.update({"Result": f"{'Folder' if event.is_directory else 'File'} created successfully"})
except Exception as e:
Expand All @@ -1780,12 +1786,14 @@ def rmtree(folder: Path) -> None:
rmtree(folder=child)
folder.rmdir()

if not self.include_hidden_files and any(
path.startswith(".") for path in self.source_location.internal_path.parts
):
return
info_textview: Dict[str, str] = {"Event": f"{'Folder' if event.is_directory else 'File'} deletion"}
try:
# Ignore hidden files if the option is disabled
if not self.include_hidden_files and any(
path.startswith(".") for path in self.source_location.internal_path.parts
):
return

info_textview.update(
{
"Source": str(self.source_location.external_path),
Expand All @@ -1794,7 +1802,8 @@ def rmtree(folder: Path) -> None:
)

## Event Replication
rmtree(folder=self.target_location.internal_path)
if self.target_location.internal_path.exists():
rmtree(folder=self.target_location.internal_path)

info_textview.update({"Result": f"{'Folder' if event.is_directory else 'File'} deleted successfully"})
except Exception as e:
Expand All @@ -1806,13 +1815,14 @@ def rmtree(folder: Path) -> None:
self.emit_event_to_textview(info=info_textview)

def on_closed(self, event: we.FileClosedEvent) -> None:
if (
not self.include_hidden_files
and any(path.startswith(".") for path in self.source_location.internal_path.parts)
) or self.source_location.internal_path.stat().st_size == 0:
return
info_textview: Dict[str, str] = {"Event": "File edition"}
try:
# Ignore hidden files if the option is disabled
if not self.include_hidden_files and any(
path.startswith(".") for path in self.source_location.internal_path.parts
):
return

info_textview.update(
{
"Source": str(self.source_location.external_path),
Expand All @@ -1821,7 +1831,8 @@ def on_closed(self, event: we.FileClosedEvent) -> None:
)

## Event Replication
shutil.copy2(src=self.source_location.internal_path, dst=self.target_location.internal_path)
if self.source_location.internal_path.exists():
shutil.copy2(src=self.source_location.internal_path, dst=self.target_location.internal_path)

info_textview.update({"Result": "File edited correctly"})
except Exception as e:
Expand Down

0 comments on commit 1d4eb43

Please sign in to comment.