Make the Escpos class accept a profile.

This is now used for the block_text function.
This commit is contained in:
Michael Elsdörfer 2016-08-25 17:30:20 +02:00
parent a8574ad9d7
commit 8b5bc9cf8a
2 changed files with 15 additions and 5 deletions

View File

@ -23,6 +23,7 @@ from .exceptions import *
from abc import ABCMeta, abstractmethod # abstract base class support from abc import ABCMeta, abstractmethod # abstract base class support
from escpos.image import EscposImage from escpos.image import EscposImage
from escpos.capabilities import get_profile
@six.add_metaclass(ABCMeta) @six.add_metaclass(ABCMeta)
@ -35,11 +36,11 @@ class Escpos(object):
device = None device = None
codepage = None codepage = None
def __init__(self, columns=32): def __init__(self, profile=None):
""" Initialize ESCPOS Printer """ Initialize ESCPOS Printer
:param columns: Text columns used by the printer. Defaults to 32.""" :param profile: Printer profile"""
self.columns = columns self.profile = get_profile(profile)
def __del__(self): def __del__(self):
""" call self.close upon deletion """ """ call self.close upon deletion """
@ -439,7 +440,7 @@ class Escpos(object):
# TODO: why is it problematic to print an empty string? # TODO: why is it problematic to print an empty string?
raise TextError() raise TextError()
def block_text(self, txt, columns=None): def block_text(self, txt, font=None, columns=None):
""" Text is printed wrapped to specified columns """ Text is printed wrapped to specified columns
Text has to be encoded in unicode. Text has to be encoded in unicode.
@ -448,7 +449,7 @@ class Escpos(object):
:param columns: amount of columns :param columns: amount of columns
:return: None :return: None
""" """
col_count = self.columns if columns is None else columns col_count = self.profile.get_columns(font) if columns is None else columns
self.text(textwrap.fill(txt, col_count)) self.text(textwrap.fill(txt, col_count))
def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9, invert=False, smooth=False, def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9, invert=False, smooth=False,

View File

@ -15,6 +15,7 @@ from __future__ import unicode_literals
from nose.tools import with_setup from nose.tools import with_setup
import escpos.printer as printer import escpos.printer as printer
from escpos.printer import Dummy
import os import os
import filecmp import filecmp
@ -43,3 +44,11 @@ def test_function_text_dies_ist_ein_test_lf():
instance.text('Dies ist ein Test.\n') instance.text('Dies ist ein Test.\n')
instance.flush() instance.flush()
assert(filecmp.cmp('test/Dies ist ein Test.LF.txt', devfile)) assert(filecmp.cmp('test/Dies ist ein Test.LF.txt', devfile))
def test_block_text():
printer = Dummy()
printer.block_text(
"All the presidents men were eating falafel for breakfast.", font='a')
assert printer.output == \
'All the presidents men were eating falafel\nfor breakfast.'