Skip to content

Commit

Permalink
Hide command for PDF features, if pymupdf is not installed (#149). …
Browse files Browse the repository at this point in the history
…Better detection of `pymupdf` (#148).
  • Loading branch information
jiedxu authored Jun 24, 2023
1 parent 32c434b commit 97cc4d2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
name: dist
path: ./dist/*
pypi:
# Only run, if a tag is created on the default branch.
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') && github.repository == 'pastydev/cmdict'
needs: build
environment:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cmdict"
version = "0.1.5"
version = "0.1.6"
description = "A command line dictionary toolset."
authors = ["zequnyu <[email protected]>", "edxu96 <[email protected]>"]
license = "GPL-3.0"
Expand Down
8 changes: 5 additions & 3 deletions src/cmdict/pdf_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
PDF_FEATURES: bool
"""If the features for PDF are enabled."""
try:
import fitz
except ImportError:
# ``import fitz`` still works, if the directory where it comes from
# is empty, so the following command must be used.
from fitz import open
except (ImportError, ModuleNotFoundError):
PDF_FEATURES = False
else:
PDF_FEATURES = True
Expand Down Expand Up @@ -37,7 +39,7 @@ def extract_words(file_path, color):
return []

res = set()
document = fitz.open(file_path)
document = open(file_path)
for annot in _iterate_filtered_annotations(document, color):
# annotation may contain several rectangles in different rows
word_list = []
Expand Down
28 changes: 26 additions & 2 deletions src/cmdict/run_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,31 @@
_db_path = pathlib.Path(_db_file)


@click.group()
class ActiveFlagCommand(click.Group):
"""Add a keyword argument that can optionally disable a command.
Based on https://stackoverflow.com/a/55379716/10181743.
"""

def command(self, *args, active=True, **kwargs):
"""Add a command to CLI if ``active`` is true.
Args:
*args: other arguments.
active: if the command is active. Defaults to True.
**kwargs: other keyword arguments.
Returns:
Decorated function by ``click``, if ``active`` is true. The
original function, otherwise.
"""
if active:
return super(ActiveFlagCommand, self).command(*args, **kwargs)
else:
return lambda f: f


@click.group(cls=ActiveFlagCommand)
def cli():
"""Command line interface."""

Expand Down Expand Up @@ -112,7 +136,7 @@ def scan(txt_path):
_echo_warn_download()


@cli.command()
@cli.command(active=PDF_FEATURES)
@click.argument("pdf_path", type=click.Path(exists=True))
@click.option(
"--color",
Expand Down

0 comments on commit 97cc4d2

Please sign in to comment.