Skip to content

Commit

Permalink
Support Click 7.x callback handling
Browse files Browse the repository at this point in the history
Support Click version 7.x and below, which inspect the number of
arguments a callback handler supports.

Refs #206
  • Loading branch information
apyrgio committed Nov 7, 2022
1 parent 481d706 commit 8785b42
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions dangerzone/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@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:
Expand All @@ -18,11 +18,31 @@ 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:
return None
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)

0 comments on commit 8785b42

Please sign in to comment.