From e10337c838d088e3a8075c3f2771574bbf001a5d Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Thu, 14 Mar 2019 20:20:33 +0100 Subject: [PATCH 01/11] Print found directories TODO: Move to different method, since this way lower depth images are preferred --- pywal/image.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pywal/image.py b/pywal/image.py index 897f443..63ab209 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -31,7 +31,10 @@ def get_random_image(img_dir): images.remove(current_wall) elif not images: - logging.error("No images found in directory.") + 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)])) + sys.exit(1) random.shuffle(images) @@ -59,7 +62,7 @@ def get_next_image(img_dir): return os.path.join(img_dir, image) -def get(img, cache_dir=CACHE_DIR, iterative=False): +def get(img, cache_dir=CACHE_DIR, iterative=False, recursive=False): """Validate image input.""" if os.path.isfile(img): wal_img = img From e0c30a34a08feb2d8a0aa1b9f1019c27df6837fe Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 09:11:32 +0100 Subject: [PATCH 02/11] 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.") From 8b6ca5cbabc0cd982b72d4aede5639e663b5cdea Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 09:15:42 +0100 Subject: [PATCH 03/11] Check file ending --- pywal/image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pywal/image.py b/pywal/image.py index d535b67..a3ddd8a 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -22,8 +22,8 @@ def get_image_dir_recursive(img_dir): 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)) + if name.lower().endswith(file_types): + images.append(os.path.join(path, name)) return images, current_wall From b91ecad12bad39a41611b41f5db436a2cf4f34d7 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 09:38:32 +0100 Subject: [PATCH 04/11] Fix current wallpaper reuse --- pywal/image.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pywal/image.py b/pywal/image.py index a3ddd8a..ecd46ff 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -22,9 +22,10 @@ def get_image_dir_recursive(img_dir): images = [] for path, subdirs, files in os.walk(img_dir): for name in files: - if name.lower().endswith(file_types): + if name.lower().endswith(file_types) and not name.endswith(current_wall): images.append(os.path.join(path, name)) + return images, current_wall @@ -58,10 +59,7 @@ 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: + if not images: logging.error("No images found in directory.") sys.exit(1) From 3a95122157071c44f521210858a5c6bacfe9c3bb Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 09:49:21 +0100 Subject: [PATCH 05/11] Iterative images recursively --- pywal/image.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/pywal/image.py b/pywal/image.py index ecd46ff..71a12f0 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -22,9 +22,10 @@ def get_image_dir_recursive(img_dir): images = [] for path, subdirs, files in os.walk(img_dir): for name in files: - if name.lower().endswith(file_types) and not name.endswith(current_wall): - images.append(os.path.join(path, name)) - + if name.lower().endswith(file_types): + if name.endswith(current_wall): + current_wall = os.path.join(path, name) + images.append(os.path.join(path, name)) return images, current_wall @@ -59,18 +60,24 @@ 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) + if 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): +def get_next_image(img_dir, recursive): """Get the next image in a dir.""" - images, current_wall = get_image_dir(img_dir) + if recursive: + images, current_wall = get_image_dir_recursive(img_dir) + else: + images, current_wall = get_image_dir(img_dir) + images.sort(key=lambda img: [int(x) if x.isdigit() else x for x in re.split('([0-9]+)', img)]) @@ -86,7 +93,7 @@ def get_next_image(img_dir): except IndexError: image = images[0] - return os.path.join(img_dir, image) + return os.path.join(img_dir if not recursive else "", image) def get(img, cache_dir=CACHE_DIR, iterative=False, recursive=False): @@ -97,7 +104,7 @@ def get(img, cache_dir=CACHE_DIR, iterative=False, recursive=False): elif os.path.isdir(img): if iterative: - wal_img = get_next_image(img) + wal_img = get_next_image(img, recursive) else: if recursive: From 31f23088d70622ea5411e63a8449fc03dc0cade1 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 10:06:04 +0100 Subject: [PATCH 06/11] Add recursive argument to cli --- pywal/__main__.py | 8 +++++++- pywal/image.py | 1 - 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 2e50ca9..b7a7aad 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -53,6 +53,11 @@ def get_args(): "flag is used: Go through the images in order " "instead of shuffled.") + arg.add_argument("--recursive", action="store_true", + help="When pywal is given a directory as input and this " + "flag is used: Search for images recursively in " + "subdirectories instead of the root only.") + arg.add_argument("--saturate", metavar="0.0-1.0", help="Set the color saturation.") @@ -159,7 +164,8 @@ def parse_args(parser): util.Color.alpha_num = args.a if args.i: - image_file = image.get(args.i, iterative=args.iterative) + image_file = image.get(args.i, iterative=args.iterative, + recursive=args.recursive) colors_plain = colors.get(image_file, args.l, args.backend, sat=args.saturate) diff --git a/pywal/image.py b/pywal/image.py index 71a12f0..8e55a7b 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -98,7 +98,6 @@ def get_next_image(img_dir, recursive): 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 From 44339e5f3e2bcbcbcafa8fe0332f89c118ba12a2 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 10:12:13 +0100 Subject: [PATCH 07/11] Remove redundant method --- pywal/image.py | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/pywal/image.py b/pywal/image.py index 8e55a7b..d894189 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -41,9 +41,12 @@ def get_image_dir(img_dir): if img.name.lower().endswith(file_types)], current_wall -def get_random_image(img_dir): +def get_random_image(img_dir, recursive): """Pick a random image file from a directory.""" - images, current_wall = get_image_dir(img_dir) + if recursive: + images, current_wall = get_image_dir_recursive(img_dir) + else: + images, current_wall = get_image_dir(img_dir) if len(images) > 2 and current_wall in images: images.remove(current_wall) @@ -53,22 +56,7 @@ def get_random_image(img_dir): 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) - - if not images: - logging.error("No images found in directory.") - sys.exit(1) - - random.shuffle(images) - return os.path.join("", images[0]) + return os.path.join(img_dir if not recursive else "", images[0]) def get_next_image(img_dir, recursive): @@ -106,10 +94,7 @@ def get(img, cache_dir=CACHE_DIR, iterative=False, recursive=False): wal_img = get_next_image(img, recursive) else: - if recursive: - wal_img = get_random_image_recursive(img) - else: - wal_img = get_random_image(img) + wal_img = get_random_image(img, recursive) else: logging.error("No valid image file found.") From bbc5a33394c024afe120c0959dd9cb8e01636321 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 10:34:45 +0100 Subject: [PATCH 08/11] Fix indentation --- pywal/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index b7a7aad..e38cb3f 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -165,7 +165,7 @@ def parse_args(parser): if args.i: image_file = image.get(args.i, iterative=args.iterative, - recursive=args.recursive) + recursive=args.recursive) colors_plain = colors.get(image_file, args.l, args.backend, sat=args.saturate) From d5a1532f184f6b33b7622f3258f63cb0c7f82a15 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 10:36:26 +0100 Subject: [PATCH 09/11] Fix unused variable --- pywal/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/image.py b/pywal/image.py index d894189..6ef74fc 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -20,7 +20,7 @@ def get_image_dir_recursive(img_dir): file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif") images = [] - for path, subdirs, files in os.walk(img_dir): + for path, _, files in os.walk(img_dir): for name in files: if name.lower().endswith(file_types): if name.endswith(current_wall): From afa3ff125924334a5d0276efda7932fd7d5b8603 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 10:40:35 +0100 Subject: [PATCH 10/11] Try to fix indentation again --- pywal/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index e38cb3f..72da80d 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -165,7 +165,7 @@ def parse_args(parser): if args.i: image_file = image.get(args.i, iterative=args.iterative, - recursive=args.recursive) + recursive=args.recursive) colors_plain = colors.get(image_file, args.l, args.backend, sat=args.saturate) From 0878cc7fadbbb601f51d267a1be75d47c41cae1b Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Fri, 15 Mar 2019 10:43:32 +0100 Subject: [PATCH 11/11] Finally fix indendation --- pywal/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 72da80d..de992b7 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -165,7 +165,7 @@ def parse_args(parser): if args.i: image_file = image.get(args.i, iterative=args.iterative, - recursive=args.recursive) + recursive=args.recursive) colors_plain = colors.get(image_file, args.l, args.backend, sat=args.saturate)