3
3
import gzip
4
4
import os
5
5
import xml .dom .minidom
6
+ from abc import ABC
7
+ from abc import abstractmethod
6
8
from typing import TYPE_CHECKING
7
9
from typing import BinaryIO
8
10
from typing import Callable
11
+ from typing import Generic
9
12
from typing import TypedDict
13
+ from typing import TypeVar
10
14
11
15
from barcode .version import version
12
16
@@ -78,8 +82,10 @@ def create_svg_object(with_doctype: bool = False) -> xml.dom.minidom.Document:
78
82
COMMENT = f"Autogenerated with python-barcode { version } "
79
83
PATH = os .path .dirname (os .path .abspath (__file__ ))
80
84
85
+ T_Output = TypeVar ("T_Output" )
81
86
82
- class BaseWriter :
87
+
88
+ class BaseWriter (ABC , Generic [T_Output ]):
83
89
"""Baseclass for all writers.
84
90
85
91
Initializes the basic writer options. Child classes can add more attributes and can
@@ -164,7 +170,8 @@ def calculate_size(self, modules_per_line: int, number_of_lines: int) -> tuple:
164
170
height += self .text_line_distance * (number_of_text_lines - 1 )
165
171
return width , height
166
172
167
- def save (self , filename : str , output ) -> str :
173
+ @abstractmethod
174
+ def save (self , filename : str , output : T_Output ) -> str :
168
175
"""Saves the rendered output to `filename`.
169
176
170
177
:param filename: Filename without extension.
@@ -307,11 +314,12 @@ def render(self, code: list[str]):
307
314
308
315
return self ._callbacks ["finish" ]()
309
316
317
+ @abstractmethod
310
318
def write (self , content , fp : BinaryIO ) -> None :
311
319
raise NotImplementedError
312
320
313
321
314
- class SVGWriter (BaseWriter ):
322
+ class SVGWriter (BaseWriter [ bytes ] ):
315
323
def __init__ (self ) -> None :
316
324
super ().__init__ (
317
325
self ._init ,
@@ -398,7 +406,7 @@ def _finish(self) -> bytes:
398
406
indent = 4 * " " , newl = os .linesep , encoding = "UTF-8"
399
407
)
400
408
401
- def save (self , filename : str , output ) -> str :
409
+ def save (self , filename : str , output : bytes ) -> str :
402
410
if self .compress :
403
411
_filename = f"{ filename } .svgz"
404
412
with gzip .open (_filename , "wb" ) as f :
@@ -419,7 +427,21 @@ def write(self, content, fp: BinaryIO) -> None:
419
427
420
428
421
429
if Image is None :
422
- ImageWriter : type | None = None
430
+ if TYPE_CHECKING :
431
+
432
+ class ImageWriter (BaseWriter ):
433
+ def __init__ (
434
+ self ,
435
+ format : str = "PNG" ,
436
+ mode : str = "RGB" ,
437
+ dpi : int = 300 ,
438
+ ) -> None : ...
439
+
440
+ def save (self , filename : str , output : T_Image ) -> str : ...
441
+
442
+ def write (self , content , fp : BinaryIO ) -> None : ...
443
+ else :
444
+ ImageWriter = None
423
445
else :
424
446
425
447
class ImageWriter (BaseWriter ): # type: ignore[no-redef]
@@ -497,7 +519,7 @@ def _paint_text(self, xpos, ypos):
497
519
def _finish (self ) -> T_Image :
498
520
return self ._image
499
521
500
- def save (self , filename : str , output ) -> str :
522
+ def save (self , filename : str , output : T_Image ) -> str :
501
523
filename = f"{ filename } .{ self .format .lower ()} "
502
524
output .save (filename , self .format .upper ())
503
525
return filename
0 commit comments