From 619d80a86734ccc11bcf37e1d72f05a06e8142f7 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Tue, 2 Aug 2016 00:45:36 +0200 Subject: [PATCH 1/5] doc add changelog-stump for post-2.1.1 release --- CHANGELOG.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 273adb4..4a04f09 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,15 @@ ********* Changelog ********* +2016-08-?? - Version 2.?.? - "Death and Gravity" +------------------------------------------------ + +changes +^^^^^^^ + +contributors +^^^^^^^^^^^^ + 2016-08-02 - Version 2.1.1 - "Contents May Differ" -------------------------------------------------- From 3d98eb8b9c8f609a9b5e16facf7c23bfbfc8efe4 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Tue, 2 Aug 2016 02:54:36 +0200 Subject: [PATCH 2/5] fix file-printer did not flush The file-printer did not automatically flush and thus behaved differently to the other printer-classes. Now the default behaviour is to flush after every call of _raw(). This can be disabled by calling the file-printer with auto_flush=False. fixes #106 --- src/escpos/printer.py | 6 +++- test/test_printer_file.py | 70 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 test/test_printer_file.py diff --git a/src/escpos/printer.py b/src/escpos/printer.py index 1ee1db7..ec936b5 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -229,13 +229,15 @@ class File(Escpos): """ - def __init__(self, devfile="/dev/usb/lp0", *args, **kwargs): + def __init__(self, devfile="/dev/usb/lp0", auto_flush=True, *args, **kwargs): """ :param devfile : Device file under dev filesystem + :param auto_flush: automatically call flush after every call of _raw() """ Escpos.__init__(self, *args, **kwargs) self.devfile = devfile + self.auto_flush = auto_flush self.open() def open(self): @@ -256,6 +258,8 @@ class File(Escpos): :type msg: bytes """ self.device.write(msg) + if self.auto_flush: + self.flush() def close(self): """ Close system file """ diff --git a/test/test_printer_file.py b/test/test_printer_file.py new file mode 100644 index 0000000..bce9a0e --- /dev/null +++ b/test/test_printer_file.py @@ -0,0 +1,70 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +"""tests for the File printer + +:author: `Patrick Kanzler `_ +:organization: `python-escpos `_ +:copyright: Copyright (c) 2016 `python-escpos `_ +:license: GNU GPL v3 +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +import six + +import mock +from hypothesis import given +from hypothesis.strategies import text + +import escpos.printer as printer + +if six.PY3: + mock_open_call = 'builtins.open' +else: + mock_open_call = '__builtin__.open' + +@given(path=text()) +@mock.patch(mock_open_call) +@mock.patch('escpos.escpos.Escpos.__init__') +def test_load_file_printer(mock_escpos, mock_open, path): + """test the loading of the file-printer""" + printer.File(devfile=path) + assert mock_escpos.called + mock_open.assert_called_with(path, "wb") + + +@given(txt=text()) +@mock.patch.object(printer.File, 'device') +@mock.patch(mock_open_call) +@mock.patch('escpos.escpos.Escpos.__init__') +def test_auto_flush(mock_escpos, mock_open, mock_device, txt): + """test auto_flush in file-printer""" + p = printer.File(auto_flush=False) + # inject the mocked device-object + p.device = mock_device + p._raw(txt) + assert not mock_device.flush.called + mock_device.reset_mock() + p = printer.File(auto_flush=True) + # inject the mocked device-object + p.device = mock_device + p._raw(txt) + assert mock_device.flush.called + + +@given(txt=text()) +@mock.patch.object(printer.File, 'device') +@mock.patch(mock_open_call) +def test_flush_on_close(mock_open, mock_device, txt): + """test flush on close in file-printer""" + p = printer.File(auto_flush=False) + # inject the mocked device-object + p.device = mock_device + p._raw(txt) + assert not mock_device.flush.called + p.close() + assert mock_device.flush.called + assert mock_device.close.called From 7c732ee615dcd7313ba81e7a1fa916a5dda32b22 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Tue, 2 Aug 2016 04:38:04 +0200 Subject: [PATCH 3/5] doc fix lists --- doc/user/methods.rst | 3 +++ doc/user/printers.rst | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/user/methods.rst b/doc/user/methods.rst index 055c3b0..12a076c 100644 --- a/doc/user/methods.rst +++ b/doc/user/methods.rst @@ -88,6 +88,7 @@ set("align", "font", "type", width, height, invert, smooth, flip) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Set text properties. + * ``align`` set horizontal position for text, the possible values are: * CENTER @@ -106,6 +107,7 @@ cut("mode") ^^^^^^^^^^^ Cut paper. + * ``mode`` set a full or partial cut. *Default:* full **Partial cut is not implemented in all printers.** @@ -132,6 +134,7 @@ control("align") ^^^^^^^^^^^^^^^^ Carrier feed and tabs. + * ``align`` is a string which takes any of the following values: * LF *for Line Feed* diff --git a/doc/user/printers.rst b/doc/user/printers.rst index cdf3c0f..073aef3 100644 --- a/doc/user/printers.rst +++ b/doc/user/printers.rst @@ -32,6 +32,7 @@ Network("host", port) ^^^^^^^^^^^^^^^^^^^^^ Based on socket + * ``host`` is an alphanumeric host name, could be either DNS host name or IP address. * ``port`` to write to (default = 9100) @@ -40,7 +41,8 @@ Problems with a network-attached printer can have numerous causes. Make sure tha Often you can check the IP address by triggering the self-test of the device. As a next step try to send text manually to the device. You could use for example: -:: +.. :: + echo "OK\n" | nc IPADDRESS 9100 # the port number is often 9100 @@ -50,4 +52,5 @@ File("file\_name") ^^^^^^^^^^^^^^^^^^ Printcap printers + * ``file_name`` is the full path to the device file name From 38f983593142ca4357adecf31d186180140627a1 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Mon, 1 Aug 2016 12:24:07 +0200 Subject: [PATCH 4/5] fix printing of CODE128 The control sequence {A or {B or {C can't be part of the qr code. For this the user has to supply this sequence. --- doc/index.rst | 1 + doc/user/barcode.rst | 34 ++++++++++++++++++++++++++++++++++ src/escpos/constants.py | 5 +---- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 doc/user/barcode.rst diff --git a/doc/index.rst b/doc/index.rst index 8e876a0..060fc7f 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -18,6 +18,7 @@ Content user/raspi user/todo user/usage + user/barcode .. toctree:: :maxdepth: 1 diff --git a/doc/user/barcode.rst b/doc/user/barcode.rst new file mode 100644 index 0000000..5d45419 --- /dev/null +++ b/doc/user/barcode.rst @@ -0,0 +1,34 @@ +Printing Barcodes +----------------- +:Last Reviewed: 2016-07-31 + +Most ESC/POS-printers implement barcode-printing. +The barcode-commandset is implemented in the barcode-method. +For a list of compatible barcodes you should check the manual of your printer. +As a rule of thumb: even older Epson-models support most 1D-barcodes. +To be sure just try some implementations and have a look at the notices below. + +barcode-method +~~~~~~~~~~~~~~ +The barcode-method is rather low-level and orients itself on the implementation of ESC/POS. +In the future this class could be supplemented by a high-level class that helps the user generating the payload. + +.. py:currentmodule:: escpos.escpos + +.. automethod:: Escpos.barcode + :noindex: + +CODE128 +~~~~~~~ +Code128 barcodes need a certain format. +For now the user has to make sure that the payload is correct. +For alphanumeric CODE128 you have to preface your payload with `{B`. + +.. code-block:: Python + + from escpos.printer import Dummy, Serial + p = Serial() + # print CODE128 012ABCDabcd + p.barcode("{B012ABCDabcd", "CODE128", function_type="B") + +A very good description on CODE128 is also on `Wikipedia `_. diff --git a/src/escpos/constants.py b/src/escpos/constants.py index 74b26eb..93721cb 100644 --- a/src/escpos/constants.py +++ b/src/escpos/constants.py @@ -168,10 +168,7 @@ BARCODE_TYPE_B = { 'NW7': _SET_BARCODE_TYPE(71), 'CODABAR': _SET_BARCODE_TYPE(71), # Same as NW7 'CODE93': _SET_BARCODE_TYPE(72), - # These are all the same barcode, but using different charcter sets - 'CODE128A': _SET_BARCODE_TYPE(73) + b'{A', # CODE128 character set A - 'CODE128B': _SET_BARCODE_TYPE(73) + b'{B', # CODE128 character set B - 'CODE128C': _SET_BARCODE_TYPE(73) + b'{C', # CODE128 character set C + 'CODE128': _SET_BARCODE_TYPE(73), 'GS1-128': _SET_BARCODE_TYPE(74), 'GS1 DATABAR OMNIDIRECTIONAL': _SET_BARCODE_TYPE(75), 'GS1 DATABAR TRUNCATED': _SET_BARCODE_TYPE(76), From d2e2ea88a697c71fba106033026212e4f687b70b Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Tue, 2 Aug 2016 16:05:22 +0200 Subject: [PATCH 5/5] doc write changelog for version v2.1.2 --- CHANGELOG.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4a04f09..65c2c9a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,15 +1,18 @@ ********* Changelog ********* -2016-08-?? - Version 2.?.? - "Death and Gravity" +2016-08-02 - Version 2.1.2 - "Death and Gravity" ------------------------------------------------ changes ^^^^^^^ +- fix File-printer: flush after every call of _raw() +- fix lists in documentation +- fix CODE128: by adding the control character to the barcode-selection-sequence the barcode became unusable contributors ^^^^^^^^^^^^ - +- Patrick Kanzler 2016-08-02 - Version 2.1.1 - "Contents May Differ" --------------------------------------------------