Skip to content

Commit

Permalink
Refactor to include top level application options
Browse files Browse the repository at this point in the history
  • Loading branch information
serra committed Aug 23, 2024
1 parent 6e0faa4 commit c9eeeec
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
27 changes: 23 additions & 4 deletions src/floridayvine/__main__.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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()

Expand Down
28 changes: 13 additions & 15 deletions src/floridayvine/minio.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 10 additions & 1 deletion tests/test_upload_files_to_vine.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
14 changes: 2 additions & 12 deletions work/backlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,11 @@ Containers are pushed to <ghcr.io/serraict/vine-floriday-adapter>.

## 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.
Expand Down

0 comments on commit c9eeeec

Please sign in to comment.