From 4ab124fceb80949e6d10f85e43d048f8b7629ec7 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 8 Feb 2013 12:30:29 +0100 Subject: [PATCH] Don't process directories that do not contain images. --- sigal/gallery.py | 26 ++++++++++++++++++-------- tests/sample/empty/fake.txt | 0 tests/test_gallery.py | 7 +++++-- 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 tests/sample/empty/fake.txt diff --git a/sigal/gallery.py b/sigal/gallery.py index 6a335a3..831b163 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -53,22 +53,26 @@ class Gallery: def build_paths(self): "Build the list of directories with images" - self.paths = {'paths_list': []} + self.paths = {'paths_list': [], 'skipped_dir': []} 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) dirnames.sort(key=str.lower) - self.paths[relpath] = { - 'img': [ - f for f in filenames - if os.path.splitext(f)[1] in self.settings['ext_list']], - 'subdir': dirnames - } + images = [f for f in filenames + if os.path.splitext(f)[1] in self.settings['ext_list']] + + # skip this directory if it doesn't contain images + if relpath != '.' and not images: + self.paths['skipped_dir'].append(relpath) + self.logger.info("Directory '%s' is empty", relpath) + continue + + self.paths['paths_list'].append(relpath) + self.paths[relpath] = {'img': images, 'subdir': dirnames} self.paths[relpath].update(get_metadata(path)) if relpath != '.': @@ -79,6 +83,12 @@ class Gallery: alb_thumb = self.find_representative(relpath) self.paths[relpath]['representative'] = alb_thumb + # cleanup: remove skipped directories + for path in self.paths['paths_list']: + subdir = iter(self.paths[path]['subdir']) + self.paths[path]['subdir'] = [ + d for d in subdir if d not in self.paths['skipped_dir']] + def find_representative(self, path): "Find the representative image for a given path" diff --git a/tests/sample/empty/fake.txt b/tests/sample/empty/fake.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 4dac44c..768a0bc 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -29,10 +29,13 @@ class TestGallery(unittest.TestCase): def test_filelist(self): paths = self.gal.paths - self.assertItemsEqual(paths.keys(), - ['paths_list', '.', 'dir1', 'dir2', 'dir1/test']) + self.assertItemsEqual( + paths.keys(), + ['paths_list', 'skipped_dir', '.', 'dir1', 'dir2', 'dir1/test']) + self.assertListEqual(paths['paths_list'], ['.', 'dir1', 'dir1/test', 'dir2']) + self.assertListEqual(paths['skipped_dir'], ['empty']) self.assertListEqual(paths['.']['img'], []) self.assertItemsEqual(paths['.']['subdir'], ['dir1', 'dir2'])