From 206822ac696ad8ca249d1d7140f020f5b5a2d7b5 Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Wed, 22 May 2019 16:39:26 +0500 Subject: [PATCH 1/6] Enable Windows Support --- src/escpos/printer.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index d7fc6ac..1763c5c 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -17,6 +17,7 @@ import usb.core import usb.util import serial import socket +import sys from .escpos import Escpos from .exceptions import USBNotFoundError @@ -68,19 +69,22 @@ class Usb(Escpos): self.idVendor = self.device.idVendor self.idProduct = self.device.idProduct - check_driver = None + # detach_kernel_driver() doesn't really work on Windows, + # causing the library to not work on that platform. + if sys.platform != 'win32': + check_driver = None - try: - check_driver = self.device.is_kernel_driver_active(0) - except NotImplementedError: - pass - - if check_driver is None or check_driver: try: - self.device.detach_kernel_driver(0) - except usb.core.USBError as e: - if check_driver is not None: - print("Could not detatch kernel driver: {0}".format(str(e))) + check_driver = self.device.is_kernel_driver_active(0) + except NotImplementedError: + pass + + if check_driver is None or check_driver: + try: + self.device.detach_kernel_driver(0) + except usb.core.USBError as e: + if check_driver is not None: + print("Could not detatch kernel driver: {0}".format(str(e))) try: self.device.set_configuration() From d20646b2a941addfc1ca0cacc6d6cdabcfad41d0 Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Wed, 22 May 2019 17:01:13 +0500 Subject: [PATCH 2/6] Make windows enablement code more intelligent --- src/escpos/printer.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index 1763c5c..ed38d4f 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -17,7 +17,6 @@ import usb.core import usb.util import serial import socket -import sys from .escpos import Escpos from .exceptions import USBNotFoundError @@ -69,9 +68,11 @@ class Usb(Escpos): self.idVendor = self.device.idVendor self.idProduct = self.device.idProduct - # detach_kernel_driver() doesn't really work on Windows, - # causing the library to not work on that platform. - if sys.platform != 'win32': + # pyusb has three backends: libusb0, libusb1 and openusb but + # only libusb1 backend implements the methods is_kernel_driver_active() + # and detach_kernel_driver(). This change helps enable this + # library to work on Windows. + if sef.device.backend.__module__.endswith("libusb1"): check_driver = None try: From 035c42558168bfc3d53867a82115ecb917587808 Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Wed, 22 May 2019 17:09:27 +0500 Subject: [PATCH 3/6] Add author --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 6000080..f9e4de9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -20,6 +20,7 @@ Michael Billington Michael Elsdörfer mrwunderbar666 Nathan Bookham +Omer Akram Patrick Kanzler primax79 Qian Linfeng From 7c01a30d6c9c7422bf940659cb7b0c3142be4cc1 Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Wed, 22 May 2019 17:20:15 +0500 Subject: [PATCH 4/6] fix a typo --- src/escpos/printer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index ed38d4f..b20876b 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -72,7 +72,7 @@ class Usb(Escpos): # only libusb1 backend implements the methods is_kernel_driver_active() # and detach_kernel_driver(). This change helps enable this # library to work on Windows. - if sef.device.backend.__module__.endswith("libusb1"): + if self.device.backend.__module__.endswith("libusb1"): check_driver = None try: From 29ef88f5915b0a34ed0fc11e68af7b950872c8eb Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Wed, 22 May 2019 17:25:33 +0500 Subject: [PATCH 5/6] Better comment --- src/escpos/printer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index b20876b..648b4bb 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -70,8 +70,8 @@ class Usb(Escpos): # pyusb has three backends: libusb0, libusb1 and openusb but # only libusb1 backend implements the methods is_kernel_driver_active() - # and detach_kernel_driver(). This change helps enable this - # library to work on Windows. + # and detach_kernel_driver(). + # This helps enable this library to work on Windows. if self.device.backend.__module__.endswith("libusb1"): check_driver = None From 91ff83e5066e8ab262b1c1ae6d71acf46763ca84 Mon Sep 17 00:00:00 2001 From: Justin Vieira Date: Wed, 29 May 2019 11:21:20 -0400 Subject: [PATCH 6/6] Update to use pyyaml safe_load(), as load() is unsafe and disabled on some systems --- AUTHORS | 1 + src/escpos/capabilities.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 6000080..af7e937 100644 --- a/AUTHORS +++ b/AUTHORS @@ -11,6 +11,7 @@ Dmytro Katyukha Gerard Marull-Paretas Hark Joel Lehtonen +Justin Vieira kennedy Kristi ldos diff --git a/src/escpos/capabilities.py b/src/escpos/capabilities.py index 0cf76e1..c4b6ab5 100644 --- a/src/escpos/capabilities.py +++ b/src/escpos/capabilities.py @@ -38,7 +38,7 @@ else: if full_load: logger.debug('Loading and pickling capabilities') with open(capabilities_path) as cp, open(pickle_path, 'wb') as pp: - CAPABILITIES = yaml.load(cp) + CAPABILITIES = yaml.safe_load(cp) pickle.dump(CAPABILITIES, pp, protocol=2) logger.debug('Finished loading capabilities took %.2fs', time.time() - t0)