diff --git a/examples/codepage_tables.py b/examples/codepage_tables.py index e638b3c..7d5c008 100644 --- a/examples/codepage_tables.py +++ b/examples/codepage_tables.py @@ -46,8 +46,8 @@ def print_codepage(printer, codepage): printer._raw("{} ".format(hex(x)[2:])) printer.set() - for y in range(0,16): - byte = six.int2byte(x*16+y) + for y in range(0, 16): + byte = six.int2byte(x * 16 + y) if byte in (ESC, CTL_LF, CTL_FF, CTL_CR, CTL_HT, CTL_VT): byte = ' ' @@ -56,5 +56,6 @@ def print_codepage(printer, codepage): printer._raw(sep) printer._raw('\n') + if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/src/escpos/capabilities.py b/src/escpos/capabilities.py index a3bfd3a..3ff9e20 100644 --- a/src/escpos/capabilities.py +++ b/src/escpos/capabilities.py @@ -3,7 +3,6 @@ import six from os import path import yaml - # Load external printer database with open(path.join(path.dirname(__file__), 'capabilities.json')) as f: CAPABILITIES = yaml.load(f) @@ -11,7 +10,6 @@ with open(path.join(path.dirname(__file__), 'capabilities.json')) as f: PROFILES = CAPABILITIES['profiles'] - class NotSupported(Exception): """Raised if a requested feature is not suppored by the printer profile. @@ -61,7 +59,6 @@ class BaseProfile(object): return {v: k for k, v in self.codePages.items()} - def get_profile(name=None, **kwargs): """Get the profile by name; if no name is given, return the default profile. @@ -92,17 +89,18 @@ def get_profile_class(name): def clean(s): - # Remove invalid characters - s = re.sub('[^0-9a-zA-Z_]', '', s) - # Remove leading characters until we find a letter or underscore - s = re.sub('^[^a-zA-Z_]+', '', s) - return str(s) + # Remove invalid characters + s = re.sub('[^0-9a-zA-Z_]', '', s) + # Remove leading characters until we find a letter or underscore + s = re.sub('^[^a-zA-Z_]+', '', s) + return str(s) class Profile(get_profile_class('default')): """ For users, who want to provide their profile """ + def __init__(self, columns=None, features=None): super(Profile, self).__init__() @@ -114,7 +112,3 @@ class Profile(get_profile_class('default')): return self.columns return super(Profile, self).get_columns(font) - - - - diff --git a/src/escpos/codepages.py b/src/escpos/codepages.py index 05d5852..e22e1d0 100644 --- a/src/escpos/codepages.py +++ b/src/escpos/codepages.py @@ -12,11 +12,12 @@ class CodePageManager: def get_all(self): return self.data.values() - def get_encoding_name(self, encoding): + @staticmethod + def get_encoding_name(encoding): # TODO resolve the encoding alias return encoding.upper() def get_encoding(self, encoding): return self.data[encoding] -CodePages = CodePageManager(CAPABILITIES['encodings']) \ No newline at end of file +CodePages = CodePageManager(CAPABILITIES['encodings']) diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 7fce107..85898a5 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -81,7 +81,7 @@ class Escpos(object): :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 - """ + """ im = EscposImage(img_source) if im.height > fragment_height: @@ -93,13 +93,14 @@ class Escpos(object): impl=impl, fragment_height=fragment_height) return - + if impl == "bitImageRaster": # GS v 0, raster format bit image 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()) - + if impl == "graphics": # GS ( L raster format graphics 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() self._image_send_graphics_data(b'0', b'p', header + raster_data) self._image_send_graphics_data(b'0', b'2', b'') - + if impl == "bitImageColumn": # ESC *, column format bit image 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.") header = self._int_low_high(len(data) + len(m) + 2, 2) self._raw(GS + b'(k' + header + cn + fn + m + data) - + @staticmethod def _int_low_high(inp_number, out_bytes): """ 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. :param txt: text to be printed + :param font: font to be used, can be :code:`a` or :code`b` :param columns: amount of columns :return: None """ @@ -539,7 +541,7 @@ class Escpos(object): if divisor not in LINESPACING_FUNCS: 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))): 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)): diff --git a/src/escpos/katakana.py b/src/escpos/katakana.py index 14b2e61..927a59b 100644 --- a/src/escpos/katakana.py +++ b/src/escpos/katakana.py @@ -30,13 +30,12 @@ def encode_katakana(text): if char in TXT_ENC_KATAKANA_MAP: encoded.append(TXT_ENC_KATAKANA_MAP[char]) 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. pass return b"".join(encoded) - TXT_ENC_KATAKANA_MAP = { # Maps UTF-8 Katakana symbols to KATAKANA Page Codes # TODO: has this really to be hardcoded? diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index 3dbd256..0179110 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -67,7 +67,8 @@ class Encoder(object): ).format(encoding, ','.join(self.codepages.keys()))) return encoding - def _get_codepage_char_list(self, encoding): + @staticmethod + def _get_codepage_char_list(encoding): """Get codepage character list 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 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 :param char: char to encode diff --git a/test/test_cli.py b/test/test_cli.py index c9b2189..6b45036 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -30,7 +30,7 @@ printer: ) -class TestCLI(): +class TestCLI: """ Contains setups, teardowns, and tests for CLI """ diff --git a/test/test_function_barcode.py b/test/test_function_barcode.py index 2edd712..710e5cd 100644 --- a/test/test_function_barcode.py +++ b/test/test_function_barcode.py @@ -35,4 +35,4 @@ def test_lacks_support(bctype, supports_b): with pytest.raises(BarcodeTypeError): instance.barcode('test', bctype) - assert instance.output == b'' \ No newline at end of file + assert instance.output == b'' diff --git a/test/test_functions.py b/test/test_functions.py index 22ee737..96b923a 100644 --- a/test/test_functions.py +++ b/test/test_functions.py @@ -17,10 +17,10 @@ def test_line_spacing_rest(): def test_line_spacing_error_handling(): printer = Dummy() with assert_raises(ValueError): - printer.line_spacing(99, divisor=44) + printer.line_spacing(99, divisor=44) with assert_raises(ValueError): - printer.line_spacing(divisor=80, spacing=86) + printer.line_spacing(divisor=80, spacing=86) with assert_raises(ValueError): - printer.line_spacing(divisor=360, spacing=256) + printer.line_spacing(divisor=360, spacing=256) with assert_raises(ValueError): - printer.line_spacing(divisor=180, spacing=256) \ No newline at end of file + printer.line_spacing(divisor=180, spacing=256) diff --git a/test/test_magicencode.py b/test/test_magicencode.py index faaf66d..fe0edcf 100644 --- a/test/test_magicencode.py +++ b/test/test_magicencode.py @@ -22,7 +22,6 @@ from escpos.katakana import encode_katakana from escpos.exceptions import CharCodeError, Error - class TestEncoder: """ Tests the single encoders. diff --git a/test/test_printer_file.py b/test/test_printer_file.py index 4d1b188..d234de8 100644 --- a/test/test_printer_file.py +++ b/test/test_printer_file.py @@ -26,6 +26,7 @@ if six.PY3: else: mock_open_call = '__builtin__.open' + @given(path=text()) def test_load_file_printer(mocker, path): """test the loading of the file-printer""" diff --git a/test/test_profile.py b/test/test_profile.py index d75c88c..5f4fa4b 100644 --- a/test/test_profile.py +++ b/test/test_profile.py @@ -35,4 +35,4 @@ class TestCustomProfile: assert Profile(columns=10).get_columns('sdfasdf') == 10 def test_features(self): - assert Profile(features={'foo': True}).supports('foo') \ No newline at end of file + assert Profile(features={'foo': True}).supports('foo')