diff --git a/src/floridayvine/__main__.py b/src/floridayvine/__main__.py index 34a323f..d90d112 100644 --- a/src/floridayvine/__main__.py +++ b/src/floridayvine/__main__.py @@ -1,12 +1,18 @@ +from dataclasses import dataclass from typing_extensions import Annotated import typer from importlib.metadata import version -from .minio import upload_directory +from .minio import MinioClient import os app = typer.Typer() +@dataclass +class Common: + minio: MinioClient + + def get_version(): return version("floridayvine") @@ -17,24 +23,37 @@ def print_version(): @app.command() -def about(): +def about(ctx: typer.Context): print( "Floriday Vine is a Python package to ingest Floriday trade information into Serra Vine." ) print(f" v{get_version()}") - print(f" Minio endpoint: {os.getenv('MINIO_ENDPOINT', 'play.min.io')}") + print(f" Minio endpoint: {ctx.obj.minio.endpoint}") @app.command() def upload( + ctx: typer.Context, path: Annotated[str, typer.Argument()], bucket: Annotated[str, typer.Argument(envvar="DEFAULT_BUCKET")] = "floriday", target_dir: Annotated[str, typer.Argument(envvar="DEFAULT_DIR")] = "inbox", ): - upload_directory(bucket, target_dir, path) + """Upload a directory to Minio""" + clt = ctx.obj.minio + clt.upload_directory(bucket, target_dir, path) print(f" {path} --> {bucket}/{target_dir} ... upload complete") +@app.callback() +def common( + ctx: typer.Context, + minio_endpoint: Annotated[str, typer.Option(envvar="MINIO_ENDPOINT")], + minio_access_key: Annotated[str, typer.Option(envvar="MINIO_ACCESS_KEY")], + minio_secret_key: str = typer.Option(envvar="MINIO_SECRET_KEY"), +): + ctx.obj = Common(MinioClient(minio_endpoint, minio_access_key, minio_secret_key)) + + def main(): app() diff --git a/src/floridayvine/minio.py b/src/floridayvine/minio.py index 46e5a4d..c6f04c1 100644 --- a/src/floridayvine/minio.py +++ b/src/floridayvine/minio.py @@ -1,21 +1,19 @@ from minio import Minio import os -minio_endpoint = os.getenv("MINIO_ENDPOINT", "play.min.io") -minio_access_key = os.getenv("MINIO_ACCESS_KEY") -minio_secret_key = os.getenv("MINIO_SECRET_KEY") -minio_client = Minio(minio_endpoint, minio_access_key, minio_secret_key, secure=False) +class MinioClient: + def __init__(self, endpoint: str, access_key: str, secret_key: str): + self.endpoint = endpoint + self.client = Minio(endpoint, access_key, secret_key, secure=False) + def upload_directory(self, bucket: str, target_path: str, source_path: str): + if not os.path.isdir(source_path): + raise NotADirectoryError(f"{source_path} is not a directory.") + for root, dirs, files in os.walk(source_path): + for file in files: + full_path = os.path.join(root, file) + self.upload_file(bucket, os.path.join(target_path, file), full_path) -def upload_directory(bucket: str, target_path: str, source_path: str): - if not os.path.isdir(source_path): - raise NotADirectoryError(f"{source_path} is not a directory.") - for root, dirs, files in os.walk(source_path): - for file in files: - full_path = os.path.join(root, file) - upload_file(bucket, os.path.join(target_path, file), full_path) - - -def upload_file(target_bucket: str, target_file: str, source_file: str): - minio_client.fput_object(target_bucket, target_file, source_file) + def upload_file(self, target_bucket: str, target_file: str, source_file: str): + self.client.fput_object(target_bucket, target_file, source_file) diff --git a/tests/test_upload_files_to_vine.py b/tests/test_upload_files_to_vine.py index 4bca60d..854bf98 100644 --- a/tests/test_upload_files_to_vine.py +++ b/tests/test_upload_files_to_vine.py @@ -1,9 +1,18 @@ import subprocess import pytest -from floridayvine.minio import minio_client import glob import os +from minio import Minio + +# Since we do not specify a minio cli options in our tests, +# floridayvine will fallback to the environment variables, +# so we can use those for our assertions too. +minio_endpoint = os.getenv("MINIO_ENDPOINT") +minio_access_key = os.getenv("MINIO_ACCESS_KEY") +minio_secret_key = os.getenv("MINIO_SECRET_KEY") +minio_client = Minio(minio_endpoint, minio_access_key, minio_secret_key, secure=False) + # this module only runs in an integration test environment: pytestmark = pytest.mark.integration diff --git a/work/backlog.md b/work/backlog.md index e96dabf..e5f26ed 100644 --- a/work/backlog.md +++ b/work/backlog.md @@ -7,21 +7,11 @@ Containers are pushed to . ## Doing -Add the Docker image to Serra Vine. - -* [x] The image works out-of-the-box so that we can test integration with our Serra Vine instance. - * [x] url of minio server is configurable - * [x] credentials are configurable form the Serra Vine Host - * [x] location of source (test) data is configurable - * [x] target location is configurable -* [x] Write a manual on how to include it in Serra Vine -* [x] Have a cronjob of some kind that periodically runs the upload - -## Next - * Goal: make it deployable * Refactor configuration variables and command line parameters +## Next + ## Later * Goal: connect to Floriday.