diff --git a/sigal/gallery.py b/sigal/gallery.py index a214743..a1e7be8 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -51,10 +51,11 @@ class Gallery: def build_paths(self): "Build the list of directories with images" - self.paths = {} + self.paths = {'paths_list': []} for path, dirnames, filenames in os.walk(self.input_dir): relpath = os.path.relpath(path, self.input_dir) + self.paths['paths_list'].append(relpath) # sort images and sub-albums by name filenames.sort(key=str.lower) @@ -95,8 +96,9 @@ class Gallery: self.build_paths() check_or_create_dir(self.output_dir) - # loop on directories - for path in self.paths.keys(): + # loop on directories in reversed order, to process subdirectories + # before their parent + for path in reversed(self.paths['paths_list']): imglist = [os.path.join(self.input_dir, path, f) for f in self.paths[path]['img']] diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 52d9f2f..4645cc5 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -29,7 +29,10 @@ class TestGallery(unittest.TestCase): def test_filelist(self): paths = self.gal.paths - self.assertItemsEqual(paths.keys(), ['.', 'dir1', 'dir2', 'dir1/test']) + self.assertItemsEqual(paths.keys(), + ['paths_list', '.', 'dir1', 'dir2', 'dir1/test']) + self.assertListEqual(paths['paths_list'], + ['.', 'dir1', 'dir1/test', 'dir2']) self.assertListEqual(paths['.']['img'], []) self.assertItemsEqual(paths['.']['subdir'], ['dir1', 'dir2'])