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
|
||||
"""
|
||||
|
||||
import serial
|
||||
|
||||
import functools
|
||||
|
||||
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):
|
||||
"""Serial printer.
|
||||
@ -25,14 +53,15 @@ class Serial(Escpos):
|
||||
|
||||
"""
|
||||
|
||||
@dependency_pyserial
|
||||
def __init__(
|
||||
self,
|
||||
devfile="/dev/ttyS0",
|
||||
baudrate=9600,
|
||||
bytesize=8,
|
||||
timeout=1,
|
||||
parity=serial.PARITY_NONE,
|
||||
stopbits=serial.STOPBITS_ONE,
|
||||
parity=None,
|
||||
stopbits=None,
|
||||
xonxoff=False,
|
||||
dsrdtr=True,
|
||||
*args,
|
||||
@ -54,13 +83,20 @@ class Serial(Escpos):
|
||||
self.baudrate = baudrate
|
||||
self.bytesize = bytesize
|
||||
self.timeout = timeout
|
||||
if parity:
|
||||
self.parity = parity
|
||||
else:
|
||||
self.parity = serial.PARITY_NONE
|
||||
if stopbits:
|
||||
self.stopbits = stopbits
|
||||
else:
|
||||
self.stopbits = serial.STOPBITS_ONE
|
||||
self.xonxoff = xonxoff
|
||||
self.dsrdtr = dsrdtr
|
||||
|
||||
self.open()
|
||||
|
||||
@dependency_pyserial
|
||||
def open(self):
|
||||
"""Set up serial port and set is as escpos device."""
|
||||
if self.device is not None and self.device.is_open:
|
||||
|
Loading…
x
Reference in New Issue
Block a user