add wrapper that thros RuntimeError if not importable for pycups

This commit is contained in:
Patrick Kanzler 2023-08-17 00:18:30 +02:00
parent 1a2273a5b3
commit 33329867d2
3 changed files with 134 additions and 111 deletions

View File

@ -2,7 +2,7 @@
"""printer implementations."""
# from .win32raw import Win32Raw
# from .cups import CupsPrinter
from .cups import CupsPrinter
from .dummy import Dummy
from .file import File
from .lp import LP
@ -17,7 +17,7 @@ __all__ = [
"Serial",
"LP",
"Dummy",
# "CupsPrinter",
"CupsPrinter",
# "Win32Raw",
]

View File

@ -8,19 +8,41 @@
:license: MIT
"""
from ..escpos import Escpos
_CUPSPRINT = False
try:
import functools
import tempfile
from ..escpos import Escpos
#: keeps track if the pycups dependency could be loaded (:py:class:`escpos.printer.CupsPrinter`)
_DEP_PYCUPS = False
try:
import cups
_CUPSPRINT = True
_DEP_PYCUPS = True
except ImportError:
pass
if _CUPSPRINT:
# TODO: dev build mode that let's the wrapper bypass?
def dependency_pycups(func):
"""Indicate dependency on pycups."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
"""Throw a RuntimeError if pycups is not imported."""
if not _DEP_PYCUPS:
raise RuntimeError(
"Printing with PyCups requires the pycups library to"
"be installed. Please refer to the documentation on"
"what to install and install the dependencies for pycups."
)
return func(*args, **kwargs)
return wrapper
class CupsPrinter(Escpos):
"""Simple CUPS printer connector.
@ -38,6 +60,7 @@ if _CUPSPRINT:
"""
@dependency_pycups
def __init__(self, printer_name=None, *args, **kwargs):
"""Class constructor for CupsPrinter.
@ -94,6 +117,7 @@ if _CUPSPRINT:
self.pending_job = False
raise ValueError("Printer job not opened")
@dependency_pycups
def send(self):
"""Send the print job to the printer."""
if self.pending_job:

View File

@ -22,7 +22,6 @@ deps = jaconv
pytest-mock
hypothesis>4
python-barcode
pycups
commands = pytest
passenv = ESCPOS_CAPABILITIES_PICKLE_DIR, ESCPOS_CAPABILITIES_FILE, CI, TRAVIS, TRAVIS_*, APPVEYOR, APPVEYOR_*, CODECOV_*
setenv = PY_IGNORE_IMPORTMISMATCH=1