mirror of
https://github.com/dylanaraps/pywal.git
synced 2024-11-25 09:23:08 +01:00
Basic functionality
This commit is contained in:
parent
e10337c838
commit
e0c30a34a0
@ -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.")
|
||||
|
Loading…
Reference in New Issue
Block a user