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

Add --trust-repo option to download CLI command #807

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Again resolve SonarCloud complain: Remove unused parameter token
  • Loading branch information
juhoinkinen committed Sep 30, 2024
commit caf0325c157de180385d69a6291fe208457b9cca
2 changes: 1 addition & 1 deletion annif/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def run_download(project_ids_pattern, repo_id, token, revision, force, trust_rep
`--trust-repo` option needs to be used.
"""

hfh_util.check_is_download_allowed(trust_repo, repo_id, token)
hfh_util.check_is_download_allowed(trust_repo, repo_id)

project_ids = hfh_util.get_matching_project_ids_from_hf_hub(
project_ids_pattern, repo_id, token, revision
Expand Down
2 changes: 1 addition & 1 deletion annif/hfh_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
logger = annif.logger


def check_is_download_allowed(trust_repo, repo_id, token):
def check_is_download_allowed(trust_repo, repo_id):
"""Check if downloading from the specified repository is allowed based on the trust
option and cache status."""
if trust_repo:
Expand All @@ -43,14 +43,14 @@


def _is_repo_in_cache(repo_id):
from huggingface_hub import CacheNotFound, scan_cache_dir

Check warning on line 46 in annif/hfh_util.py

View check run for this annotation

Codecov / codecov/patch

annif/hfh_util.py#L46

Added line #L46 was not covered by tests

try:
cache = scan_cache_dir()
except CacheNotFound as err:
logger.debug(str(err) + "\nNo HFH cache found.")
return False
return repo_id in [info.repo_id for info in cache.repos]

Check warning on line 53 in annif/hfh_util.py

View check run for this annotation

Codecov / codecov/patch

annif/hfh_util.py#L48-L53

Added lines #L48 - L53 were not covered by tests


def get_matching_projects(pattern: str) -> list[AnnifProject]:
Expand Down
9 changes: 3 additions & 6 deletions tests/test_hfh_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
def test_download_allowed_trust_repo(mock_is_repo_in_cache, caplog):
trust_repo = True
repo_id = "dummy-repo"
token = "dummy-token"

with caplog.at_level(logging.WARNING, logger="annif"):
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id, token)
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id)
assert (
'Download allowed from "dummy-repo" because "--trust-repo" flag is used.'
in caplog.text
Expand All @@ -34,10 +33,9 @@ def test_download_allowed_trust_repo(mock_is_repo_in_cache, caplog):
def test_download_allowed_repo_in_cache(mock_is_repo_in_cache, caplog):
trust_repo = False
repo_id = "dummy-repo"
token = "dummy-token"

with caplog.at_level(logging.DEBUG, logger="annif"):
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id, token)
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id)
assert (
'Download allowed from "dummy-repo" because repo is already in cache.'
in caplog.text
Expand All @@ -48,10 +46,9 @@ def test_download_allowed_repo_in_cache(mock_is_repo_in_cache, caplog):
def test_download_not_allowed(mock_is_repo_in_cache):
trust_repo = False
repo_id = "dummy-repo"
token = "dummy-token"

with pytest.raises(OperationFailedException) as excinfo:
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id, token)
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id)
assert (
str(excinfo.value)
== 'Cannot download projects from untrusted repo "dummy-repo"'
Expand Down
Loading