From 7f3ede2e1dc8eb4447863f4ef4a64fafc00a65ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zdzis=C5=82aw=20Krajewski?= Date: Wed, 9 Jan 2019 18:40:21 +0100 Subject: [PATCH 1/7] Add communication error in status function. --- thermalprinter/exceptions.py | 3 +++ thermalprinter/thermalprinter.py | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/thermalprinter/exceptions.py b/thermalprinter/exceptions.py index 631d259..b7fe4da 100644 --- a/thermalprinter/exceptions.py +++ b/thermalprinter/exceptions.py @@ -18,3 +18,6 @@ class ThermalPrinterConstantError(ThermalPrinterError): class ThermalPrinterValueError(ThermalPrinterError): """ Value error handling class. """ + +class ThermalPrinterCommunicationError(ThermalPrinterError): + """ Communication error handling class """ diff --git a/thermalprinter/thermalprinter.py b/thermalprinter/thermalprinter.py index 9bfd013..25c64d4 100644 --- a/thermalprinter/thermalprinter.py +++ b/thermalprinter/thermalprinter.py @@ -10,7 +10,8 @@ from .constants import (BarCodePosition, CharSet, Chinese, CodePage, CodePageConverted, Command) -from .exceptions import ThermalPrinterAttributeError, ThermalPrinterValueError +from .exceptions import (ThermalPrinterAttributeError, ThermalPrinterValueError, + ThermalPrinterCommunicationError) from .validate import (validate_barcode, validate_barcode_position, validate_charset, validate_chinese_format, validate_codepage) @@ -586,9 +587,12 @@ def sleep(self, seconds=1): self.__is_sleeping = True self.send_command(Command.ESC, 56, seconds, seconds >> 8) - def status(self): + def status(self, suppress_communication_error=True): """ Check the printer status. If RX pin is not connected, all values will be set to True. + Setting suppress_communication_error to False will cause + rising ThermalPrinterCommunicationError in case lack of + response from printer. Return a dict: movement: False if the movement is not connected @@ -607,6 +611,10 @@ def status(self): ret['paper'] = stat & 0b00000100 == 0 ret['voltage'] = stat & 0b00001000 == 0 ret['temp'] = stat & 0b01000000 == 0 + + elif not suppress_communication_error: + raise ThermalPrinterCommunicationError() + return ret def strike(self, state=False): From 1e05725c32877af6b64dc568f82696c360756ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zdzis=C5=82aw=20Krajewski?= Date: Wed, 9 Jan 2019 19:00:41 +0100 Subject: [PATCH 2/7] Update tests for status function --- tests/test_methods.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_methods.py b/tests/test_methods.py index 535eb5d..ff67915 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -221,7 +221,8 @@ def test_signature_sleep(methods): def test_signature_status(methods): methods.remove(extract_stack(None, 2)[1][2].replace('test_signature_', '')) - sig = ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None) + sig = ArgSpec(args=['self', 'suppress_communication_error'], varargs=None, + keywords=None, defaults=(True,)) assert getargspec(ThermalPrinter.status) == sig From d78ec80e1073acefe4e97941894c5f2ae78038b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zdzis=C5=82aw=20Krajewski?= Date: Wed, 9 Jan 2019 19:23:15 +0100 Subject: [PATCH 3/7] Code styling --- thermalprinter/thermalprinter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/thermalprinter/thermalprinter.py b/thermalprinter/thermalprinter.py index 25c64d4..94da2d3 100644 --- a/thermalprinter/thermalprinter.py +++ b/thermalprinter/thermalprinter.py @@ -10,7 +10,8 @@ from .constants import (BarCodePosition, CharSet, Chinese, CodePage, CodePageConverted, Command) -from .exceptions import (ThermalPrinterAttributeError, ThermalPrinterValueError, +from .exceptions import (ThermalPrinterAttributeError, + ThermalPrinterValueError, ThermalPrinterCommunicationError) from .validate import (validate_barcode, validate_barcode_position, validate_charset, validate_chinese_format, From 70bf74abf424d49db18137e5873153bdfc948993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zdzis=C5=82aw=20Krajewski?= Date: Wed, 9 Jan 2019 19:47:28 +0100 Subject: [PATCH 4/7] Code styling --- thermalprinter/exceptions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/thermalprinter/exceptions.py b/thermalprinter/exceptions.py index b7fe4da..0b4f808 100644 --- a/thermalprinter/exceptions.py +++ b/thermalprinter/exceptions.py @@ -19,5 +19,6 @@ class ThermalPrinterConstantError(ThermalPrinterError): class ThermalPrinterValueError(ThermalPrinterError): """ Value error handling class. """ + class ThermalPrinterCommunicationError(ThermalPrinterError): """ Communication error handling class """ From 06743777485db8b7d1170be5441c954d5c481209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zdzis=C5=82aw=20Krajewski?= Date: Wed, 9 Jan 2019 19:53:26 +0100 Subject: [PATCH 5/7] Code styling, rename status function parameter --- tests/test_methods.py | 2 +- thermalprinter/thermalprinter.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_methods.py b/tests/test_methods.py index ff67915..709703e 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -221,7 +221,7 @@ def test_signature_sleep(methods): def test_signature_status(methods): methods.remove(extract_stack(None, 2)[1][2].replace('test_signature_', '')) - sig = ArgSpec(args=['self', 'suppress_communication_error'], varargs=None, + sig = ArgSpec(args=['self', 'raise_on_error'], varargs=None, keywords=None, defaults=(True,)) assert getargspec(ThermalPrinter.status) == sig diff --git a/thermalprinter/thermalprinter.py b/thermalprinter/thermalprinter.py index 94da2d3..ae9297a 100644 --- a/thermalprinter/thermalprinter.py +++ b/thermalprinter/thermalprinter.py @@ -588,7 +588,7 @@ def sleep(self, seconds=1): self.__is_sleeping = True self.send_command(Command.ESC, 56, seconds, seconds >> 8) - def status(self, suppress_communication_error=True): + def status(self, raise_on_error=True): """ Check the printer status. If RX pin is not connected, all values will be set to True. Setting suppress_communication_error to False will cause @@ -613,7 +613,7 @@ def status(self, suppress_communication_error=True): ret['voltage'] = stat & 0b00001000 == 0 ret['temp'] = stat & 0b01000000 == 0 - elif not suppress_communication_error: + elif raise_on_error: raise ThermalPrinterCommunicationError() return ret From e41bc66cc8b8bb9cf3bc8a58dfce9afc55509bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Wed, 9 Jan 2019 23:26:04 +0100 Subject: [PATCH 6/7] Update thermalprinter.py --- thermalprinter/thermalprinter.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/thermalprinter/thermalprinter.py b/thermalprinter/thermalprinter.py index ae9297a..ccd1aa4 100644 --- a/thermalprinter/thermalprinter.py +++ b/thermalprinter/thermalprinter.py @@ -591,9 +591,9 @@ def sleep(self, seconds=1): def status(self, raise_on_error=True): """ Check the printer status. If RX pin is not connected, all values will be set to True. - Setting suppress_communication_error to False will cause - rising ThermalPrinterCommunicationError in case lack of - response from printer. + Setting raise_on_error to False will cause rising + ThermalPrinterCommunicationError in case lack of + response from the printer. Return a dict: movement: False if the movement is not connected @@ -612,7 +612,6 @@ def status(self, raise_on_error=True): ret['paper'] = stat & 0b00000100 == 0 ret['voltage'] = stat & 0b00001000 == 0 ret['temp'] = stat & 0b01000000 == 0 - elif raise_on_error: raise ThermalPrinterCommunicationError() From 647d0b39a8b71f87579d5c6062c3215dce63ff04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Wed, 9 Jan 2019 23:27:57 +0100 Subject: [PATCH 7/7] Update thermalprinter.py --- thermalprinter/thermalprinter.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/thermalprinter/thermalprinter.py b/thermalprinter/thermalprinter.py index ccd1aa4..69672ca 100644 --- a/thermalprinter/thermalprinter.py +++ b/thermalprinter/thermalprinter.py @@ -591,9 +591,9 @@ def sleep(self, seconds=1): def status(self, raise_on_error=True): """ Check the printer status. If RX pin is not connected, all values will be set to True. - Setting raise_on_error to False will cause rising - ThermalPrinterCommunicationError in case lack of - response from the printer. + If raise_on_error is set to true (default) will cause rising + ThermalPrinterCommunicationError in case lack of response + from the printer. Return a dict: movement: False if the movement is not connected