-
Notifications
You must be signed in to change notification settings - Fork 2
/
album.py
399 lines (314 loc) · 14.4 KB
/
album.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding:utf-8 -*-
# Album classes for use with siglican plugin along with some helper
# methods for context building. This code is largely a copy of gallery.py
# from Sigal.
# Copyright (c) 2009-2014 - Simon Conseil
# Copyright (c) 2013 - Christophe-Marie Duquesne
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# siglican:
# Copyright (c) 2014 - Scott Boone (https://github.com/sawall/)
# Minor updates from Sigal.
import os
import logging
from collections import defaultdict
from PIL import Image as PILImage
from .compat import strxfrm, UnicodeMixin, url_quote
from .utils import read_markdown, url_from_path
from .image import process_image, get_exif_tags
class Media(UnicodeMixin):
"""Base Class for media files.
Attributes:
- ``type``: ``"image"`` or ``"video"``.
- ``filename``: Filename of the resized image.
- ``thumbnail``: Location of the corresponding thumbnail image.
- ``big``: If not None, location of the unmodified image.
- ``exif``: If not None contains a dict with the most common tags. For more
information, see :ref:`simple-exif-data`.
- ``raw_exif``: If not ``None``, it contains the raw EXIF tags.
"""
type = ''
extensions = ()
def __init__(self, filename, path, settings):
self.src_filename = self.filename = self.url = filename
self.path = path
self.settings = settings
self.src_path = os.path.join(settings['SIGLICAN_SOURCE'], path, filename)
self.dst_path = os.path.join(settings['SIGLICAN_DESTINATION'], path, filename)
self.thumb_name = get_thumb(self.settings, self.filename)
self.thumb_path = os.path.join(settings['SIGLICAN_DESTINATION'], path, self.thumb_name)
self.logger = logging.getLogger(__name__)
self.raw_exif = None
self.exif = None
self.date = None
self._get_metadata()
#signals.media_initialized.send(self)
def __repr__(self):
return "<%s>(%r)" % (self.__class__.__name__, str(self))
def __unicode__(self):
return os.path.join(self.path, self.filename)
@property
def thumbnail(self):
"""Path to the thumbnail image (relative to the album directory)."""
# cleanup: make this deal better with SIGLICAN_MAKE_THUMBS: False
return url_from_path(self.thumb_name)
def _get_metadata(self):
""" Get image metadata from filename.md: title, description, meta."""
self.description = ''
self.meta = {}
self.title = ''
descfile = os.path.splitext(self.src_path)[0] + '.md'
if os.path.isfile(descfile):
meta = read_markdown(descfile)
for key, val in meta.items():
setattr(self, key, val)
class Image(Media):
"""Gather all informations on an image file."""
type = 'image'
extensions = ('.jpg', '.jpeg', '.png')
def __init__(self, filename, path, settings):
super(Image, self).__init__(filename, path, settings)
self.raw_exif, self.exif = get_exif_tags(self.src_path)
if self.exif is not None and 'dateobj' in self.exif:
self.date = self.exif['dateobj']
class Video(Media):
"""Gather all informations on a video file."""
type = 'video'
extensions = ('.mov', '.avi', '.mp4', '.webm', '.ogv')
def __init__(self, filename, path, settings):
super(Video, self).__init__(filename, path, settings)
base = os.path.splitext(filename)[0]
self.src_filename = filename
self.filename = self.url = base + '.webm'
self.dst_path = os.path.join(settings['SIGLICAN_DESTINATION'], path, base + '.webm')
# minimally modified from Sigal's gallery.Album class
class Album(object):
description_file = "index.md"
output_file = 'index.html'
def __init__(self, path, settings, dirnames, filenames, gallery):
self.path = path
self.name = path.split(os.path.sep)[-1]
self.gallery = gallery
self.settings = settings
self.orig_path = None
self._thumbnail = None
# set up source and destination paths
if path == '.':
self.src_path = settings['SIGLICAN_SOURCE']
self.dst_path = settings['SIGLICAN_DESTINATION']
else:
self.src_path = os.path.join(settings['SIGLICAN_SOURCE'], path)
self.dst_path = os.path.join(settings['SIGLICAN_DESTINATION'], path)
self.logger = logging.getLogger(__name__)
self._get_metadata() # this reads the index.md file
# optionally add index.html to the URLs
# ** don't understand purpose of this; default is False
self.url_ext = self.output_file if settings['SIGLICAN_INDEX_IN_URL'] else ''
# creates appropriate subdirectory for the album
self.index_url = url_from_path(os.path.relpath(
settings['SIGLICAN_DESTINATION'], self.dst_path)) + '/' + self.url_ext
# sort sub-albums
dirnames.sort(key=strxfrm, reverse=settings['SIGLICAN_ALBUMS_SORT_REVERSE'])
self.subdirs = dirnames
#: List of all medias in the album (:class:`~sigal.gallery.Image` and
#: :class:`~sigal.gallery.Video`).
self.medias = medias = []
self.medias_count = defaultdict(int)
# create Media objects
for f in filenames:
ext = os.path.splitext(f)[1]
if ext.lower() in Image.extensions:
media = Image(f, self.path, settings)
elif ext.lower() in Video.extensions:
media = Video(f, self.path, settings)
else:
continue
self.medias_count[media.type] += 1
medias.append(media)
# sort images
if medias:
medias_sort_attr = settings['SIGLICAN_MEDIAS_SORT_ATTR']
if medias_sort_attr == 'date':
key = lambda s: s.date or datetime.now()
else:
key = lambda s: strxfrm(getattr(s, medias_sort_attr))
medias.sort(key=key, reverse=settings['SIGLICAN_MEDIAS_SORT_REVERSE'])
#signals.album_initialized.send(self)
# string representation of Album for debug statements
def __repr__(self):
return "<%s>(path=%r, title=%r, assets:%r)" % (self.__class__.__name__,
self.path, self.title, self.medias)
def __unicode__(self):
return (u"{} : ".format(self.path) +
', '.join("{} {}s".format(count, _type)
for _type, count in self.medias_count.items()))
def __len__(self):
return len(self.medias)
def __iter__(self):
return iter(self.medias)
def _get_metadata(self):
"""Get album metadata from `description_file` (`index.md`):
-> title, thumbnail image, description
"""
descfile = os.path.join(self.src_path, self.description_file)
self.description = ''
self.meta = {}
# default: get title from directory name
self.title = os.path.basename(self.path if self.path != '.'
else self.src_path)
if os.path.isfile(descfile):
meta = read_markdown(descfile)
for key, val in meta.items():
setattr(self, key, val)
def create_output_directories(self):
"""Create output directories for thumbnails and original images."""
def check_or_create_dir(path):
if not os.path.isdir(path):
os.makedirs(path)
check_or_create_dir(self.dst_path)
if self.medias:
check_or_create_dir(os.path.join(self.dst_path,
self.settings['SIGLICAN_THUMB_DIR']))
#if self.medias and self.settings['SIGLICAN_KEEP_ORIG']:
# self.orig_path = os.path.join(self.dst_path, self.settings['SIGLICAN_ORIG_DIR'])
# check_or_create_dir(self.orig_path)
@property
def images(self):
"""List of images (:class:`~sigal.gallery.Image`)."""
for media in self.medias:
if media.type == 'image':
yield media
@property
def videos(self):
"""List of videos (:class:`~sigal.gallery.Video`)."""
for media in self.medias:
if media.type == 'video':
yield media
@property
def albums(self):
"""List of :class:`~sigal.gallery.Album` objects for each
sub-directory.
"""
root_path = self.path if self.path != '.' else ''
return [self.gallery.albums[os.path.join(root_path, path)]
for path in self.subdirs]
@property
def url(self):
"""URL of the album, relative to its parent."""
url = self.name.encode('utf-8')
return url_quote(url) + '/' + self.url_ext
@property
def thumbnail(self):
"""Path to the thumbnail of the album."""
if self._thumbnail:
# stop if it is already set
return url_from_path(self._thumbnail)
# Test the thumbnail from the Markdown file.
thumbnail = self.meta.get('thumbnail', [''])[0]
if thumbnail and os.path.isfile(os.path.join(self.src_path, thumbnail)):
self._thumbnail = os.path.join(self.name, get_thumb(self.settings,
thumbnail))
self.logger.debug("Thumbnail for %r : %s", self, self._thumbnail)
return url_from_path(self._thumbnail)
else:
# find and return the first landscape image
for f in self.medias:
ext = os.path.splitext(f.filename)[1]
if ext.lower() in Image.extensions:
im = PILImage.open(f.src_path)
if im.size[0] > im.size[1]:
self._thumbnail = os.path.join(self.name, f.thumbnail)
self.logger.debug(
"Use 1st landscape image as thumbnail for %r : %s",
self, self._thumbnail)
return url_from_path(self._thumbnail)
# else simply return the 1st media file
if not self._thumbnail and self.medias:
self._thumbnail = os.path.join(self.name, self.medias[0].thumbnail)
self.logger.debug("Use the 1st image as thumbnail for %r : %s",
self, self._thumbnail)
return url_from_path(self._thumbnail)
# use the thumbnail of their sub-directories
if not self._thumbnail:
for path, album in self.gallery.get_albums(self.path):
if album.thumbnail:
self._thumbnail = os.path.join(self.name, album.thumbnail)
self.logger.debug(
"Using thumbnail from sub-directory for %r : %s",
self, self._thumbnail)
return url_from_path(self._thumbnail)
self.logger.error('Thumbnail not found for %r', self)
return None
@property
def breadcrumb(self):
"""List of ``(url, title)`` tuples defining the current breadcrumb
path.
"""
if self.path == '.':
return []
path = self.path
breadcrumb = [((self.url_ext or '.'), self.title)]
while True:
path = os.path.normpath(os.path.join(path, '..'))
if path == '.':
break
url = (url_from_path(os.path.relpath(path, self.path)) + '/' +
self.url_ext)
breadcrumb.append((url, self.gallery.albums[path].title))
breadcrumb.reverse()
return breadcrumb
# TODO: delete this and related settings; this is not a use case that
# a Pelican plugin should handle
@property
def zip(self):
"""Make a ZIP archive with all media files and return its path.
If the ``zip_gallery`` setting is set,it contains the location of a zip
archive with all original images of the corresponding directory.
"""
zip_gallery = self.settings['SIGLICAN_ZIP_GALLERY']
if zip_gallery and len(self) > 0:
archive_path = os.path.join(self.dst_path, zip_gallery)
archive = zipfile.ZipFile(archive_path, 'w')
if self.settings['SIGLICAN_ZIP_MEDIA_FORMAT'] == 'orig':
for p in self:
archive.write(p.src_path, os.path.split(p.src_path)[1])
else:
for p in self:
archive.write(p.dst_path, os.path.split(p.dst_path)[1])
archive.close()
self.logger.debug('Created ZIP archive %s', archive_path)
return zip_gallery
else:
return None
# ** TODO: move as part of utils cleanup
def get_thumb(settings, filename):
"""Return the path to the thumb.
examples:
>>> default_settings = create_settings()
>>> get_thumb(default_settings, "bar/foo.jpg")
"bar/thumbnails/foo.jpg"
>>> get_thumb(default_settings, "bar/foo.png")
"bar/thumbnails/foo.png"
for videos, it returns a jpg file:
>>> get_thumb(default_settings, "bar/foo.webm")
"bar/thumbnails/foo.jpg"
"""
path, filen = os.path.split(filename)
name, ext = os.path.splitext(filen)
if ext.lower() in Video.extensions:
ext = '.jpg'
return os.path.join(path, settings['SIGLICAN_THUMB_DIR'], settings['SIGLICAN_THUMB_PREFIX'] +
name + settings['SIGLICAN_THUMB_SUFFIX'] + ext)