Skip to content

Commit 73afc4a

Browse files
committed
Add some more type hints
1 parent b587602 commit 73afc4a

1 file changed

Lines changed: 36 additions & 47 deletions

File tree

barcode/writer.py

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from barcode.version import version
1212

1313
if TYPE_CHECKING:
14+
from typing import Generator
15+
from typing import Literal
1416

1517
class InternalText(TypedDict):
1618
start: list
@@ -77,20 +79,19 @@ def create_svg_object(with_doctype=False):
7779
class BaseWriter:
7880
"""Baseclass for all writers.
7981
80-
Initializes the basic writer options. Childclasses can add more
81-
attributes and can set them directly or using
82-
`self.set_options(option=value)`.
82+
Initializes the basic writer options. Child classes can add more attributes and can
83+
set them directly or using ``self.set_options(option=value)``.
8384
8485
:param initialize: Callback for initializing the inheriting writer.
85-
Is called: `callback_initialize(raw_code)`
86+
Is called: ``callback_initialize(raw_code)``
8687
:param paint_module:
8788
Callback for painting one barcode module.
88-
Is called: `callback_paint_module(xpos, ypos, width, color)`
89+
Is called: ``callback_paint_module(xpos, ypos, width, color)``
8990
:param paint_text: Callback for painting the text under the barcode.
90-
Is called: `callback_paint_text(xpos, ypos)` using `self.text`
91+
Is called: ``callback_paint_text(xpos, ypos)`` using `self.text`
9192
as text.
9293
:param finish: Callback for doing something with the completely rendered
93-
output. Is called: `return callback_finish()` and must return the
94+
output. Is called: ``return callback_finish()`` and must return the
9495
rendered output.
9596
"""
9697

@@ -125,17 +126,13 @@ def __init__(
125126
self.margin_top = 1
126127
self.margin_bottom = 1
127128

128-
def calculate_size(self, modules_per_line, number_of_lines):
129+
def calculate_size(self, modules_per_line: int, number_of_lines: int) -> tuple:
129130
"""Calculates the size of the barcode in pixel.
130131
131-
:parameters:
132-
modules_per_line : Integer
133-
Number of modules in one line.
134-
number_of_lines : Integer
135-
Number of lines of the barcode.
132+
:param modules_per_line: Number of modules in one line.
133+
:param number_of_lines: Number of lines of the barcode.
136134
137135
:returns: Width and height of the barcode in pixel.
138-
:rtype: Tuple
139136
"""
140137
width = 2 * self.quiet_zone + modules_per_line * self.module_width
141138
height = (
@@ -149,60 +146,49 @@ def calculate_size(self, modules_per_line, number_of_lines):
149146
height += self.text_line_distance * (number_of_text_lines - 1)
150147
return width, height
151148

152-
def save(self, filename, output):
149+
def save(self, filename: str, output) -> str:
153150
"""Saves the rendered output to `filename`.
154151
155-
:parameters:
156-
filename : String
157-
Filename without extension.
158-
output : String
159-
The rendered output.
152+
:param filename: Filename without extension.
153+
:param output: The rendered output.
160154
161155
:returns: The full filename with extension.
162-
:rtype: String
163156
"""
164157
raise NotImplementedError
165158

166-
def register_callback(self, action, callback):
167-
"""Register one of the three callbacks if not given at instance
168-
creation.
159+
def register_callback(
160+
self,
161+
action: Literal["initialize", "paint_module", "paint_text", "finish"],
162+
callback: Callable,
163+
) -> None:
164+
"""Register one of the three callbacks if not given at instance creation.
169165
170-
:parameters:
171-
action : String
172-
One of 'initialize', 'paint_module', 'paint_text', 'finish'.
173-
callback : Function
174-
The callback function for the given action.
166+
:param action: One of 'initialize', 'paint_module', 'paint_text', 'finish'.
167+
:param callback: The callback function for the given action.
175168
"""
176169
self._callbacks[action] = callback
177170

178-
def set_options(self, options):
171+
def set_options(self, options: dict) -> None:
179172
"""Sets the given options as instance attributes (only
180173
if they are known).
181174
182-
:parameters:
183-
options : Dict
184-
All known instance attributes and more if the childclass
185-
has defined them before this call.
186-
187-
:rtype: None
175+
:param options: All known instance attributes and more if the child class
176+
has defined them before this call.
188177
"""
189178
for key, val in options.items():
190179
key = key.lstrip("_")
191180
if hasattr(self, key):
192181
setattr(self, key, val)
193182

194-
def packed(self, line):
183+
def packed(self, line: str) -> Generator[tuple[int, float], str, None]:
195184
"""
196185
Pack line to list give better gfx result, otherwise in can
197186
result in aliasing gaps
198187
'11010111' -> [2, -1, 1, -1, 3]
199188
200189
This method will yield a sequence of pairs (width, height_factor).
201190
202-
:parameters:
203-
line: String
204-
A string matching the writer spec
205-
(only contain 0 or 1 or G).
191+
:param line: A string matching the writer spec (only contain 0 or 1 or G).
206192
"""
207193
line += " "
208194
c = 1
@@ -314,8 +300,11 @@ def write(self, content, fp: BinaryIO) -> None:
314300

315301
class SVGWriter(BaseWriter):
316302
def __init__(self) -> None:
317-
BaseWriter.__init__(
318-
self, self._init, self._create_module, self._create_text, self._finish
303+
super().__init__(
304+
self._init,
305+
self._create_module,
306+
self._create_text,
307+
self._finish,
319308
)
320309
self.compress = False
321310
self.with_doctype = True
@@ -391,12 +380,12 @@ def _finish(self):
391380
indent=4 * " ", newl=os.linesep, encoding="UTF-8"
392381
)
393382

394-
def save(self, filename, output):
383+
def save(self, filename: str, output) -> str:
395384
if self.compress:
396385
_filename = f"{filename}.svgz"
397-
f = gzip.open(_filename, "wb")
398-
f.write(output)
399-
f.close()
386+
with gzip.open(_filename, "wb") as f:
387+
f.write(output)
388+
f.close()
400389
else:
401390
_filename = f"{filename}.svg"
402391
with open(_filename, "wb") as f:

0 commit comments

Comments
 (0)