Skip to content

Commit

Permalink
adds a list barcodes in input bucket method to digifeeds cli
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Sep 27, 2024
1 parent c73e2cb commit 24656eb
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 1 deletion.
7 changes: 7 additions & 0 deletions aim/cli/digifeeds.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import typer
from aim.digifeeds.add_to_db import add_to_db as add_to_digifeeds_db
from aim.digifeeds.list_barcodes_in_bucket import list_barcodes_in_bucket
from aim.digifeeds.database import models, main


app = typer.Typer()


Expand All @@ -15,3 +17,8 @@ def add_to_db(barcode: str):
def load_statuses():
with main.SessionLocal() as db_session:
models.load_statuses(session=db_session)


@app.command()
def list_barcodes_in_input_bucket():
print(list_barcodes_in_bucket(), end="")
19 changes: 19 additions & 0 deletions aim/digifeeds/list_barcodes_in_bucket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import boto3
from aim.services import S


def list_barcodes_in_bucket():
s3 = boto3.client(
"s3",
aws_access_key_id=S.digifeeds_s3_access_key,
aws_secret_access_key=S.digifeeds_s3_secret_access_key,
)
prefix = S.digifeeds_s3_input_path + "/"
response = s3.list_objects_v2(
Bucket=S.digifeeds_s3_bucket,
Prefix=prefix,
Delimiter="/",
)
paths = [object["Prefix"] for object in response["CommonPrefixes"]]
barcodes = [path.split("/")[1] for path in paths]
return barcodes
11 changes: 11 additions & 0 deletions aim/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
("alma_api_url", str),
("digifeeds_api_url", str),
("digifeeds_set_id", str),
("digifeeds_s3_access_key", str),
("digifeeds_s3_secret_access_key", str),
("digifeeds_s3_bucket", str),
("digifeeds_s3_input_path", str),
],
)

Expand All @@ -29,4 +33,11 @@
digifeeds_set_id=os.getenv("DIGIFEEDS_SET_ID") or "digifeeds_set_id",
alma_api_key=os.getenv("ALMA_API_KEY") or "alma_api_key",
alma_api_url="https://api-na.hosted.exlibrisgroup.com/almaws/v1",
digifeeds_s3_access_key=os.getenv("DIGIFEEDS_S3_ACCESS_KEY")
or "digifeeds_s3_access_key",
digifeeds_s3_secret_access_key=os.getenv("DIGIFEEDS_S3_SECRET_ACCESS_KEY")
or "digifeeds_s3_secret_access_key",
digifeeds_s3_bucket=os.getenv("DIGIFEEDS_S3_BUCKET") or "digifeeds_s3_bucket",
digifeeds_s3_input_path=os.getenv("DIGIFEEDS_S3_INPUT_PATH")
or "path_to_input_barcodes",
)
321 changes: 320 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fastapi = {extras = ["standard"], version = "^0.114.2"}
httpx = "^0.27.2"
alembic = "^1.13.2"
typer = "^0.12.5"
boto3 = "^1.35.28"


[tool.poetry.group.dev.dependencies]
Expand All @@ -27,6 +28,7 @@ ruff = "^0.6.6"
responses = "^0.25.3"
pytest-socket = "^0.7.0"
pytest-mock = "^3.14.0"
moto = {extras = ["s3"], version = "^5.0.15"}


[tool.pytest.ini_options]
Expand Down
11 changes: 11 additions & 0 deletions tests/cli/test_digifeeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,14 @@ def test_load_statuses(mocker):
assert session_local_mock.call_count == 1
assert load_statuse_mock.call_count == 1
assert result.exit_code == 0


def test_list_barcodes_in_input_bucket(mocker):
list_barcodes_mock = mocker.patch(
"aim.cli.digifeeds.list_barcodes_in_bucket",
return_value=["barcode1", "barcode2"],
)
result = runner.invoke(app, ["digifeeds", "list-barcodes-in-input-bucket"])
assert list_barcodes_mock.call_count == 1
assert result.exit_code == 0
assert "['barcode1', 'barcode2']" == result.stdout
26 changes: 26 additions & 0 deletions tests/digifeeds/test_list_barcodes_in_bucket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import boto3
from moto import mock_aws
from aim.services import S
from aim.digifeeds.list_barcodes_in_bucket import list_barcodes_in_bucket


@mock_aws
def test_list_barcodes_in_bucket():
conn = boto3.resource(
"s3",
aws_access_key_id=S.digifeeds_s3_access_key,
aws_secret_access_key=S.digifeeds_s3_secret_access_key,
)
conn.create_bucket(Bucket=S.digifeeds_s3_bucket)

barcode1 = conn.Object(
S.digifeeds_s3_bucket, f"{S.digifeeds_s3_input_path}/barcode1/some_file.txt"
)
barcode1.put(Body="some text")
barcode2 = conn.Object(
S.digifeeds_s3_bucket, f"{S.digifeeds_s3_input_path}/barcode2/some_file.txt"
)
barcode2.put(Body="some text")

result = list_barcodes_in_bucket()
assert result == ["barcode1", "barcode2"]

0 comments on commit 24656eb

Please sign in to comment.