Skip to content

Commit

Permalink
Merge pull request #192 from trapperhoney/master
Browse files Browse the repository at this point in the history
Implement album sorting
  • Loading branch information
saimn committed Jan 18, 2016
2 parents b11cb28 + 86b5967 commit ea3eae4
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ alphabetical order):
- Sébastien Maccagnoni-Munch
- Simon Conseil
- @t-animal
- @trapperhoney
- Thomas Misilo
- Tobias Preuss
- Vikram Shirgur
Expand Down
24 changes: 20 additions & 4 deletions sigal/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def __init__(self, path, settings, dirnames, filenames, gallery):
self.name = path.split(os.path.sep)[-1]
self.gallery = gallery
self.settings = settings
self.subdirs = dirnames
self.output_file = settings['output_filename']
self._thumbnail = None

Expand All @@ -240,10 +241,6 @@ def __init__(self, path, settings, dirnames, filenames, gallery):
self.index_url = url_from_path(os.path.relpath(
settings['destination'], self.dst_path)) + '/' + self.url_ext

# sort sub-albums
dirnames.sort(key=strxfrm, reverse=settings['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 = []
Expand Down Expand Up @@ -313,6 +310,20 @@ def create_output_directories(self):
self.orig_path = join(self.dst_path, self.settings['orig_dir'])
check_or_create_dir(self.orig_path)

def sort_subdirs(self, albums_sort_attr):
if self.subdirs:
if albums_sort_attr:
root_path = self.path if self.path != '.' else ''
key = lambda s: strxfrm(getattr(
self.gallery.albums[join(root_path, s)], albums_sort_attr))
else:
key = strxfrm

self.subdirs.sort(key=key,
reverse=self.settings['albums_sort_reverse'])

signals.albums_sorted.send(self)

def sort_medias(self, medias_sort_attr):
if self.medias:
if medias_sort_attr == 'date':
Expand Down Expand Up @@ -525,6 +536,11 @@ def __init__(self, settings, ncpu=None):
album.create_output_directories()
albums[relpath] = album

with progressbar(albums.values(), label="Sorting albums",
file=self.progressbar_target) as progress_albums:
for album in progress_albums:
album.sort_subdirs(settings['albums_sort_attr'])

with progressbar(albums.values(), label="Sorting media",
file=self.progressbar_target) as progress_albums:
for album in progress_albums:
Expand Down
1 change: 1 addition & 0 deletions sigal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


_DEFAULT_CONFIG = {
'albums_sort_attr': 'name',
'albums_sort_reverse': False,
'autorotate_images': True,
'colorbox_column_size': 4,
Expand Down
1 change: 1 addition & 0 deletions sigal/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
gallery_initialized = signal('gallery_initialized')
gallery_build = signal('gallery_build')
media_initialized = signal('media_initialized')
albums_sorted = signal('albums_sorted')
medias_sorted = signal('medias_sorted')
3 changes: 3 additions & 0 deletions sigal/templates/sigal.conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
# Use symbolic links instead of copying the original images
# orig_link = False

# Attribute of Album objects which is used to sort medias (eg 'title').
# albums_sort_attr = 'name'

# Reverse sort for albums
# albums_sort_reverse = False

Expand Down
Binary file added tests/sample/pictures/dir1/test3/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/sample/pictures/dir1/test3/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Title: 01 First title alphabetically
30 changes: 28 additions & 2 deletions tests/test_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'title': 'An example gallery',
'name': 'dir1',
'thumbnail': 'dir1/test1/thumbnails/11.tn.jpg',
'subdirs': ['test1', 'test2'],
'subdirs': ['test1', 'test2', 'test3'],
'medias': [],
},
'dir1/test1': {
Expand All @@ -34,6 +34,13 @@
'subdirs': [],
'medias': ['21.jpg', '22.jpg', 'archlinux-kiss-1024x640.png'],
},
'dir1/test3': {
'title': '01 First title alphabetically',
'name': 'test3',
'thumbnail': 'test3/thumbnails/3.tn.jpg',
'subdirs': [],
'medias': ['3.jpg'],
},
'dir2': {
'title': 'Another example gallery with a very long name',
'name': 'dir2',
Expand Down Expand Up @@ -165,10 +172,29 @@ def test_album_medias(settings):
def test_albums_sort(settings):
gal = Gallery(settings, ncpu=1)
album = REF['dir1']
subdirs = list(album['subdirs'])

settings['albums_sort_reverse'] = False
a = Album('dir1', settings, album['subdirs'], album['medias'], gal)
a.sort_subdirs('')
assert [alb.name for alb in a.albums] == subdirs

settings['albums_sort_reverse'] = True
a = Album('dir1', settings, album['subdirs'], album['medias'], gal)
assert [im.filename for im in a.images] == list(reversed(album['medias']))
a.sort_subdirs('')
assert [alb.name for alb in a.albums] == list(reversed(subdirs))

titles = [im.title for im in a.albums]
titles.sort()
settings['albums_sort_reverse'] = False
a = Album('dir1', settings, album['subdirs'], album['medias'], gal)
a.sort_subdirs('title')
assert [im.title for im in a.albums] == titles

settings['albums_sort_reverse'] = True
a = Album('dir1', settings, album['subdirs'], album['medias'], gal)
a.sort_subdirs('title')
assert [im.title for im in a.albums] == list(reversed(titles))


def test_medias_sort(settings):
Expand Down

0 comments on commit ea3eae4

Please sign in to comment.