diff --git a/dangerzone/args.py b/dangerzone/args.py new file mode 100644 index 0000000..1665450 --- /dev/null +++ b/dangerzone/args.py @@ -0,0 +1,26 @@ +from typing import Optional + +import click + +from . import errors +from .document import Document + + +@errors.handle_document_errors +def validate_input_filename( + ctx: click.Context, param: str, value: Optional[str] +) -> Optional[str]: + if value is None: + return None + Document.validate_input_filename(value) + return value + + +@errors.handle_document_errors +def validate_output_filename( + ctx: click.Context, param: str, value: Optional[str] +) -> Optional[str]: + if value is None: + return None + Document.validate_output_filename(value) + return value diff --git a/dangerzone/cli.py b/dangerzone/cli.py index 1c008b2..f51af9a 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -7,7 +7,7 @@ from typing import Optional import click from colorama import Back, Fore, Style -from . import container, errors +from . import args, container, errors from .container import convert from .document import Document from .global_common import GlobalCommon @@ -20,9 +20,13 @@ def print_header(s: str) -> None: @click.command() -@click.option("--output-filename", help="Default is filename ending with -safe.pdf") +@click.option( + "--output-filename", + callback=args.validate_output_filename, + help="Default is filename ending with -safe.pdf", +) @click.option("--ocr-lang", help="Language to OCR, defaults to none") -@click.argument("filename", required=True) +@click.argument("filename", required=True, callback=args.validate_input_filename) @errors.handle_document_errors def cli_main( output_filename: Optional[str], ocr_lang: Optional[str], filename: str diff --git a/dangerzone/errors.py b/dangerzone/errors.py index 5f6c84d..98b2759 100644 --- a/dangerzone/errors.py +++ b/dangerzone/errors.py @@ -1,9 +1,12 @@ import functools import logging import sys +from typing import Any, Callable, TypeVar, cast import click +F = TypeVar("F", bound=Callable[..., Any]) + log = logging.getLogger(__name__) @@ -11,11 +14,11 @@ class DocumentFilenameException(Exception): """Exception for document-related filename errors.""" -def handle_document_errors(func): +def handle_document_errors(func: F) -> F: """Log document-related errors and exit gracefully.""" @functools.wraps(func) - def wrapper(*args, **kwargs): + def wrapper(*args, **kwargs): # type: ignore try: return func(*args, **kwargs) except DocumentFilenameException as e: @@ -26,4 +29,4 @@ def handle_document_errors(func): click.echo(str(e)) exit(1) - return wrapper + return cast(F, wrapper) diff --git a/dangerzone/gui/__init__.py b/dangerzone/gui/__init__.py index 5ca6fce..5aaafbb 100644 --- a/dangerzone/gui/__init__.py +++ b/dangerzone/gui/__init__.py @@ -10,7 +10,7 @@ import click import colorama from PySide2 import QtCore, QtGui, QtWidgets -from .. import errors +from .. import args, errors from ..document import Document from ..global_common import GlobalCommon from .common import GuiCommon @@ -50,7 +50,7 @@ class ApplicationWrapper(QtCore.QObject): @click.command() -@click.argument("filename", required=False) +@click.argument("filename", required=False, callback=args.validate_input_filename) @errors.handle_document_errors def gui_main(filename: Optional[str]) -> bool: setup_logging()