Style/health fixes

This commit is contained in:
hugovk 2015-04-01 16:47:01 +03:00
parent 20fd366eb9
commit 2879819ce7
5 changed files with 128 additions and 109 deletions

View File

@ -24,13 +24,11 @@
# See the README file for information on usage and redistribution. # See the README file for information on usage and redistribution.
# #
from PIL import Image, ImageFile, ImagePalette, _binary
__version__ = "0.9" __version__ = "0.9"
from PIL import Image, ImageFile, ImagePalette, _binary
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Helpers # Helpers
@ -271,7 +269,7 @@ def _save(im, fp, filename):
pass # write uncompressed file pass # write uncompressed file
if im.mode in RAWMODE: if im.mode in RAWMODE:
imOut = im im_out = im
else: else:
# convert on the fly (EXPERIMENTAL -- I'm not sure PIL # convert on the fly (EXPERIMENTAL -- I'm not sure PIL
# should automatically convert images on save...) # should automatically convert images on save...)
@ -279,9 +277,9 @@ def _save(im, fp, filename):
palette_size = 256 palette_size = 256
if im.palette: if im.palette:
palette_size = len(im.palette.getdata()[1]) // 3 palette_size = len(im.palette.getdata()[1]) // 3
imOut = im.convert("P", palette=1, colors=palette_size) im_out = im.convert("P", palette=1, colors=palette_size)
else: else:
imOut = im.convert("L") im_out = im.convert("L")
# header # header
try: try:
@ -290,7 +288,7 @@ def _save(im, fp, filename):
palette = None palette = None
im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True)
header, usedPaletteColors = getheader(imOut, palette, im.encoderinfo) header, used_palette_colors = getheader(im_out, palette, im.encoderinfo)
for s in header: for s in header:
fp.write(s) fp.write(s)
@ -315,26 +313,26 @@ def _save(im, fp, filename):
else: else:
transparency = int(transparency) transparency = int(transparency)
# optimize the block away if transparent color is not used # optimize the block away if transparent color is not used
transparentColorExists = True transparent_color_exists = True
# adjust the transparency index after optimize # adjust the transparency index after optimize
if usedPaletteColors is not None and len(usedPaletteColors) < 256: if used_palette_colors is not None and len(used_palette_colors) < 256:
for i in range(len(usedPaletteColors)): for i in range(len(used_palette_colors)):
if usedPaletteColors[i] == transparency: if used_palette_colors[i] == transparency:
transparency = i transparency = i
transparentColorExists = True transparent_color_exists = True
break break
else: else:
transparentColorExists = False transparent_color_exists = False
# transparency extension block # transparency extension block
if transparentColorExists: if transparent_color_exists:
fp.write(b"!" + fp.write(b"!" +
o8(249) + # extension intro o8(249) + # extension intro
o8(4) + # length o8(4) + # length
o8(1) + # transparency info present o8(1) + # transparency info present
o16(0) + # duration o16(0) + # duration
o8(transparency) # transparency index o8(transparency) + # transparency index
+ o8(0)) o8(0))
# local image header # local image header
fp.write(b"," + fp.write(b"," +
@ -344,9 +342,9 @@ def _save(im, fp, filename):
o8(flags) + # flags o8(flags) + # flags
o8(8)) # bits o8(8)) # bits
imOut.encoderconfig = (8, interlace) im_out.encoderconfig = (8, interlace)
ImageFile._save(imOut, fp, [("gif", (0, 0)+im.size, 0, ImageFile._save(im_out, fp, [("gif", (0, 0)+im.size, 0,
RAWMODE[imOut.mode])]) RAWMODE[im_out.mode])])
fp.write(b"\0") # end of image data fp.write(b"\0") # end of image data
@ -422,74 +420,75 @@ def getheader(im, palette=None, info=None):
if im.mode == "P": if im.mode == "P":
if palette and isinstance(palette, bytes): if palette and isinstance(palette, bytes):
sourcePalette = palette[:768] source_palette = palette[:768]
else: else:
sourcePalette = im.im.getpalette("RGB")[:768] source_palette = im.im.getpalette("RGB")[:768]
else: # L-mode else: # L-mode
if palette and isinstance(palette, bytes): if palette and isinstance(palette, bytes):
sourcePalette = palette[:768] source_palette = palette[:768]
else: else:
sourcePalette = bytearray([i//3 for i in range(768)]) source_palette = bytearray([i//3 for i in range(768)])
usedPaletteColors = paletteBytes = None used_palette_colors = palette_bytes = None
if im.mode in ("P", "L") and optimize: if im.mode in ("P", "L") and optimize:
usedPaletteColors = [] used_palette_colors = []
# check which colors are used # check which colors are used
i = 0 i = 0
for count in im.histogram(): for count in im.histogram():
if count: if count:
usedPaletteColors.append(i) used_palette_colors.append(i)
i += 1 i += 1
# create the new palette if not every color is used # create the new palette if not every color is used
if len(usedPaletteColors) < 256: if len(used_palette_colors) < 256:
paletteBytes = b"" palette_bytes = b""
newPositions = {} new_positions = {}
i = 0 i = 0
# pick only the used colors from the palette # pick only the used colors from the palette
for oldPosition in usedPaletteColors: for oldPosition in used_palette_colors:
paletteBytes += sourcePalette[oldPosition*3:oldPosition*3+3] palette_bytes += source_palette[oldPosition*3:oldPosition*3+3]
newPositions[oldPosition] = i new_positions[oldPosition] = i
i += 1 i += 1
# replace the palette color id of all pixel with the new id # replace the palette color id of all pixel with the new id
imageBytes = bytearray(im.tobytes()) image_bytes = bytearray(im.tobytes())
for i in range(len(imageBytes)): for i in range(len(image_bytes)):
imageBytes[i] = newPositions[imageBytes[i]] image_bytes[i] = new_positions[image_bytes[i]]
im.frombytes(bytes(imageBytes)) im.frombytes(bytes(image_bytes))
newPaletteBytes = (paletteBytes + new_palette_bytes = (palette_bytes +
(768 - len(paletteBytes)) * b'\x00') (768 - len(palette_bytes)) * b'\x00')
im.putpalette(newPaletteBytes) im.putpalette(new_palette_bytes)
im.palette = ImagePalette.ImagePalette("RGB", palette=paletteBytes, im.palette = ImagePalette.ImagePalette("RGB",
size=len(paletteBytes)) palette=palette_bytes,
size=len(palette_bytes))
if not paletteBytes: if not palette_bytes:
paletteBytes = sourcePalette palette_bytes = source_palette
# Logical Screen Descriptor # Logical Screen Descriptor
# calculate the palette size for the header # calculate the palette size for the header
import math import math
colorTableSize = int(math.ceil(math.log(len(paletteBytes)//3, 2)))-1 color_table_size = int(math.ceil(math.log(len(palette_bytes)//3, 2)))-1
if colorTableSize < 0: if color_table_size < 0:
colorTableSize = 0 color_table_size = 0
# size of global color table + global color table flag # size of global color table + global color table flag
header.append(o8(colorTableSize + 128)) header.append(o8(color_table_size + 128))
# background + reserved/aspect # background + reserved/aspect
header.append(o8(0) + o8(0)) header.append(o8(0) + o8(0))
# end of Logical Screen Descriptor # end of Logical Screen Descriptor
# add the missing amount of bytes # add the missing amount of bytes
# the palette has to be 2<<n in size # the palette has to be 2<<n in size
actualTargetSizeDiff = (2 << colorTableSize) - len(paletteBytes)//3 actual_target_size_diff = (2 << color_table_size) - len(palette_bytes)//3
if actualTargetSizeDiff > 0: if actual_target_size_diff > 0:
paletteBytes += o8(0) * 3 * actualTargetSizeDiff palette_bytes += o8(0) * 3 * actual_target_size_diff
# Header + Logical Screen Descriptor + Global Color Table # Header + Logical Screen Descriptor + Global Color Table
header.append(paletteBytes) header.append(palette_bytes)
return header, usedPaletteColors return header, used_palette_colors
def getdata(im, offset=(0, 0), **params): def getdata(im, offset=(0, 0), **params):
@ -497,7 +496,7 @@ def getdata(im, offset=(0, 0), **params):
The first string is a local image header, the rest contains The first string is a local image header, the rest contains
encoded image data.""" encoded image data."""
class collector: class Collector:
data = [] data = []
def write(self, data): def write(self, data):
@ -505,7 +504,7 @@ def getdata(im, offset=(0, 0), **params):
im.load() # make sure raster data is available im.load() # make sure raster data is available
fp = collector() fp = Collector()
try: try:
im.encoderinfo = params im.encoderinfo = params

View File

@ -262,7 +262,7 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
return FreeTypeFont(font, size, index, encoding) return FreeTypeFont(font, size, index, encoding)
except IOError: except IOError:
ttf_filename = os.path.basename(font) ttf_filename = os.path.basename(font)
dirs = [] dirs = []
if sys.platform == "win32": if sys.platform == "win32":
# check the windows font repository # check the windows font repository
@ -281,11 +281,11 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
elif sys.platform == 'darwin': elif sys.platform == 'darwin':
dirs += ['/Library/Fonts', '/System/Library/Fonts', dirs += ['/Library/Fonts', '/System/Library/Fonts',
os.path.expanduser('~/Library/Fonts')] os.path.expanduser('~/Library/Fonts')]
ext = os.path.splitext(ttf_filename)[1] ext = os.path.splitext(ttf_filename)[1]
firstFontWithADifferentExtension = None first_font_with_a_different_extension = None
for dir in dirs: for directory in dirs:
for walkroot, walkdir, walkfilenames in os.walk(dir): for walkroot, walkdir, walkfilenames in os.walk(directory):
for walkfilename in walkfilenames: for walkfilename in walkfilenames:
if ext and walkfilename == ttf_filename: if ext and walkfilename == ttf_filename:
fontpath = os.path.join(walkroot, walkfilename) fontpath = os.path.join(walkroot, walkfilename)
@ -294,10 +294,11 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
fontpath = os.path.join(walkroot, walkfilename) fontpath = os.path.join(walkroot, walkfilename)
if os.path.splitext(fontpath)[1] == '.ttf': if os.path.splitext(fontpath)[1] == '.ttf':
return FreeTypeFont(fontpath, size, index, encoding) return FreeTypeFont(fontpath, size, index, encoding)
if not ext and firstFontWithADifferentExtension == None: if not ext and first_font_with_a_different_extension is None:
firstFontWithADifferentExtension = fontpath first_font_with_a_different_extension = fontpath
if firstFontWithADifferentExtension: if first_font_with_a_different_extension:
return FreeTypeFont(firstFontWithADifferentExtension, size, index, encoding) return FreeTypeFont(first_font_with_a_different_extension, size,
index, encoding)
raise raise
@ -310,15 +311,15 @@ def load_path(filename):
:return: A font object. :return: A font object.
:exception IOError: If the file could not be read. :exception IOError: If the file could not be read.
""" """
for dir in sys.path: for directory in sys.path:
if isDirectory(dir): if isDirectory(directory):
if not isinstance(filename, str): if not isinstance(filename, str):
if bytes is str: if bytes is str:
filename = filename.encode("utf-8") filename = filename.encode("utf-8")
else: else:
filename = filename.decode("utf-8") filename = filename.decode("utf-8")
try: try:
return load(os.path.join(dir, filename)) return load(os.path.join(directory, filename))
except IOError: except IOError:
pass pass
raise IOError("cannot find font file") raise IOError("cannot find font file")

View File

@ -12,14 +12,13 @@
# #
# See the README file for information on usage and redistribution. # See the README file for information on usage and redistribution.
# #
__version__ = "0.1"
from PIL import Image, ImageFile from PIL import Image, ImageFile
import struct import struct
import os import os
import io import io
__version__ = "0.1"
def _parse_codestream(fp): def _parse_codestream(fp):
"""Parse the JPEG 2000 codestream to extract the size and component """Parse the JPEG 2000 codestream to extract the size and component
@ -208,8 +207,8 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
def _accept(prefix): def _accept(prefix):
return (prefix[:4] == b'\xff\x4f\xff\x51' return (prefix[:4] == b'\xff\x4f\xff\x51' or
or prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a') prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a')
# ------------------------------------------------------------ # ------------------------------------------------------------

View File

@ -22,6 +22,7 @@ try:
self._saved = None self._saved = None
self._is_saved = False self._is_saved = False
self._value = value self._value = value
def __enter__(self): def __enter__(self):
# Patch the attr on the object # Patch the attr on the object
if hasattr(self._parent_obj, self._attr_name): if hasattr(self._parent_obj, self._attr_name):
@ -31,6 +32,7 @@ try:
else: else:
setattr(self._parent_obj, self._attr_name, self._value) setattr(self._parent_obj, self._attr_name, self._value)
self._is_saved = False self._is_saved = False
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
# Restore the original value # Restore the original value
if self._is_saved: if self._is_saved:
@ -48,14 +50,14 @@ try:
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
self.assertEqual(ttf.path, FONT_PATH) self.assertEqual(ttf.path, FONT_PATH)
self.assertEqual(ttf.size, FONT_SIZE) self.assertEqual(ttf.size, FONT_SIZE)
ttf_copy = ttf.font_variant() ttf_copy = ttf.font_variant()
self.assertEqual(ttf_copy.path, FONT_PATH) self.assertEqual(ttf_copy.path, FONT_PATH)
self.assertEqual(ttf_copy.size, FONT_SIZE) self.assertEqual(ttf_copy.size, FONT_SIZE)
ttf_copy = ttf.font_variant(size=FONT_SIZE+1) ttf_copy = ttf.font_variant(size=FONT_SIZE+1)
self.assertEqual(ttf_copy.size, FONT_SIZE+1) self.assertEqual(ttf_copy.size, FONT_SIZE+1)
second_font_path = "Tests/fonts/DejaVuSans.ttf" second_font_path = "Tests/fonts/DejaVuSans.ttf"
ttf_copy = ttf.font_variant(font=second_font_path) ttf_copy = ttf.font_variant(font=second_font_path)
self.assertEqual(ttf_copy.path, second_font_path) self.assertEqual(ttf_copy.path, second_font_path)
@ -234,61 +236,77 @@ try:
self.assert_image_equal(im, target_img) self.assert_image_equal(im, target_img)
def _test_fake_loading_font(self, path_to_fake, fontname): def _test_fake_loading_font(self, path_to_fake, fontname):
#Make a copy of FreeTypeFont so we can patch the original # Make a copy of FreeTypeFont so we can patch the original
free_type_font = copy.deepcopy(ImageFont.FreeTypeFont) free_type_font = copy.deepcopy(ImageFont.FreeTypeFont)
with SimplePatcher(ImageFont, '_FreeTypeFont', free_type_font): with SimplePatcher(ImageFont, '_FreeTypeFont', free_type_font):
def loadable_font(filepath, size, index, encoding): def loadable_font(filepath, size, index, encoding):
if filepath == path_to_fake: if filepath == path_to_fake:
return ImageFont._FreeTypeFont(FONT_PATH, size, index, encoding) return ImageFont._FreeTypeFont(FONT_PATH, size, index,
return ImageFont._FreeTypeFont(filepath, size, index, encoding) encoding)
return ImageFont._FreeTypeFont(filepath, size, index,
encoding)
with SimplePatcher(ImageFont, 'FreeTypeFont', loadable_font): with SimplePatcher(ImageFont, 'FreeTypeFont', loadable_font):
font = ImageFont.truetype(fontname) font = ImageFont.truetype(fontname)
#Make sure it's loaded # Make sure it's loaded
name = font.getname() name = font.getname()
self.assertEqual(('FreeMono', 'Regular'), name) self.assertEqual(('FreeMono', 'Regular'), name)
@unittest.skipIf(sys.platform.startswith('win32'),
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") "requires Unix or MacOS")
def test_find_linux_font(self): def test_find_linux_font(self):
#A lot of mocking here - this is more for hitting code and catching # A lot of mocking here - this is more for hitting code and
#syntax like errors # catching syntax like errors
fontDirectory = '/usr/local/share/fonts' font_directory = '/usr/local/share/fonts'
with SimplePatcher(sys, 'platform', 'linux'): with SimplePatcher(sys, 'platform', 'linux'):
patched_env = copy.deepcopy(os.environ) patched_env = copy.deepcopy(os.environ)
patched_env['XDG_DATA_DIRS'] = '/usr/share/:/usr/local/share/' patched_env['XDG_DATA_DIRS'] = '/usr/share/:/usr/local/share/'
with SimplePatcher(os, 'environ', patched_env): with SimplePatcher(os, 'environ', patched_env):
def fake_walker(path): def fake_walker(path):
if path == fontDirectory: if path == font_directory:
return [(path, [], ['Arial.ttf', 'Single.otf', 'Duplicate.otf', 'Duplicate.ttf'], )] return [(path, [], [
'Arial.ttf', 'Single.otf', 'Duplicate.otf',
'Duplicate.ttf'], )]
return [(path, [], ['some_random_font.ttf'], )] return [(path, [], ['some_random_font.ttf'], )]
with SimplePatcher(os, 'walk', fake_walker): with SimplePatcher(os, 'walk', fake_walker):
# Test that the font loads both with and without the extension # Test that the font loads both with and without the
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial.ttf') # extension
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial') self._test_fake_loading_font(
font_directory+'/Arial.ttf', 'Arial.ttf')
# Test that non-ttf fonts can be found without the extension self._test_fake_loading_font(
self._test_fake_loading_font(fontDirectory+'/Single.otf', 'Single') font_directory+'/Arial.ttf', 'Arial')
# Test that ttf fonts are preferred if the extension is not specified
self._test_fake_loading_font(fontDirectory+'/Duplicate.ttf', 'Duplicate')
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") # Test that non-ttf fonts can be found without the
# extension
self._test_fake_loading_font(
font_directory+'/Single.otf', 'Single')
# Test that ttf fonts are preferred if the extension is
# not specified
self._test_fake_loading_font(
font_directory+'/Duplicate.ttf', 'Duplicate')
@unittest.skipIf(sys.platform.startswith('win32'),
"requires Unix or MacOS")
def test_find_osx_font(self): def test_find_osx_font(self):
#Like the linux test, more cover hitting code rather than testing # Like the linux test, more cover hitting code rather than testing
#correctness. # correctness.
fontDirectory = '/System/Library/Fonts' font_directory = '/System/Library/Fonts'
with SimplePatcher(sys, 'platform', 'darwin'): with SimplePatcher(sys, 'platform', 'darwin'):
def fake_walker(path): def fake_walker(path):
if path == fontDirectory: if path == font_directory:
return [(path, [], ['Arial.ttf', 'Single.otf', 'Duplicate.otf', 'Duplicate.ttf'], )] return [(path, [],
['Arial.ttf', 'Single.otf',
'Duplicate.otf', 'Duplicate.ttf'], )]
return [(path, [], ['some_random_font.ttf'], )] return [(path, [], ['some_random_font.ttf'], )]
with SimplePatcher(os, 'walk', fake_walker): with SimplePatcher(os, 'walk', fake_walker):
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial.ttf') self._test_fake_loading_font(
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial') font_directory+'/Arial.ttf', 'Arial.ttf')
self._test_fake_loading_font(
self._test_fake_loading_font(fontDirectory+'/Single.otf', 'Single') font_directory+'/Arial.ttf', 'Arial')
self._test_fake_loading_font(
self._test_fake_loading_font(fontDirectory+'/Duplicate.ttf', 'Duplicate') font_directory+'/Single.otf', 'Single')
self._test_fake_loading_font(
font_directory+'/Duplicate.ttf', 'Duplicate')
except ImportError: except ImportError:

View File

@ -14,6 +14,7 @@ class TestPyroma(PillowTestCase):
def setUp(self): def setUp(self):
try: try:
import pyroma import pyroma
assert pyroma # Ignore warning
except ImportError: except ImportError:
self.skipTest("ImportError") self.skipTest("ImportError")
@ -26,9 +27,10 @@ class TestPyroma(PillowTestCase):
# Assert # Assert
if 'rc' in PILLOW_VERSION: if 'rc' in PILLOW_VERSION:
#Pyroma needs to chill about RC versions and not kill all our tests. # Pyroma needs to chill about RC versions
self.assertEqual(rating, (9, # and not kill all our tests.
['The packages version number does not comply with PEP-386.'])) self.assertEqual(rating, (9, [
'The packages version number does not comply with PEP-386.']))
else: else:
# Should have a perfect score # Should have a perfect score