mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-14 10:13:13 +00:00
Use lru_cache and 'with open' to clean up code
This commit is contained in:
parent
8a98b5ab8c
commit
99b99f28d4
1 changed files with 11 additions and 26 deletions
|
|
@ -1,52 +1,37 @@
|
|||
import os
|
||||
|
||||
from django.core.checks import register, Warning
|
||||
from django.utils.lru_cache import lru_cache
|
||||
|
||||
from willow.image import Image
|
||||
|
||||
|
||||
_has_jpeg_support = None
|
||||
_has_png_support = None
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def has_jpeg_support():
|
||||
global _has_jpeg_support
|
||||
|
||||
if _has_jpeg_support is None:
|
||||
wagtail_jpg = os.path.join(os.path.dirname(__file__), 'check_files', 'wagtail.jpg')
|
||||
succeeded = True
|
||||
f = open(wagtail_jpg, 'rb')
|
||||
wagtail_jpg = os.path.join(os.path.dirname(__file__), 'check_files', 'wagtail.jpg')
|
||||
succeeded = True
|
||||
|
||||
with open(wagtail_jpg, 'rb') as f:
|
||||
try:
|
||||
Image.open(f)
|
||||
except (IOError, Image.LoaderError):
|
||||
succeeded = False
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
_has_jpeg_support = succeeded
|
||||
|
||||
return _has_jpeg_support
|
||||
return succeeded
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def has_png_support():
|
||||
global _has_png_support
|
||||
|
||||
if _has_png_support is None:
|
||||
wagtail_png = os.path.join(os.path.dirname(__file__), 'check_files', 'wagtail.png')
|
||||
succeeded = True
|
||||
f = open(wagtail_png, 'rb')
|
||||
wagtail_png = os.path.join(os.path.dirname(__file__), 'check_files', 'wagtail.png')
|
||||
succeeded = True
|
||||
|
||||
with open(wagtail_png, 'rb') as f:
|
||||
try:
|
||||
Image.open(f)
|
||||
except (IOError, Image.LoaderError):
|
||||
succeeded = False
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
_has_png_support = succeeded
|
||||
|
||||
return _has_png_support
|
||||
return succeeded
|
||||
|
||||
|
||||
@register()
|
||||
|
|
|
|||
Loading…
Reference in a new issue