Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ignore option #131

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ repos:
- --configfile=tests/bandit.yaml
files: ^(src|tests)/.+\.py$
- repo: https://github.com/PyCQA/isort
rev: 5.11.4
rev: 5.12.0
hooks:
- id: isort
args:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ drive:
- "png"
- "jpg"
- "jpeg"
ignore:
- "node_modules"
- "*.md"
photos:
destination: "photos"
remove_obsolete: false
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
icloudpy==0.3.3
ruamel.yaml==0.16.12
python-magic==0.4.27
python-magic==0.4.27
requests~=2.28.1
pathspec~=0.11.0
29 changes: 23 additions & 6 deletions src/sync_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@

import magic
from icloudpy import exceptions
from pathspec import PathSpec

from src import LOGGER, config_parser


def wanted_file(filters, file_path):
def wanted_file(filters, ignore, file_path):
"""Check if file is wanted."""
if not file_path:
return False
if ignore:
if PathSpec.from_lines("gitwildmatch", ignore).match_file(file_path):
LOGGER.debug(f"Skipping the unwanted file {file_path}")
return False
if not filters or len(filters) == 0:
return True
for file_extension in filters:
Expand All @@ -27,8 +32,11 @@ def wanted_file(filters, file_path):
return False


def wanted_folder(filters, root, folder_path):
def wanted_folder(filters, ignore, root, folder_path):
"""Check if folder is wanted."""
if ignore:
if PathSpec.from_lines("gitwildmatch", ignore).match_file(f"{folder_path}/"):
return False
if not filters or not folder_path or not root or len(filters) == 0:
# Nothing to filter, return True
return True
Expand Down Expand Up @@ -65,12 +73,14 @@ def wanted_parent_folder(filters, root, folder_path):
return False


def process_folder(item, destination_path, filters, root):
def process_folder(item, destination_path, filters, ignore, root):
"""Process the given folder."""
if not (item and destination_path and root):
return None
new_directory = os.path.join(destination_path, item.name)
if not wanted_folder(filters=filters, folder_path=new_directory, root=root):
if not wanted_folder(
filters=filters, ignore=ignore, folder_path=new_directory, root=root
):
LOGGER.debug(f"Skipping the unwanted folder {new_directory} ...")
return None
os.makedirs(new_directory, exist_ok=True)
Expand Down Expand Up @@ -188,12 +198,12 @@ def download_file(item, local_file):
return True


def process_file(item, destination_path, filters, files):
def process_file(item, destination_path, filters, ignore, files):
"""Process given item as file."""
if not (item and destination_path and files is not None):
return False
local_file = os.path.join(destination_path, item.name)
if not wanted_file(filters=filters, file_path=local_file):
if not wanted_file(filters=filters, ignore=ignore, file_path=local_file):
return False
files.add(local_file)
item_is_package = is_package(item=item)
Expand Down Expand Up @@ -236,6 +246,7 @@ def sync_directory(
root,
top=True,
filters=None,
ignore=None,
remove=False,
):
"""Sync folder."""
Expand All @@ -250,6 +261,7 @@ def sync_directory(
filters=filters["folders"]
if filters and "folders" in filters
else None,
ignore=ignore,
root=root,
)
if not new_folder:
Expand All @@ -264,6 +276,7 @@ def sync_directory(
root=root,
top=False,
filters=filters,
ignore=ignore,
)
)
except Exception:
Expand All @@ -284,6 +297,7 @@ def sync_directory(
filters=filters["file_extensions"]
if filters and "file_extensions" in filters
else None,
ignore=ignore,
files=files,
)
except Exception:
Expand All @@ -306,5 +320,8 @@ def sync_drive(config, drive):
filters=config["drive"]["filters"]
if "drive" in config and "filters" in config["drive"]
else None,
ignore=config["drive"]["ignore"]
if "drive" in config and "ignore" in config["drive"]
else None,
remove=config_parser.get_drive_remove_obsolete(config=config),
)
3 changes: 3 additions & 0 deletions tests/data/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ drive:
destination: "./drive"
remove_obsolete: true
sync_interval: -1
ignore:
- "*.psd"
- .git/
filters:
# File filters to be included in syncing iCloud drive content
folders:
Expand Down