def random(orig_colors: ColorList, config: ConfigParser): """Yeah, coz how could we live without a random filter?""" try: include_str = config["filters:random"]["include"] except KeyError: filter_names = [*{i.name for i in entry_points()["larry.filters"]}] filter_names.remove("random") else: filter_names = [*{i.strip() for i in include_str.split()}] if not filter_names: return orig_colors try: chains = int(config["filters:random"]["chains"]) except (KeyError, ValueError): chains = 1 new_colors = orig_colors iters = rand.randint(1, chains) for _ in range(iters): filter_name = rand.choice(filter_names) filter_ = load_filter(filter_name) LOGGER.debug("random: running filter: %s", filter_name) new_colors = filter_(new_colors, config) return new_colors
def plugin(colors: ColorList, config: ConfigType) -> None: """run a command with the colors as stdin""" LOGGER.debug("command plugin begin") exe = config["command"] colors_str = "\n".join(str(i) for i in colors) LOGGER.debug('command="%s"', exe) subprocess.run(exe, check=False, shell=True, input=colors_str.encode())
def start(config: ConfigType) -> None: address = config.get("listen_address", "localhost") port = int(config["port"]) LOGGER.debug("Starting vim server on %s:%s", address, port) loop = asyncio.get_event_loop() server = loop.create_server(VimProtocol, address, port) loop.create_task(server)
def run_every(interval: float, loop) -> None: """Run *callback* immediately and then every *interval* seconds after""" global HANDLER if HANDLER: LOGGER.info("received signal to change wallpaper") HANDLER.cancel() run() if interval == 0: return HANDLER = loop.call_later(interval, run_every, interval, loop)
def run() -> None: config = load_config() if config["larry"].getboolean("pause", False): LOGGER.info("Larry is paused") return raw_image_data = read_file(os.path.expanduser(config["larry"]["input"])) image = Image.from_bytes(raw_image_data) orig_colors = list(image.get_colors()) orig_colors.sort(key=Color.luminocity) colors_str = config["larry"].get("colors", "").strip().split() if colors_str: LOGGER.debug("using colors from config") colors = [Color(i.strip()) for i in colors_str] else: colors = orig_colors.copy() filter_names = config["larry"].get("filter", "gradient").split() for filter_name in filter_names: try: filter_ = load_filter(filter_name) except FilterNotFound: error_message = f"Color filter {filter_name} not found. Skipping." LOGGER.exception(error_message) else: LOGGER.debug("Calling filter %s", filter_name) colors = filter_(colors, config) LOGGER.debug("new colors: %s", colors) if colors != orig_colors: image.replace(orig_colors, colors) outfile = os.path.expanduser(config["larry"]["output"]) write_file(outfile, bytes(image)) # now run any plugins if "larry" not in config.sections(): return plugins = config["larry"].get("plugins", "").split() loop = asyncio.get_event_loop() for plugin_name in plugins: loop.call_soon(do_plugin, plugin_name, colors)
def get_new_colors(config: str, from_colors: ColorList): bg_color = from_colors[0] vim_configs = [*process_config(config)] targets = Color.generate_from(list(from_colors), len(vim_configs)) to_colors: List[Tuple[str, str]] = [] for vim_config in vim_configs: target = next(targets) if vim_config.key == "fg" else bg_color to_color = vim_config.color.colorify(target) key = f"gui{vim_config.key}" to_colors.append((vim_config.name, f"{key}={to_color}")) LOGGER.debug("vim colors: %s", to_colors) return to_colors
def main(args=None): """Main program entry point""" logging.basicConfig() args = parse_args(args or sys.argv[1:]) config = load_config() if args.debug or config["larry"].getboolean("debug", fallback=False): LOGGER.setLevel("DEBUG") LOGGER.debug("args=%s", args) if args.list_plugins: list_plugins() return if args.list_filters: list_filters() return loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGUSR1, run_every, args.interval, loop) loop.call_soon(run_every, args.interval, loop) try: loop.run_forever() except KeyboardInterrupt: LOGGER.info("User interrupted") loop.stop() finally: loop.close()
def do_plugin(plugin_name: str, colors: ColorList) -> None: plugin = load(plugin_name) config = get_config(plugin_name) LOGGER.debug("Running plugin for %s", plugin_name) plugin(colors, config)