Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions doc/examples/complex_label_with_qrcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
# First let's import all the needed Classes

from labelprinterkit.backends.network import TCPBackend
from labelprinterkit.printers import P750W
from labelprinterkit.label import Label, Box, Text, QRCode, Padding
from labelprinterkit.job import Job
from labelprinterkit.constants import Media
from labelprinterkit.job import Job
from labelprinterkit.label import Label, Box, Text, QRCode, Padding
from labelprinterkit.printers.P750W import P750W

# The label will be created for a 12mm band. The 12mm has 70 pixel/points width.
# So let's create a QR code with 70 pixels width.
Expand All @@ -42,7 +42,7 @@
label.image.save('/tmp/label.png')

# Create job with configuration and add label as page
job = Job(Media.W12)
job = Job(Media.TZE_12)
job.add_page(label)

# Use TCP backend to connect to printer
Expand Down
9 changes: 7 additions & 2 deletions labelprinterkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from __future__ import annotations
from pkg_resources import get_distribution

from pkg_resources import get_distribution,DistributionNotFound

__all__ = ["printers", "backends", "label", "job", "page"]
__version__ = get_distribution(__name__).version
try:
__version__ = get_distribution(__name__).version
except DistributionNotFound:
# package is not installed
pass
9 changes: 8 additions & 1 deletion labelprinterkit/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import NewType, Type

from labelprinterkit.commands.BaseCommand import BaseCommand


class BaseBackend(ABC):
...


Command = NewType('Command', BaseCommand)


class UniDirectionalBackend(BaseBackend):
@abstractmethod
def write(self, data: bytes):
def write(self, data: bytes | Type[Command]):
...


Expand Down
8 changes: 6 additions & 2 deletions labelprinterkit/backends/bluetooth.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from typing import Type

try:
import serial
except ImportError:
serial = None
from . import BiDirectionalBackend
from . import BiDirectionalBackend, Command


class BTSerialBackend(BiDirectionalBackend):
Expand All @@ -24,7 +26,9 @@ def __init__(self, dev_path: str):
raise OSError('Device not found')
self._dev = dev

def write(self, data: bytes):
def write(self, data: bytes | Type[Command]):
if isinstance(data, Command):
data = data.to_bytes()
self._dev.write(data)

def read(self, count: int) -> bytes:
Expand Down
14 changes: 10 additions & 4 deletions labelprinterkit/backends/network.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import annotations

import socket
from typing import Type

from ..commands.BaseCommand import BaseCommand

try:
from pysnmp.hlapi import SnmpEngine, getCmd, UdpTransportTarget, Udp6TransportTarget,\
from pysnmp.hlapi import SnmpEngine, getCmd, UdpTransportTarget, Udp6TransportTarget, \
ContextData, ObjectType, ObjectIdentity, CommunityData
except ImportError:
SnmpEngine = None
Expand All @@ -14,7 +18,7 @@
ObjectIdentity = None
CommunityData = None

from . import UniDirectionalBackend
from . import UniDirectionalBackend, Command


class TCPBackend(UniDirectionalBackend):
Expand All @@ -34,7 +38,9 @@ def __init__(self, host, port=9100, timeout=10):
raise ConnectionError(f"Connection to {host} failed.")
self._sock = sock

def write(self, data: bytes) -> None:
def write(self, data: bytes | Type[Command]):
if issubclass(type(data), BaseCommand):
data = data.to_bytes()
sent = self._sock.send(data)
if sent == 0:
raise IOError("Socket connection broken")
Expand Down Expand Up @@ -62,7 +68,7 @@ def get_status(self):
transport,
ContextData(),
ObjectType(ObjectIdentity('.1.3.6.1.4.1.2435.3.3.9.1.6.1.0'))
)
)

error_indication, _, _, variables = next(iterator)
if error_indication:
Expand Down
8 changes: 6 additions & 2 deletions labelprinterkit/backends/usb.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from time import sleep
from typing import Type

import usb.core
import usb.util

from . import BaseBackend
from . import BaseBackend, Command


class PyUSBBackend(BaseBackend):
Expand All @@ -23,7 +25,9 @@ def is_usb_printer(dev) -> bool:
return True
return False

def write(self, data: bytes):
def write(self, data: bytes | Type[Command]):
if isinstance(data, Command):
data = data.to_bytes()
self._dev.write(0x2, data)

def read(self, count: int) -> bytes | None:
Expand Down
22 changes: 22 additions & 0 deletions labelprinterkit/commands/AdvancedModeSettings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import functools
from enum import Enum
from typing import List

from .BaseCommand import BaseCommand


class AdvancedModeSettings(BaseCommand):
class Settings(Enum):
DRAFT_PRINTING = 0b00000001
HALF_CUT = 0b00000100
NO_CHAIN_PRINTING = 0b00001000
SPECIAL_TAPE = 0b00010000
HIGH_RESOLUTION = 0b01000000
NO_BUFFER_CLEARING = 0b10000000

def __init__(self, settings: List[Settings] | None = None):
super().__init__()
self.settings = settings or []

def to_bytes(self) -> bytes:
return b'\x1BiM' + functools.reduce(lambda a, b: a | b.value, self.settings, 0x00).to_bytes()
12 changes: 12 additions & 0 deletions labelprinterkit/commands/BaseCommand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from abc import ABC, abstractmethod


class BaseCommand(ABC):
def __mul__(self, other) -> bytes:
if not isinstance(other, int):
raise TypeError("Can only multiply by an int")
return self.to_bytes() * other

@abstractmethod
def to_bytes(self) -> bytes:
...
6 changes: 6 additions & 0 deletions labelprinterkit/commands/Initialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .BaseCommand import BaseCommand


