From e0c30a34a08feb2d8a0aa1b9f1019c27df6837fe Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 09:11:32 +0100 Subject: [PATCH] Basic functionality --- pywal/image.py | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/pywal/image.py b/pywal/image.py index 63ab209..d535b67 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -12,6 +12,22 @@ from . import util from . import wallpaper +def get_image_dir_recursive(img_dir): + """Get all images in a directory recursively.""" + current_wall = wallpaper.get() + current_wall = os.path.basename(current_wall) + + file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif") + + images = [] + for path, subdirs, files in os.walk(img_dir): + for name in files: + print(os.path.join(path, name)) + images.append(os.path.join(path, name)) + + return images, current_wall + + def get_image_dir(img_dir): """Get all images in a directory.""" current_wall = wallpaper.get() @@ -31,16 +47,29 @@ def get_random_image(img_dir): images.remove(current_wall) elif not images: - logging.error("No images found in directory, picking random directory.") - logging.info("Found dirs: {}".format([dir_file.name for dir_file in - os.scandir(img_dir)])) - + logging.error("No images found in directory.") sys.exit(1) random.shuffle(images) return os.path.join(img_dir, images[0]) +def get_random_image_recursive(img_dir): + """Pick a random image file from a directory recursively.""" + images, current_wall = get_image_dir_recursive(img_dir) + + if len(images) > 2 and current_wall in images: + images.remove(current_wall) + + elif not images: + logging.error("No images found in directory.") + sys.exit(1) + + print(images) + random.shuffle(images) + return os.path.join("", images[0]) + + def get_next_image(img_dir): """Get the next image in a dir.""" images, current_wall = get_image_dir(img_dir) @@ -64,6 +93,7 @@ def get_next_image(img_dir): def get(img, cache_dir=CACHE_DIR, iterative=False, recursive=False): """Validate image input.""" + recursive = True # TODO: Remove if os.path.isfile(img): wal_img = img @@ -72,7 +102,10 @@ def get(img, cache_dir=CACHE_DIR, iterative=False, recursive=False): wal_img = get_next_image(img) else: - wal_img = get_random_image(img) + if recursive: + wal_img = get_random_image_recursive(img) + else: + wal_img = get_random_image(img) else: logging.error("No valid image file found.")