reformat PEP8 and similar issues
This commit is contained in:
parent
2ea8e69c66
commit
c2fc464c55
|
@ -46,8 +46,8 @@ def print_codepage(printer, codepage):
|
||||||
printer._raw("{} ".format(hex(x)[2:]))
|
printer._raw("{} ".format(hex(x)[2:]))
|
||||||
printer.set()
|
printer.set()
|
||||||
|
|
||||||
for y in range(0,16):
|
for y in range(0, 16):
|
||||||
byte = six.int2byte(x*16+y)
|
byte = six.int2byte(x * 16 + y)
|
||||||
|
|
||||||
if byte in (ESC, CTL_LF, CTL_FF, CTL_CR, CTL_HT, CTL_VT):
|
if byte in (ESC, CTL_LF, CTL_FF, CTL_CR, CTL_HT, CTL_VT):
|
||||||
byte = ' '
|
byte = ' '
|
||||||
|
@ -56,5 +56,6 @@ def print_codepage(printer, codepage):
|
||||||
printer._raw(sep)
|
printer._raw(sep)
|
||||||
printer._raw('\n')
|
printer._raw('\n')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -3,7 +3,6 @@ import six
|
||||||
from os import path
|
from os import path
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
# Load external printer database
|
# Load external printer database
|
||||||
with open(path.join(path.dirname(__file__), 'capabilities.json')) as f:
|
with open(path.join(path.dirname(__file__), 'capabilities.json')) as f:
|
||||||
CAPABILITIES = yaml.load(f)
|
CAPABILITIES = yaml.load(f)
|
||||||
|
@ -11,7 +10,6 @@ with open(path.join(path.dirname(__file__), 'capabilities.json')) as f:
|
||||||
PROFILES = CAPABILITIES['profiles']
|
PROFILES = CAPABILITIES['profiles']
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class NotSupported(Exception):
|
class NotSupported(Exception):
|
||||||
"""Raised if a requested feature is not suppored by the
|
"""Raised if a requested feature is not suppored by the
|
||||||
printer profile.
|
printer profile.
|
||||||
|
@ -61,7 +59,6 @@ class BaseProfile(object):
|
||||||
return {v: k for k, v in self.codePages.items()}
|
return {v: k for k, v in self.codePages.items()}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_profile(name=None, **kwargs):
|
def get_profile(name=None, **kwargs):
|
||||||
"""Get the profile by name; if no name is given, return the
|
"""Get the profile by name; if no name is given, return the
|
||||||
default profile.
|
default profile.
|
||||||
|
@ -92,17 +89,18 @@ def get_profile_class(name):
|
||||||
|
|
||||||
|
|
||||||
def clean(s):
|
def clean(s):
|
||||||
# Remove invalid characters
|
# Remove invalid characters
|
||||||
s = re.sub('[^0-9a-zA-Z_]', '', s)
|
s = re.sub('[^0-9a-zA-Z_]', '', s)
|
||||||
# Remove leading characters until we find a letter or underscore
|
# Remove leading characters until we find a letter or underscore
|
||||||
s = re.sub('^[^a-zA-Z_]+', '', s)
|
s = re.sub('^[^a-zA-Z_]+', '', s)
|
||||||
return str(s)
|
return str(s)
|
||||||
|
|
||||||
|
|
||||||
class Profile(get_profile_class('default')):
|
class Profile(get_profile_class('default')):
|
||||||
"""
|
"""
|
||||||
For users, who want to provide their profile
|
For users, who want to provide their profile
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, columns=None, features=None):
|
def __init__(self, columns=None, features=None):
|
||||||
super(Profile, self).__init__()
|
super(Profile, self).__init__()
|
||||||
|
|
||||||
|
@ -114,7 +112,3 @@ class Profile(get_profile_class('default')):
|
||||||
return self.columns
|
return self.columns
|
||||||
|
|
||||||
return super(Profile, self).get_columns(font)
|
return super(Profile, self).get_columns(font)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,11 +12,12 @@ class CodePageManager:
|
||||||
def get_all(self):
|
def get_all(self):
|
||||||
return self.data.values()
|
return self.data.values()
|
||||||
|
|
||||||
def get_encoding_name(self, encoding):
|
@staticmethod
|
||||||
|
def get_encoding_name(encoding):
|
||||||
# TODO resolve the encoding alias
|
# TODO resolve the encoding alias
|
||||||
return encoding.upper()
|
return encoding.upper()
|
||||||
|
|
||||||
def get_encoding(self, encoding):
|
def get_encoding(self, encoding):
|
||||||
return self.data[encoding]
|
return self.data[encoding]
|
||||||
|
|
||||||
CodePages = CodePageManager(CAPABILITIES['encodings'])
|
CodePages = CodePageManager(CAPABILITIES['encodings'])
|
||||||
|
|
|
@ -81,7 +81,7 @@ class Escpos(object):
|
||||||
:param impl: choose image printing mode between `bitImageRaster`, `graphics` or `bitImageColumn`
|
:param impl: choose image printing mode between `bitImageRaster`, `graphics` or `bitImageColumn`
|
||||||
:param fragment_height: Images larger than this will be split into multiple fragments *default:* 960
|
:param fragment_height: Images larger than this will be split into multiple fragments *default:* 960
|
||||||
|
|
||||||
"""
|
"""
|
||||||
im = EscposImage(img_source)
|
im = EscposImage(img_source)
|
||||||
|
|
||||||
if im.height > fragment_height:
|
if im.height > fragment_height:
|
||||||
|
@ -93,13 +93,14 @@ class Escpos(object):
|
||||||
impl=impl,
|
impl=impl,
|
||||||
fragment_height=fragment_height)
|
fragment_height=fragment_height)
|
||||||
return
|
return
|
||||||
|
|
||||||
if impl == "bitImageRaster":
|
if impl == "bitImageRaster":
|
||||||
# GS v 0, raster format bit image
|
# GS v 0, raster format bit image
|
||||||
density_byte = (0 if high_density_horizontal else 1) + (0 if high_density_vertical else 2)
|
density_byte = (0 if high_density_horizontal else 1) + (0 if high_density_vertical else 2)
|
||||||
header = GS + b"v0" + six.int2byte(density_byte) + self._int_low_high(im.width_bytes, 2) + self._int_low_high(im.height, 2)
|
header = GS + b"v0" + six.int2byte(density_byte) + self._int_low_high(im.width_bytes, 2) +\
|
||||||
|
self._int_low_high(im.height, 2)
|
||||||
self._raw(header + im.to_raster_format())
|
self._raw(header + im.to_raster_format())
|
||||||
|
|
||||||
if impl == "graphics":
|
if impl == "graphics":
|
||||||
# GS ( L raster format graphics
|
# GS ( L raster format graphics
|
||||||
img_header = self._int_low_high(im.width, 2) + self._int_low_high(im.height, 2)
|
img_header = self._int_low_high(im.width, 2) + self._int_low_high(im.height, 2)
|
||||||
|
@ -111,7 +112,7 @@ class Escpos(object):
|
||||||
raster_data = im.to_raster_format()
|
raster_data = im.to_raster_format()
|
||||||
self._image_send_graphics_data(b'0', b'p', header + raster_data)
|
self._image_send_graphics_data(b'0', b'p', header + raster_data)
|
||||||
self._image_send_graphics_data(b'0', b'2', b'')
|
self._image_send_graphics_data(b'0', b'2', b'')
|
||||||
|
|
||||||
if impl == "bitImageColumn":
|
if impl == "bitImageColumn":
|
||||||
# ESC *, column format bit image
|
# ESC *, column format bit image
|
||||||
density_byte = (1 if high_density_horizontal else 0) + (32 if high_density_vertical else 0)
|
density_byte = (1 if high_density_horizontal else 0) + (32 if high_density_vertical else 0)
|
||||||
|
@ -198,7 +199,7 @@ class Escpos(object):
|
||||||
raise ValueError("cn and fn must be one byte each.")
|
raise ValueError("cn and fn must be one byte each.")
|
||||||
header = self._int_low_high(len(data) + len(m) + 2, 2)
|
header = self._int_low_high(len(data) + len(m) + 2, 2)
|
||||||
self._raw(GS + b'(k' + header + cn + fn + m + data)
|
self._raw(GS + b'(k' + header + cn + fn + m + data)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _int_low_high(inp_number, out_bytes):
|
def _int_low_high(inp_number, out_bytes):
|
||||||
""" Generate multiple bytes for a number: In lower and higher parts, or more parts as needed.
|
""" Generate multiple bytes for a number: In lower and higher parts, or more parts as needed.
|
||||||
|
@ -398,6 +399,7 @@ class Escpos(object):
|
||||||
Text has to be encoded in unicode.
|
Text has to be encoded in unicode.
|
||||||
|
|
||||||
:param txt: text to be printed
|
:param txt: text to be printed
|
||||||
|
:param font: font to be used, can be :code:`a` or :code`b`
|
||||||
:param columns: amount of columns
|
:param columns: amount of columns
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
@ -539,7 +541,7 @@ class Escpos(object):
|
||||||
|
|
||||||
if divisor not in LINESPACING_FUNCS:
|
if divisor not in LINESPACING_FUNCS:
|
||||||
raise ValueError("divisor must be either 360, 180 or 60")
|
raise ValueError("divisor must be either 360, 180 or 60")
|
||||||
if (divisor in [360, 180] \
|
if (divisor in [360, 180]
|
||||||
and (not(0 <= spacing <= 255))):
|
and (not(0 <= spacing <= 255))):
|
||||||
raise ValueError("spacing must be a int between 0 and 255 when divisor is 360 or 180")
|
raise ValueError("spacing must be a int between 0 and 255 when divisor is 360 or 180")
|
||||||
if divisor == 60 and (not(0 <= spacing <= 85)):
|
if divisor == 60 and (not(0 <= spacing <= 85)):
|
||||||
|
|
|
@ -30,13 +30,12 @@ def encode_katakana(text):
|
||||||
if char in TXT_ENC_KATAKANA_MAP:
|
if char in TXT_ENC_KATAKANA_MAP:
|
||||||
encoded.append(TXT_ENC_KATAKANA_MAP[char])
|
encoded.append(TXT_ENC_KATAKANA_MAP[char])
|
||||||
else:
|
else:
|
||||||
#TODO doesn't this discard all that is not in the map? Can we be shure that the input does contain only
|
# TODO doesn't this discard all that is not in the map? Can we be sure that the input does contain only
|
||||||
# encodable characters? We could at least throw an exception if encoding is not possible.
|
# encodable characters? We could at least throw an exception if encoding is not possible.
|
||||||
pass
|
pass
|
||||||
return b"".join(encoded)
|
return b"".join(encoded)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TXT_ENC_KATAKANA_MAP = {
|
TXT_ENC_KATAKANA_MAP = {
|
||||||
# Maps UTF-8 Katakana symbols to KATAKANA Page Codes
|
# Maps UTF-8 Katakana symbols to KATAKANA Page Codes
|
||||||
# TODO: has this really to be hardcoded?
|
# TODO: has this really to be hardcoded?
|
||||||
|
|
|
@ -67,7 +67,8 @@ class Encoder(object):
|
||||||
).format(encoding, ','.join(self.codepages.keys())))
|
).format(encoding, ','.join(self.codepages.keys())))
|
||||||
return encoding
|
return encoding
|
||||||
|
|
||||||
def _get_codepage_char_list(self, encoding):
|
@staticmethod
|
||||||
|
def _get_codepage_char_list(encoding):
|
||||||
"""Get codepage character list
|
"""Get codepage character list
|
||||||
|
|
||||||
Gets characters 128-255 for a given code page, as an array.
|
Gets characters 128-255 for a given code page, as an array.
|
||||||
|
@ -126,7 +127,8 @@ class Encoder(object):
|
||||||
is_encodable = char in available_map
|
is_encodable = char in available_map
|
||||||
return is_ascii or is_encodable
|
return is_ascii or is_encodable
|
||||||
|
|
||||||
def _encode_char(self, char, charmap, defaultchar):
|
@staticmethod
|
||||||
|
def _encode_char(char, charmap, defaultchar):
|
||||||
""" Encode a single character with the given encoding map
|
""" Encode a single character with the given encoding map
|
||||||
|
|
||||||
:param char: char to encode
|
:param char: char to encode
|
||||||
|
|
|
@ -30,7 +30,7 @@ printer:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestCLI():
|
class TestCLI:
|
||||||
""" Contains setups, teardowns, and tests for CLI
|
""" Contains setups, teardowns, and tests for CLI
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
@ -35,4 +35,4 @@ def test_lacks_support(bctype, supports_b):
|
||||||
with pytest.raises(BarcodeTypeError):
|
with pytest.raises(BarcodeTypeError):
|
||||||
instance.barcode('test', bctype)
|
instance.barcode('test', bctype)
|
||||||
|
|
||||||
assert instance.output == b''
|
assert instance.output == b''
|
||||||
|
|
|
@ -17,10 +17,10 @@ def test_line_spacing_rest():
|
||||||
def test_line_spacing_error_handling():
|
def test_line_spacing_error_handling():
|
||||||
printer = Dummy()
|
printer = Dummy()
|
||||||
with assert_raises(ValueError):
|
with assert_raises(ValueError):
|
||||||
printer.line_spacing(99, divisor=44)
|
printer.line_spacing(99, divisor=44)
|
||||||
with assert_raises(ValueError):
|
with assert_raises(ValueError):
|
||||||
printer.line_spacing(divisor=80, spacing=86)
|
printer.line_spacing(divisor=80, spacing=86)
|
||||||
with assert_raises(ValueError):
|
with assert_raises(ValueError):
|
||||||
printer.line_spacing(divisor=360, spacing=256)
|
printer.line_spacing(divisor=360, spacing=256)
|
||||||
with assert_raises(ValueError):
|
with assert_raises(ValueError):
|
||||||
printer.line_spacing(divisor=180, spacing=256)
|
printer.line_spacing(divisor=180, spacing=256)
|
||||||
|
|
|
@ -22,7 +22,6 @@ from escpos.katakana import encode_katakana
|
||||||
from escpos.exceptions import CharCodeError, Error
|
from escpos.exceptions import CharCodeError, Error
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestEncoder:
|
class TestEncoder:
|
||||||
"""
|
"""
|
||||||
Tests the single encoders.
|
Tests the single encoders.
|
||||||
|
|
|
@ -26,6 +26,7 @@ if six.PY3:
|
||||||
else:
|
else:
|
||||||
mock_open_call = '__builtin__.open'
|
mock_open_call = '__builtin__.open'
|
||||||
|
|
||||||
|
|
||||||
@given(path=text())
|
@given(path=text())
|
||||||
def test_load_file_printer(mocker, path):
|
def test_load_file_printer(mocker, path):
|
||||||
"""test the loading of the file-printer"""
|
"""test the loading of the file-printer"""
|
||||||
|
|
|
@ -35,4 +35,4 @@ class TestCustomProfile:
|
||||||
assert Profile(columns=10).get_columns('sdfasdf') == 10
|
assert Profile(columns=10).get_columns('sdfasdf') == 10
|
||||||
|
|
||||||
def test_features(self):
|
def test_features(self):
|
||||||
assert Profile(features={'foo': True}).supports('foo')
|
assert Profile(features={'foo': True}).supports('foo')
|
||||||
|
|
Loading…
Reference in New Issue