API Documentation

This document provides detailed information about the Safe Resource Packer API.

Core Classes

SafeResourcePacker

The main class for resource packing operations.

from safe_resource_packer import SafeResourcePacker

packer = SafeResourcePacker(threads=8, debug=False)

Constructor Parameters

Methods

process_resources(source_path, generated_path, output_pack, output_loose)

Process resources and classify them for packing or loose deployment.

Parameters:

Returns:

Example:

pack_count, loose_count, skip_count = packer.process_resources(
    source_path="/path/to/skyrim/Data",
    generated_path="/path/to/bodyslide/output",
    output_pack="./pack",
    output_loose="./loose"
)
cleanup_temp()

Clean up temporary directories created during processing.

packer.cleanup_temp()

PathClassifier

Handles file classification based on path matching and hash comparison.

from safe_resource_packer.classifier import PathClassifier

classifier = PathClassifier(debug=False)

Constructor Parameters

Methods

classify_by_path(source_root, generated_root, out_pack, out_loose, threads=8)

Classify all files in generated directory.

Parameters:

Returns:

find_file_case_insensitive(root, rel_path)

Find file with case-insensitive matching.

Parameters:

Returns:

get_skipped_files()

Get list of skipped files.

Returns:

Utility Functions

Logging Functions

from safe_resource_packer.utils import log, set_debug, write_log_file

# Enable debug mode
set_debug(True)

# Log a message
log("Processing started")

# Log debug-only message
log("Debug info", debug_only=True)

# Write logs to file
write_log_file("processing.log")

log(message, debug_only=False)

Log a message with timestamp.

Parameters:

set_debug(debug_mode)

Set global debug mode.

Parameters:

write_log_file(path)

Write all logs to a file.

Parameters:

File Functions

from safe_resource_packer.utils import file_hash, print_progress

# Calculate file hash
hash_value = file_hash("/path/to/file.txt")

# Display progress
print_progress(50, 100, "Processing", "file.txt")

file_hash(path)

Calculate SHA1 hash of a file.

Parameters:

Returns:

Print a progress bar.

Parameters:

Log Management

from safe_resource_packer.utils import get_logs, get_skipped, clear_logs

# Get all logs
logs = get_logs()

# Get skipped files
skipped = get_skipped()

# Clear all logs
clear_logs()

get_logs()

Get copy of all logs.

Returns:

get_skipped()

Get copy of all skipped files.

Returns:

clear_logs()

Clear all logs and skipped files.

Command Line Interface

The CLI is available through the safe_resource_packer.cli module:

from safe_resource_packer.cli import main

# This is equivalent to running the command line tool
main()

Error Handling

The API uses standard Python exceptions. Common exceptions you might encounter:

Example error handling:

try:
    pack_count, loose_count, skip_count = packer.process_resources(
        source_path=source,
        generated_path=generated,
        output_pack=pack_dir,
        output_loose=loose_dir
    )
except FileNotFoundError as e:
    print(f"Directory not found: {e}")
except PermissionError as e:
    print(f"Permission denied: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Threading Considerations

The API is thread-safe for concurrent processing of different file sets, but:

Performance Tips

  1. Adjust thread count: Set threads based on your CPU cores and I/O capacity
  2. Use SSDs: File hashing and copying benefit from fast storage
  3. Enable debug sparingly: Debug mode generates more output and can slow processing
  4. Process in batches: For very large file sets, consider processing in smaller batches

Memory Usage

The API is designed to be memory-efficient:

Examples

See the examples/ directory for comprehensive usage examples: