-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor 'main.py' * Update tests due to main.py and processor.py refactoring
- Loading branch information
Showing
8 changed files
with
300 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ output/* | |
.coverage | ||
coverage.* | ||
|
||
myenv/ | ||
|
||
dist/* | ||
*/*.egg-info/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from jp2_remediator.box_reader import BoxReader | ||
|
||
|
||
class BoxReaderFactory: | ||
|
||
def get_reader(self, file_path): | ||
""" | ||
Create a BoxReader instance for a given file path. | ||
:param file_path: The path to the file to be read. | ||
:return: A BoxReader instance. | ||
""" | ||
return BoxReader(file_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import datetime | ||
import os | ||
import boto3 | ||
|
||
|
||
class Processor: | ||
"""Class to process JP2 files.""" | ||
|
||
def __init__(self, factory): | ||
"""Initialize the Processor with a BoxReader factory.""" | ||
self.box_reader_factory = factory | ||
|
||
def process_file(self, file_path): | ||
"""Process a single JP2 file.""" | ||
print(f"Processing file: {file_path}") | ||
reader = self.box_reader_factory.get_reader(file_path) | ||
reader.read_jp2_file() | ||
|
||
def process_directory(self, directory_path): | ||
"""Process all JP2 files in a given directory.""" | ||
for root, _, files in os.walk(directory_path): | ||
for file in files: | ||
if file.lower().endswith(".jp2"): | ||
file_path = os.path.join(root, file) | ||
print(f"Processing file: {file_path}") | ||
reader = self.box_reader_factory.get_reader(file_path) | ||
reader.read_jp2_file() | ||
|
||
def process_s3_bucket(self, bucket_name, prefix=""): | ||
"""Process all JP2 files in a given S3 bucket.""" | ||
s3 = boto3.client("s3") | ||
response = s3.list_objects_v2(Bucket=bucket_name, Prefix=prefix) | ||
|
||
if "Contents" in response: | ||
for obj in response["Contents"]: | ||
if obj["Key"].lower().endswith(".jp2"): | ||
file_path = obj["Key"] | ||
print(f"""Processing file: {file_path} from bucket { | ||
bucket_name | ||
}""") | ||
download_path = f"/tmp/{os.path.basename(file_path)}" | ||
s3.download_file(bucket_name, file_path, download_path) | ||
reader = self.box_reader_factory.get_reader(download_path) | ||
reader.read_jp2_file() | ||
# Optionally, upload modified file back to S3 | ||
timestamp = datetime.datetime.now().strftime( | ||
"%Y%m%d" | ||
) # use "%Y%m%d_%H%M%S" for more precision | ||
s3.upload_file( | ||
download_path.replace( | ||
".jp2", f"_modified_{timestamp}.jp2" | ||
), | ||
bucket_name, | ||
file_path.replace(".jp2", f"_modified_{timestamp}.jp2") | ||
) |
Oops, something went wrong.