diff --git a/sigal/gallery.py b/sigal/gallery.py index 07a967b..1052a11 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -108,6 +108,8 @@ class Media: return state def __setstate__(self, state): + for slot, value in state.items(): + setattr(self, slot, value) self.logger = logging.getLogger(__name__) @property @@ -283,8 +285,6 @@ class Video(Media): ext = '.' + video_format self.dst_filename = self.basename + ext self.mime = get_mime(ext) - self.dst_path = join(settings['destination'], path, - self.dst_filename) else: self.mime = get_mime(self.src_ext) @@ -794,7 +794,7 @@ class Gallery: def remove_files(self, medias): self.logger.error('Some files have failed to be processed:') for media in medias: - self.logger.error(' - %s/%s', media.dst_filename) + self.logger.error(' - %s', media.dst_filename) album = self.albums[media.path] for f in album.medias: if f.dst_filename == media.dst_filename: diff --git a/sigal/image.py b/sigal/image.py index 8c5b1de..3e05334 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -197,7 +197,6 @@ def process_image(media): thumb_fit_centering=media.settings["thumb_fit_centering"] ) except Exception as e: - __import__('pdb').set_trace() logger.info('Failed to process: %r', e) if logger.getEffectiveLevel() == logging.DEBUG: raise diff --git a/sigal/video.py b/sigal/video.py index 337b087..b3f92d4 100644 --- a/sigal/video.py +++ b/sigal/video.py @@ -157,16 +157,16 @@ def generate_thumbnail(source, outname, box, delay, fit=True, options=None, os.unlink(tmpfile) -def process_video(filepath, outpath, settings): +def process_video(media): """Process a video: resize, create thumbnail.""" logger = logging.getLogger(__name__) - filename = os.path.split(filepath)[1] - basename, ext = splitext(filename) + settings = media.settings try: - if settings['use_orig'] and is_valid_html5_video(ext): - utils.copy(filepath, outpath, symlink=settings['orig_link']) + if settings['use_orig'] and is_valid_html5_video(media.src_ext): + utils.copy(media.src_path, media.dst_path, + symlink=settings['orig_link']) else: valid_formats = ['mp4', 'webm'] video_format = settings['video_format'] @@ -176,7 +176,7 @@ def process_video(filepath, outpath, settings): valid_formats) raise ValueError - generate_video(filepath, outpath, settings, + generate_video(media.src_path, media.dst_path, settings, options=settings.get(video_format + '_options')) except Exception: if logger.getEffectiveLevel() == logging.DEBUG: @@ -185,12 +185,10 @@ def process_video(filepath, outpath, settings): return Status.FAILURE if settings['make_thumbs']: - thumb_name = os.path.join(os.path.dirname(outpath), - get_thumb(settings, filename)) try: generate_thumbnail( - outpath, - thumb_name, + media.dst_path, + media.thumb_path, settings['thumb_size'], settings['thumb_video_delay'], fit=settings['thumb_fit'], diff --git a/tests/sample/pictures/dir1/test2/21.tiff b/tests/sample/pictures/dir1/test2/21.tiff new file mode 100644 index 0000000..12c9022 Binary files /dev/null and b/tests/sample/pictures/dir1/test2/21.tiff differ diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 9b22685..61a5aca 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -31,9 +31,9 @@ REF = { 'dir1/test2': { 'title': 'test2', 'name': 'test2', - 'thumbnail': 'test2/thumbnails/21.tn.jpg', + 'thumbnail': 'test2/thumbnails/21.tn.tiff', 'subdirs': [], - 'medias': ['21.jpg', '22.jpg', 'CMB_Timeline300_no_WMAP.jpg'], + 'medias': ['21.tiff', '22.jpg', 'CMB_Timeline300_no_WMAP.jpg'], }, 'dir1/test3': { 'title': '01 First title alphabetically', @@ -116,14 +116,15 @@ def test_media_iptc_override(settings): # Markdown parsing adds formatting. Let's just focus on content assert "Markdown description beats iptc" in img_with_md.description img_no_md = Image('1.jpg', 'iptcTest', settings) - assert img_no_md.title == 'Haemostratulus clouds over Canberra - ' + \ - '2005-12-28 at 03-25-07' - assert img_no_md.description == \ - '"Haemo" because they look like haemoglobin ' + \ - 'cells and "stratulus" because I can\'t work out whether ' + \ - 'they\'re Stratus or Cumulus clouds.\nWe\'re driving down ' + \ - 'the main drag in Canberra so it\'s Parliament House that ' + \ - 'you can see at the end of the road.' + assert img_no_md.title == ('Haemostratulus clouds over Canberra - ' + '2005-12-28 at 03-25-07') + assert img_no_md.description == ( + '"Haemo" because they look like haemoglobin ' + 'cells and "stratulus" because I can\'t work out whether ' + 'they\'re Stratus or Cumulus clouds.\nWe\'re driving down ' + 'the main drag in Canberra so it\'s Parliament House that ' + 'you can see at the end of the road.' + ) def test_image(settings, tmpdir): @@ -225,15 +226,15 @@ def test_medias_sort(settings): settings['medias_sort_reverse'] = False a = Album('dir1/test2', settings, album['subdirs'], album['medias'], gal) a.sort_medias(settings['medias_sort_attr']) - assert [im.dst_filename for im in a.images] == ['22.jpg', '21.jpg', - 'CMB_Timeline300_no_WMAP.jpg'] + assert [im.dst_filename for im in a.images] == [ + '22.jpg', 'CMB_Timeline300_no_WMAP.jpg', '21.tiff'] settings['medias_sort_attr'] = 'meta.order' settings['medias_sort_reverse'] = False a = Album('dir1/test2', settings, album['subdirs'], album['medias'], gal) a.sort_medias(settings['medias_sort_attr']) assert [im.dst_filename for im in a.images] == [ - 'CMB_Timeline300_no_WMAP.jpg', '21.jpg', '22.jpg'] + 'CMB_Timeline300_no_WMAP.jpg', '21.tiff', '22.jpg'] def test_gallery(settings, tmpdir): diff --git a/tests/test_image.py b/tests/test_image.py index d504c35..ecca13c 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -2,32 +2,37 @@ import os from unittest.mock import patch import pytest -from PIL import Image +from PIL import Image as PILImage from sigal import init_logging from sigal.image import (generate_image, generate_thumbnail, get_exif_data, get_exif_tags, get_iptc_data, get_size, process_image) +from sigal.gallery import Image from sigal.settings import Status, create_settings CURRENT_DIR = os.path.dirname(__file__) +SRCDIR = os.path.join(CURRENT_DIR, 'sample', 'pictures') TEST_IMAGE = 'KeckObservatory20071020.jpg' -SRCFILE = os.path.join(CURRENT_DIR, 'sample', 'pictures', 'dir2', TEST_IMAGE) +SRCFILE = os.path.join(SRCDIR, 'dir2', TEST_IMAGE) TEST_GIF_IMAGE = 'example.gif' -SRC_GIF_FILE = os.path.join(CURRENT_DIR, 'sample', 'pictures', - 'dir1', 'test1', TEST_GIF_IMAGE) +SRC_GIF_FILE = os.path.join(SRCDIR, 'dir1', 'test1', TEST_GIF_IMAGE) def test_process_image(tmpdir): "Test the process_image function." - status = process_image('foo.txt', 'none.txt', {}) + status = process_image(Image('foo.txt', 'bar', create_settings())) assert status == Status.FAILURE - settings = create_settings(img_processor='ResizeToFill', make_thumbs=False) - status = process_image(SRCFILE, str(tmpdir), settings) + settings = create_settings(img_processor='ResizeToFill', + make_thumbs=False, + source=os.path.join(SRCDIR, 'dir2'), + destination=str(tmpdir)) + image = Image(TEST_IMAGE, '.', settings) + status = process_image(image) assert status == Status.SUCCESS - im = Image.open(os.path.join(str(tmpdir), TEST_IMAGE)) + im = PILImage.open(os.path.join(str(tmpdir), TEST_IMAGE)) assert im.size == settings['img_size'] @@ -40,7 +45,7 @@ def test_generate_image(tmpdir): copy_exif_data=True) options = None if i == 0 else {'quality': 85} generate_image(SRCFILE, dstfile, settings, options=options) - im = Image.open(dstfile) + im = PILImage.open(dstfile) assert im.size == size @@ -55,7 +60,7 @@ def test_generate_image_imgformat(tmpdir): img_format=outfmt) options = {'quality': 85} generate_image(SRCFILE, dstfile, settings, options=options) - im = Image.open(dstfile) + im = PILImage.open(dstfile) assert im.format == outfmt def test_resize_image_portrait(tmpdir): @@ -68,7 +73,7 @@ def test_resize_image_portrait(tmpdir): portrait_dst = str(tmpdir.join(portrait_image)) generate_image(portrait_src, portrait_dst, settings) - im = Image.open(portrait_dst) + im = PILImage.open(portrait_dst) # In the default mode, PILKit resizes in a way to never make an image # smaller than either of the lengths, the other is scaled accordingly. @@ -80,7 +85,7 @@ def test_resize_image_portrait(tmpdir): landscape_dst = str(tmpdir.join(landscape_image)) generate_image(landscape_src, landscape_dst, settings) - im = Image.open(landscape_dst) + im = PILImage.open(landscape_dst) assert im.size[1] == 200 @@ -132,13 +137,13 @@ def test_generate_thumbnail(tmpdir, image, path, wide_size, high_size): dstfile = str(tmpdir.join(image)) for size in [(200, 150), (150, 200)]: generate_thumbnail(path, dstfile, size) - im = Image.open(dstfile) + im = PILImage.open(dstfile) assert im.size == size for size, thumb_size in [((200, 150), wide_size), ((150, 200), high_size)]: generate_thumbnail(path, dstfile, size, fit=False) - im = Image.open(dstfile) + im = PILImage.open(dstfile) assert im.size == thumb_size diff --git a/tests/test_video.py b/tests/test_video.py index 4b17b6c..bff29ee 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -2,13 +2,15 @@ import os import pytest +from sigal.gallery import Video from sigal.settings import Status, create_settings from sigal.video import (generate_thumbnail, generate_video, process_video, video_size) CURRENT_DIR = os.path.dirname(__file__) +SRCDIR = os.path.join(CURRENT_DIR, 'sample', 'pictures') TEST_VIDEO = 'example video.ogv' -SRCFILE = os.path.join(CURRENT_DIR, 'sample', 'pictures', 'video', TEST_VIDEO) +SRCFILE = os.path.join(SRCDIR, 'video', TEST_VIDEO) def test_video_size(): @@ -27,17 +29,20 @@ def test_generate_thumbnail(tmpdir): def test_process_video(tmpdir): base, ext = os.path.splitext(TEST_VIDEO) - settings = create_settings(video_format='ogv', use_orig=True, - orig_link=True) - process_video(SRCFILE, str(tmpdir), settings) + settings = create_settings(video_format='ogv', + use_orig=True, orig_link=True, + source=os.path.join(SRCDIR, 'video'), + destination=str(tmpdir)) + video = Video(TEST_VIDEO, '.', settings) + process_video(video) dstfile = str(tmpdir.join(base + '.ogv')) assert os.path.realpath(dstfile) == SRCFILE settings = create_settings(video_format='mjpg') - assert process_video(SRCFILE, str(tmpdir), settings) == Status.FAILURE + assert process_video(video) == Status.FAILURE settings = create_settings(thumb_video_delay=-1) - assert process_video(SRCFILE, str(tmpdir), settings) == Status.FAILURE + assert process_video(video) == Status.FAILURE @pytest.mark.parametrize("fmt", ['webm', 'mp4'])