mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
Merge pull request #1233 from radarhere/features
Separated out feature checking from selftest
This commit is contained in:
commit
06c8d715f2
67
PIL/features.py
Normal file
67
PIL/features.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
from PIL import Image
|
||||
|
||||
modules = {
|
||||
"pil": "PIL._imaging",
|
||||
"tkinter": "PIL._imagingtk",
|
||||
"freetype2": "PIL._imagingft",
|
||||
"littlecms2": "PIL._imagingcms",
|
||||
"webp": "PIL._webp",
|
||||
"transp_webp": ("WEBP", "WebPDecoderBuggyAlpha")
|
||||
}
|
||||
|
||||
|
||||
def check_module(feature):
|
||||
if feature not in modules:
|
||||
raise ValueError("Unknown module %s" % feature)
|
||||
|
||||
module = modules[feature]
|
||||
|
||||
method_to_call = None
|
||||
if type(module) is tuple:
|
||||
module, method_to_call = module
|
||||
|
||||
try:
|
||||
imported_module = __import__(module)
|
||||
except ImportError:
|
||||
# If a method is being checked, None means that
|
||||
# rather than the method failing, the module required for the method
|
||||
# failed to be imported first
|
||||
return None if method_to_call else False
|
||||
|
||||
if method_to_call:
|
||||
method = getattr(imported_module, method_to_call)
|
||||
return method() is True
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
def get_supported_modules():
|
||||
supported_modules = []
|
||||
for feature in modules:
|
||||
if check_module(feature):
|
||||
supported_modules.append(feature)
|
||||
return supported_modules
|
||||
|
||||
codecs = {
|
||||
"jpg": "jpeg",
|
||||
"jpg_2000": "jpeg2k",
|
||||
"zlib": "zip",
|
||||
"libtiff": "libtiff"
|
||||
}
|
||||
|
||||
|
||||
def check_codec(feature):
|
||||
if feature not in codecs:
|
||||
raise ValueError("Unknown codec %s" % feature)
|
||||
|
||||
codec = codecs[feature]
|
||||
|
||||
return codec + "_encoder" in dir(Image.core)
|
||||
|
||||
|
||||
def get_supported_codecs():
|
||||
supported_codecs = []
|
||||
for feature in codecs:
|
||||
if check_codec(feature):
|
||||
supported_codecs.append(feature)
|
||||
return supported_codecs
|
35
Tests/test_features.py
Normal file
35
Tests/test_features.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
from helper import unittest, PillowTestCase
|
||||
|
||||
from PIL import features
|
||||
|
||||
|
||||
class TestFeatures(PillowTestCase):
|
||||
|
||||
def test_check_features(self):
|
||||
for feature in features.modules:
|
||||
self.assertTrue(
|
||||
features.check_module(feature) in [True, False, None])
|
||||
for feature in features.codecs:
|
||||
self.assertTrue(features.check_codec(feature) in [True, False])
|
||||
|
||||
def test_supported_features(self):
|
||||
self.assertTrue(type(features.get_supported_modules()) is list)
|
||||
self.assertTrue(type(features.get_supported_codecs()) is list)
|
||||
|
||||
def test_unsupported_codec(self):
|
||||
# Arrange
|
||||
codec = "unsupported_codec"
|
||||
# Act / Assert
|
||||
self.assertRaises(ValueError, lambda: features.check_codec(codec))
|
||||
|
||||
def test_unsupported_module(self):
|
||||
# Arrange
|
||||
module = "unsupported_module"
|
||||
# Act / Assert
|
||||
self.assertRaises(ValueError, lambda: features.check_module(module))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
# End of file
|
60
selftest.py
60
selftest.py
|
@ -9,6 +9,7 @@ if "--installed" in sys.argv:
|
|||
del sys.path[0]
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFilter, ImageMath
|
||||
from PIL import features
|
||||
|
||||
if "--installed" in sys.argv:
|
||||
sys.path.insert(0, sys_path_0)
|
||||
|
@ -162,22 +163,6 @@ def testimage():
|
|||
"""
|
||||
|
||||
|
||||
def check_module(feature, module):
|
||||
try:
|
||||
__import__(module)
|
||||
except ImportError:
|
||||
print("***", feature, "support not installed")
|
||||
else:
|
||||
print("---", feature, "support ok")
|
||||
|
||||
|
||||
def check_codec(feature, codec):
|
||||
if codec + "_encoder" not in dir(Image.core):
|
||||
print("***", feature, "support not installed")
|
||||
else:
|
||||
print("---", feature, "support ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# check build sanity
|
||||
|
||||
|
@ -189,23 +174,34 @@ if __name__ == "__main__":
|
|||
print("Python modules loaded from", os.path.dirname(Image.__file__))
|
||||
print("Binary modules loaded from", os.path.dirname(Image.core.__file__))
|
||||
print("-"*68)
|
||||
check_module("PIL CORE", "PIL._imaging")
|
||||
check_module("TKINTER", "PIL._imagingtk")
|
||||
check_codec("JPEG", "jpeg")
|
||||
check_codec("JPEG 2000", "jpeg2k")
|
||||
check_codec("ZLIB (PNG/ZIP)", "zip")
|
||||
check_codec("LIBTIFF", "libtiff")
|
||||
check_module("FREETYPE2", "PIL._imagingft")
|
||||
check_module("LITTLECMS2", "PIL._imagingcms")
|
||||
check_module("WEBP", "PIL._webp")
|
||||
try:
|
||||
from PIL import _webp
|
||||
if _webp.WebPDecoderBuggyAlpha():
|
||||
print("***", "Transparent WEBP", "support not installed")
|
||||
for name, feature in [
|
||||
("pil", "PIL CORE"),
|
||||
("tkinter", "TKINTER"),
|
||||
("freetype2", "FREETYPE2"),
|
||||
("littlecms2", "LITTLECMS2"),
|
||||
("webp", "WEBP"),
|
||||
("transp_webp", "Transparent WEBP")
|
||||
]:
|
||||
supported = features.check_module(name)
|
||||
|
||||
if supported is None:
|
||||
# A method was being tested, but the module required
|
||||
# for the method could not be correctly imported
|
||||
pass
|
||||
elif supported:
|
||||
print("---", feature, "support ok")
|
||||
else:
|
||||
print("---", "Transparent WEBP", "support ok")
|
||||
except Exception:
|
||||
pass
|
||||
print("***", feature, "support not installed")
|
||||
for name, feature in [
|
||||
("jpg", "JPEG"),
|
||||
("jpg_2000", "OPENJPEG (JPEG2000)"),
|
||||
("zlib", "ZLIB (PNG/ZIP)"),
|
||||
("libtiff", "LIBTIFF")
|
||||
]:
|
||||
if features.check_codec(name):
|
||||
print("---", feature, "support ok")
|
||||
else:
|
||||
print("***", feature, "support not installed")
|
||||
print("-"*68)
|
||||
|
||||
# use doctest to make sure the test program behaves as documented!
|
||||
|
|
Loading…
Reference in New Issue
Block a user