-
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.
- Loading branch information
Showing
5 changed files
with
167 additions
and
117 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 |
---|---|---|
|
@@ -5,6 +5,8 @@ output/* | |
.coverage | ||
coverage.* | ||
|
||
myenv/ | ||
|
||
dist/* | ||
*/*.egg-info/* | ||
__pycache__ | ||
|
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 |
---|---|---|
@@ -1,34 +1,62 @@ | ||
import sys | ||
import os | ||
import argparse | ||
from jp2_remediator.processor import Processor | ||
|
||
|
||
def main(): | ||
if len(sys.argv) != 3: | ||
print("Usage: python script.py <folder_path1> <folder_path2>") | ||
sys.exit(1) | ||
|
||
folder_path1 = sys.argv[1] | ||
folder_path2 = sys.argv[2] | ||
|
||
if not os.path.isdir(folder_path1): | ||
print(f"Error: {folder_path1} is not a valid directory.") | ||
sys.exit(1) | ||
|
||
if not os.path.isdir(folder_path2): | ||
print(f"Error: {folder_path2} is not a valid directory.") | ||
sys.exit(1) | ||
|
||
print(f"Folder 1: {folder_path1}") | ||
print(f"Folder 2: {folder_path2}") | ||
def main(): | ||
processor = Processor() | ||
|
||
parser = argparse.ArgumentParser(description="JP2 file processor") | ||
|
||
# Create mutually exclusive subparsers for specifying input source | ||
subparsers = parser.add_subparsers( | ||
title="Input source", dest="input_source" | ||
) | ||
|
||
# Subparser for processing a single JP2 file | ||
file_parser = subparsers.add_parser( | ||
"file", help="Process a single JP2 file" | ||
) | ||
file_parser.add_argument( | ||
"file", help="Path to a single JP2 file to process" | ||
) | ||
file_parser.set_defaults( | ||
func=lambda args: processor.process_file(args.file) | ||
) | ||
|
||
# Subparser for processing all JP2 files in a directory | ||
directory_parser = subparsers.add_parser( | ||
"directory", help="Process all JP2 files in a directory" | ||
) | ||
directory_parser.add_argument( | ||
"directory", help="Path to a directory of JP2 files to process" | ||
) | ||
directory_parser.set_defaults( | ||
func=lambda args: processor.process_directory(args.directory) | ||
) | ||
|
||
# Subparser for processing all JP2 files in an S3 bucket | ||
bucket_parser = subparsers.add_parser( | ||
"bucket", help="Process all JP2 files in an S3 bucket" | ||
) | ||
bucket_parser.add_argument( | ||
"bucket", help="Name of the AWS S3 bucket to process JP2 files from" | ||
) | ||
bucket_parser.add_argument( | ||
"--prefix", help="Prefix of files in the AWS S3 bucket (optional)", | ||
default="" | ||
) | ||
bucket_parser.set_defaults( | ||
func=lambda args: processor.process_s3_bucket(args.bucket, args.prefix) | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if hasattr(args, "func"): | ||
args.func(args) | ||
else: | ||
parser.print_help() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
|
||
def hello_world(): | ||
print("Hello, world!") | ||
|
||
|
||
def add_one(number): | ||
return number + 1 | ||
main() |
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,57 @@ | ||
import datetime | ||
import os | ||
import boto3 | ||
|
||
from jp2_remediator.box_reader import BoxReader | ||
|
||
|
||
class Processor: | ||
"""Class to process JP2 files.""" | ||
|
||
def process_file(self, file_path): | ||
"""Process a single JP2 file.""" | ||
print(f"Processing file: {file_path}") | ||
reader = BoxReader(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 = BoxReader(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 = BoxReader(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"), | ||
) | ||
|
||
|