function main_v22
Command-line entry point for a reMarkable PDF upload tool that handles argument parsing, folder listing, and PDF document uploads to a reMarkable device.
/tf/active/vicechatdev/e-ink-llm/cloudtest/upload_pdf_new.py
127 - 184
moderate
Purpose
This function serves as the main entry point for a CLI application that uploads PDF files to a reMarkable tablet. It supports three modes: (1) displaying usage information when no arguments are provided, (2) listing available folders on the device when only the script is run, and (3) uploading a PDF to the device with optional folder specification. The function handles all user interaction, error handling, and cleanup operations.
Source Code
def main():
"""Main entry point"""
print("š Starting reMarkable PDF Upload Tool...")
# Show usage if no arguments
if len(sys.argv) == 1:
print_usage()
print("\n" + "="*50)
# Parse arguments
pdf_path = None
document_name = None
parent_uuid = None
if len(sys.argv) >= 3:
pdf_path = sys.argv[1]
document_name = sys.argv[2]
if len(sys.argv) >= 4:
parent_uuid = sys.argv[3]
uploader = None
try:
uploader = RemarkablePDFUploader()
# Show available folders if no upload requested
if not pdf_path:
print("\nš Available folders:")
folders = uploader.list_folders()
if folders:
for uuid, name in folders.items():
print(f" š {name} (UUID: {uuid})")
print("\nš” Use folder UUID as 3rd parameter to upload to specific folder")
else:
print(" No folders found or error accessing folders")
return 0
# Perform upload
success = uploader.upload_pdf(pdf_path, document_name, parent_uuid)
if success:
print("\nā
Upload completed successfully!")
print("š± Check your reMarkable device - the document should now be visible!")
return 0
else:
print("\nā Upload failed!")
return 1
except KeyboardInterrupt:
print("\nā Upload cancelled by user")
return 1
except Exception as e:
print(f"\nā Upload error: {e}")
return 1
finally:
if uploader:
uploader.cleanup()
Return Value
Returns an integer exit code: 0 for success (either successful upload or successful folder listing), 1 for failure (upload error, user cancellation, or exception). This follows standard Unix exit code conventions where 0 indicates success and non-zero indicates failure.
Dependencies
sysostempfilepathlibshutilcloudtest.upload_manager
Required Imports
import sys
import os
import tempfile
from pathlib import Path
from cloudtest.upload_manager import RemarkableUploadManager
import shutil
Usage Example
# Run from command line:
# Show usage and list folders:
python script.py
# Upload PDF to root:
python script.py /path/to/document.pdf "My Document"
# Upload PDF to specific folder:
python script.py /path/to/document.pdf "My Document" "folder-uuid-here"
# Programmatic usage (not recommended, use as CLI):
if __name__ == '__main__':
exit_code = main()
sys.exit(exit_code)
Best Practices
- Always run this function as the main entry point of the script using if __name__ == '__main__': main()
- Ensure the RemarkablePDFUploader class is properly initialized with necessary credentials before use
- The function properly handles cleanup in a finally block, ensuring resources are released even on error
- Exit codes follow Unix conventions (0=success, 1=failure) for proper shell integration
- Keyboard interrupts (Ctrl+C) are gracefully handled to allow user cancellation
- The function provides clear emoji-based visual feedback for better user experience
- When uploading to a specific folder, first run without arguments to list available folder UUIDs
- PDF file paths should be absolute or relative to the current working directory
- Document names can contain spaces and special characters as they are passed as command-line arguments
Tags
Similar Components
AI-powered semantic similarity - components with related functionality: