Skip to content

Commit

Permalink
Add switch/DI for local file manager through config
Browse files Browse the repository at this point in the history
  • Loading branch information
tomeldar committed Aug 21, 2023
1 parent 4c294c1 commit ac975c3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Data shipping script for airborne products
- `python3 -m pip install -e .`
- `python3 main.py`

For running locally, add "storageMode": "local" to config.json

## Generating build

- `pyinstaller main.py --onefile -n airborne-dsa`
Expand Down
14 changes: 10 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from services.config_manager import ConfigManager

from services.file_watcher import FileWatcher
from services.local_file_manager import LocalFileManager
from services.s3_file_manager import S3FileManager

# from airborne_dsa.config_manager import ConfigManager
Expand Down Expand Up @@ -159,14 +160,18 @@ def main() -> None:

# Setup
config = ConfigManager("config.json")
s3_client = S3FileManager(
config.aws_access_key_id, config.aws_secret_access_key, config.bucket
file_manager = (
S3FileManager(
config.aws_access_key_id, config.aws_secret_access_key, config.bucket
)
if config.storage_mode == "remote"
else LocalFileManager()
)

mission_name, mission_time = get_mission_details()
# Create mission file
try:
s3_client.upload_empty_file(
file_manager.upload_empty_file(
f"MISSION/{mission_name}_{mission_time.strftime('%Y%m%d_%H%M')}Z.txt"
)
print(f"Created mission: {mission_name}")
Expand All @@ -185,7 +190,7 @@ def upload_product(file_path: str) -> None:
)

print(f"Uploading {os.path.basename(file_path)}")
s3_client.upload_file(file_path, key)
file_manager.upload_file(file_path, key)
print(
f"Successfully uploaded {os.path.basename(file_path)} as {key} to {config.bucket}"
)
Expand Down Expand Up @@ -217,3 +222,4 @@ def upload_product(file_path: str) -> None:
log_file = open("ERROR.txt", "w")
log_file.write(str(error))
log_file.close()
print(error)
9 changes: 9 additions & 0 deletions services/config_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Loads and validates config.json file against a config json schema"""
import locale
import json
from typing import Literal
import jsonschema


Expand All @@ -14,6 +15,7 @@ def __init__(self, file_path: str) -> None:
"bucket": {"type": "string"},
"awsAccessKeyId": {"type": "string"},
"awsSecretAccessKey": {"type": "string"},
"storageMode": {"type": "string", "enum": ["local", "remote"]},
},
"required": ["bucket", "awsAccessKeyId", "awsSecretAccessKey"],
"additionalProperties": False,
Expand All @@ -39,3 +41,10 @@ def aws_access_key_id(self) -> str:
def aws_secret_access_key(self) -> str:
"""Your AWS secret key"""
return self.config["awsSecretAccessKey"]

@property
def storage_mode(self) -> Literal["local", "remote"]:
"""Whether to use local or remote storage"""
return (
"remote" if "storageMode" not in self.config else self.config["storageMode"]
)
10 changes: 10 additions & 0 deletions services/local_file_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class LocalFileManager:
def __init__(self) -> None:
print("Using faux file manager. No files will be uploaded")
print()

def upload_file(self, file_path: str, key: str):
pass

def upload_empty_file(self, file_key: str) -> None:
pass

0 comments on commit ac975c3

Please sign in to comment.