Support Click 7.x callback handling

Support Click version 7.x and below, which inspect the number of
arguments a callback handler supports.

Refs #206
This commit is contained in:
Alex Pyrgiotis 2022-11-01 14:17:45 +02:00
parent ef5abe1419
commit 5a3a46cd46
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA

View file

@ -7,7 +7,7 @@ from .document import Document
@errors.handle_document_errors
def validate_input_filename(
def _validate_input_filename(
ctx: click.Context, param: str, value: Optional[str]
) -> Optional[str]:
if value is None:
@ -18,7 +18,7 @@ def validate_input_filename(
@errors.handle_document_errors
def validate_output_filename(
def _validate_output_filename(
ctx: click.Context, param: str, value: Optional[str]
) -> Optional[str]:
if value is None:
@ -26,3 +26,23 @@ def validate_output_filename(
filename = Document.normalize_filename(value)
Document.validate_output_filename(filename)
return filename
# XXX: Click versions 7.x and below inspect the number of arguments that the
# callback handler supports. Unfortunately, common Python decorators (such as
# `handle_document_errors()`) mask this number, so we need to reinstate it
# somehow [1]. The simplest way to do so is using a wrapper function.
#
# Once we stop supporting Click 7.x, we can remove the wrappers below.
#
# [1]: https://github.com/freedomofpress/dangerzone/issues/206#issuecomment-1297336863
def validate_input_filename(
ctx: click.Context, param: str, value: Optional[str]
) -> Optional[str]:
return _validate_input_filename(ctx, param, value)
def validate_output_filename(
ctx: click.Context, param: str, value: Optional[str]
) -> Optional[str]:
return _validate_output_filename(ctx, param, value)