From f8d1a91001740fbd4740f407194da2a96af1b4c8 Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 16 Mar 2020 15:07:33 -0400 Subject: [PATCH] Ran black, updated to pylint 2.x --- .github/workflows/build.yml | 2 +- .pylintrc | 3 +- adafruit_thermal_printer/__init__.py | 16 +- adafruit_thermal_printer/thermal_printer.py | 175 +++++++++++------- .../thermal_printer_264.py | 30 +-- .../thermal_printer_legacy.py | 52 +++--- docs/conf.py | 110 ++++++----- examples/thermal_printer_simpletest.py | 34 ++-- setup.py | 50 +++-- 9 files changed, 266 insertions(+), 206 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fff3aa9..1dad804 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,7 +40,7 @@ jobs: source actions-ci/install.sh - name: Pip install pylint, black, & Sphinx run: | - pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme + pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme - name: Library version run: git describe --dirty --always --tags - name: PyLint diff --git a/.pylintrc b/.pylintrc index cd65e95..d8f0ee8 100644 --- a/.pylintrc +++ b/.pylintrc @@ -119,7 +119,8 @@ spelling-store-unknown-words=no [MISCELLANEOUS] # List of note tags to take in consideration, separated by a comma. -notes=FIXME,XXX,TODO +# notes=FIXME,XXX,TODO +notes=FIXME,XXX [TYPECHECK] diff --git a/adafruit_thermal_printer/__init__.py b/adafruit_thermal_printer/__init__.py index 2f45381..b0a8fed 100644 --- a/adafruit_thermal_printer/__init__.py +++ b/adafruit_thermal_printer/__init__.py @@ -7,9 +7,17 @@ * Author(s): Tony DiCola """ -from adafruit_thermal_printer.thermal_printer import JUSTIFY_LEFT, \ -JUSTIFY_CENTER, JUSTIFY_RIGHT, SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE, \ -UNDERLINE_THIN, UNDERLINE_THICK +from adafruit_thermal_printer.thermal_printer import ( + JUSTIFY_LEFT, + JUSTIFY_CENTER, + JUSTIFY_RIGHT, + SIZE_SMALL, + SIZE_MEDIUM, + SIZE_LARGE, + UNDERLINE_THIN, + UNDERLINE_THICK, +) + def get_printer_class(version): """Retrieve the class to construct for an instance of the specified @@ -20,10 +28,12 @@ def get_printer_class(version): """ assert version is not None assert version >= 0.0 + # pylint: disable=import-outside-toplevel if version < 2.64: import adafruit_thermal_printer.thermal_printer_legacy as thermal_printer elif version < 2.68: import adafruit_thermal_printer.thermal_printer_264 as thermal_printer else: import adafruit_thermal_printer.thermal_printer as thermal_printer + # pylint: enable=import-outside-toplevel return thermal_printer.ThermalPrinter diff --git a/adafruit_thermal_printer/thermal_printer.py b/adafruit_thermal_printer/thermal_printer.py index c63f5ef..b377ffe 100644 --- a/adafruit_thermal_printer/thermal_printer.py +++ b/adafruit_thermal_printer/thermal_printer.py @@ -59,21 +59,21 @@ # pylint: disable=bad-whitespace # Internally used constants. -_UPDOWN_MASK = const(1 << 2) -_BOLD_MASK = const(1 << 3) +_UPDOWN_MASK = const(1 << 2) +_BOLD_MASK = const(1 << 3) _DOUBLE_HEIGHT_MASK = const(1 << 4) -_DOUBLE_WIDTH_MASK = const(1 << 5) -_STRIKE_MASK = const(1 << 6) +_DOUBLE_WIDTH_MASK = const(1 << 5) +_STRIKE_MASK = const(1 << 6) # External constants: -JUSTIFY_LEFT = const(0) -JUSTIFY_CENTER = const(1) -JUSTIFY_RIGHT = const(2) -SIZE_SMALL = const(0) -SIZE_MEDIUM = const(1) -SIZE_LARGE = const(2) -UNDERLINE_THIN = const(0) -UNDERLINE_THICK = const(1) +JUSTIFY_LEFT = const(0) +JUSTIFY_CENTER = const(1) +JUSTIFY_RIGHT = const(2) +SIZE_SMALL = const(0) +SIZE_MEDIUM = const(1) +SIZE_LARGE = const(2) +UNDERLINE_THIN = const(0) +UNDERLINE_THICK = const(1) # pylint: enable=bad-whitespace @@ -111,14 +111,14 @@ class ThermalPrinter: # as class-level variables that users can reference (i.e. # ThermalPrinter.UPC_A, etc) and write code that is independent of the # printer firmware version. - UPC_A = 65 - UPC_E = 66 - EAN13 = 67 - EAN8 = 68 - CODE39 = 69 - ITF = 70 + UPC_A = 65 + UPC_E = 66 + EAN13 = 67 + EAN8 = 68 + CODE39 = 69 + ITF = 70 CODABAR = 71 - CODE93 = 72 + CODE93 = 72 CODE128 = 73 # pylint: enable=bad-whitespace @@ -149,11 +149,19 @@ def __set__(self, obj, val): obj._set_print_mode(self._mask) else: obj._unset_print_mode(self._mask) + # pylint: enable=protected-access # pylint: enable=too-few-public-methods - def __init__(self, uart, *, byte_delay_s=0.00057346, dot_feed_s=0.0021, - dot_print_s=0.03, auto_warm_up=True): + def __init__( + self, + uart, + *, + byte_delay_s=0.00057346, + dot_feed_s=0.0021, + dot_print_s=0.03, + auto_warm_up=True + ): """Thermal printer class. Requires a serial UART connection with at least the TX pin connected. Take care connecting RX as the printer will output a 5V signal which can damage boards! If RX is unconnected @@ -199,21 +207,21 @@ def _wait_timeout(self): def _write_char(self, char): # Write a single character to the printer. - if char == '\r': + if char == "\r": return # Strip carriage returns by skipping them. self._wait_timeout() - self._uart.write(bytes(char, 'ascii')) + self._uart.write(bytes(char, "ascii")) delay = self._byte_delay_s # Add extra delay for newlines or moving past the last column. - if char == '\n' or self._column == self._max_column: + if char == "\n" or self._column == self._max_column: if self._column == 0: # Feed line delay - delay += ((self._char_height + self._line_spacing) * \ - self._dot_feed_s) + delay += (self._char_height + self._line_spacing) * self._dot_feed_s else: # Text line delay - delay += ((self._char_height * self._dot_print_s) + \ - (self._line_spacing * self._dot_feed_s)) + delay += (self._char_height * self._dot_print_s) + ( + self._line_spacing * self._dot_feed_s + ) self._column = 0 else: self._column += 1 @@ -221,14 +229,16 @@ def _write_char(self, char): def _write_print_mode(self): # Write the printer mode to the printer. - self.send_command('\x1B!{0}'.format(chr(self._print_mode))) # ESC + '!' + print mode byte + self.send_command( + "\x1B!{0}".format(chr(self._print_mode)) + ) # ESC + '!' + print mode byte # Adjust character height and column count based on print mode. self._char_height = 48 if self._print_mode & _DOUBLE_HEIGHT_MASK else 24 self._max_column = 16 if self._print_mode & _DOUBLE_WIDTH_MASK else 32 def _set_print_mode(self, mask): # Enable the specified bits of the print mode. - self._print_mode |= (mask & 0xFF) + self._print_mode |= mask & 0xFF self._write_print_mode() def _unset_print_mode(self, mask): @@ -238,7 +248,7 @@ def _unset_print_mode(self, mask): def send_command(self, command): """Send a command string to the printer.""" - self._uart.write(bytes(command, 'ascii')) + self._uart.write(bytes(command, "ascii")) # Do initialization in warm_up instead of the initializer because this # initialization takes a long time (5 seconds) and shouldn't happen during @@ -271,22 +281,22 @@ def warm_up(self, heat_time=120): # possibly paper 'stiction'. More heating interval = clearer print, # but slower printing speed. # Send ESC + '7' (print settings) + heating dots, heat time, heat interval. - self.send_command('\x1B7\x0B{0}\x28'.format(chr(heat_time))) + self.send_command("\x1B7\x0B{0}\x28".format(chr(heat_time))) # Print density description from manual: # DC2 # n Set printing density # D4..D0 of n is used to set the printing density. Density is # 50% + 5% * n(D4-D0) printing density. # D7..D5 of n is used to set the printing break time. Break time # is n(D7-D5)*250us. - print_density = 10 # 100% (? can go higher, text is darker but fuzzy) + print_density = 10 # 100% (? can go higher, text is darker but fuzzy) print_break_time = 2 # 500 uS dc2_value = (print_break_time << 5) | print_density - self.send_command('\x12#{0}'.format(chr(dc2_value))) # DC2 + '#' + value + self.send_command("\x12#{0}".format(chr(dc2_value))) # DC2 + '#' + value def reset(self): """Reset the printer.""" # Issue a reset command to the printer. (ESC + @) - self.send_command('\x1B@') + self.send_command("\x1B@") # Reset internal state: self._column = 0 self._max_column = 32 @@ -295,10 +305,9 @@ def reset(self): self._barcode_height = 50 # Configure tab stops on recent printers. # ESC + 'D' + tab stop value list ending with null to terminate. - self.send_command('\x1BD\x04\x08\x10\x14\x18\x1C\x00') - + self.send_command("\x1BD\x04\x08\x10\x14\x18\x1C\x00") - def print(self, text, end='\n'): + def print(self, text, end="\n"): """Print a line of text. Optionally specify the end keyword to override the new line printed after the text (set to None to disable the new line entirely). @@ -318,9 +327,9 @@ def print_barcode(self, text, barcode_type): assert 0 <= barcode_type <= 255 assert 0 <= len(text) <= 255 self.feed(1) # Recent firmware can't print barcode w/o feed first??? - self.send_command('\x1DH\x02') # Print label below barcode - self.send_command('\x1Dw\x03') # Barcode width 3 (0.375/1.0mm thin/thick) - self.send_command('\x1Dk{0}'.format(chr(barcode_type))) # Barcode type + self.send_command("\x1DH\x02") # Print label below barcode + self.send_command("\x1Dw\x03") # Barcode width 3 (0.375/1.0mm thin/thick) + self.send_command("\x1Dk{0}".format(chr(barcode_type))) # Barcode type # Write length and then string (note this only works with 2.64+). self.send_command(chr(len(text))) self.send_command(text) @@ -349,8 +358,9 @@ def _print_bitmap(self, width, height, data): for row_start in range(0, height, chunk_height_limit): # Issue up to chunkHeightLimit rows at a time. chunk_height = min(height - row_start, chunk_height_limit) - self.send_command('\x12*{0}{1}'.format(chr(chunk_height), - chr(row_bytes_clipped))) + self.send_command( + "\x12*{0}{1}".format(chr(chunk_height), chr(row_bytes_clipped)) + ) for _ in range(chunk_height): for _ in range(row_bytes_clipped): # Drop down to low level UART access to avoid newline and @@ -364,11 +374,12 @@ def _print_bitmap(self, width, height, data): def test_page(self): """Print a test page.""" - self.send_command('\x12T') # DC2 + 'T' for test page + self.send_command("\x12T") # DC2 + 'T' for test page # Delay for 26 lines w/text (ea. 24 dots high) + # 26 text lines (feed 6 dots) + blank line - self._set_timeout(self._dot_print_s * 24 * 26 + \ - self._dot_feed_s * (6 * 26 + 30)) + self._set_timeout( + self._dot_print_s * 24 * 26 + self._dot_feed_s * (6 * 26 + 30) + ) def set_defaults(self): """Set default printing and text options. This is useful to reset back @@ -392,17 +403,22 @@ def set_defaults(self): def _set_justify(self, val): assert 0 <= val <= 2 if val == JUSTIFY_LEFT: - self.send_command('\x1Ba\x00') # ESC + 'a' + 0 + self.send_command("\x1Ba\x00") # ESC + 'a' + 0 elif val == JUSTIFY_CENTER: - self.send_command('\x1Ba\x01') # ESC + 'a' + 1 + self.send_command("\x1Ba\x01") # ESC + 'a' + 1 elif val == JUSTIFY_RIGHT: - self.send_command('\x1Ba\x02') # ESC + 'a' + 2 + self.send_command("\x1Ba\x02") # ESC + 'a' + 2 # pylint: disable=line-too-long # Write-only property, can't assume we can read state from the printer # since there is no command for it and hooking up RX is discouraged # (5V will damage many boards). - justify = property(None, _set_justify, None, "Set the justification of text, must be a value of JUSTIFY_LEFT, JUSTIFY_CENTER, or JUSTIFY_RIGHT.") + justify = property( + None, + _set_justify, + None, + "Set the justification of text, must be a value of JUSTIFY_LEFT, JUSTIFY_CENTER, or JUSTIFY_RIGHT.", + ) # pylint: enable=line-too-long def _set_size(self, val): @@ -410,54 +426,69 @@ def _set_size(self, val): if val == SIZE_SMALL: self._char_height = 24 self._max_column = 32 - self.send_command('\x1D!\x00') # ASCII GS + '!' + 0x00 + self.send_command("\x1D!\x00") # ASCII GS + '!' + 0x00 elif val == SIZE_MEDIUM: self._char_height = 48 self._max_column = 32 - self.send_command('\x1D!\x01') # ASCII GS + '!' + 0x01 + self.send_command("\x1D!\x01") # ASCII GS + '!' + 0x01 elif val == SIZE_LARGE: self._char_height = 48 self._max_column = 16 - self.send_command('\x1D!\x11') # ASCII GS + '!' + 0x11 + self.send_command("\x1D!\x11") # ASCII GS + '!' + 0x11 self._column = 0 # pylint: disable=line-too-long # Write-only property, can't assume we can read state from the printer # since there is no command for it and hooking up RX is discouraged # (5V will damage many boards). - size = property(None, _set_size, None, "Set the size of text, must be a value of SIZE_SMALL, SIZE_MEDIUM, or SIZE_LARGE.") + size = property( + None, + _set_size, + None, + "Set the size of text, must be a value of SIZE_SMALL, SIZE_MEDIUM, or SIZE_LARGE.", + ) # pylint: enable=line-too-long def _set_underline(self, val): assert val is None or (0 <= val <= 1) if val is None: # Turn off underline. - self.send_command('\x1B-\x00') # ESC + '-' + 0 + self.send_command("\x1B-\x00") # ESC + '-' + 0 elif val == UNDERLINE_THIN: - self.send_command('\x1B-\x01') # ESC + '-' + 1 + self.send_command("\x1B-\x01") # ESC + '-' + 1 elif val == UNDERLINE_THICK: - self.send_command('\x1B-\x02') # ESC + '-' + 2 + self.send_command("\x1B-\x02") # ESC + '-' + 2 # pylint: disable=line-too-long # Write-only property, can't assume we can read state from the printer # since there is no command for it and hooking up RX is discouraged # (5V will damage many boards). - underline = property(None, _set_underline, None, "Set the underline state of the text, must be None (off), UNDERLINE_THIN, or UNDERLINE_THICK.") + underline = property( + None, + _set_underline, + None, + "Set the underline state of the text, must be None (off), UNDERLINE_THIN, or UNDERLINE_THICK.", + ) # pylint: enable=line-too-long def _set_inverse(self, inverse): # Set the inverse printing state to enabled disabled with the specified # boolean value. This requires printer firmare 2.68+ if inverse: - self.send_command('\x1DB\x01') # ESC + 'B' + 1 + self.send_command("\x1DB\x01") # ESC + 'B' + 1 else: - self.send_command('\x1DB\x00') # ESC + 'B' + 0 + self.send_command("\x1DB\x00") # ESC + 'B' + 0 # pylint: disable=line-too-long # Write-only property, can't assume we can read inverse state from the # printer since there is no command for it and hooking up RX is discouraged # (5V will damage many boards). - inverse = property(None, _set_inverse, None, "Set the inverse printing mode boolean to enable or disable inverse printing.") + inverse = property( + None, + _set_inverse, + None, + "Set the inverse printing mode boolean to enable or disable inverse printing.", + ) # pylint: enable=line-too-long upside_down = _PrintModeBit(_UPDOWN_MASK) @@ -473,31 +504,31 @@ def _set_inverse(self, inverse): def feed(self, lines): """Advance paper by specified number of blank lines.""" assert 0 <= lines <= 255 - self.send_command('\x1Bd{0}'.format(chr(lines))) + self.send_command("\x1Bd{0}".format(chr(lines))) self._set_timeout(self._dot_feed_s * self._char_height) self._column = 0 def feed_rows(self, rows): """Advance paper by specified number of pixel rows.""" assert 0 <= rows <= 255 - self.send_command('\x1BJ{0}'.format(chr(rows))) + self.send_command("\x1BJ{0}".format(chr(rows))) self._set_timeout(rows * self._dot_feed_s) self._column = 0 def flush(self): """Flush data pending in the printer.""" - self.send_command('\f') + self.send_command("\f") def offline(self): """Put the printer into an offline state. No other commands can be sent until an online call is made. """ - self.send_command('\x1B=\x00') # ESC + '=' + 0 + self.send_command("\x1B=\x00") # ESC + '=' + 0 def online(self): """Put the printer into an online state after previously put offline. """ - self.send_command('\x1B=\x01') # ESC + '=' + 1 + self.send_command("\x1B=\x01") # ESC + '=' + 1 def has_paper(self): """Return a boolean indicating if the printer has paper. You MUST have @@ -506,7 +537,7 @@ def has_paper(self): the RX line! """ # This only works with firmware 2.64+: - self.send_command('\x1Bv\x00') # ESC + 'v' + 0 + self.send_command("\x1Bv\x00") # ESC + 'v' + 0 status = self._uart.read(1) if status is None: return False @@ -519,13 +550,13 @@ def _set_line_height(self, height): """ assert 24 <= height <= 255 self._line_spacing = height - 24 - self.send_command('\x1B3{0}'.format(chr(height))) # ESC + '3' + height + self.send_command("\x1B3{0}".format(chr(height))) # ESC + '3' + height def _set_barcode_height(self, height): """Set the barcode height in pixels. Must be a value 1 - 255.""" assert 1 <= height <= 255 self._barcode_height = height - self.send_command('\x1Dh{0}'.format(chr(height))) # ASCII GS + 'h' + height + self.send_command("\x1Dh{0}".format(chr(height))) # ASCII GS + 'h' + height def _set_charset(self, charset=0): """Alters the character set for ASCII characters 0x23-0x7E. See @@ -533,7 +564,7 @@ def _set_charset(self, charset=0): supported on more recent firmware printers! """ assert 0 <= charset <= 15 - self.send_command('\x1BR{0}'.format(chr(charset))) # ESC + 'R' + charset + self.send_command("\x1BR{0}".format(chr(charset))) # ESC + 'R' + charset def _set_code_page(self, code_page=0): """Select alternate code page for upper ASCII symbols 0x80-0xFF. See @@ -541,12 +572,12 @@ def _set_code_page(self, code_page=0): on more recent firmware printers! """ assert 0 <= code_page <= 47 - self.send_command('\x1Bt{0}'.format(chr(code_page))) # ESC + 't' + code page + self.send_command("\x1Bt{0}".format(chr(code_page))) # ESC + 't' + code page def tab(self): """Print a tab (i.e. move to next 4 character block). Note this is only supported on more recent firmware printers!""" - self.send_command('\t') + self.send_command("\t") # Increment to the next position that's every 4 spaces. # I.e. increment by 4 and go to the floor/first position of the block. self._column = (self._column + 4) & 0b11111100 diff --git a/adafruit_thermal_printer/thermal_printer_264.py b/adafruit_thermal_printer/thermal_printer_264.py index ed58920..e0dd38a 100644 --- a/adafruit_thermal_printer/thermal_printer_264.py +++ b/adafruit_thermal_printer/thermal_printer_264.py @@ -45,7 +45,7 @@ # pylint: disable=bad-whitespace # Internally used constants. -_INVERSE_MASK = const(1 << 1) # Not in 2.6.8 firmware +_INVERSE_MASK = const(1 << 1) # Not in 2.6.8 firmware # pylint: enable=bad-whitespace @@ -62,20 +62,20 @@ class ThermalPrinter(thermal_printer.ThermalPrinter): # as class-level variables that users can reference (i.e. # ThermalPrinter.UPC_A, etc) and write code that is independent of the # printer firmware version. - UPC_A = 65 - UPC_E = 66 - EAN13 = 67 - EAN8 = 68 - CODE39 = 69 - ITF = 70 + UPC_A = 65 + UPC_E = 66 + EAN13 = 67 + EAN8 = 68 + CODE39 = 69 + ITF = 70 CODABAR = 71 - CODE93 = 72 + CODE93 = 72 CODE128 = 73 # pylint: enable=bad-whitespace - - def __init__(self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021, - dot_print_s=0.03): + def __init__( + self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021, dot_print_s=0.03 + ): """Thermal printer class. Requires a serial UART connection with at least the TX pin connected. Take care connecting RX as the printer will output a 5V signal which can damage boards! If RX is unconnected @@ -86,8 +86,12 @@ def __init__(self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021, understand the workings of the printer and how delays, baud rate, number of dots, heat time, etc. relate to each other. """ - super().__init__(uart, byte_delay_s=byte_delay_s, dot_feed_s=dot_feed_s, - dot_print_s=dot_print_s) + super().__init__( + uart, + byte_delay_s=byte_delay_s, + dot_feed_s=dot_feed_s, + dot_print_s=dot_print_s, + ) # Inverse on older printers (pre 2.68) uses a print mode bit instead of # specific commands. diff --git a/adafruit_thermal_printer/thermal_printer_legacy.py b/adafruit_thermal_printer/thermal_printer_legacy.py index 94fed16..85ad05b 100644 --- a/adafruit_thermal_printer/thermal_printer_legacy.py +++ b/adafruit_thermal_printer/thermal_printer_legacy.py @@ -45,7 +45,7 @@ # pylint: disable=bad-whitespace # Internally used constants. -_INVERSE_MASK = const(1 << 1) # Not in 2.6.8 firmware +_INVERSE_MASK = const(1 << 1) # Not in 2.6.8 firmware # pylint: enable=bad-whitespace @@ -60,21 +60,22 @@ class ThermalPrinter(thermal_printer.ThermalPrinter): # as class-level variables that users can reference (i.e. # ThermalPrinter.UPC_A, etc) and write code that is independent of the # printer firmware version. - UPC_A = 0 - UPC_E = 1 - EAN13 = 2 - EAN8 = 3 - CODE39 = 4 - I25 = 5 - CODEBAR = 6 - CODE93 = 7 - CODE128 = 8 - CODE11 = 9 - MSI = 10 + UPC_A = 0 + UPC_E = 1 + EAN13 = 2 + EAN8 = 3 + CODE39 = 4 + I25 = 5 + CODEBAR = 6 + CODE93 = 7 + CODE128 = 8 + CODE11 = 9 + MSI = 10 # pylint: enable=bad-whitespace - def __init__(self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021, - dot_print_s=0.03): + def __init__( + self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021, dot_print_s=0.03 + ): """Thermal printer class. Requires a serial UART connection with at least the TX pin connected. Take care connecting RX as the printer will output a 5V signal which can damage boards! If RX is unconnected @@ -85,9 +86,12 @@ def __init__(self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021, understand the workings of the printer and how delays, baud rate, number of dots, heat time, etc. relate to each other. """ - super().__init__(uart, byte_delay_s=byte_delay_s, dot_feed_s=dot_feed_s, - dot_print_s=dot_print_s) - + super().__init__( + uart, + byte_delay_s=byte_delay_s, + dot_feed_s=dot_feed_s, + dot_print_s=dot_print_s, + ) def print_barcode(self, text, barcode_type): """Print a barcode with the specified text/number (the meaning @@ -99,20 +103,20 @@ def print_barcode(self, text, barcode_type): assert 0 <= barcode_type <= 255 assert 0 <= len(text) <= 255 self.feed(1) # Recent firmware can't print barcode w/o feed first??? - self.send_command('\x1DH\x02') # Print label below barcode - self.send_command('\x1Dw\x03') # Barcode width 3 (0.375/1.0mm thin/thick) - self.send_command('\x1Dk{0}'.format(chr(barcode_type))) # Barcode type + self.send_command("\x1DH\x02") # Print label below barcode + self.send_command("\x1Dw\x03") # Barcode width 3 (0.375/1.0mm thin/thick) + self.send_command("\x1Dk{0}".format(chr(barcode_type))) # Barcode type # Pre-2.64 firmware prints the text and then a null character to end. # Instead of the length of text as a prefix. self.send_command(text) - self.send_command('\x00') + self.send_command("\x00") self._set_timeout((self._barcode_height + 40) * self._dot_print_s) self._column = 0 def reset(self): """Reset the printer.""" # Issue a reset command to the printer. (ESC + @) - self.send_command('\x1B@') + self.send_command("\x1B@") # Reset internal state: self._column = 0 self._max_column = 32 @@ -125,7 +129,7 @@ def feed(self, lines): """Advance paper by specified number of blank lines.""" # Just send line feeds for older printers. for _ in range(lines): - self._write_char('\n') + self._write_char("\n") def has_paper(self): """Return a boolean indicating if the printer has paper. You MUST have @@ -138,7 +142,7 @@ def has_paper(self): """ # The paper check command is different for older firmware: - self.send_command('\x1Br\x00') # ESC + 'r' + 0 + self.send_command("\x1Br\x00") # ESC + 'r' + 0 status = self._uart.read(1) if status is None: return False diff --git a/docs/conf.py b/docs/conf.py index 47d61e5..e7b51f0 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -2,7 +2,8 @@ import os import sys -sys.path.insert(0, os.path.abspath('..')) + +sys.path.insert(0, os.path.abspath("..")) # -- General configuration ------------------------------------------------ @@ -10,9 +11,9 @@ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.viewcode', + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx", + "sphinx.ext.viewcode", ] # Uncomment the below if you use native CircuitPython modules such as @@ -20,29 +21,32 @@ # autodoc module docs will fail to generate with a warning. # autodoc_mock_imports = ["micropython"]#, "adafruit_thermal_printer"] -intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} +intersphinx_mapping = { + "python": ("https://docs.python.org/3.4", None), + "CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None), +} # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] -source_suffix = '.rst' +source_suffix = ".rst" # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = u'adafruit_thermal_printer Library' -copyright = u'2017 Tony DiCola' -author = u'Tony DiCola' +project = u"adafruit_thermal_printer Library" +copyright = u"2017 Tony DiCola" +author = u"Tony DiCola" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = u'1.0' +version = u"1.0" # The full version, including alpha/beta/rc tags. -release = u'1.0' +release = u"1.0" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -54,7 +58,7 @@ # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '.env', 'CODE_OF_CONDUCT.md'] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".env", "CODE_OF_CONDUCT.md"] # The reST default role (used for this markup: `text`) to use for all # documents. @@ -66,7 +70,7 @@ add_function_parentheses = True # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False @@ -80,59 +84,62 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' +on_rtd = os.environ.get("READTHEDOCS", None) == "True" if not on_rtd: # only import and set the theme if we're building docs locally try: import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), '.'] + + html_theme = "sphinx_rtd_theme" + html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."] except: - html_theme = 'default' - html_theme_path = ['.'] + html_theme = "default" + html_theme_path = ["."] else: - html_theme_path = ['.'] + html_theme_path = ["."] # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] # The name of an image file (relative to this directory) to use as a favicon of # the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. # -html_favicon = '_static/favicon.ico' +html_favicon = "_static/favicon.ico" # Output file base name for HTML help builder. -htmlhelp_basename = 'adafruit_thermal_printerLibrarydoc' +htmlhelp_basename = "adafruit_thermal_printerLibrarydoc" # -- Options for LaTeX output --------------------------------------------- latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - # - # 'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - # - # 'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - # - # 'preamble': '', - - # Latex figure (float) alignment - # - # 'figure_align': 'htbp', + # The paper size ('letterpaper' or 'a4paper'). + # + # 'papersize': 'letterpaper', + # The font size ('10pt', '11pt' or '12pt'). + # + # 'pointsize': '10pt', + # Additional stuff for the LaTeX preamble. + # + # 'preamble': '', + # Latex figure (float) alignment + # + # 'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'adafruit_thermal_printerLibrary.tex', u'adafruit_thermal_printer Library Documentation', - author, 'manual'), + ( + master_doc, + "adafruit_thermal_printerLibrary.tex", + u"adafruit_thermal_printer Library Documentation", + author, + "manual", + ), ] # -- Options for manual page output --------------------------------------- @@ -140,8 +147,13 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - (master_doc, 'adafruit_thermal_printerlibrary', u'adafruit_thermal_printer Library Documentation', - [author], 1) + ( + master_doc, + "adafruit_thermal_printerlibrary", + u"adafruit_thermal_printer Library Documentation", + [author], + 1, + ) ] # -- Options for Texinfo output ------------------------------------------- @@ -150,7 +162,13 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'adafruit_thermal_printerLibrary', u' adafruit_thermal_printer Library Documentation', - author, 'adafruit_thermal_printerLibrary', 'One line description of project.', - 'Miscellaneous'), + ( + master_doc, + "adafruit_thermal_printerLibrary", + u" adafruit_thermal_printer Library Documentation", + author, + "adafruit_thermal_printerLibrary", + "One line description of project.", + "Miscellaneous", + ), ] diff --git a/examples/thermal_printer_simpletest.py b/examples/thermal_printer_simpletest.py index 76e1485..062262f 100644 --- a/examples/thermal_printer_simpletest.py +++ b/examples/thermal_printer_simpletest.py @@ -39,9 +39,9 @@ # Check if the printer has paper. This only works if the RX line is connected # on your board (but BE CAREFUL as mentioned above this RX line is 5V!) if printer.has_paper(): - print('Printer has paper!') + print("Printer has paper!") else: - print('Printer might be out of paper, or RX is disconnected!') + print("Printer might be out of paper, or RX is disconnected!") # Print a test page: printer.test_page() @@ -50,74 +50,74 @@ printer.feed(2) # Print a line of text: -printer.print('Hello world!') +printer.print("Hello world!") # Print a bold line of text: printer.bold = True -printer.print('Bold hello world!') +printer.print("Bold hello world!") printer.bold = False # Print a normal/thin underline line of text: printer.underline = adafruit_thermal_printer.UNDERLINE_THIN -printer.print('Thin underline!') +printer.print("Thin underline!") # Print a thick underline line of text: printer.underline = adafruit_thermal_printer.UNDERLINE_THICK -printer.print('Thick underline!') +printer.print("Thick underline!") # Disable underlines. printer.underline = None # Print an inverted line. printer.inverse = True -printer.print('Inverse hello world!') +printer.print("Inverse hello world!") printer.inverse = False # Print an upside down line. printer.upside_down = True -printer.print('Upside down hello!') +printer.print("Upside down hello!") printer.upside_down = False # Print a double height line. printer.double_height = True -printer.print('Double height!') +printer.print("Double height!") printer.double_height = False # Print a double width line. printer.double_width = True -printer.print('Double width!') +printer.print("Double width!") printer.double_width = False # Print a strike-through line. printer.strike = True -printer.print('Strike-through hello!') +printer.print("Strike-through hello!") printer.strike = False # Print medium size text. printer.size = adafruit_thermal_printer.SIZE_MEDIUM -printer.print('Medium size text!') +printer.print("Medium size text!") # Print large size text. printer.size = adafruit_thermal_printer.SIZE_LARGE -printer.print('Large size text!') +printer.print("Large size text!") # Back to normal / small size text. printer.size = adafruit_thermal_printer.SIZE_SMALL # Print center justified text. printer.justify = adafruit_thermal_printer.JUSTIFY_CENTER -printer.print('Center justified!') +printer.print("Center justified!") # Print right justified text. printer.justify = adafruit_thermal_printer.JUSTIFY_RIGHT -printer.print('Right justified!') +printer.print("Right justified!") # Back to left justified / normal text. printer.justify = adafruit_thermal_printer.JUSTIFY_LEFT # Print a UPC barcode. -printer.print('UPCA barcode:') -printer.print_barcode('123456789012', printer.UPC_A) +printer.print("UPCA barcode:") +printer.print_barcode("123456789012", printer.UPC_A) # Feed a few lines to see everything. printer.feed(2) diff --git a/setup.py b/setup.py index 5aa6d60..977ecb1 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,7 @@ # Always prefer setuptools over distutils from setuptools import setup, find_packages + # To use a consistent encoding from codecs import open from os import path @@ -14,47 +15,38 @@ here = path.abspath(path.dirname(__file__)) # Get the long description from the README file -with open(path.join(here, 'README.rst'), encoding='utf-8') as f: +with open(path.join(here, "README.rst"), encoding="utf-8") as f: long_description = f.read() setup( - name='adafruit-circuitpython-thermal-printer', - + name="adafruit-circuitpython-thermal-printer", use_scm_version=True, - setup_requires=['setuptools_scm'], - - description='CircuitPython library for controlling thermal printers.', + setup_requires=["setuptools_scm"], + description="CircuitPython library for controlling thermal printers.", long_description=long_description, - long_description_content_type='text/x-rst', - + long_description_content_type="text/x-rst", # The project's main homepage. - url='https://github.com/adafruit/Adafruit_CircuitPython_Thermal_Printer', - + url="https://github.com/adafruit/Adafruit_CircuitPython_Thermal_Printer", # Author details - author='Adafruit Industries', - author_email='circuitpython@adafruit.com', - - install_requires=['Adafruit-Blinka', 'pyserial'], - + author="Adafruit Industries", + author_email="circuitpython@adafruit.com", + install_requires=["Adafruit-Blinka", "pyserial"], # Choose your license - license='MIT', - + license="MIT", # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[ - 'Development Status :: 3 - Alpha', - 'Intended Audience :: Developers', - 'Topic :: Software Development :: Libraries', - 'Topic :: System :: Hardware', - 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "Topic :: Software Development :: Libraries", + "Topic :: System :: Hardware", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", ], - # What does your project relate to? - keywords='adafruit thermal printer hardware micropython circuitpython', - + keywords="adafruit thermal printer hardware micropython circuitpython", # You can just specify the packages manually here if your project is # simple. Or you can use find_packages(). - packages=['adafruit_thermal_printer'], + packages=["adafruit_thermal_printer"], )