def get_colors(img, quiet): """Generate a colorscheme using imagemagick.""" # Cache the wallpaper name. util.save_file(img, CACHE_DIR / "wal") # Cache the sequences file. cache_file = pathlib.Path(CACHE_DIR / "schemes" / img.replace("/", "_")) if cache_file.is_file(): colors = util.read_file(cache_file) print("colors: Found cached colorscheme.") else: print("colors: Generating a colorscheme...") if not quiet: util.disown("notify-send", "wal: Generating a colorscheme...") # Generate the colors. colors = gen_colors(img) colors = sort_colors(colors) # Cache the colorscheme. util.save_file("\n".join(colors), cache_file) print("colors: Generated colorscheme") if not quiet: util.disown("notify-send", "wal: Generation complete.") return colors
def reload_colors(vte): """Reload the current scheme.""" sequence_file = CACHE_DIR / "sequences" if sequence_file.is_file(): sequences = "".join(util.read_file(sequence_file)) # If vte mode was used, remove the unsupported sequence. if vte: sequences = re.sub(r"\]708;\#.{6}", "", sequences) print(sequences, end="") exit(0)
def reload_colors(vte): """Reload colors.""" sequence_file = pathlib.Path(CACHE_DIR / "sequences") if sequence_file.is_file(): sequences = "".join(util.read_file(sequence_file)) # If vte mode was used, remove the problem sequence. if vte: sequences = re.sub(r"\]708;\#.{6}", "", sequences) # Make the terminal interpret escape sequences. print(sequences, end="") exit(0)
def random_img(img_dir): """Pick a random image file from a directory.""" current_wall = pathlib.Path(CACHE_DIR / "wal") if current_wall.is_file(): current_wall = util.read_file(current_wall) current_wall = os.path.basename(current_wall[0]) # Add all images to a list excluding the current wallpaper. file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif") images = [ img for img in os.listdir(img_dir) if img.endswith(file_types) and img != current_wall ] return pathlib.Path(img_dir / random.choice(images))
def get_random_image(img_dir): """Pick a random image file from a directory.""" current_wall = CACHE_DIR / "wal" if current_wall.is_file(): current_wall = util.read_file(current_wall) current_wall = os.path.basename(current_wall[0]) # Add all images to a list excluding the current wallpaper. file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif") images = [ img for img in os.scandir(img_dir) if img.name.endswith(file_types) and img.name != current_wall ] # If no images are found, use the current wallpaper. if not images: print("image: No new images found (nothing to do), exiting...") quit(1) return img_dir / random.choice(images).name
def test_read_file(self): """> Read colors from a file.""" result = util.read_file("tests/test_files/test_file") self.assertEqual(result[0], "/home/dylan/Pictures/Wallpapers/1.jpg")
def is_file_contents(self, tmp_file, pattern): """> Check for pattern in file.""" content = util.read_file(tmp_file) self.assertEqual(content[6], pattern)