Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Printer with cups #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions escpos/conn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .network import NetworkConnection
from .serial import SerialConnection
from .usb import USBConnection
from .cups import CupsConnection


__all__ = [
Expand All @@ -39,6 +40,7 @@
'NetworkConnection',
'SerialConnection',
'USBConnection',
'CupsConnection',
]

if six.PY2:
Expand All @@ -51,6 +53,7 @@
NETWORK = 'network'
SERIAL = 'serial'
USB = 'usb'
CUPS = 'cups'


ConnectionTypeInfo = namedtuple('ConnectionTypeInfo', [
Expand Down Expand Up @@ -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."""
83 changes: 83 additions & 0 deletions escpos/conn/cups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- 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

from tempfile import NamedTemporaryFile


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``).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provavelmente a documentação precisa ser ajustada, pois parece não coincidir com a implementação. No código, o separador é uma vírgula. Poderia ajustar, por favor? :)

"""
cups_ip, printer_name = setting.rsplit(',', 1)
return cls(cups_ip, printer_name, **kwargs)

def __init__(self, cups_ip, printer_name, **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

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))
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()