class Initialize(BaseCommand):
def to_bytes(self) -> bytes:
return b'\x1b@'
6 changes: 6 additions & 0 deletions labelprinterkit/commands/Invalidate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .BaseCommand import BaseCommand


class Invalidate(BaseCommand):
def to_bytes(self) -> bytes:
return b'\x00'
13 changes: 13 additions & 0 deletions labelprinterkit/commands/Print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .BaseCommand import BaseCommand


class Print(BaseCommand):
def __init__(self, feed: bool = False):
super().__init__()
self.feed = feed

def to_bytes(self) -> bytes:
if self.feed:
return b'\x1A'
else:
return b'\x0C'
57 changes: 57 additions & 0 deletions labelprinterkit/commands/PrintInformation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from enum import Enum

from .BaseCommand import BaseCommand
from ..constants import MediaType


class PrintInformation(BaseCommand):
class Page(Enum):
STARTING_PAGE = 0
OTHER_PAGE = 1
LAST_PAGE = 2

def __init__(self,
media_type: MediaType | None,
high_resolution: bool | None,
media_width: int | None,
raster_number: int | None = 0,
page: Page = Page.STARTING_PAGE,
recovery: bool = False,
):
super().__init__()
self.media_type = media_type
self.high_resolution = high_resolution
self.media_width = media_width
self.raster_number = raster_number
self.page = page
self.recovery = recovery

def to_bytes(self) -> bytes:
valid = 0x00
if self.media_type is not None:
valid = valid | 0x02
if self.media_width is not None:
valid = valid | 0x04
if self.recovery:
valid = valid | 0x80

media_type = 0x00
if media_type == MediaType.LAMINATED_TAPE and self.high_resolution:
media_type = 0x09
elif media_type == MediaType.HEATSHRINK_TUBE_21:
media_type = 0x11
elif media_type == MediaType.HEATSHRINK_TUBE_31:
media_type = 0x17
elif media_type == MediaType.FLE_TAPE:
media_type = 0x13

return b'\x1Biz' + \
valid.to_bytes() + \
media_type.to_bytes() + \
self.media_width.to_bytes() if self.media_width is not None else b'\x00' + \
b'\x00' + \
(
self.raster_number if self.raster_number is not None else 0).to_bytes(
3, 'big') + \
self.page.value.to_bytes() + \
b'\x00'
10 changes: 10 additions & 0 deletions labelprinterkit/commands/RasterGraphicsTransfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .BaseCommand import BaseCommand


class RasterGraphicsTransfer(BaseCommand):
def __init__(self, data: bytes):
super().__init__()
self.data = data

def to_bytes(self) -> bytes:
return b'G' + len(self.data).to_bytes(2, 'big') + self.data
16 changes: 16 additions & 0 deletions labelprinterkit/commands/SelectCompressionMode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from enum import Enum

from .BaseCommand import BaseCommand


class SelectCompressionMode(BaseCommand):
class Compression(Enum):
NO_COMPRESSION = 0x00
TIFF = 0x02

def __init__(self, compression: Compression):
super().__init__()
self.compression = compression

def to_bytes(self) -> bytes:
return b'M' + self.compression.value.to_bytes()
10 changes: 10 additions & 0 deletions labelprinterkit/commands/SpecifyCutEachPages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .BaseCommand import BaseCommand


class SpecifyCutEachPages(BaseCommand):
def __init__(self, pages: int):
super().__init__()
self.pages = pages

def to_bytes(self) -> bytes:
return b'\x1Bid' + self.pages.to_bytes()
10 changes: 10 additions & 0 deletions labelprinterkit/commands/SpecifyMarginAmount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .BaseCommand import BaseCommand


class SpecifyMarginAmount(BaseCommand):
def __init__(self, margin: int):
super().__init__()
self.margin = margin

def to_bytes(self) -> bytes:
return b'\x1Bid' + self.margin.to_bytes(2, 'little')
6 changes: 6 additions & 0 deletions labelprinterkit/commands/StatusInformationRequest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .BaseCommand import BaseCommand


class StatusInformationRequest(BaseCommand):
def to_bytes(self) -> bytes:
return b'\x1BiS'
17 changes: 17 additions & 0 deletions labelprinterkit/commands/SwitchDynamicCommandMode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from enum import Enum

from .BaseCommand import BaseCommand


class SwitchDynamicCommandMode(BaseCommand):
class Modes(Enum):
ESC_P = 0x00
RASTER = 0x01
P_TOUCH_TEMPLATE = 0x02

def __init__(self, mode: Modes = Modes.RASTER):
super().__init__()
self.mode = mode

def to_bytes(self) -> bytes:
return b'\x1Bia' + self.mode.value.to_bytes()
18 changes: 18 additions & 0 deletions labelprinterkit/commands/VariousModeSettings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import functools
from enum import Enum
from typing import List

from .BaseCommand import BaseCommand


class VariousModeSettings(BaseCommand):
class Settings(Enum):
AUTO_CUT = 0x40
MIRROR_PRINTING = 0x80

def __init__(self, settings: List[Settings] | None = None):
super().__init__()
self.settings = settings or []

def to_bytes(self) -> bytes:
return b'\x1BiM' + functools.reduce(lambda a, b: a | b, self.settings, 0x00).to_bytes()
6 changes: 6 additions & 0 deletions labelprinterkit/commands/ZeroRasterGraphics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .BaseCommand import BaseCommand


class ZeroRasterGraphics(BaseCommand):
def to_bytes(self) -> bytes:
return b'Z'
Empty file.
Loading