Last active
March 2, 2025 18:27
-
-
Save shahpnmlab/a64511131edce7ab4891971276e3c217 to your computer and use it in GitHub Desktop.
Copy all movies from from src to dest from an mdoc file
This file contains hidden or 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
from pathlib import Path | |
import mdocfile | |
import shutil | |
import typer | |
import logging | |
from typing import Optional | |
# Configure logging | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s - %(levelname)s - %(message)s' | |
) | |
logger = logging.getLogger(__name__) | |
app = typer.Typer(no_args_is_help=True) | |
@app.command() | |
def main( | |
mdoc_input: Path = typer.Argument(..., help="Path to mdoc file or directory containing mdoc files"), | |
src_dir: Path = typer.Option(..., "--s", help="Path to where the frames are stored"), | |
dest_dir: Path = typer.Option(..., "--d", help="Path where the frames should be copied to"), | |
save_frames_text_file: bool = typer.Option(False, "--save2txt", help="Just save file names to a text file") | |
) -> None: | |
""" | |
Process mdoc files and copy or list associated frames. | |
Accepts either a single mdoc file or a directory containing mdoc files. | |
""" | |
try: | |
# Validate input parameters | |
if not mdoc_input.exists(): | |
raise ValueError(f"Invalid or non-existent input path: {mdoc_input}") | |
if not src_dir.exists(): | |
raise ValueError(f"Invalid or non-existent source directory: {src_dir}") | |
if not save_frames_text_file and not dest_dir.exists(): | |
raise ValueError(f"Invalid or non-existent destination directory: {dest_dir}") | |
# Determine if input is a file or directory | |
mdocs = [] | |
if mdoc_input.is_file() and mdoc_input.suffix == '.mdoc': | |
# Single mdoc file | |
mdocs = [mdoc_input] | |
elif mdoc_input.is_dir(): | |
# Directory of mdoc files | |
mdocs = sorted(list(mdoc_input.glob('*.mdoc'))) | |
if not mdocs: | |
raise FileNotFoundError(f"No mdoc files found in {mdoc_input}") | |
else: | |
raise ValueError(f"Input must be either a .mdoc file or a directory containing .mdoc files: {mdoc_input}") | |
# Process each mdoc file | |
for mdoc_file in mdocs: | |
logger.info(f"Processing mdoc file: {mdoc_file}") | |
md = mdocfile.read(mdoc_file) | |
if 'SubFramePath' not in md: | |
logger.warning(f"No SubFramePath found in {mdoc_file}") | |
continue | |
if save_frames_text_file: | |
# Save frame names to text file | |
output_file = dest_dir / f"{mdoc_file.stem}_frames.txt" | |
with open(output_file, 'w') as f: | |
for frame in md['SubFramePath']: | |
f.write(f"{frame.name}\n") | |
logger.info(f"Frame names saved to: {output_file}") | |
else: | |
# Copy frames to destination directory | |
for frame in md['SubFramePath']: | |
src_path = src_dir / frame.name | |
dest_path = dest_dir / frame.name | |
if not src_path.exists(): | |
logger.warning(f"Source frame not found: {src_path}") | |
continue | |
try: | |
shutil.copy2(src_path, dest_path) | |
logger.info(f"Copied {frame.name} to {dest_path}") | |
except Exception as e: | |
logger.error(f"Error copying {frame.name}: {str(e)}") | |
except Exception as e: | |
logger.error(f"An error occurred: {str(e)}") | |
raise typer.Exit(code=1) | |
if __name__ == "__main__": | |
app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment