Skip to content

Commit

Permalink
hooks up hathifiles poll to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Jan 23, 2025
1 parent b2e6f19 commit 125aa4d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 deletions.
26 changes: 26 additions & 0 deletions aim/cli/hathifiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import typer
from aim.hathifiles import poll
from aim.services import S

app = typer.Typer()


@app.command()
def create_store_file():
f"""
Genereates a new store file at {S.hathifiles_store_path} if one does not
already exist. The new file is based on the latest hathi_files_list.json
from hathitrust.org
"""
poll.create_store_file()


@app.command()
def check_for_new_update_files():
"""
Pulls the latest hathi_files_list.json from hathitrust.org and checks if
there are any update files that aren't in the store. If there are new files
it notifies the argo events webhook and replaces the store file with the old
files and the new ones.
"""
poll.check_for_new_update_files()
6 changes: 6 additions & 0 deletions aim/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@

import typer
import aim.cli.digifeeds as digifeeds
import aim.cli.hathifiles as hathifiles

app = typer.Typer()
app.add_typer(
digifeeds.app, name="digifeeds", help="Commands related to the digifeeds process"
)
app.add_typer(
hathifiles.app,
name="hathifiles",
help="Commands related to the hathifiles database",
)


if __name__ == "__main__": # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions aim/hathifiles/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def replace_store(self, store_path: str = S.hathifiles_store_path):
S.logger.info("Update store SUCCESS")


def check_for_new_files(
def check_for_new_update_files(
latest_update_files: list | None = None,
store: list | None = None,
new_file_handler_klass: type[NewFileHandler] = NewFileHandler,
new_file_handler_klass: Type[NewFileHandler] = NewFileHandler,
):
if latest_update_files is None: # pragma: no cover
latest_update_files = get_latest_update_files()
Expand Down
7 changes: 7 additions & 0 deletions docs/api/aim.cli.hathifiles.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
aim.cli.hathifiles module
=========================

.. automodule:: aim.cli.hathifiles
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/api/aim.cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Submodules
:maxdepth: 4

aim.cli.digifeeds
aim.cli.hathifiles
aim.cli.main
23 changes: 23 additions & 0 deletions tests/cli/test_hathifiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typer.testing import CliRunner
from aim.cli.main import app
from aim.hathifiles import poll

runner = CliRunner()


def test_hathifiles_create_store_file(mocker):
create_store_file_mock = mocker.patch.object(poll, "create_store_file")

result = runner.invoke(app, ["hathifiles", "create-store-file"])

assert result.exit_code == 0
assert create_store_file_mock.call_count == 1


def test_hathifiles_check_for_new_update_files(mocker):
create_files_mock = mocker.patch.object(poll, "check_for_new_update_files")

result = runner.invoke(app, ["hathifiles", "check-for-new-update-files"])

assert result.exit_code == 0
assert create_files_mock.call_count == 1
6 changes: 3 additions & 3 deletions tests/hathifiles/test_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
get_latest_update_files,
get_store,
create_store_file,
check_for_new_files,
check_for_new_update_files,
NewFileHandler,
)

Expand Down Expand Up @@ -76,7 +76,7 @@ def replace_store(self):

def test_check_for_new_files_when_no_new_files():
with capture_logs() as cap_logs:
check_for_new_files(
check_for_new_update_files(
latest_update_files=["file1"],
store=["other_file", "file1"],
new_file_handler_klass=FakeNewFileHandler,
Expand All @@ -86,7 +86,7 @@ def test_check_for_new_files_when_no_new_files():

def test_check_for_new_files_when_there_are_new_files():
with capture_logs() as cap_logs:
check_for_new_files(
check_for_new_update_files(
latest_update_files=["file1", "file2", "file3"],
store=["other_file", "file1"],
new_file_handler_klass=FakeNewFileHandler,
Expand Down

0 comments on commit 125aa4d

Please sign in to comment.