From 206822ac696ad8ca249d1d7140f020f5b5a2d7b5 Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Wed, 22 May 2019 16:39:26 +0500 Subject: [PATCH 1/5] 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/5] 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/5] 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/5] 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/5] 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