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

Raise FileExistsError in Resolver.workspace_from_url if clobber_mets is False #1268

Merged
merged 2 commits into from
Aug 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/ocrd/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ def download_to_directory(self, directory, url, basename=None, if_exists='skip',
log.debug("Stop early, src_path and dst_path are the same: '%s' (url: '%s')" % (src_path, url))
return str(ret)

# Respect 'if_exists' arg
# Respect 'if_exists' kwarg
if dst_path.exists():
if if_exists == 'skip':
log.debug(f"File already exists but if_exists == {if_exists}, skipping.")
return str(ret)
if if_exists == 'raise':
raise FileExistsError(f"File already exists and if_exists == 'raise': {dst_path}")
elif if_exists == 'raise':
raise FileExistsError(f"File already exists and if_exists == '{if_exists}': {dst_path}")
else:
log.debug(f"File already exists but if_exists == {if_exists}, overwriting.")

# Create dst_path parent dir
dst_path.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -218,7 +221,7 @@ def workspace_from_url(

log.debug("workspace_from_url\nmets_basename='%s'\nmets_url='%s'\nsrc_baseurl='%s'\ndst_dir='%s'",
mets_basename, mets_url, src_baseurl, dst_dir)
self.download_to_directory(dst_dir, mets_url, basename=mets_basename, if_exists='overwrite' if clobber_mets else 'skip')
self.download_to_directory(dst_dir, mets_url, basename=mets_basename, if_exists='overwrite' if clobber_mets else 'raise')

workspace = Workspace(self, dst_dir, mets_basename=mets_basename, baseurl=src_baseurl, mets_server_url=mets_server_url)

Expand Down
17 changes: 11 additions & 6 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def test_workspace_from_url_kant_with_resources(mock_request, tmp_path):

@patch.object(Session, "get")
def test_workspace_from_url_kant_with_resources_existing_local(mock_request, tmp_path):
"""
Fail with clobber_mets=False, succeeed with clobber_mets=True
"""

# arrange
url_src = 'https://raw.githubusercontent.com/OCR-D/assets/master/data/kant_aufklaerung_1784/data/mets.xml'
Expand All @@ -127,12 +130,14 @@ def test_workspace_from_url_kant_with_resources_existing_local(mock_request, tmp
dst_mets = Path(dst_dir, 'mets.xml')
shutil.copyfile(src_mets, dst_mets)

# act
Resolver().workspace_from_url(url_src, clobber_mets=False, dst_dir=dst_dir)
# fail
with pytest.raises(FileExistsError) as exc:
Resolver().workspace_from_url(url_src, clobber_mets=False, dst_dir=dst_dir)
assert mock_request.call_count == 0

# assert
# no real request was made, since mets already present
assert mock_request.call_count == 0
# succeed
Resolver().workspace_from_url(url_src, clobber_mets=True, dst_dir=dst_dir)
assert mock_request.call_count == 1


@patch.object(Session, "get")
Expand Down Expand Up @@ -229,7 +234,7 @@ def test_workspace_from_nothing_noclobber(tmp_path):
ws2 = Resolver().workspace_from_nothing(tmp_path)
assert ws2.directory == tmp_path

with pytest.raises(Exception) as exc:
with pytest.raises(FileExistsError) as exc:
Resolver().workspace_from_nothing(tmp_path)

# assert
Expand Down
Loading