Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### Core Modules
- `__main__.py` - Main entry point with CLI argument parsing and workflow orchestration
- `validation.py` - CSV validation and input handling functions
- `download.py` - Core image downloading functionality with retry logic
- `buddy_check.py` - Checksum validation and download verification using BuddyCheck class
- `utils.py` - Helper functions for CSV processing, logging, and image downsampling
Expand Down
47 changes: 6 additions & 41 deletions src/cautiousrobot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import pandas as pd
import argparse
import hashlib
import os
import sys
from sumbuddy import get_checksums
from cautiousrobot.utils import process_csv, check_existing_images
from cautiousrobot.buddy_check import BuddyCheck
from cautiousrobot.download import download_images
from cautiousrobot.validation import (
validate_csv_extension,
validate_filename_uniqueness,
handle_missing_filenames,
setup_expected_columns,
)
from cautiousrobot.__about__ import __version__

def parse_args():
Expand Down Expand Up @@ -53,46 +58,6 @@ def parse_args():
return parser.parse_args()


def validate_csv_extension(csv_path):
"""Validate that the input file has a .csv extension."""
if not csv_path.endswith(".csv"):
sys.exit("Expected CSV for input file; extension should be '.csv'")


def setup_expected_columns(args):
"""Set up the expected columns dictionary for CSV processing."""
subfolders = args.subdir_col
expected_cols = {
"filename_col": args.img_name_col.lower(),
"url_col": args.url_col.lower()
}
if subfolders:
subfolders = subfolders.lower()
expected_cols["subfolders"] = subfolders
return expected_cols, subfolders


def validate_filename_uniqueness(data_df, filename_col):
"""Validate that the filename column contains unique values."""
if data_df.loc[data_df[filename_col].notna()].shape[0] != data_df[filename_col].nunique():
sys.exit(f"{filename_col} is not a unique identifier for this dataset, please choose a column with unique values for filenames.")


def handle_missing_filenames(data_df, filename_col, url_col):
"""Handle cases where URLs exist but filenames are missing."""
urls_no_name = len(data_df.loc[(data_df[filename_col].isna() & (data_df[url_col].notna()))])
if urls_no_name > 0:
ignore = input(f"'{filename_col}' is missing values for {urls_no_name} URLs. Proceed with download ignoring these URLs? [y/n]: ")
if ignore.lower() != "y":
sys.exit("Exited without executing.")


def validate_output_directory(img_dir):
"""Validate that the output directory doesn't already exist."""
if os.path.exists(img_dir):
sys.exit(f"'{img_dir}' already exists. Exited without executing.")


def setup_log_paths(csv_path):
"""Set up the log file paths based on the CSV path."""
metadata_path = csv_path.split(".")[0]
Expand Down
37 changes: 37 additions & 0 deletions src/cautiousrobot/validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#CSV validation and input handling functions.
Comment thread
egrace479 marked this conversation as resolved.
Outdated

import sys


Comment thread
egrace479 marked this conversation as resolved.
Outdated
def validate_csv_extension(csv_path):
"""Validate that the input file has a .csv extension."""
if not csv_path.endswith(".csv"):
sys.exit("Expected CSV for input file; extension should be '.csv'")


def validate_filename_uniqueness(data_df, filename_col):
"""Validate that the filename column contains unique values."""
if data_df.loc[data_df[filename_col].notna()].shape[0] != data_df[filename_col].nunique():
sys.exit(f"{filename_col} is not a unique identifier for this dataset, please choose a column with unique values for filenames.")


def handle_missing_filenames(data_df, filename_col, url_col):
"""Handle cases where URLs exist but filenames are missing."""
urls_no_name = len(data_df.loc[(data_df[filename_col].isna() & (data_df[url_col].notna()))])
if urls_no_name > 0:
ignore = input(f"'{filename_col}' is missing values for {urls_no_name} URLs. Proceed with download ignoring these URLs? [y/n]: ")
if ignore.lower() != "y":
sys.exit("Exited without executing.")


def setup_expected_columns(args):
"""Set up the expected columns dictionary for CSV processing."""
subfolders = args.subdir_col
expected_cols = {
"filename_col": args.img_name_col.lower(),
"url_col": args.url_col.lower()
}
if subfolders:
subfolders = subfolders.lower()
expected_cols["subfolders"] = subfolders
return expected_cols, subfolders
Loading