diff --git a/src/screener/__main__.py b/src/screener/__main__.py index 12c3e28..fd8820e 100644 --- a/src/screener/__main__.py +++ b/src/screener/__main__.py @@ -1,63 +1,7 @@ -import sys -from argparse import ArgumentParser -from pathlib import Path - -from screener.checker import Checker -from screener.parser import parse_epub, parse_kindle -from screener.reader import EpubFileReader, KindleFileReader - - -def init_argparse() -> ArgumentParser: - parser = ArgumentParser( - prog="screener", - usage="%(prog)s [OPTION] [FILE]...", - description="Check e-book files for security and privacy issues.", - ) - parser.add_argument( - "-v", - "--version", - action="version", - version=f"{parser.prog} version 0.5.0", - ) - parser.add_argument("files", nargs="*") - return parser - - -def check_file(file: Path) -> Checker: - checker = Checker(file) - match extension := file.suffix: - case ".epub": - with EpubFileReader(file) as epub: - parse_epub(checker, epub.file_path) - case ".azw3" | ".mobi": - with KindleFileReader(file) as azw3: - parse_kindle(checker, azw3.file_path) - case _: - msg = f"unsupported file extension: {extension}" - raise ValueError(msg) - return checker - - -def main() -> None: - parser = init_argparse() - args = parser.parse_args() - if not args.files: - print("No files specified. Run with -h for help.", file=sys.stderr) - exit_code = 0 - for file in args.files: - if file == "-": - print("stdin not supported", file=sys.stderr) - continue - checker = check_file(Path(file)) - if checker.diagnostics: - exit_code = 1 - for diagnostic in checker.diagnostics: - print(diagnostic) - else: - print(f"{checker.file_path.name} is safe") - - sys.exit(exit_code) - - -if __name__ == "__main__": - main() +from __future__ import annotations + +import sys + +from screener.cli import main + +sys.exit(main()) diff --git a/src/screener/cli.py b/src/screener/cli.py new file mode 100644 index 0000000..165c1c0 --- /dev/null +++ b/src/screener/cli.py @@ -0,0 +1,60 @@ +from __future__ import annotations + +import sys +from argparse import ArgumentParser +from pathlib import Path +from typing import TYPE_CHECKING + +from screener.checker import Checker +from screener.parser import parse_epub, parse_kindle +from screener.reader import EpubFileReader, KindleFileReader + +if TYPE_CHECKING: + from collections.abc import Sequence + + +def check_file(file: Path) -> Checker: + checker = Checker(file) + match extension := file.suffix: + case ".epub": + with EpubFileReader(file) as epub: + parse_epub(checker, epub.file_path) + case ".azw3" | ".mobi": + with KindleFileReader(file) as azw3: + parse_kindle(checker, azw3.file_path) + case _: + msg = f"unsupported file extension: {extension}" + raise ValueError(msg) + return checker + + +def main(argv: Sequence[str] | None = None) -> int: + parser = ArgumentParser( + prog="screener", + usage="%(prog)s [OPTION] [FILE]...", + description="Check e-book files for security and privacy issues.", + ) + parser.add_argument( + "-v", + "--version", + action="version", + version=f"{parser.prog} version 0.5.0", + ) + parser.add_argument("files", nargs="*") + args = parser.parse_args(argv) + if not args.files: + print("No files specified. Run with -h for help.", file=sys.stderr) + exit_code = 0 + for file in args.files: + if file == "-": + print("stdin not supported", file=sys.stderr) + continue + checker = check_file(Path(file)) + if checker.diagnostics: + exit_code = 1 + for diagnostic in checker.diagnostics: + print(diagnostic) + else: + print(f"{checker.file_path.name} is safe") + + return exit_code