REFACTOR fix minor PEP8 and similar mistakes

This commit is contained in:
Patrick Kanzler 2016-01-08 03:34:14 +01:00
parent 0e907644d9
commit f25521f22f
6 changed files with 47 additions and 30 deletions

View File

@ -1,2 +1,5 @@
import constants, escpos, exceptions, printer
import constants
import escpos
import exceptions
import printer
__all__ = ["constants", "escpos", "exceptions", "printer"]

View File

@ -46,9 +46,11 @@ __license__ = "MIT"
import re
from escpos import printer
epson = printer.Usb(0x0416, 0x5011)
# TODO: Un-hardcode this
def _print_text_file(path):
"""Print the given text file"""
epson.set(align='left')
@ -56,10 +58,12 @@ def _print_text_file(path):
for line in fobj:
epson.text(line)
def _print_image_file(path):
"""Print the given image file."""
epson.fullimage(path, histeq=False, width=384)
def print_files(args):
"""The 'print' subcommand"""
for path in args.paths:
@ -74,6 +78,7 @@ def print_files(args):
KNOWN_BARCODE_TYPES = ['UPC-A', 'UPC-E', 'EAN13', 'ITF']
re_barcode_escape = re.compile(r'^%(?P<type>\S+)\s(?P<data>[0-9X]+)$')
def echo(args): # pylint: disable=unused-argument
"""TTY-like line-by-line keyboard-to-printer echo loop."""
try:
@ -94,6 +99,7 @@ def echo(args): # pylint: disable=unused-argument
from PIL import Image, ImageDraw
def _stall_test(width, height):
"""Generate a pattern to detect print glitches due to vertical stalling."""
img = Image.new('1', (width, height))
@ -101,6 +107,7 @@ def _stall_test(width, height):
img.putpixel(pos, not sum(pos) % 10)
return img
def _test_basic():
"""The original test code from python-escpos's Usage wiki page"""
epson.set(align='left')
@ -120,6 +127,7 @@ def _test_basic():
# Cut paper
epson.cut()
def _test_barcodes():
"""Print test barcodes for all ESCPOS-specified formats."""
for name, data in (
@ -138,6 +146,7 @@ def _test_barcodes():
epson.text('\n%s\n' % name)
epson.barcode(data, name, 64, 2, '', '')
def _test_patterns(width=384, height=255):
"""Print a set of test patterns for raster image output."""
# Test our guess of the paper width
@ -152,6 +161,7 @@ def _test_patterns(width=384, height=255):
epson.image(_stall_test(width, height))
epson.image(_stall_test(width / 2, height))
def test(args):
"""The 'test' subcommand"""
if args.barcodes:
@ -161,6 +171,7 @@ def test(args):
else:
_test_basic()
# }}}
def main():
@ -168,6 +179,7 @@ def main():
# pylint: disable=bad-continuation
import argparse
parser = argparse.ArgumentParser(
description="Command-line interface to python-escpos")
subparsers = parser.add_subparsers(title='subcommands')
@ -194,6 +206,7 @@ def main():
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()

View File

@ -15,10 +15,8 @@ except ImportError:
from PIL import Image
import qrcode
import time
import textwrap
import binascii
import os
import operator
from .constants import *
@ -26,6 +24,7 @@ from .exceptions import *
from abc import ABCMeta, abstractmethod # abstract base class support
class Escpos(object):
""" ESC/POS Printer object
@ -64,9 +63,9 @@ class Escpos(object):
else:
image_border = 32 - (size % 32)
if (image_border % 2) == 0:
return (round(image_border / 2), round(image_border / 2))
return round(image_border / 2), round(image_border / 2)
else:
return (round(image_border / 2), round((image_border / 2) + 1))
return round(image_border / 2), round((image_border / 2) + 1)
def _print_image(self, line, size):
""" Print formatted image
@ -222,11 +221,11 @@ class Escpos(object):
for y in range(height):
for x in range(width):
value = image.getpixel((x, y))
value = (value << 8) | value;
value |= (value << 8)
if value == 0:
temp |= mask
mask = mask >> 1
mask >>= 1
i += 1
if i == 8:
@ -413,7 +412,7 @@ class Escpos(object):
:param columns: amount of columns
:return: None
"""
colCount = self.columns if columns == None else columns
colCount = self.columns if columns is None else columns
self.text(textwrap.fill(txt, colCount))
def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9):

View File

@ -153,6 +153,7 @@ class CharCodeError(Error):
def __str__(self):
return "Valid char code must be set"
class USBNotFoundError(Error):
""" Device wasn't found (probably not plugged in)

View File

@ -13,7 +13,6 @@ import serial
import socket
from .escpos import *
from .constants import *
from .exceptions import *
@ -208,9 +207,9 @@ class File(Escpos):
:param msg: arbitrary code to be printed
"""
if type(msg) is str:
self.device.write(msg.encode());
self.device.write(msg.encode())
else:
self.device.write(msg);
self.device.write(msg)
def __del__(self):
""" Close system file """

View File

@ -5,10 +5,12 @@ import sys
from setuptools import setup
from setuptools.command.test import test as TestCommand
def read(fname):
"""read file from same path as setup.py"""
return open(os.path.join(os.path.dirname(__file__), fname)).read()
class Tox(TestCommand):
"""proxy class that enables tox to be run with setup.py test"""
user_options = [('tox-args=', 'a', "Arguments to pass to tox")]