Skip to content
Open
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: 5 additions & 3 deletions src/rmqrcode/console.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python
import argparse
import sys
from pathlib import Path
from typing import Union

from rmqrcode import (
DataTooLongError,
Expand All @@ -12,12 +14,12 @@
)


def _show_error_and_exit(msg):
def _show_error_and_exit(msg: str):
print(msg, file=sys.stderr)
sys.exit(1)


def _make_qr(data, ecc, version, fit_strategy):
def _make_qr(data: str, ecc: ErrorCorrectionLevel, version: Union[str, None], fit_strategy: FitStrategy):
if version is None:
qr = rMQR.fit(data, ecc=ecc, fit_strategy=fit_strategy)
else:
Expand All @@ -30,7 +32,7 @@ def _make_qr(data, ecc, version, fit_strategy):
return qr


def _save_image(qr, output):
def _save_image(qr: rMQR, output: Union[str, bytes, Path]):
image = QRImage(qr)
try:
image.save(output)
Expand Down
4 changes: 2 additions & 2 deletions src/rmqrcode/encoder/alphanumeric_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def _encoded_bits(cls, data):
return res

@classmethod
def _group_by_2characters(cls, data):
res = []
def _group_by_2characters(cls, data: str):
res: list[str] = []
while data != "":
res.append(data[:2])
data = data[2:]
Expand Down
4 changes: 2 additions & 2 deletions src/rmqrcode/encoder/byte_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def mode_indicator(cls):
return "011"

@classmethod
def _encoded_bits(cls, s):
def _encoded_bits(cls, data):
res = ""
encoded = s.encode("utf-8")
encoded = data.encode("utf-8")
for byte in encoded:
res += bin(byte)[2:].zfill(8)
return res
Expand Down
12 changes: 6 additions & 6 deletions src/rmqrcode/encoder/encoder_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class EncoderBase(ABC):

@classmethod
@abstractmethod
def mode_indicator(cls):
def mode_indicator(cls) -> str:
"""Mode indicator defined in the Table 2.

Returns:
Expand All @@ -17,7 +17,7 @@ def mode_indicator(cls):

@classmethod
@abstractmethod
def encode(cls, data, character_count_indicator_length):
def encode(cls, data: str, character_count_indicator_length: int) -> str:
"""Encodes data and returns it.

Args:
Expand All @@ -42,7 +42,7 @@ def encode(cls, data, character_count_indicator_length):

@classmethod
@abstractmethod
def _encoded_bits(cls, data):
def _encoded_bits(cls, data: str) -> str:
"""Encodes data and returns it.

This method encodes the raw data without the meta data like the mode
Expand All @@ -59,7 +59,7 @@ def _encoded_bits(cls, data):

@classmethod
@abstractmethod
def length(cls, data):
def length(cls, data: str, character_count_indicator_length: int) -> int:
"""Compute the length of the encoded bits.

Args:
Expand All @@ -73,7 +73,7 @@ def length(cls, data):

@classmethod
@abstractmethod
def characters_num(cls, data):
def characters_num(cls, data: str) -> int:
"""Returns the number of the characters of the data.

Args:
Expand All @@ -87,7 +87,7 @@ def characters_num(cls, data):

@classmethod
@abstractmethod
def is_valid_characters(cls, data):
def is_valid_characters(cls, data: str) -> bool:
"""Checks whether the data does not include invalid character.

Args:
Expand Down
4 changes: 2 additions & 2 deletions src/rmqrcode/encoder/numeric_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def _encoded_bits(cls, data):
return res

@classmethod
def _group_by_3characters(cls, data):
res = []
def _group_by_3characters(cls, data: str):
res: list[str] = []
while data != "":
res.append(data[:3])
data = data[3:]
Expand Down
2 changes: 1 addition & 1 deletion src/rmqrcode/format/alignment_pattern_coordinates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AlignmentPatternCoordinates = {
AlignmentPatternCoordinates: dict[int, list[int]] = {
27: [],
43: [21],
59: [19, 39],
Expand Down
2 changes: 1 addition & 1 deletion src/rmqrcode/format/mask.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def mask(x, y):
def mask(x: int, y: int):
return (y // 2 + x // 3) % 2 == 0
23 changes: 22 additions & 1 deletion src/rmqrcode/format/rmqr_versions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
from typing import TypedDict

from ..encoder import AlphanumericEncoder, ByteEncoder, KanjiEncoder, NumericEncoder
from ..encoder.encoder_base import EncoderBase
from .error_correction_level import ErrorCorrectionLevel

rMQRVersions = {

class BlockDef(TypedDict):
num: int
c: int
k: int


class rMQRVersion(TypedDict):
version_indicator: int
height: int
width: int
remainder_bits: int
character_count_indicator_length: dict[type[EncoderBase], int]
codewords_total: int
blocks: dict[ErrorCorrectionLevel, list[BlockDef]]
number_of_data_bits: dict[ErrorCorrectionLevel, int]


rMQRVersions: dict[str, rMQRVersion] = {
"R7x43": {
"version_indicator": 0b00000,
"height": 7,
Expand Down
11 changes: 8 additions & 3 deletions src/rmqrcode/qr_image.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from pathlib import Path
from typing import Union

from PIL import Image, ImageDraw

from .rmqrcode import rMQR


class QRImage:
def __init__(self, qr, module_size=10):
def __init__(self, qr: rMQR, module_size: int = 10):
self._module_size = module_size
qr_list = qr.to_list()
self._img = Image.new("RGB", (len(qr_list[0]) * module_size, len(qr_list) * module_size), (255, 255, 255))
Expand All @@ -20,10 +25,10 @@ def get_ndarray(self):

return np.array(self._img)

def save(self, name):
def save(self, name: Union[str, bytes, Path]):
self._img.save(name)

def _make_image(self, qr_list):
def _make_image(self, qr_list: list[list[int]]):
draw = ImageDraw.Draw(self._img)
for y in range(len(qr_list)):
for x in range(len(qr_list[0])):
Expand Down
Loading