Source code for ciowarehouse.handlers.handler_directory_cover

"""A file *handler* for directories with a cover."""

from os.path import join, basename, exists, getmtime
from shutil import rmtree
from warnings import catch_warnings, simplefilter

from PIL import Image

from ciowarehouse.lib.utils import THUMBNAIL_LARGE, thumbnail_create
from ..lib.utils import IGNORED_PREFIX, IGNORED_SUFFIX
from ..lib.i18n import _
from .handler_directory import HandlerDirectory


IMAGES_DIR = 'Images'


# =============================================================================
[docs]def includeme(configurator): """Function to include a CioWarehouse handler. :type configurator: pyramid.config.Configurator :param configurator: Object used to do configuration declaration within the application. """ HandlerDirectory.register(configurator, HandlerDirectoryCover)
# =============================================================================
[docs]class HandlerDirectoryCover(HandlerDirectory): """Class to manage a Cover directory.""" uid = 'directory_cover' label = _('Cover directory handling') # -------------------------------------------------------------------------
[docs] @classmethod def thumbnails_obsolete(cls, abs_file, thumb_dir): """Check if thumbnails are obsolete. See: :meth:`ciowarehouse.handlers.Handler.thumbnails_obsolete` """ cover = cls._cover(abs_file) has_cover = cover is not None has_thumbnails = exists(thumb_dir) return (has_cover and not has_thumbnails) \ or (not has_cover and has_thumbnails) \ or (has_cover and has_thumbnails and getmtime(cover) > getmtime(thumb_dir))
# -------------------------------------------------------------------------
[docs] def thumbnails( self, warehouse, abs_file, thumb_dir, request=None, registry=None): """Create the small and large thumbnails representing the file. See: :meth:`ciowarehouse.handlers.Handler.thumbnails` """ # pylint: disable = unused-argument if not warehouse.lock(abs_file): return # pragma: nocover # Find cover cover = self._cover(abs_file) if cover is None: if exists(thumb_dir): rmtree(thumb_dir) warehouse.unlock(abs_file) return # Load cover try: with catch_warnings(): simplefilter('ignore') image = Image.open(cover) except (IOError, Image.DecompressionBombError): # pragma: nocover warehouse.unlock(abs_file) return # Wartermark watermark = Image.open( self.abspath_from_home('handler_directory_cover.png')) watermark.thumbnail(( warehouse.thumbnail_sizes[0][0] / 4, warehouse.thumbnail_sizes[0][1] / 4)) # Create thumbnails abs_thumb = join(thumb_dir, '{0}.jpg'.format(THUMBNAIL_LARGE)) thumbnail_create( image, warehouse.thumbnail_sizes, abs_thumb, watermark) warehouse.unlock(abs_file)
# ------------------------------------------------------------------------- @classmethod def _cover(cls, abs_dir): """Look for the cover image. :param str abs_dir: Absolute path to the current directory. :rtype: str """ name = basename(abs_dir).lower() cover = join(abs_dir, IMAGES_DIR, f'{name}.jpg') if exists(cover): return cover cover = join(abs_dir, f'{name}.jpg') if exists(cover): return cover if name[0] != IGNORED_PREFIX or name[-1:] != IGNORED_SUFFIX: return None name = name[1:-1] cover = join(abs_dir, IMAGES_DIR, f'{name}.jpg') if exists(cover): return cover cover = join(abs_dir, f'{name}.jpg') return cover if exists(cover) else None