added check for usability
This commit is contained in:
parent
3643a22a10
commit
e4424dca00
@ -26,6 +26,13 @@ except ImportError:
|
|||||||
|
|
||||||
# TODO: dev build mode that let's the wrapper bypass?
|
# TODO: dev build mode that let's the wrapper bypass?
|
||||||
|
|
||||||
|
def is_usable():
|
||||||
|
"""Indicate whether this component can be used due to dependencies."""
|
||||||
|
usable = False
|
||||||
|
if not _DEP_PYCUPS:
|
||||||
|
usable = True
|
||||||
|
return usable
|
||||||
|
|
||||||
|
|
||||||
def dependency_pycups(func):
|
def dependency_pycups(func):
|
||||||
"""Indicate dependency on pycups."""
|
"""Indicate dependency on pycups."""
|
||||||
@ -33,7 +40,7 @@ def dependency_pycups(func):
|
|||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
"""Throw a RuntimeError if pycups is not imported."""
|
"""Throw a RuntimeError if pycups is not imported."""
|
||||||
if not _DEP_PYCUPS:
|
if is_usable():
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"Printing with PyCups requires the pycups library to"
|
"Printing with PyCups requires the pycups library to"
|
||||||
"be installed. Please refer to the documentation on"
|
"be installed. Please refer to the documentation on"
|
||||||
|
@ -11,6 +11,11 @@
|
|||||||
from ..escpos import Escpos
|
from ..escpos import Escpos
|
||||||
|
|
||||||
|
|
||||||
|
def is_usable():
|
||||||
|
"""Indicate whether this component can be used due to dependencies."""
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class Dummy(Escpos):
|
class Dummy(Escpos):
|
||||||
"""Dummy printer.
|
"""Dummy printer.
|
||||||
|
|
||||||
|
@ -11,6 +11,11 @@
|
|||||||
from ..escpos import Escpos
|
from ..escpos import Escpos
|
||||||
|
|
||||||
|
|
||||||
|
def is_usable():
|
||||||
|
"""Indicate whether this component can be used due to dependencies."""
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class File(Escpos):
|
class File(Escpos):
|
||||||
"""Generic file printer.
|
"""Generic file printer.
|
||||||
|
|
||||||
|
@ -16,13 +16,21 @@ import sys
|
|||||||
from ..escpos import Escpos
|
from ..escpos import Escpos
|
||||||
|
|
||||||
|
|
||||||
|
def is_usable():
|
||||||
|
"""Indicate whether this component can be used due to dependencies."""
|
||||||
|
usable = False
|
||||||
|
if not sys.platform.startswith("win"):
|
||||||
|
usable = True
|
||||||
|
return usable
|
||||||
|
|
||||||
|
|
||||||
def dependency_linux_lp(func):
|
def dependency_linux_lp(func):
|
||||||
"""Indicate dependency on non Windows."""
|
"""Indicate dependency on non Windows."""
|
||||||
|
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
"""Throw a RuntimeError if not on a non-Windows system."""
|
"""Throw a RuntimeError if not on a non-Windows system."""
|
||||||
if not sys.platform.startswith("win"):
|
if is_usable():
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"This printer driver depends on LP which is not"
|
"This printer driver depends on LP which is not"
|
||||||
"available on Windows systems."
|
"available on Windows systems."
|
||||||
|
@ -13,6 +13,11 @@ import socket
|
|||||||
from ..escpos import Escpos
|
from ..escpos import Escpos
|
||||||
|
|
||||||
|
|
||||||
|
def is_usable():
|
||||||
|
"""Indicate whether this component can be used due to dependencies."""
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class Network(Escpos):
|
class Network(Escpos):
|
||||||
"""Network printer.
|
"""Network printer.
|
||||||
|
|
||||||
|
@ -24,13 +24,21 @@ except ImportError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def is_usable():
|
||||||
|
"""Indicate whether this component can be used due to dependencies."""
|
||||||
|
usable = False
|
||||||
|
if not _DEP_PYSERIAL:
|
||||||
|
usable = True
|
||||||
|
return usable
|
||||||
|
|
||||||
|
|
||||||
def dependency_pyserial(func):
|
def dependency_pyserial(func):
|
||||||
"""Indicate dependency on pyserial."""
|
"""Indicate dependency on pyserial."""
|
||||||
|
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
"""Throw a RuntimeError if pyserial not installed."""
|
"""Throw a RuntimeError if pyserial not installed."""
|
||||||
if not _DEP_PYSERIAL:
|
if is_usable():
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"Printing with Serial requires the pyserial library to"
|
"Printing with Serial requires the pyserial library to"
|
||||||
"be installed. Please refer to the documentation on"
|
"be installed. Please refer to the documentation on"
|
||||||
|
@ -7,13 +7,47 @@
|
|||||||
:copyright: Copyright (c) 2012-2023 Bashlinux and python-escpos
|
:copyright: Copyright (c) 2012-2023 Bashlinux and python-escpos
|
||||||
:license: MIT
|
:license: MIT
|
||||||
"""
|
"""
|
||||||
|
import functools
|
||||||
import usb.core
|
|
||||||
import usb.util
|
|
||||||
|
|
||||||
from ..escpos import Escpos
|
from ..escpos import Escpos
|
||||||
from ..exceptions import USBNotFoundError
|
from ..exceptions import USBNotFoundError
|
||||||
|
|
||||||
|
#: keeps track if the usb dependency could be loaded (:py:class:`escpos.printer.Usb`)
|
||||||
|
_DEP_USB = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
import usb.core
|
||||||
|
import usb.util
|
||||||
|
|
||||||
|
_DEP_USB = True
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def is_usable():
|
||||||
|
"""Indicate whether this component can be used due to dependencies."""
|
||||||
|
usable = False
|
||||||
|
if not _DEP_USB:
|
||||||
|
usable = True
|
||||||
|
return usable
|
||||||
|
|
||||||
|
|
||||||
|
def dependency_usb(func):
|
||||||
|
"""Indicate dependency on usb."""
|
||||||
|
|
||||||
|
@functools.wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
"""Throw a RuntimeError if usb not installed."""
|
||||||
|
if is_usable():
|
||||||
|
raise RuntimeError(
|
||||||
|
"Printing with USB connection requires a usb library to"
|
||||||
|
"be installed. Please refer to the documentation on"
|
||||||
|
"what to install and install the dependencies for USB."
|
||||||
|
)
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
class Usb(Escpos):
|
class Usb(Escpos):
|
||||||
"""USB printer.
|
"""USB printer.
|
||||||
@ -37,7 +71,7 @@ class Usb(Escpos):
|
|||||||
out_ep=0x01,
|
out_ep=0x01,
|
||||||
*args,
|
*args,
|
||||||
**kwargs
|
**kwargs
|
||||||
): # noqa: N803
|
):
|
||||||
"""Initialize USB printer.
|
"""Initialize USB printer.
|
||||||
|
|
||||||
:param idVendor: Vendor ID
|
:param idVendor: Vendor ID
|
||||||
@ -59,6 +93,7 @@ class Usb(Escpos):
|
|||||||
usb_args["idProduct"] = idProduct
|
usb_args["idProduct"] = idProduct
|
||||||
self.open(usb_args)
|
self.open(usb_args)
|
||||||
|
|
||||||
|
@dependency_usb
|
||||||
def open(self, usb_args):
|
def open(self, usb_args):
|
||||||
"""Search device on USB tree and set it as escpos device.
|
"""Search device on USB tree and set it as escpos device.
|
||||||
|
|
||||||
@ -110,6 +145,7 @@ class Usb(Escpos):
|
|||||||
"""Read a data buffer and return it to the caller."""
|
"""Read a data buffer and return it to the caller."""
|
||||||
return self.device.read(self.in_ep, 16)
|
return self.device.read(self.in_ep, 16)
|
||||||
|
|
||||||
|
@dependency_usb
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Release USB interface."""
|
"""Release USB interface."""
|
||||||
if self.device:
|
if self.device:
|
||||||
|
@ -8,18 +8,45 @@
|
|||||||
:license: MIT
|
:license: MIT
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import functools
|
||||||
|
|
||||||
from ..escpos import Escpos
|
from ..escpos import Escpos
|
||||||
|
|
||||||
_WIN32PRINT = False
|
#: keeps track if the win32print dependency could be loaded (:py:class:`escpos.printer.Win32Raw`)
|
||||||
|
_DEP_WIN32PRINT = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import win32print
|
import win32print
|
||||||
|
|
||||||
_WIN32PRINT = True
|
_DEP_WIN32PRINT = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if _WIN32PRINT:
|
def is_usable():
|
||||||
|
"""Indicate whether this component can be used due to dependencies."""
|
||||||
|
usable = False
|
||||||
|
if not _DEP_WIN32PRINT:
|
||||||
|
usable = True
|
||||||
|
return usable
|
||||||
|
|
||||||
|
|
||||||
|
def dependency_win32print(func):
|
||||||
|
"""Indicate dependency on win32print."""
|
||||||
|
|
||||||
|
@functools.wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
"""Throw a RuntimeError if win32print not installed."""
|
||||||
|
if is_usable():
|
||||||
|
raise RuntimeError(
|
||||||
|
"Printing with Win32Raw requires a win32print library to"
|
||||||
|
"be installed. Please refer to the documentation on"
|
||||||
|
"what to install and install the dependencies for win32print."
|
||||||
|
)
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
class Win32Raw(Escpos):
|
class Win32Raw(Escpos):
|
||||||
"""Printer binding for win32 API.
|
"""Printer binding for win32 API.
|
||||||
@ -33,6 +60,7 @@ if _WIN32PRINT:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@dependency_win32print
|
||||||
def __init__(self, printer_name=None, *args, **kwargs):
|
def __init__(self, printer_name=None, *args, **kwargs):
|
||||||
"""Initialize default printer."""
|
"""Initialize default printer."""
|
||||||
Escpos.__init__(self, *args, **kwargs)
|
Escpos.__init__(self, *args, **kwargs)
|
||||||
@ -43,6 +71,7 @@ if _WIN32PRINT:
|
|||||||
self.hPrinter = None
|
self.hPrinter = None
|
||||||
self.open()
|
self.open()
|
||||||
|
|
||||||
|
@dependency_win32print
|
||||||
def open(self, job_name="python-escpos"):
|
def open(self, job_name="python-escpos"):
|
||||||
"""Open connection to default printer."""
|
"""Open connection to default printer."""
|
||||||
if self.printer_name is None:
|
if self.printer_name is None:
|
||||||
@ -53,6 +82,7 @@ if _WIN32PRINT:
|
|||||||
)
|
)
|
||||||
win32print.StartPagePrinter(self.hPrinter)
|
win32print.StartPagePrinter(self.hPrinter)
|
||||||
|
|
||||||
|
@dependency_win32print
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Close connection to default printer."""
|
"""Close connection to default printer."""
|
||||||
if not self.hPrinter:
|
if not self.hPrinter:
|
||||||
@ -62,6 +92,7 @@ if _WIN32PRINT:
|
|||||||
win32print.ClosePrinter(self.hPrinter)
|
win32print.ClosePrinter(self.hPrinter)
|
||||||
self.hPrinter = None
|
self.hPrinter = None
|
||||||
|
|
||||||
|
@dependency_win32print
|
||||||
def _raw(self, msg):
|
def _raw(self, msg):
|
||||||
"""Print any command sent in raw format.
|
"""Print any command sent in raw format.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user