diff --git a/bin/rzr b/bin/rzr index 40a708a..2753be3 100644 --- a/bin/rzr +++ b/bin/rzr @@ -1,5 +1,6 @@ #!/usr/bin/env python +from argparse import RawTextHelpFormatter from colour import Color from openrazer.client import DeviceManager from openrazer.client import constants as razer_constants @@ -11,6 +12,29 @@ import time import toml +def print_greeter(): + + logo = """ + _ _ _ + /\ \ /\ \ /\ \ + / \ \ / \ \ / \ \ + / /\ \ \ __/ /\ \ \ / /\ \ \ + / / /\ \_\/___/ /\ \ \ / / /\ \_\ + / / /_/ / /\___\/ / / / / / /_/ / / + / / /__\/ / / / / / / /__\/ / + / / /_____/ / / / _ / / /_____/ + / / /\ \ \ \ \ \__/\_\ / / /\ \ \ +/ / / \ \ \ \ \___\/ // / / \ \ \ +\/_/ \_\/ \/___/_/ \/_/ \_\/ + """ + + print(logo) + + +def error(msg): + print("ERROR: {}".format(msg)) + + def get_color_tuple(color_string): # Convert color string to RGB tuple @@ -38,8 +62,8 @@ def apply_lightmap(device_profile): ), None, ) - if device == None: - print("device '{}' not found".format(device_profile["device"])) + if not device: + error("device '{}' not found".format(device_profile["name"])) exit(1) # Open lightmap @@ -48,16 +72,11 @@ def apply_lightmap(device_profile): "{}/{}.toml".format(lightmap_directory, device_profile["lightmap"]) ) except FileNotFoundError: - print("the lightmap '{}' doesn't exist".format(device_profile["lightmap"])) - if len(os.listdir(lightmap_directory)) > 0: - print("found the following lightmaps:") - for lightmap_file in os.listdir(lightmap_directory): - print(" - {}".format(lightmap_file[:-5])) - else: - print("found no lightmaps") + error("the lightmap '{}' doesn't exist".format(device_profile["lightmap"])) + list_lightmaps() exit(1) except Exception as e: - print("failed to load lightmap '{}': {}".format(device_profile["lightmap"], e)) + error("failed to load lightmap '{}': {}".format(device_profile["lightmap"], e)) exit(1) # Set light colors @@ -68,14 +87,14 @@ def apply_lightmap(device_profile): lightmap[light][0], lightmap[light][1] ] = color_tuple except KeyError: - print( + error( "light '{}' is not available in lightmap '{}'".format( light, device_profile["lightmap"] ) ) exit(1) except Exception as e: - print("failed to set light '{}': {}".format(light, e)) + error("failed to set light '{}': {}".format(light, e)) exit(1) # Apply light colors @@ -86,11 +105,6 @@ def iterate_lights(): global device_manager - # Print number of devices - print("found the following devices:") - for device in device_manager.devices: - print(" - {}".format(device.name)) - # Turn all lights off for device in device_manager.devices: device.fx.none() @@ -112,71 +126,122 @@ def iterate_lights(): time.sleep(1) +def list_devices(): + + print("The following devices are available:") + for device in device_manager.devices: + print(" - {}".format(device.name)) + + +def list_lightmaps(): + + global lightmap_directory + + if len(os.listdir(lightmap_directory)) > 0: + print("Available lightmaps:") + for lightmap_file in os.listdir(lightmap_directory): + print(" - {}".format(lightmap_file[:-5])) + else: + print("No lightmaps available") + + +def list_profiles(): + + global profile_directory + + if len(os.listdir(profile_directory)) > 0: + print("Available profiles:") + for profile_file in os.listdir(profile_directory): + print(" - {}".format(profile_file[:-5])) + else: + print("No profiles available") + + if __name__ == "__main__": - global device_manager, lightmap_directory - - # Initialise variables - profile_directory = "{}/.config/rzr/profiles".format(Path.home()) - lightmap_directory = "{}/.config/rzr/lightmaps".format(Path.home()) - - # Create config folders if not existent - Path(profile_directory).mkdir(parents=True, exist_ok=True) - Path(lightmap_directory).mkdir(parents=True, exist_ok=True) + global device_manager, lightmap_directory, profile_directory # Create device manager try: device_manager = DeviceManager() except Exception as e: - print("failed to load device manager: {}".format(e)) - print("is the openrazer-daemon running?") + error("failed to load device manager: {}".format(e)) + print("Is the openrazer-daemon running?") exit(1) # Parse arguments parser = argparse.ArgumentParser( - description="Set color profiles of your Razer devices." + description="Set color profiles of your Razer devices.", + formatter_class=RawTextHelpFormatter, ) parser.add_argument( - "profile", - metavar="PROFILE", + "command", + metavar="COMMAND", nargs="?", - help="the profile which shall be applied (ignored if --iterate is set)", + default="list-devices", + help="One of the following:\n list-devices - list devices (default)\n list-lightmaps - list lightmaps\n list-profiles - list available profiles\n iterate-lights - iterate lights of all devices\n - apply the given profile", ) parser.add_argument( - "-i", - "--iterate", - action="store_true", - help="iterate through all Razer devices and turn on one light after another", + "-ld", + "--lightmap-directory", + default="{}/.config/rzr/lightmaps".format(Path.home()), + help="Path to directory with lightmaps (default: ~/.config/rzr/lightmaps)", + ) + parser.add_argument( + "-pd", + "--profile-directory", + default="{}/.config/rzr/profiles".format(Path.home()), + help="Path to directory with profiles (default: ~/.config/rzr/profiles)", ) args = parser.parse_args() # Print greeter - print("rzr") + print_greeter() - if args.iterate: + # Check if directories exist + directories_available = True + lightmap_directory = args.lightmap_directory + profile_directory = args.profile_directory + + if not os.path.exists(lightmap_directory): + error( + "lightmap directory '{}' doesn't exist, please create it".format( + lightmap_directory + ) + ) + directories_available = False + if not os.path.exists(profile_directory): + error( + "profile directory '{}' doesn't exist, please create it".format( + profile_directory + ) + ) + directories_available = False + if not directories_available: + exit(1) + + # Execute command + if args.command == "list-devices": + list_devices() + elif args.command == "list-lightmaps": + list_lightmaps() + elif args.command == "list-profiles": + list_profiles() + elif args.command == "iterate-lights": iterate_lights() - elif args.profile: + elif args.command: # Load profile try: - profile = toml.load("{}/{}.toml".format(profile_directory, args.profile)) + profile = toml.load("{}/{}.toml".format(profile_directory, args.command)) except FileNotFoundError: - print("the profile '{}' doesn't exist".format(args.profile)) - if len(os.listdir(profile_directory)) > 0: - print("found the following profiles:") - for profile_file in os.listdir(profile_directory): - print(" - {}".format(profile_file[:-5])) - else: - print("found no profiles") + error("the profile '{}' doesn't exist".format(args.command)) + list_profiles() exit(1) except Exception as e: print(type(e)) - print("error while loading profile: {}".format(args.profile)) + error("couldn't load profile '{}': {}".format(args.command, e)) exit(1) for device in profile: apply_lightmap(profile[device]) - print("profile '{}' applied".format(args.profile)) - - else: - print("either set a profile or the --iterate flag") - exit(1) + print("Profile '{}' applied".format(args.command))