From 3643a22a10540ae22fe01403301cba880e062779 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Thu, 17 Aug 2023 00:35:25 +0200 Subject: [PATCH] add dependency check for pyserial --- src/escpos/printer/serial.py | 46 ++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/escpos/printer/serial.py b/src/escpos/printer/serial.py index e2bc342..eb1d4ff 100644 --- a/src/escpos/printer/serial.py +++ b/src/escpos/printer/serial.py @@ -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 - self.parity = parity - self.stopbits = stopbits + 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: