easy_thumbnails.management: 77 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/easy_thumbnails/management/__init__.py

Stats: 0 executed, 74 missed, 3 excluded, 50 ignored

  1. import os
  2. import re
  3. from easy_thumbnails.conf import settings
  4. re_thumbnail_file = re.compile(r'(?P<source_filename>.+)_(?P<x>\d+)x(?P<y>\d+)'
  5. r'(?:_(?P<options>\w+))?_q(?P<quality>\d+)'
  6. r'(?:.[^.]+)?$')
  7. def all_thumbnails(path, recursive=True, prefix=None, subdir=None):
  8. """
  9. Return a dictionary referencing all files which match the thumbnail format.
  10. Each key is a source image filename, relative to path.
  11. Each value is a list of dictionaries as explained in `thumbnails_for_file`.
  12. """
  13. if prefix is None:
  14. prefix = settings.THUMBNAIL_PREFIX
  15. if subdir is None:
  16. subdir = settings.THUMBNAIL_SUBDIR
  17. thumbnail_files = {}
  18. if not path.endswith('/'):
  19. path = '%s/' % path
  20. len_path = len(path)
  21. if recursive:
  22. all = os.walk(path)
  23. else:
  24. files = []
  25. for file in os.listdir(path):
  26. if os.path.isfile(os.path.join(path, file)):
  27. files.append(file)
  28. all = [(path, [], files)]
  29. for dir_, subdirs, files in all:
  30. rel_dir = dir_[len_path:]
  31. for file in files:
  32. thumb = re_thumbnail_file.match(file)
  33. if not thumb:
  34. continue
  35. d = thumb.groupdict()
  36. source_filename = d.pop('source_filename')
  37. if prefix:
  38. source_path, source_filename = os.path.split(source_filename)
  39. if not source_filename.startswith(prefix):
  40. continue
  41. source_filename = os.path.join(source_path,
  42. source_filename[len(prefix):])
  43. d['options'] = d['options'] and d['options'].split('_') or []
  44. if subdir and rel_dir.endswith(subdir):
  45. rel_dir = rel_dir[:-len(subdir)]
  46. # Corner-case bug: if the filename didn't have an extension but did
  47. # have an underscore, the last underscore will get converted to a
  48. # '.'.
  49. m = re.match(r'(.*)_(.*)', source_filename)
  50. if m:
  51. source_filename = '%s.%s' % m.groups()
  52. filename = os.path.join(rel_dir, source_filename)
  53. thumbnail_file = thumbnail_files.setdefault(filename, [])
  54. d['filename'] = os.path.join(dir_, file)
  55. thumbnail_file.append(d)
  56. return thumbnail_files
  57. def thumbnails_for_file(relative_source_path, root=None, basedir=None,
  58. subdir=None, prefix=None):
  59. """
  60. Return a list of dictionaries, one for each thumbnail belonging to the
  61. source image.
  62. The following list explains each key of the dictionary:
  63. `filename` -- absolute thumbnail path
  64. `x` and `y` -- the size of the thumbnail
  65. `options` -- list of options for this thumbnail
  66. `quality` -- quality setting for this thumbnail
  67. """
  68. if root is None:
  69. root = settings.MEDIA_ROOT
  70. if prefix is None:
  71. prefix = settings.THUMBNAIL_PREFIX
  72. if subdir is None:
  73. subdir = settings.THUMBNAIL_SUBDIR
  74. if basedir is None:
  75. basedir = settings.THUMBNAIL_BASEDIR
  76. source_dir, filename = os.path.split(relative_source_path)
  77. thumbs_path = os.path.join(root, basedir, source_dir, subdir)
  78. if not os.path.isdir(thumbs_path):
  79. return []
  80. files = all_thumbnails(thumbs_path, recursive=False, prefix=prefix,
  81. subdir='')
  82. return files.get(filename, [])
  83. def delete_thumbnails(relative_source_path, root=None, basedir=None,
  84. subdir=None, prefix=None):
  85. """
  86. Delete all thumbnails for a source image.
  87. """
  88. thumbs = thumbnails_for_file(relative_source_path, root, basedir, subdir,
  89. prefix)
  90. return _delete_using_thumbs_list(thumbs)
  91. def _delete_using_thumbs_list(thumbs):
  92. deleted = 0
  93. for thumb_dict in thumbs:
  94. filename = thumb_dict['filename']
  95. try:
  96. os.remove(filename)
  97. except:
  98. pass
  99. else:
  100. deleted += 1
  101. return deleted
  102. def delete_all_thumbnails(path, recursive=True):
  103. """
  104. Delete all files within a path which match the thumbnails pattern.
  105. By default, matching files from all sub-directories are also removed. To
  106. only remove from the path directory, set recursive=False.
  107. """
  108. total = 0
  109. for thumbs in all_thumbnails(path, recursive=recursive).values():
  110. total += _delete_using_thumbs_list(thumbs)
  111. return total