Skip to content

Commit

Permalink
Add dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkjin99 committed Feb 27, 2024
1 parent bdc07e4 commit 1c37119
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 51 deletions.
48 changes: 48 additions & 0 deletions src/obs3dian/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from dataclasses import dataclass
import json
from pathlib import Path
import typer

APP_NAME = "obs3dian"
SETUP_FILE_NAME = "config.json"
APP_DIR_PATH = typer.get_app_dir(APP_NAME)


@dataclass(frozen=True)
class Configuration:
profile_name: str = ""
aws_access_key: str = ""
aws_secret_key: str = ""
bucket_name: str = "obs3dian"
output_folder_path: str = "./output"
image_folder_path: str = "./images"


def load_configs() -> Configuration:
"""
Load config json file from app dir
Returns:
dict: config json
"""

try:
config_path = Path(APP_DIR_PATH) / SETUP_FILE_NAME
with config_path.open("r") as f:
configs = json.load(f)

return Configuration(**configs)

except FileNotFoundError as e:
print("Config file is not founded... input your configuration\n")
raise e


def save_config(json_data: dict):
app_dir_path = Path(APP_DIR_PATH) # create app setting folder
if not app_dir_path.exists():
app_dir_path.mkdir()

config_path = app_dir_path / SETUP_FILE_NAME
with config_path.open("w") as f:
json.dump(json_data, f) # write config.json
94 changes: 45 additions & 49 deletions src/obs3dian/main.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
import concurrent.futures as concurrent_futures
import json

import typer
from pathlib import Path
import time

from .core import create_obs3dian_runner
from .config import load_configs, save_config, APP_NAME, Configuration
from .s3 import S3

APP_NAME = "obs3dian"
SETUP_FILE_NAME = "config.json"
APP_DIR_PATH = typer.get_app_dir(APP_NAME)

app = typer.Typer(name=APP_NAME)


def _load_configs() -> dict:
"""
Load config json file from app dir
Returns:
dict: config json
"""
try:
config_path = Path(APP_DIR_PATH) / SETUP_FILE_NAME
with config_path.open("r") as f:
configs = json.load(f)
return configs

except FileNotFoundError as e:
print("Config file is not founded... input your configuration\n")
raise e


def _convert_path_absoulte(path: Path, strict=True) -> Path:
"""
Expand Down Expand Up @@ -79,20 +59,25 @@ def apply():
Raises:
e: invalid output path
"""
configs: dict = _load_configs()
configs: Configuration = load_configs()
output_folder_path = _convert_path_absoulte(
Path(configs["output_folder_path"]), strict=False
Path(configs.output_folder_path), strict=False
)

s3 = S3(configs["profile_name"], configs["bucket_name"])
s3 = S3(
configs.profile_name,
configs.aws_access_key,
configs.aws_secret_key,
configs.bucket_name,
)
print("Connected to AWS S3")

if s3.create_bucket():
print(f"Bucket {configs['bucket_name']} created")
print(f"Bucket {configs.bucket_name} created")
print("Bucket has public read access so anyone can see files in your bucket")
s3.put_public_access_policy()
else:
print(f"Bucket {configs['bucket_name']} is already exists")
print(f"Bucket {configs.bucket_name} is already exists")

if not output_folder_path.exists():
try:
Expand All @@ -117,20 +102,30 @@ def config():
)
try:
# Last config data
configs: dict = _load_configs()
default_profile_name = configs["profile_name"]
default_bucket_name = configs["bucket_name"]
default_output_path = configs["output_folder_path"]
default_image_path = configs["image_folder_path"]
configs: Configuration = load_configs()

except FileNotFoundError:
# First init default
default_profile_name = "default"
default_bucket_name = "obs3dian"
default_output_path = "."
default_image_path = "."
configs: Configuration = Configuration()

default_profile_name = configs.profile_name
default_bucket_name = configs.bucket_name
default_output_path = configs.output_folder_path
default_image_path = configs.image_folder_path
default_aws_access_key = configs.aws_access_key
default_aws_secret_key = configs.aws_secret_key

profile_name = default_input("AWS CLI Profile Name", default_profile_name)

if not profile_name:
aws_access_key = default_input(
"AWS AccessKey (Optional if you input profile)", default_aws_access_key
)
aws_secret_key = default_input(
"AWS Secret Key (Optional if you input profile)", default_aws_secret_key
)
if not (aws_access_key and aws_secret_key):
raise ValueError("You must provide AWS key or AWS-CLI profile name")

profile_name = default_input("AWS Profile Name", default_profile_name)
bucket_name = default_input("S3 bucket Name", default_bucket_name)

output_path = Path(default_input("Output Path", default_output_path))
Expand All @@ -141,18 +136,14 @@ def config():

json_data = {
"profile_name": profile_name,
"aws_access_key": aws_access_key,
"aws_secret_key": aws_secret_key,
"bucket_name": bucket_name,
"output_folder_path": str(output_path),
"image_folder_path": str(image_folder_path),
}

app_dir_path = Path(APP_DIR_PATH) # create app setting folder
if not app_dir_path.exists():
app_dir_path.mkdir()

config_path = app_dir_path / SETUP_FILE_NAME
with config_path.open("w") as f:
json.dump(json_data, f) # write config.json
save_config(json_data)


@app.command()
Expand All @@ -170,12 +161,17 @@ def run(user_input_path: Path):
typer.echo("") # new line

user_input_path = _convert_path_absoulte(user_input_path)
configs: dict = _load_configs()
output_folder_path = _convert_path_absoulte(Path(configs["output_folder_path"]))
s3 = S3(configs["profile_name"], configs["bucket_name"])
configs: Configuration = load_configs()
output_folder_path = _convert_path_absoulte(Path(configs.output_folder_path))
s3 = S3(
configs.profile_name,
configs.aws_access_key,
configs.aws_secret_key,
configs.bucket_name,
)

runner = create_obs3dian_runner(
s3, Path(configs["image_folder_path"]), output_folder_path
s3, Path(configs.image_folder_path), output_folder_path
) # create main function

if user_input_path.is_dir():
Expand Down
17 changes: 15 additions & 2 deletions src/obs3dian/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ class S3:
To use this class you need AWS CLI profile which has permission to create public bucket and put images.
"""

def __init__(self, profile_name: str, bucket_name: str) -> None:
def __init__(
self,
profile_name: str,
aws_access_key: str,
aws_secret_key: str,
bucket_name: str,
) -> None:
try:
self.session = boto3.Session(profile_name=profile_name)
if profile_name: # if user has cli profile
self.session = boto3.Session(profile_name=profile_name)
else:
self.session = boto3.Session(
aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key,
)

self.s3 = self.session.client("s3")
self.bucket_name = bucket_name
return
Expand Down

0 comments on commit 1c37119

Please sign in to comment.