File Creators and Metadata
This example demonstrates the usage of file creators and metadata operations in the True-Core collections module.
Key Concepts
File Creators - Specialized classes for creating dummy files of various types - Support for common file formats (PDF, DOCX, JPG, etc.) - Customizable file sizes and content
DummyFile Class - Unified interface for creating multiple file types - Support for custom file types with custom headers - Batch file creation capabilities
Multimedia Creation - Create test images with specified dimensions - Generate test videos from images - Create audio files with specified duration and frequency
File Metadata - Access basic file properties (name, size, extension) - Calculate file checksums (MD5) - Get comprehensive file statistics
Example Code
Creating Files with Specific Creators
)
def demonstrate_file_creators():
"""Shows how to use various file creators."""
# Create a PDF file
pdf_creator = PDFFileCreator()
pdf_file = pdf_creator.create_file("test.pdf", size=1024)
print(f"Created PDF file: {pdf_file}")
# Create a JPG file
jpg_creator = JPGFileCreator()
jpg_file = jpg_creator.create_file("test.jpg", size=2048)
print(f"Created JPG file: {jpg_file}")
# Create an MP3 file
Using the DummyFile Class
mp3_file = mp3_creator.create_file("test.mp3", size=4096)
print(f"Created MP3 file: {mp3_file}")
def demonstrate_dummy_file():
"""Shows how to use the DummyFile class for batch creation."""
dummy = DummyFile(default_size=1024)
# Create files of different types
dummy.create_file(".pdf", "document.pdf")
dummy.create_file(".txt", "notes.txt")
dummy.create_file(".jpg", "image.jpg")
# Create a custom file type
dummy.custom_file(
"custom.bin",
".bin",
header=b'CUSTOM',
Creating Multimedia Files
)
print("Created files:", dummy.created_files)
def demonstrate_multimedia_creation():
"""Shows how to create multimedia files."""
dummy = DummyFile()
# Create a test image
with tempfile.TemporaryDirectory() as temp_dir:
image_path = os.path.join(temp_dir, "test_image.jpg")
dummy.create_image(image_path)
print(f"Created test image: {image_path}")
# Create a video from the image
video_path = os.path.join(temp_dir, "test_video.mp4")
dummy.create_static_video(image_path, video_path)
print(f"Created test video: {video_path}")
Working with File Metadata
# Create an audio file
audio_path = os.path.join(temp_dir, "test_audio.wav")
dummy.create_audio(audio_path)
print(f"Created test audio: {audio_path}")
def demonstrate_metadata():
"""Shows how to work with file metadata."""
# Create a test file
with tempfile.NamedTemporaryFile(delete=False) as temp:
temp.write(b"Test content")
temp_path = temp.name
# Create a File object and get its metadata
file = File(temp_path)
# Get basic properties
print(f"File name: {file.filename}")
print(f"Extension: {file.extension}")
print(f"Size: {file.size} bytes")
print(f"MD5: {file.md5}")
print(f"MIME type: {file.mime_type}")
# Get comprehensive stats
stats = file.get_stats()
print("\nComprehensive stats:")
print(f"Created: {stats.created}")
print(f"Modified: {stats.modified}")
print(f"Accessed: {stats.accessed}")
print(f"Permissions: {stats.permissions}")
Key Features
File Creation - Support for multiple file formats - Customizable file sizes - Custom headers for specialized formats
Multimedia Support - Image generation - Video creation - Audio file generation
Metadata Operations - Basic file properties - File