Skip to content

Commit 0c1a28a

Browse files
authored
Merge pull request #27 from adafruit/pylint-update
Ran black, updated to pylint 2.x
2 parents 746bc5d + 62d9041 commit 0c1a28a

14 files changed

+168
-114
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
source actions-ci/install.sh
4141
- name: Pip install pylint, black, & Sphinx
4242
run: |
43-
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
43+
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
4444
- name: Library version
4545
run: git describe --dirty --always --tags
4646
- name: PyLint

adafruit_imageload/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
* Author(s): Scott Shawcroft
2929
3030
"""
31+
# pylint: disable=import-outside-toplevel
3132

3233
__version__ = "0.0.0-auto.0"
3334
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3435

36+
3537
def load(filename, *, bitmap=None, palette=None):
3638
"""Load pixel values (indices or colors) into a bitmap and colors into a palette.
3739
@@ -46,11 +48,14 @@ def load(filename, *, bitmap=None, palette=None):
4648
file.seek(0)
4749
if header.startswith(b"BM"):
4850
from . import bmp
51+
4952
return bmp.load(file, bitmap=bitmap, palette=palette)
5053
if header.startswith(b"P"):
5154
from . import pnm
55+
5256
return pnm.load(file, header, bitmap=bitmap, palette=palette)
5357
if header.startswith(b"GIF"):
5458
from . import gif
59+
5560
return gif.load(file, bitmap=bitmap, palette=palette)
5661
raise RuntimeError("Unsupported image format")

