diff --git a/app.py b/app.py new file mode 100644 index 0000000..986292c --- /dev/null +++ b/app.py @@ -0,0 +1,7 @@ +from typer import Typer +import identify_file_type + +app = Typer() +VERSION = "0.1.0-alpha" + +app.add_typer(identify_file_type.app, name="identify_file_type") diff --git a/identify_file_type.py b/identify_file_type.py index e69de29..0973c79 100644 --- a/identify_file_type.py +++ b/identify_file_type.py @@ -0,0 +1,40 @@ +from typer import Typer, Argument +from rich import print, console +from log import create_rich_logger +from pprint import pprint +import argparse +import pathlib +import shutil +import tempfile +import zipfile +import magic + +app = Typer() +logger = create_rich_logger() + + +@app.command() +def identify(files: list[str] = Argument(..., help="File paths to identify")): + """Identifies the file type of provided files with incorrect extensions.""" + for file_path in files: + try: + with open(file_path, "rb") as f: + file_type = magic.from_buffer(f.read(2048)) + + if file_type.startswith("ZIP archive"): + with zipfile.ZipFile(file_path, "r") as z: + print(f"{file_path}: ZIP archive containing:") + for member in z.namelist(): + Text.print(f" - {member}") + elif file_type.startswith("TAR archive"): + with tarfile.open(file_path, "r") as tar: + print(f"{file_path}: TAR archive containing:") + for member in tar.getmembers(): + print(f" - {member.name}") + else: + print(f"{file_path}: {file_type}") + + except FileNotFoundError: + log.error(f"Error: File not found: {file_path}") + + print(f"[bold green]Identified file type: {file_type}[/bold green]") diff --git a/main.py b/main.py index 3d8d67f..e71f33c 100644 --- a/main.py +++ b/main.py @@ -1,25 +1,12 @@ -from typer import Typer, Argument -from rich import print, console -from pprint import pprint -import argparse -import pathlib -import shutil -import tempfile -import zipfile +from typer import Typer from log import create_rich_logger +import identify_file_type app = Typer() VERSION = "0.1.0-alpha" logger = create_rich_logger() - -@app.command() -def identify(files: list[str] = Argument(..., help="File paths to identify")): - """Identifies the file type of provided files with incorrect extensions.""" - # ... (file identification logic) - - print("[bold green]Identified file type:[/bold green]") - +app.add_typer(identify_file_type.app, name="identify_file_type") if __name__ == "__main__": app()