Skip to content

Commit

Permalink
Refactor entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tjkuson committed Sep 28, 2024
1 parent c8c8b16 commit b040721
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
70 changes: 7 additions & 63 deletions src/screener/__main__.py
Original file line number Diff line number Diff line change
@@ -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())
60 changes: 60 additions & 0 deletions src/screener/cli.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b040721

Please sign in to comment.