filer.utils.filer_easy_thumbnails: 50 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/filer/utils/filer_easy_thumbnails.py

Stats: 0 executed, 46 missed, 4 excluded, 39 ignored

  1. #-*- coding: utf-8 -*-
  2. from easy_thumbnails.files import Thumbnailer
  3. import os
  4. import re
  5. from filer import settings as filer_settings
  6. # match the source filename using `__` as the seperator. ``opts_and_ext`` is non
  7. # greedy so it should match the last occurence of `__`.
  8. # in ``ThumbnailerNameMixin.get_thumbnail_name`` we ensure that there is no `__`
  9. # in the opts part.
  10. RE_ORIGINAL_FILENAME = re.compile(r"^(?P<source_filename>.*)__(?P<opts_and_ext>.*?)$")
  11. def thumbnail_to_original_filename(thumbnail_name):
  12. m = RE_ORIGINAL_FILENAME.match(thumbnail_name)
  13. if m:
  14. return m.group(1)
  15. return None
  16. class ThumbnailerNameMixin(object):
  17. thumbnail_basedir = ''
  18. thumbnail_subdir = ''
  19. thumbnail_prefix = ''
  20. def get_thumbnail_name(self, thumbnail_options, transparent=False):
  21. """
  22. A version of ``Thumbnailer.get_thumbnail_name`` that produces a
  23. reproducible thumbnail name that can be converted back to the original
  24. filename.
  25. """
  26. path, source_filename = os.path.split(self.name)
  27. if transparent:
  28. extension = self.thumbnail_transparency_extension
  29. else:
  30. extension = self.thumbnail_extension
  31. extension = extension or 'jpg'
  32. thumbnail_options = thumbnail_options.copy()
  33. size = tuple(thumbnail_options.pop('size'))
  34. quality = thumbnail_options.pop('quality', self.thumbnail_quality)
  35. initial_opts = ['%sx%s' % size, 'q%s' % quality]
  36. opts = thumbnail_options.items()
  37. opts.sort() # Sort the options so the file name is consistent.
  38. opts = ['%s' % (v is not True and '%s-%s' % (k, v) or k)
  39. for k, v in opts if v]
  40. all_opts = '_'.join(initial_opts + opts)
  41. basedir = self.thumbnail_basedir
  42. subdir = self.thumbnail_subdir
  43. #make sure our magic delimiter is not used in all_opts
  44. all_opts = all_opts.replace('__', '_')
  45. filename = u'%s__%s.%s' % (source_filename, all_opts, extension)
  46. return os.path.join(basedir, path, subdir, filename)
  47. class ActionThumbnailerMixin(object):
  48. thumbnail_basedir = ''
  49. thumbnail_subdir = ''
  50. thumbnail_prefix = ''
  51. def get_thumbnail_name(self, thumbnail_options, transparent=False):
  52. """
  53. A version of ``Thumbnailer.get_thumbnail_name`` that returns the original
  54. filename to resize.
  55. """
  56. path, filename = os.path.split(self.name)
  57. basedir = self.thumbnail_basedir
  58. subdir = self.thumbnail_subdir
  59. return os.path.join(basedir, path, subdir, filename)
  60. def thumbnail_exists(self, thumbnail_name):
  61. return False
  62. class FilerThumbnailer(ThumbnailerNameMixin, Thumbnailer):
  63. def __init__(self, *args, **kwargs):
  64. self.thumbnail_basedir = kwargs.pop('thumbnail_basedir', '')
  65. super(FilerThumbnailer, self).__init__(*args, **kwargs)
  66. class FilerActionThumbnailer(ActionThumbnailerMixin, Thumbnailer):
  67. pass