adafruit_imageload/bmp/__init__.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
* Author(s): Scott Shawcroft
2929
3030
"""
31+
# pylint: disable=import-outside-toplevel
3132

3233
__version__ = "0.0.0-auto.0"
3334
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3435

36+
3537
def load(file, *, bitmap=None, palette=None):
3638
"""Loads a bmp image from the open ``file``.
3739
@@ -42,19 +44,19 @@ def load(file, *, bitmap=None, palette=None):
4244
:param object palette: Type to store the palette. Must have API similar to
4345
`displayio.Palette`. Will be skipped if None"""
4446
file.seek(10)
45-
data_start = int.from_bytes(file.read(4), 'little')
47+
data_start = int.from_bytes(file.read(4), "little")
4648
# f.seek(14)
4749
# bmp_header_length = int.from_bytes(file.read(4), 'little')
4850
# print(bmp_header_length)
49-
file.seek(0x12) # Width of the bitmap in pixels
50-
width = int.from_bytes(file.read(4), 'little')
51-
height = int.from_bytes(file.read(4), 'little')
52-
file.seek(0x1c) # Number of bits per pixel
53-
color_depth = int.from_bytes(file.read(2), 'little')
54-
file.seek(0x1e) # Compression type
55-
compression = int.from_bytes(file.read(2), 'little')
56-
file.seek(0x2e) # Number of colors in the color palette
57-
colors = int.from_bytes(file.read(4), 'little')
51+
file.seek(0x12) # Width of the bitmap in pixels
52+
width = int.from_bytes(file.read(4), "little")
53+
height = int.from_bytes(file.read(4), "little")
54+
file.seek(0x1C) # Number of bits per pixel
55+
color_depth = int.from_bytes(file.read(2), "little")
56+
file.seek(0x1E) # Compression type
57+
compression = int.from_bytes(file.read(2), "little")
58+
file.seek(0x2E) # Number of colors in the color palette
59+
colors = int.from_bytes(file.read(4), "little")
5860

5961
if colors == 0 and color_depth >= 16:
6062
raise NotImplementedError("True color BMP unsupported")
@@ -65,5 +67,15 @@ def load(file, *, bitmap=None, palette=None):
6567
if colors == 0:
6668
colors = 2 ** color_depth
6769
from . import indexed
68-
return indexed.load(file, width, height, data_start, colors, color_depth,
69-
compression, bitmap=bitmap, palette=palette)
70+
71+
return indexed.load(
72+
file,
73+
width,
74+
height,
75+
data_start,
76+
colors,
77+
color_depth,
78+
compression,
79+
bitmap=bitmap,
80+
palette=palette,
81+
)

adafruit_imageload/bmp/indexed.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@
3232
__version__ = "0.0.0-auto.0"
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3434

35-
def load(file, width, height, data_start, colors, color_depth, compression, *,
36-
bitmap=None, palette=None):
35+
36+
def load(
37+
file,
38+
width,
39+
height,
40+
data_start,
41+
colors,
42+
color_depth,
43+
compression,
44+
*,
45+
bitmap=None,
46+
palette=None
47+
):
3748
"""Loads indexed bitmap data into bitmap and palette objects.
3849
3950
:param file file: The open bmp file
@@ -51,26 +62,25 @@ def load(file, width, height, data_start, colors, color_depth, compression, *,
5162
for value in range(colors):
5263
c_bytes = file.read(4)
5364
# Need to swap red & blue bytes (bytes 0 and 2)
54-
palette[value] = bytes(b''.join([c_bytes[2:3],
55-
c_bytes[1:2],
56-
c_bytes[0:1],
57-
c_bytes[3:1]]))
65+
palette[value] = bytes(
66+
b"".join([c_bytes[2:3], c_bytes[1:2], c_bytes[0:1], c_bytes[3:1]])
67+
)
5868

5969
if bitmap:
6070
minimum_color_depth = 1
6171
while colors > 2 ** minimum_color_depth:
6272
minimum_color_depth *= 2
6373

64-
#convert unsigned int to signed int when height is negative
65-
if height > 0x7fffffff:
74+
# convert unsigned int to signed int when height is negative
75+
if height > 0x7FFFFFFF:
6676
height = height - 4294967296
6777
bitmap = bitmap(width, abs(height), colors)
6878
file.seek(data_start)
6979
line_size = width // (8 // color_depth)
7080
if width % (8 // color_depth) != 0:
7181
line_size += 1
7282
if line_size % 4 != 0:
73-
line_size += (4 - line_size % 4)
83+
line_size += 4 - line_size % 4
7484

7585
mask = (1 << minimum_color_depth) - 1
7686
if height > 0:
@@ -91,18 +101,22 @@ def load(file, width, height, data_start, colors, color_depth, compression, *,
91101

92102
for x in range(width):
93103
i = x // pixels_per_byte
94-
pixel = (chunk[i] >> (8 - color_depth*(x % pixels_per_byte + 1))) & mask
104+
pixel = (
105+
chunk[i] >> (8 - color_depth * (x % pixels_per_byte + 1))
106+
) & mask
95107
bitmap[offset + x] = pixel
96108
elif compression in (1, 2):
97109
decode_rle(
98110
bitmap=bitmap,
99111
file=file,
100112
compression=compression,
101113
y_range=(range1, range2, range3),
102-
width=width)
114+
width=width,
115+
)
103116

104117
return bitmap, palette
105118

119+
106120
def decode_rle(bitmap, file, compression, y_range, width):
107121
"""Helper to decode RLE images"""
108122
# pylint: disable=too-many-locals,too-many-nested-blocks,too-many-branches
@@ -218,16 +232,12 @@ def decode_rle(bitmap, file, compression, y_range, width):
218232
# 0xab 3 times, the output pixel values would be: 0x0a
219233
# 0x0b 0x0a (notice how it ends at 0x0a) rather than
220234
# 0x0a 0x0b 0x0a 0x0b 0x0a 0x0b
221-
run_values = [
222-
run_buf[1] >> 4,
223-
run_buf[1] & 0x0F
224-
]
235+
run_values = [run_buf[1] >> 4, run_buf[1] & 0x0F]
225236
for i in range(0, min(run_length_px, width_remaining)):
226237
bitmap[offset + i] = run_values[i % 2]
227238
else:
228239
run_value = run_buf[1]
229240
for i in range(0, min(run_length_px, width_remaining)):
230241
bitmap[offset + i] = run_value
231242

232-
233243
x = x + run_length_px

adafruit_imageload/gif.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def load(file, *, bitmap=None, palette=None):
4747
:param object palette: Type to store the palette. Must have API similar to
4848
`displayio.Palette`. Will be skipped if None"""
4949
header = file.read(6)
50-
if header not in {b'GIF87a', b'GIF89a'}:
50+
if header not in {b"GIF87a", b"GIF89a"}:
5151
raise ValueError("Not a GIF file")
52-
width, height, flags, _, _ = struct.unpack('<HHBBB', file.read(7))
52+
width, height, flags, _, _ = struct.unpack("<HHBBB", file.read(7))
5353
if (flags & 0x80) != 0:
5454
palette_size = 1 << ((flags & 0x07) + 1)
5555
palette_obj = palette(palette_size)
@@ -61,13 +61,13 @@ def load(file, *, bitmap=None, palette=None):
6161
bitmap_obj = bitmap(width, height, (1 << color_bits) - 1)
6262
while True:
6363
block_type = file.read(1)[0]
64-
if block_type == 0x2c: # frame
64+
if block_type == 0x2C: # frame
6565
_read_frame(file, bitmap_obj)
66-
elif block_type == 0x21: # extension
66+
elif block_type == 0x21: # extension
6767
_ = file.read(1)[0]
6868
# 0x01 = label, 0xfe = comment
6969
_ = bytes(_read_blockstream(file))
70-
elif block_type == 0x3b: # terminator
70+
elif block_type == 0x3B: # terminator
7171
break
7272
else:
7373
raise ValueError("Bad block type")
@@ -76,7 +76,7 @@ def load(file, *, bitmap=None, palette=None):
7676

7777
def _read_frame(file, bitmap):
7878
"""Read a signle frame and apply it to the bitmap."""
79-
ddx, ddy, width, _, flags = struct.unpack('<HHHHB', file.read(9))
79+
ddx, ddy, width, _, flags = struct.unpack("<HHHHB", file.read(9))
8080
if (flags & 0x40) != 0:
8181
raise NotImplementedError("Interlacing not supported")
8282
if (flags & 0x80) != 0:
@@ -111,6 +111,7 @@ class EndOfData(Exception):
111111

112112
class LZWDict:
113113
"""A dictionary of LZW codes."""
114+
114115
def __init__(self, code_size):
115116
self.code_size = code_size
116117
self.clear_code = 1 << code_size
@@ -121,27 +122,29 @@ def __init__(self, code_size):
121122

122123
def clear(self):
123124
"""Reset the dictionary to default codes."""
124-
self.last = b''
125+
self.last = b""
125126
self.code_len = self.code_size + 1
126127
self.codes[:] = []
127128

128129
def decode(self, code):
129130
"""Decode a code."""
130131
if code == self.clear_code:
131132
self.clear()
132-
return b''
133-
elif code == self.end_code:
133+
return b""
134+
if code == self.end_code:
134135
raise EndOfData()
135-
elif code < self.clear_code:
136+
if code < self.clear_code:
136137
value = bytes([code])
137138
elif code <= len(self.codes) + self.end_code:
138139
value = self.codes[code - self.end_code - 1]
139140
else:
140141
value = self.last + self.last[0:1]
141142
if self.last:
142143
self.codes.append(self.last + value[0:1])
143-
if (len(self.codes) + self.end_code + 1 >= 1 << self.code_len and
144-
self.code_len < 12):
144+
if (
145+
len(self.codes) + self.end_code + 1 >= 1 << self.code_len
146+
and self.code_len < 12
147+
):
145148
self.code_len += 1
146149
self.last = value
147150
return value
@@ -151,7 +154,7 @@ def lzw_decode(data, code_size):
151154
"""Decode LZW-compressed data."""
152155
dictionary = LZWDict(code_size)
153156
bit = 0
154-
byte = next(data) # pylint: disable=stop-iteration-return
157+
byte = next(data) # pylint: disable=stop-iteration-return
155158
try:
156159
while True:
157160
code = 0
@@ -160,8 +163,8 @@ def lzw_decode(data, code_size):
160163
bit += 1
161164
if bit >= 8:
162165
bit = 0
163-
byte = next(data) # pylint: disable=stop-iteration-return
166+
byte = next(data) # pylint: disable=stop-iteration-return
164167
yield dictionary.decode(code)
165168
except EndOfData:
166169
while True:
167-
next(data) # pylint: disable=stop-iteration-return
170+
next(data) # pylint: disable=stop-iteration-return

adafruit_imageload/pnm/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Author(s): Matt Land, Brooke Storm, Sam McGahan
2929
3030
"""
31+
# pylint: disable=import-outside-toplevel
3132

3233
__version__ = "0.0.0-auto.0"
3334
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"

adafruit_imageload/pnm/pgm/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Author(s): Matt Land, Brooke Storm, Sam McGahan
2929
3030
"""
31+
# pylint: disable=import-outside-toplevel
3132

3233

3334
def load(file, magic_number, header, *, bitmap=None, palette=None):

0 commit comments

Comments
 (0)