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

View File

@ -8,21 +8,43 @@
:license: MIT :license: MIT
""" """
import functools
import tempfile
from ..escpos import Escpos from ..escpos import Escpos
_CUPSPRINT = False #: keeps track if the pycups dependency could be loaded (:py:class:`escpos.printer.CupsPrinter`)
try: _DEP_PYCUPS = False
import tempfile
try:
import cups import cups
_CUPSPRINT = True _DEP_PYCUPS = True
except ImportError: except ImportError:
pass pass
if _CUPSPRINT:
class CupsPrinter(Escpos): # 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. """Simple CUPS printer connector.
.. note:: .. note::
@ -38,6 +60,7 @@ if _CUPSPRINT:
""" """
@dependency_pycups
def __init__(self, printer_name=None, *args, **kwargs): def __init__(self, printer_name=None, *args, **kwargs):
"""Class constructor for CupsPrinter. """Class constructor for CupsPrinter.
@ -94,6 +117,7 @@ if _CUPSPRINT:
self.pending_job = False self.pending_job = False
raise ValueError("Printer job not opened") raise ValueError("Printer job not opened")
@dependency_pycups
def send(self): def send(self):
"""Send the print job to the printer.""" """Send the print job to the printer."""
if self.pending_job: if self.pending_job:

View File

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