mirror of
https://github.com/python-escpos/python-escpos
synced 2025-07-15 08:43:30 +00:00
add dependency check for pyserial
This commit is contained in:
parent
c58381230b
commit
3643a22a10
@ -8,10 +8,38 @@
|
|||||||
:license: MIT
|
:license: MIT
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import serial
|
|
||||||
|
import functools
|
||||||
|
|
||||||
from ..escpos import Escpos
|
from ..escpos import Escpos
|
||||||
|
|
||||||
|
#: keeps track if the pyserial dependency could be loaded (:py:class:`escpos.printer.Serial`)
|
||||||
|
_DEP_PYSERIAL = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
import serial
|
||||||
|
|
||||||
|
_DEP_PYSERIAL = True
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def dependency_pyserial(func):
|
||||||
|
"""Indicate dependency on pyserial."""
|
||||||
|
|
||||||
|
@functools.wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
"""Throw a RuntimeError if pyserial not installed."""
|
||||||
|
if not _DEP_PYSERIAL:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Printing with Serial requires the pyserial library to"
|
||||||
|
"be installed. Please refer to the documentation on"
|
||||||
|
"what to install and install the dependencies for pyserial."
|
||||||
|
)
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
class Serial(Escpos):
|
class Serial(Escpos):
|
||||||
"""Serial printer.
|
"""Serial printer.
|
||||||
@ -25,14 +53,15 @@ class Serial(Escpos):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@dependency_pyserial
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
devfile="/dev/ttyS0",
|
devfile="/dev/ttyS0",
|
||||||
baudrate=9600,
|
baudrate=9600,
|
||||||
bytesize=8,
|
bytesize=8,
|
||||||
timeout=1,
|
timeout=1,
|
||||||
parity=serial.PARITY_NONE,
|
parity=None,
|
||||||
stopbits=serial.STOPBITS_ONE,
|
stopbits=None,
|
||||||
xonxoff=False,
|
xonxoff=False,
|
||||||
dsrdtr=True,
|
dsrdtr=True,
|
||||||
*args,
|
*args,
|
||||||
@ -54,13 +83,20 @@ class Serial(Escpos):
|
|||||||
self.baudrate = baudrate
|
self.baudrate = baudrate
|
||||||
self.bytesize = bytesize
|
self.bytesize = bytesize
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.parity = parity
|
if parity:
|
||||||
self.stopbits = stopbits
|
self.parity = parity
|
||||||
|
else:
|
||||||
|
self.parity = serial.PARITY_NONE
|
||||||
|
if stopbits:
|
||||||
|
self.stopbits = stopbits
|
||||||
|
else:
|
||||||
|
self.stopbits = serial.STOPBITS_ONE
|
||||||
self.xonxoff = xonxoff
|
self.xonxoff = xonxoff
|
||||||
self.dsrdtr = dsrdtr
|
self.dsrdtr = dsrdtr
|
||||||
|
|
||||||
self.open()
|
self.open()
|
||||||
|
|
||||||
|
@dependency_pyserial
|
||||||
def open(self):
|
def open(self):
|
||||||
"""Set up serial port and set is as escpos device."""
|
"""Set up serial port and set is as escpos device."""
|
||||||
if self.device is not None and self.device.is_open:
|
if self.device is not None and self.device.is_open:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user