Skip to content

Commit

Permalink
Change pattern of optional imports
Browse files Browse the repository at this point in the history
Avoid additional `have_*` variables and just use the module name
instead.
  • Loading branch information
mara004 committed Sep 8, 2022
1 parent 4b675eb commit 626d59c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
6 changes: 2 additions & 4 deletions src/pypdfium2/_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
try:
import argcomplete
except ImportError:
have_argcomplete = False
else:
have_argcomplete = True
argcomplete = None


Subcommands = {
Expand Down Expand Up @@ -56,7 +54,7 @@ def parse_args(argv=sys.argv[1:]):
for cmd in Subcommands.values():
cmd.attach_parser(subparsers)

if have_argcomplete:
if argcomplete is not None:
argcomplete.autocomplete(parser)

return parser.parse_args(argv)
Expand Down
6 changes: 2 additions & 4 deletions src/pypdfium2/_helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
try:
import uharfbuzz as harfbuzz
except ImportError:
have_harfbuzz = False
else:
have_harfbuzz = True
harfbuzz = None

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -535,7 +533,7 @@ class HarfbuzzFont:
""" Harfbuzz font data helper class. """

def __init__(self, font_path):
if not have_harfbuzz:
if harfbuzz is None:
raise RuntimeError("Font helpers require uharfbuzz to be installed.")
self.blob = harfbuzz.Blob.from_file_path(font_path)
self.face = harfbuzz.Face(self.blob)
Expand Down
16 changes: 5 additions & 11 deletions src/pypdfium2/_helpers/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,17 @@
try:
import uharfbuzz as harfbuzz
except ImportError:
have_harfbuzz = False
else:
have_harfbuzz = True
harfbuzz = None

try:
import PIL.Image
except ImportError:
have_pil = False
else:
have_pil = True
PIL = None

try:
import numpy.ctypeslib
except ImportError:
have_numpy = False
else:
have_numpy = True
numpy = None


class PdfPage:
Expand Down Expand Up @@ -451,7 +445,7 @@ def render_tonumpy(self, **kwargs):
(numpy.ndarray, str): NumPy array, and color format.
"""

if not have_numpy:
if numpy is None:
raise RuntimeError("NumPy library needs to be installed for render_tonumpy().")

c_array, cl_format, (width, height) = self.render_base(**kwargs)
Expand All @@ -471,7 +465,7 @@ def render_topil(self, **kwargs):
PIL.Image.Image: An image of the page.
"""

if not have_pil:
if PIL is None:
raise RuntimeError("Pillow library needs to be installed for render_topil().")

c_array, cl_src, size = self.render_base(**kwargs)
Expand Down

0 comments on commit 626d59c

Please sign in to comment.