add wrapper that thros RuntimeError if not importable for pycups
This commit is contained in:
parent
1a2273a5b3
commit
33329867d2
@ -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",
|
||||
]
|
||||
|
||||
|
@ -8,21 +8,43 @@
|
||||
:license: MIT
|
||||
"""
|
||||
|
||||
import functools
|
||||
import tempfile
|
||||
|
||||
from ..escpos import Escpos
|
||||
|
||||
_CUPSPRINT = False
|
||||
try:
|
||||
import tempfile
|
||||
#: 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:
|
||||
|
||||
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.
|
||||
|
||||
.. note::
|
||||
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user