Skip to content

Commit

Permalink
Enhancement #1774: Add option to bypass checking for .osxphotos_expor…
Browse files Browse the repository at this point in the history
…t.db in the export folder. (#1775)

* enhancement-1774: first adjustments.
Missing adjustment to find_first_file_in_branch.

* enhancement-1774: Adjustment to find_first_file_in_branch to return list of first file found.

* * Reusing option --ignore-exportdb
* Removed exclusivity of options --update and --ignore-exportdb
* Search for files logic will also **not be done** in case options --exportdb or --no-exportdb are used.
* Adjusted option definition. Maybe a bit lengthy 😞 !
* Renamed function find_files_in_branch into find_first_file_in_branch and revised logic to return on first occurrence found, when --ignore-exportdb is not active.
* When --update is not used, added warnings when --ignore-exportdb is in use to make it compatible with interactive questions.

* Addressing topics:

- find_first_file_in_branch should now return either None or a single str with the file path instead of a list or empty list.
- Change other_db_files to other_db_file and the subsequent warning that lists out the files to now say "file" vs "files".

* adjusted test "test_export_update_parent_folder" and "test_export_update_child_folder"

* * Error in Tests: [Fatal Python error: Segmentation fault](https://github.com/RhetTbull/osxphotos/actions/runs/12612637331/job/35149593923#step:9:261)

* Unable to re-run Tests up-stream :(

* Adjusting some long lines and forcing commit!
  • Loading branch information
oPromessa authored Jan 5, 2025
1 parent f840086 commit 13d0481
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
57 changes: 32 additions & 25 deletions osxphotos/cli/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@
"--ignore-exportdb",
"-F",
is_flag=True,
help="If exporting to a directory that already contains an export database "
help="1) If exporting to a directory that already contains an export database "
"and --update is not specified, do not prompt to continue but instead continue "
"the export. "
"Normally, if you export to a directory that already contains an export database "
Expand All @@ -803,7 +803,10 @@
"Use --ignore-exportdb to skip this prompt and continue the export. "
"The resulting export database will contain the combined state of both export sets. "
"Short option is '-F' (mnemonic: force export). "
"See also --update.",
"2) For advanced use: when used with --update, --ignore-exportdb will skip searching "
"for export database files in the parent or child of the export directory; "
"thus avoiding what could be a time comsuming search. "
"3) See also --update.",
)
@click.option(
"--no-exportdb",
Expand Down Expand Up @@ -1591,7 +1594,6 @@ def export_cli(
("syndicated", "not_syndicated"),
("saved_to_library", "not_saved_to_library"),
("shared_moment", "not_shared_moment"),
("ignore_exportdb", "update"),
("no_exportdb", "update"),
("no_exportdb", "force_update"),
]
Expand Down Expand Up @@ -1742,12 +1744,15 @@ def export_cli(
if not no_exportdb and exportdb and exportdb != str(expected_exportdb):
if expected_exportdb.exists():
rich_click_echo(
f"[warning]Warning: export database is '{exportdb}' but found '{OSXPHOTOS_EXPORT_DB}' in {dest}; using '{exportdb}'",
f"[warning]Warning: export database is '{exportdb}' but found "
f"'{OSXPHOTOS_EXPORT_DB}' in {dest}; using '{exportdb}'",
err=True,
)
if pathlib.Path(exportdb).resolve().parent != pathlib.Path(dest):
rich_click_echo(
f"[warning]Warning: export database '{pathlib.Path(exportdb).resolve()}' is in a different directory than export destination '{dest}'",
f"[warning]Warning: export database "
f"'{pathlib.Path(exportdb).resolve()}' is in a different "
f"directory than export destination '{dest}'",
err=True,
)

Expand All @@ -1765,24 +1770,28 @@ def export_cli(
"Please confirm that you want to continue without using --update",
err=True,
)
if not ignore_exportdb and not click.confirm("Do you want to continue?"):
if ignore_exportdb:
rich_click_echo(
"[warning]Warning: option --ignore-exportdb enabled: ignoring export database; "
"osxphotos will not consider state of previous export which may result in duplicate files."
)
elif not click.confirm("Do you want to continue?"):
return 1

# check that export isn't in the parent or child of a previously exported library
other_db_files = find_files_in_branch(dest, OSXPHOTOS_EXPORT_DB)
if other_db_files:
if not (ignore_exportdb or exportdb or no_exportdb) and (
other_db_file := find_first_file_in_branch(dest, OSXPHOTOS_EXPORT_DB)
):
rich_click_echo(
"[warning]WARNING: found other export database files in this destination directory branch. "
+ "This likely means you are attempting to export files into a directory "
+ "that is either the parent or a child directory of a previous export. "
+ "Proceeding may cause your exported files to be overwritten.",
"[warning]WARNING: found other export database file in this destination directory branch. "
"This likely means you are attempting to export files into a directory "
"that is either the parent or a child directory of a previous export. "
"Proceeding may cause your exported files to be overwritten.",
err=True,
)
rich_click_echo(
f"You are exporting to {dest}, found {OSXPHOTOS_EXPORT_DB} files in:"
f"You are exporting to {dest}, found {OSXPHOTOS_EXPORT_DB} file in: {other_db_file}"
)
for other_db in other_db_files:
rich_click_echo(f"{other_db}")
if not click.confirm("Do you want to continue?"):
return 1

Expand Down Expand Up @@ -3000,12 +3009,12 @@ def get_dirnames_from_template(
return dest_paths


def find_files_in_branch(pathname, filename):
"""Search a directory branch to find file(s) named filename
The branch searched includes all folders below pathname and
the parent tree of pathname but not pathname itself.
def find_first_file_in_branch(pathname, filename):
"""Search a directory branch to find file(s) named filename.
The branch searched includes all folders below pathname and
the parent tree of pathname but not pathname itself.
e.g. find filename in children folders and parent folders
e.g. find filename in children folders and parent folders
Args:
pathname: str, full path of directory to search
Expand All @@ -3015,14 +3024,12 @@ def find_files_in_branch(pathname, filename):
"""

pathname = pathlib.Path(pathname).resolve()
files = []

# walk down the tree
for root, _, filenames in os.walk(pathname):
# for directory in directories:
for fname in filenames:
if fname == filename and pathlib.Path(root) != pathname:
files.append(os.path.join(root, fname))
return os.path.join(root, fname)

# walk up the tree
path = pathlib.Path(pathname)
Expand All @@ -3031,9 +3038,9 @@ def find_files_in_branch(pathname, filename):
for fname in filenames:
filepath = os.path.join(root, fname)
if fname == filename and os.path.isfile(filepath):
files.append(os.path.join(root, fname))
return filepath

return files
return None


def collect_files_to_keep(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5623,7 +5623,7 @@ def test_export_update_child_folder():
input="N\n",
)
assert result.exit_code != 0
assert "WARNING: found other export database files" in result.output
assert "WARNING: found other export database file" in result.output


def test_export_update_parent_folder():
Expand All @@ -5647,7 +5647,7 @@ def test_export_update_parent_folder():
input="N\n",
)
assert result.exit_code != 0
assert "WARNING: found other export database files" in result.output
assert "WARNING: found other export database file" in result.output


@pytest.mark.skipif(exiftool is None, reason="exiftool not installed")
Expand Down

0 comments on commit 13d0481

Please sign in to comment.