Don't process directories that do not contain images.
This commit is contained in:
@@ -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"
|
||||
|
||||
|
||||
0
tests/sample/empty/fake.txt
Normal file
0
tests/sample/empty/fake.txt
Normal file
@@ -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'])
|
||||
|
||||
Reference in New Issue
Block a user