reworking the exception model
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
class BrotherQLError(Exception): pass
|
||||||
|
class BrotherQLUnsupportedCmd(BrotherQLError): pass
|
||||||
|
class BrotherQLUnknownModel(BrotherQLError): pass
|
||||||
|
class BrotherQLRasterError(BrotherQLError): pass
|
||||||
|
|
||||||
|
|||||||
+25
-8
@@ -8,6 +8,7 @@ import numpy as np
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from brother_ql.raster import BrotherQLRaster
|
from brother_ql.raster import BrotherQLRaster
|
||||||
|
from brother_ql import BrotherQLError, BrotherQLUnsupportedCmd
|
||||||
|
|
||||||
try:
|
try:
|
||||||
stdout = sys.stdout.buffer
|
stdout = sys.stdout.buffer
|
||||||
@@ -36,6 +37,7 @@ def main():
|
|||||||
logging.basicConfig(level=args.loglevel)
|
logging.basicConfig(level=args.loglevel)
|
||||||
|
|
||||||
qlr = BrotherQLRaster(args.model)
|
qlr = BrotherQLRaster(args.model)
|
||||||
|
qlr.exception_on_warning = True
|
||||||
device_pixel_width = qlr.get_pixel_width()
|
device_pixel_width = qlr.get_pixel_width()
|
||||||
|
|
||||||
im = Image.open(args.image)
|
im = Image.open(args.image)
|
||||||
@@ -50,21 +52,36 @@ def main():
|
|||||||
arr[black_idx] = 0
|
arr[black_idx] = 0
|
||||||
|
|
||||||
|
|
||||||
qlr.add_switch_mode()
|
try:
|
||||||
|
qlr.add_switch_mode()
|
||||||
|
except BrotherQLUnsupportedCmd:
|
||||||
|
pass
|
||||||
qlr.add_invalidate()
|
qlr.add_invalidate()
|
||||||
qlr.add_initialize()
|
qlr.add_initialize()
|
||||||
qlr.add_switch_mode()
|
try:
|
||||||
|
qlr.add_switch_mode()
|
||||||
|
except BrotherQLUnsupportedCmd:
|
||||||
|
pass
|
||||||
qlr.mtype = 0x0A
|
qlr.mtype = 0x0A
|
||||||
qlr.mwidth = 62
|
qlr.mwidth = 62
|
||||||
qlr.mlength = 0
|
qlr.mlength = 0
|
||||||
qlr.add_media_and_quality(im.size[1])
|
qlr.add_media_and_quality(im.size[1])
|
||||||
qlr.add_autocut(True)
|
try:
|
||||||
qlr.add_cut_every(1)
|
qlr.add_autocut(True)
|
||||||
qlr.dpi_600 = False
|
qlr.add_cut_every(1)
|
||||||
qlr.cut_at_end = True
|
except BrotherQLUnsupportedCmd:
|
||||||
qlr.add_expanded_mode()
|
pass
|
||||||
|
try:
|
||||||
|
qlr.dpi_600 = False
|
||||||
|
qlr.cut_at_end = True
|
||||||
|
qlr.add_expanded_mode()
|
||||||
|
except BrotherQLUnsupportedCmd:
|
||||||
|
pass
|
||||||
qlr.add_margins()
|
qlr.add_margins()
|
||||||
qlr.add_compression(True)
|
try:
|
||||||
|
qlr.add_compression(True)
|
||||||
|
except BrotherQLUnsupportedCmd:
|
||||||
|
pass
|
||||||
qlr.add_raster_data(arr)
|
qlr.add_raster_data(arr)
|
||||||
qlr.add_print()
|
qlr.add_print()
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,8 @@ cuttingsupport = [
|
|||||||
'QL-720NW',
|
'QL-720NW',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
expandedmode = cuttingsupport
|
||||||
|
|
||||||
compressionsupport = [
|
compressionsupport = [
|
||||||
'QL-570',
|
'QL-570',
|
||||||
'QL-580N',
|
'QL-580N',
|
||||||
|
|||||||
+34
-11
@@ -13,15 +13,18 @@ from .devicedependent import models, \
|
|||||||
right_margin_addition, \
|
right_margin_addition, \
|
||||||
compressionsupport, \
|
compressionsupport, \
|
||||||
cuttingsupport, \
|
cuttingsupport, \
|
||||||
|
expandedmode, \
|
||||||
modesetting
|
modesetting
|
||||||
|
|
||||||
|
from . import BrotherQLError, BrotherQLUnsupportedCmd, BrotherQLUnknownModel, BrotherQLRasterError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class BrotherQLRaster(object):
|
class BrotherQLRaster(object):
|
||||||
|
|
||||||
def __init__(self, model='QL-500'):
|
def __init__(self, model='QL-500'):
|
||||||
if model not in models:
|
if model not in models:
|
||||||
raise BrotherQLRasterUnknownModel()
|
raise BrotherQLUnknownModel()
|
||||||
self.model = model
|
self.model = model
|
||||||
self.data = b''
|
self.data = b''
|
||||||
self._pquality = 1
|
self._pquality = 1
|
||||||
@@ -31,12 +34,30 @@ class BrotherQLRaster(object):
|
|||||||
self._compression = False
|
self._compression = False
|
||||||
self.exception_on_warning = False
|
self.exception_on_warning = False
|
||||||
|
|
||||||
def warn(self, problem):
|
def _warn(self, problem, kind=BrotherQLRasterError):
|
||||||
|
"""
|
||||||
|
Logs the warning message `problem` or raises a
|
||||||
|
`BrotherQLRasterError` exception (changeable via `kind`)
|
||||||
|
if `self.exception_on_warning` is set to True.
|
||||||
|
|
||||||
|
:raises BrotherQLRasterError: Or other exception \
|
||||||
|
set via the `kind` keyword argument.
|
||||||
|
"""
|
||||||
if self.exception_on_warning:
|
if self.exception_on_warning:
|
||||||
raise BrotherQLRasterError(problem)
|
raise kind(problem)
|
||||||
else:
|
else:
|
||||||
logger.warning(problem)
|
logger.warning(problem)
|
||||||
|
|
||||||
|
def unsupported(self, problem):
|
||||||
|
"""
|
||||||
|
Raises BrotherQLUnsupportedCmd if
|
||||||
|
exception_on_warning is set to True.
|
||||||
|
Issues a logger warning otherwise.
|
||||||
|
|
||||||
|
:raises BrotherQLUnsupportedCmd:
|
||||||
|
"""
|
||||||
|
self._warn(problem, kind=BrotherQLUnsupportedCmd)
|
||||||
|
|
||||||
def add_initialize(self):
|
def add_initialize(self):
|
||||||
self.page_number = 0
|
self.page_number = 0
|
||||||
self.data += b'\x1B\x40' # init
|
self.data += b'\x1B\x40' # init
|
||||||
@@ -48,7 +69,7 @@ class BrotherQLRaster(object):
|
|||||||
the mode change (others are in raster mode already).
|
the mode change (others are in raster mode already).
|
||||||
"""
|
"""
|
||||||
if self.model not in modesetting:
|
if self.model not in modesetting:
|
||||||
self.warn("Trying to switch the operating mode on a printer that doesn't support the command.")
|
self.unsupported("Trying to switch the operating mode on a printer that doesn't support the command.")
|
||||||
return
|
return
|
||||||
self.data += b'\x1B\x69\x61\x01'
|
self.data += b'\x1B\x69\x61\x01'
|
||||||
|
|
||||||
@@ -100,16 +121,22 @@ class BrotherQLRaster(object):
|
|||||||
# INFO: media/quality (1B 69 7A) --> found! (payload: 8E 0A 3E 00 D2 00 00 00 00 00)
|
# INFO: media/quality (1B 69 7A) --> found! (payload: 8E 0A 3E 00 D2 00 00 00 00 00)
|
||||||
|
|
||||||
def add_autocut(self, autocut = False):
|
def add_autocut(self, autocut = False):
|
||||||
|
if self.model not in cuttingsupport:
|
||||||
|
self.unsupported("Trying to call add_autocut with a printer that doesn't support it")
|
||||||
|
return
|
||||||
self.data += b'\x1B\x69\x4D'
|
self.data += b'\x1B\x69\x4D'
|
||||||
self.data += bytes([autocut << 6])
|
self.data += bytes([autocut << 6])
|
||||||
|
|
||||||
def add_cut_every(self, n=1):
|
def add_cut_every(self, n=1):
|
||||||
|
if self.model not in cuttingsupport:
|
||||||
|
self.unsupported("Trying to call add_cut_every with a printer that doesn't support it")
|
||||||
|
return
|
||||||
self.data += b'\x1B\x69\x41'
|
self.data += b'\x1B\x69\x41'
|
||||||
self.data += bytes([n & 0xFF])
|
self.data += bytes([n & 0xFF])
|
||||||
|
|
||||||
def add_expanded_mode(self):
|
def add_expanded_mode(self):
|
||||||
if self.model not in cuttingsupport:
|
if self.model not in expandedmode:
|
||||||
self.warn("Trying to set expanded mode on a printer that doesn't support it")
|
self.unsupported("Trying to set expanded mode (dpi/cutting at end) on a printer that doesn't support it")
|
||||||
return
|
return
|
||||||
self.data += b'\x1B\x69\x4B'
|
self.data += b'\x1B\x69\x4B'
|
||||||
flags = 0x00
|
flags = 0x00
|
||||||
@@ -123,7 +150,7 @@ class BrotherQLRaster(object):
|
|||||||
|
|
||||||
def add_compression(self, compression=True):
|
def add_compression(self, compression=True):
|
||||||
if self.model not in compressionsupport:
|
if self.model not in compressionsupport:
|
||||||
self.warn("Trying to set compression on a printer that doesn't support it")
|
self.unsupported("Trying to set compression on a printer that doesn't support it")
|
||||||
return
|
return
|
||||||
self._compression = compression
|
self._compression = compression
|
||||||
self.data += b'\x4D'
|
self.data += b'\x4D'
|
||||||
@@ -156,7 +183,3 @@ class BrotherQLRaster(object):
|
|||||||
self.data += b'\x1A'
|
self.data += b'\x1A'
|
||||||
else:
|
else:
|
||||||
self.data += b'\x0C'
|
self.data += b'\x0C'
|
||||||
|
|
||||||
class BrotherQLRasterError(Exception): pass
|
|
||||||
class BrotherQLRasterUnknownModel(BrotherQLRasterError): pass
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user