| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
""" |
| 4 |
Backend to support Brother QL-series printers via the linux kernel USB printer interface. |
| 5 |
Works on Linux. |
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import unicode_literals |
| 9 |
from builtins import str |
| 10 |
|
| 11 |
import glob, os, time, select |
| 12 |
|
| 13 |
from .generic import BrotherQLBackendGeneric |
| 14 |
|
| 15 |
def list_available_devices(): |
| 16 |
""" |
| 17 |
List all available devices for the linux kernel backend |
| 18 |
|
| 19 |
returns: devices: a list of dictionaries with the keys 'identifier' and 'instance': \ |
| 20 |
[ {'identifier': 'file:///dev/usb/lp0', 'instance': None}, ] \ |
| 21 |
Instance is set to None because we don't want to open (and thus potentially block) the device here. |
| 22 |
""" |
| 23 |
|
| 24 |
paths = glob.glob('/dev/usb/lp*') |
| 25 |
|
| 26 |
return [{'identifier': 'file://' + path, 'instance': None} for path in paths] |
| 27 |
|
| 28 |
class BrotherQLBackendLinuxKernel(BrotherQLBackendGeneric): |
| 29 |
""" |
| 30 |
BrotherQL backend using the Linux Kernel USB Printer Device Handles |
| 31 |
""" |
| 32 |
|
| 33 |
def __init__(self, device_specifier): |
| 34 |
""" |
| 35 |
device_specifier: string or os.open(): identifier in the \ |
| 36 |
format file:///dev/usb/lp0 or os.open() raw device handle. |
| 37 |
""" |
| 38 |
|
| 39 |
self.read_timeout = 0.01 |
| 40 |
# strategy : try_twice or select |
| 41 |
self.strategy = 'select' |
| 42 |
if isinstance(device_specifier, str): |
| 43 |
if device_specifier.startswith('file://'): |
| 44 |
device_specifier = device_specifier[7:] |
| 45 |
self.dev = os.open(device_specifier, os.O_RDWR) |
| 46 |
elif isinstance(device_specifier, int): |
| 47 |
self.dev = device_specifier |
| 48 |
else: |
| 49 |
raise NotImplementedError('Currently the printer can be specified either via an appropriate string or via an os.open() handle.') |
| 50 |
|
| 51 |
self.write_dev = self.dev |
| 52 |
self.read_dev = self.dev |
| 53 |
|
| 54 |
def _write(self, data): |
| 55 |
os.write(self.write_dev, data) |
| 56 |
|
| 57 |
def _read(self, length=32): |
| 58 |
if self.strategy == 'try_twice': |
| 59 |
data = os.read(self.read_dev, length) |
| 60 |
if data: |
| 61 |
return data |
| 62 |
else: |
| 63 |
time.sleep(self.read_timeout) |
| 64 |
return os.read(self.read_dev, length) |
| 65 |
elif self.strategy == 'select': |
| 66 |
data = b'' |
| 67 |
start = time.time() |
| 68 |
while (not data) and (time.time() - start < self.read_timeout): |
| 69 |
result, _, _ = select.select([self.read_dev], [], [], 0) |
| 70 |
if self.read_dev in result: |
| 71 |
data += os.read(self.read_dev, length) |
| 72 |
if data: break |
| 73 |
time.sleep(0.001) |
| 74 |
if not data: |
| 75 |
# one last try if still no data: |
| 76 |
return os.read(self.read_dev, length) |
| 77 |
else: |
| 78 |
return data |
| 79 |
else: |
| 80 |
raise NotImplementedError('Unknown strategy') |
| 81 |
|
| 82 |
def _dispose(self): |
| 83 |
os.close(self.dev) |