From 740fd76adbfce97cdf688f2db05f50a8bdbed44f Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Wed, 13 Apr 2022 21:25:33 -0300 Subject: [PATCH 1/3] [ADD] Cups Connection --- escpos/conn/__init__.py | 8 +++++ escpos/conn/cups.py | 76 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 escpos/conn/cups.py diff --git a/escpos/conn/__init__.py b/escpos/conn/__init__.py index e3db742..cb7c449 100644 --- a/escpos/conn/__init__.py +++ b/escpos/conn/__init__.py @@ -30,6 +30,7 @@ from .network import NetworkConnection from .serial import SerialConnection from .usb import USBConnection +from .cups import CupsConnection __all__ = [ @@ -39,6 +40,7 @@ 'NetworkConnection', 'SerialConnection', 'USBConnection', + 'CupsConnection', ] if six.PY2: @@ -51,6 +53,7 @@ NETWORK = 'network' SERIAL = 'serial' USB = 'usb' +CUPS = 'cups' ConnectionTypeInfo = namedtuple('ConnectionTypeInfo', [ @@ -89,5 +92,10 @@ name='USB', fqname='escpos.conn.usb.USBConnection', type=USBConnection)), + + (CUPS, ConnectionTypeInfo( + name='Cups', + fqname='escpos.conn.file.CupsConnection', + type=CupsConnection)), ) """Known implementations for connection with printers.""" diff --git a/escpos/conn/cups.py b/escpos/conn/cups.py new file mode 100644 index 0000000..0e84a8b --- /dev/null +++ b/escpos/conn/cups.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +# +# escpos/conn/cups.py +# +# Copyright 2022 KMEE LTDA +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from __future__ import absolute_import +from __future__ import print_function +from __future__ import unicode_literals + +import logging +import tempfile +import subprocess + +from .file import FileConnection + +from future.utils import python_2_unicode_compatible + +from ..helpers import hexdump + + +logger = logging.getLogger('escpos.conn.file') + + +@python_2_unicode_compatible +class CupsConnection(FileConnection): + + @classmethod + def create(cls, setting, **kwargs): + """Instantiate a :class:`NetworkConnection` (or subclass) object based + on a given host name and port number (eg. ``192.168.0.205:9100``). + """ + cups_ip, printer_name = setting.rsplit(',', 1) + return cls(cups_ip, printer_name, **kwargs) + + def __init__(self, cups_ip, printer_name, **kwargs): + super(CupsConnection, self).__init__(**kwargs) + self.cups_ip = cups_ip + self.printer_name = printer_name + + def open(self): + """Open system file.""" + self.device = open(self.devfile, "wb+") + if self.device is None: + print("Could not open the specified file {0}".format(self.devfile)) + + def write(self, data): + """Print any command sent in raw format. + :param bytes data: arbitrary code to be printed by cups. + """ + if logger.isEnabledFor(logging.DEBUG): + logger.debug('writing to file %s:\n%s', self, hexdump(data)) + self.device.write(data) + if self.auto_flush: + self.flush() + + + def close(self): + """Close system file.""" + super(CupsConnection, self).close() + if logger.isEnabledFor(logging.DEBUG): + logger.debug('sends to printer %s with cups %s:\n%s', self.printer_name, self.cups_ip, hexdump(data)) + + subprocess.Popen(['lp', '-h', self.cups_ip, '-d', self.printer_name, self.devfile], stdout=subprocess.PIPE, stderr=subprocess.PIPE) From 7fe712a8fbbd5d0d1eb90cddc497bdea9a6eb3ff Mon Sep 17 00:00:00 2001 From: Luis Felipe Mileo Date: Thu, 14 Apr 2022 11:20:41 -0300 Subject: [PATCH 2/3] [IMP] Namedtempfile for cups --- escpos/conn/cups.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/escpos/conn/cups.py b/escpos/conn/cups.py index 0e84a8b..02e2e97 100644 --- a/escpos/conn/cups.py +++ b/escpos/conn/cups.py @@ -30,6 +30,8 @@ from ..helpers import hexdump +from tempfile import NamedTemporaryFile + logger = logging.getLogger('escpos.conn.file') @@ -46,7 +48,8 @@ def create(cls, setting, **kwargs): return cls(cups_ip, printer_name, **kwargs) def __init__(self, cups_ip, printer_name, **kwargs): - super(CupsConnection, self).__init__(**kwargs) + self.temp_file = NamedTemporaryFile() + super(CupsConnection, self).__init__(devfile=self.temp_file.name, **kwargs) self.cups_ip = cups_ip self.printer_name = printer_name @@ -66,11 +69,14 @@ def write(self, data): if self.auto_flush: self.flush() - def close(self): """Close system file.""" super(CupsConnection, self).close() if logger.isEnabledFor(logging.DEBUG): logger.debug('sends to printer %s with cups %s:\n%s', self.printer_name, self.cups_ip, hexdump(data)) - - subprocess.Popen(['lp', '-h', self.cups_ip, '-d', self.printer_name, self.devfile], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + subprocess.Popen( + ['lp', '-h', self.cups_ip, '-d', self.printer_name, self.devfile], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + self.temp_file.close() From 9fc51571ad99fe6f650784247cf2133019a2a93b Mon Sep 17 00:00:00 2001 From: Luis Felipe Mileo Date: Thu, 14 Apr 2022 16:05:17 -0300 Subject: [PATCH 3/3] [FIX] Cups timeout --- escpos/conn/cups.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/escpos/conn/cups.py b/escpos/conn/cups.py index 02e2e97..b4c891a 100644 --- a/escpos/conn/cups.py +++ b/escpos/conn/cups.py @@ -74,9 +74,10 @@ def close(self): super(CupsConnection, self).close() if logger.isEnabledFor(logging.DEBUG): logger.debug('sends to printer %s with cups %s:\n%s', self.printer_name, self.cups_ip, hexdump(data)) - subprocess.Popen( + print_process = subprocess.Popen( ['lp', '-h', self.cups_ip, '-d', self.printer_name, self.devfile], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) + print_process.wait(timeout=3000) self.temp_file.close()