1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-09-13 09:09:58 +00:00
This commit is contained in:
Alexandre Detiste
2023-12-10 02:49:26 +01:00
parent 35565d5564
commit 6aec41fbd8
16 changed files with 116 additions and 97 deletions

View File

@@ -15,7 +15,8 @@ import textwrap
import warnings
from abc import ABCMeta, abstractmethod # abstract base class support
from re import match as re_match
from typing import List, Literal, Optional, Union
from types import TracebackType
from typing import List, Literal, Optional, Union, Any
import barcode
import qrcode
@@ -107,8 +108,7 @@ SW_BARCODE_NAMES = {
}
@six.add_metaclass(ABCMeta)
class Escpos(object):
class Escpos(object, metaclass=ABCMeta):
"""ESC/POS Printer object.
This class is the abstract base class for an Esc/Pos-printer. The printer implementations are children of this
@@ -164,7 +164,7 @@ class Escpos(object):
"""
pass
def _read(self):
def _read(self) -> bytes:
"""Read from printer.
Returns a NotImplementedError if the instance of the class doesn't override this method.
@@ -250,7 +250,7 @@ class Escpos(object):
header = (
GS
+ b"v0"
+ six.int2byte(density_byte)
+ bytes((density_byte,))
+ self._int_low_high(im.width_bytes, 2)
+ self._int_low_high(im.height, 2)
)
@@ -263,8 +263,8 @@ class Escpos(object):
)
tone = b"0"
colors = b"1"
ym = six.int2byte(1 if high_density_vertical else 2)
xm = six.int2byte(1 if high_density_horizontal else 2)
ym = b"1" if high_density_vertical else b"2"
xm = b"1" if high_density_horizontal else b"2"
header = tone + xm + ym + colors + img_header
raster_data = im.to_raster_format()
self._image_send_graphics_data(b"0", b"p", header + raster_data)
@@ -857,7 +857,7 @@ class Escpos(object):
image = my_code.writer._image
self.image(image, impl=impl, center=center)
def text(self, txt):
def text(self, txt:str) -> None:
"""Print alpha-numeric text.
The text has to be encoded in the currently selected codepage.
@@ -868,7 +868,7 @@ class Escpos(object):
"""
self.magic.write(str(txt))
def textln(self, txt=""):
def textln(self, txt: str = "") -> None:
"""Print alpha-numeric text with a newline.
The text has to be encoded in the currently selected codepage.
@@ -879,7 +879,7 @@ class Escpos(object):
"""
self.text("{}\n".format(txt))
def ln(self, count=1):
def ln(self, count: int = 1) -> None:
"""Print a newline or more.
:param count: number of newlines to print
@@ -890,7 +890,7 @@ class Escpos(object):
if count > 0:
self.text("\n" * count)
def block_text(self, txt, font="0", columns=None):
def block_text(self, txt, font="0", columns=None) -> None:
"""Print text wrapped to specific columns.
Text has to be encoded in unicode.
@@ -1297,7 +1297,7 @@ class Escpos(object):
return False
return not (status[0] & RT_MASK_ONLINE)
def paper_status(self):
def paper_status(self) -> int: # could be IntEnum
"""Query the paper status of the printer.
Returns 2 if there is plenty of paper, 1 if the paper has arrived to
@@ -1314,6 +1314,8 @@ class Escpos(object):
return 1
if status[0] & RT_MASK_PAPER == RT_MASK_PAPER:
return 2
# not reached
return 0
def target(self, type: str = "ROLL") -> None:
"""Select where to print to.
@@ -1368,7 +1370,7 @@ class Escpos(object):
self._raw(BUZZER + six.int2byte(times) + six.int2byte(duration))
class EscposIO(object):
class EscposIO:
r"""ESC/POS Printer IO object.
Allows the class to be used together with the `with`-statement. You have to define a printer instance
@@ -1414,7 +1416,7 @@ class EscposIO(object):
"""
self.params.update(kwargs)
def writelines(self, text, **kwargs):
def writelines(self, text: str, **kwargs) -> None:
"""Print text."""
params = dict(self.params)
params.update(kwargs)
@@ -1434,18 +1436,23 @@ class EscposIO(object):
self.printer.set(**params)
self.printer.text("{0}\n".format(line))
def close(self):
def close(self) -> None:
"""Close printer.
Called upon closing the `with`-statement.
"""
self.printer.close()
def __enter__(self, **kwargs):
def __enter__(self, **kwargs: Any) -> 'EscposIO':
"""Enter context."""
return self
def __exit__(self, type, value, traceback):
def __exit__(
self,
type: type[BaseException],
value: BaseException,
traceback: TracebackType
) -> None:
"""Cut and close if configured.
If :py:attr:`autocut <escpos.escpos.EscposIO.autocut>` is `True` (set by this class' constructor),