Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/qrcode/image/base.py: 57%
39 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import abc
3class BaseImage:
4 """
5 Base QRCode image output class.
6 """
7 kind = None
8 allowed_kinds = None
9 needs_context = False
10 needs_processing = False
12 def __init__(self, border, width, box_size, *args, **kwargs):
13 self.border = border
14 self.width = width
15 self.box_size = box_size
16 self.pixel_size = (self.width + self.border*2) * self.box_size
17 self._img = self.new_image(**kwargs)
19 @abc.abstractmethod
20 def drawrect(self, row, col):
21 """
22 Draw a single rectangle of the QR code.
23 """
25 def drawrect_context(self, row, col, active, context):
26 """
27 Draw a single rectangle of the QR code given the surrounding context
28 """
29 raise NotImplementedError("BaseImage.drawrect_context")
31 def process(self):
32 """
33 Processes QR code after completion
34 """
35 raise NotImplementedError("BaseImage.drawimage")
37 @abc.abstractmethod
38 def save(self, stream, kind=None):
39 """
40 Save the image file.
41 """
43 def pixel_box(self, row, col):
44 """
45 A helper method for pixel-based image generators that specifies the
46 four pixel coordinates for a single rect.
47 """
48 x = (col + self.border) * self.box_size
49 y = (row + self.border) * self.box_size
50 return [(x, y), (x + self.box_size - 1, y + self.box_size - 1)]
52 @abc.abstractmethod
53 def new_image(self, **kwargs):
54 """
55 Build the image class. Subclasses should return the class created.
56 """
58 def get_image(self, **kwargs):
59 """
60 Return the image class for further processing.
61 """
62 return self._img
64 def check_kind(self, kind, transform=None):
65 """
66 Get the image type.
67 """
68 if kind is None:
69 kind = self.kind
70 allowed = not self.allowed_kinds or kind in self.allowed_kinds
71 if transform:
72 kind = transform(kind)
73 if not allowed:
74 allowed = kind in self.allowed_kinds
75 if not allowed:
76 raise ValueError(
77 f"Cannot set {type(self).__name__} type to {kind}")
78 return kind