enhance error handling

This commit is contained in:
Denis Lehmann 2021-04-28 21:31:46 +02:00
parent ac363d83aa
commit d723394529

47
bin/rzr
View file

@ -30,8 +30,10 @@ def print_logo():
print(logo) print(logo)
def error(msg): def error(msg, e=True):
print("ERROR: {}".format(msg), file=sys.stderr) print("ERROR: {}".format(msg), file=sys.stderr)
if e:
exit(1)
def get_color_tuple(color_string): def get_color_tuple(color_string):
@ -68,7 +70,6 @@ def apply_device_profile(device_profile):
) )
if not device: if not device:
error("device '{}' not available".format(device_profile["name"])) error("device '{}' not available".format(device_profile["name"]))
exit(1)
# Open lightmap # Open lightmap
try: try:
@ -79,7 +80,8 @@ def apply_device_profile(device_profile):
error( error(
"the lightmap '{}' for device '{}' doesn't exist".format( "the lightmap '{}' for device '{}' doesn't exist".format(
device_profile["lightmap"], device_profile["name"] device_profile["lightmap"], device_profile["name"]
) ),
False,
) )
list_lightmaps() list_lightmaps()
exit(1) exit(1)
@ -89,7 +91,6 @@ def apply_device_profile(device_profile):
device_profile["lightmap"], device_profile["name"], e device_profile["lightmap"], device_profile["name"], e
) )
) )
exit(1)
# Set light colors # Set light colors
try: try:
@ -104,14 +105,12 @@ def apply_device_profile(device_profile):
light, device_profile["lightmap"], device_profile["name"] light, device_profile["lightmap"], device_profile["name"]
) )
) )
exit(1)
except Exception as e: except Exception as e:
error( error(
"failed to set light '{}' for device '{}': {}".format( "failed to set light '{}' for device '{}': {}".format(
light, device_profile["name"], e light, device_profile["name"], e
) )
) )
exit(1)
# Apply light colors # Apply light colors
device.fx.advanced.draw() device.fx.advanced.draw()
@ -124,12 +123,11 @@ def apply_profile(name):
try: try:
profile = toml.load("{}/{}.toml".format(profile_directory, name)) profile = toml.load("{}/{}.toml".format(profile_directory, name))
except FileNotFoundError: except FileNotFoundError:
error("the profile '{}' doesn't exist".format(name)) error("the profile '{}' doesn't exist".format(name), False)
list_profiles() list_profiles()
exit(1) exit(1)
except Exception as e: except Exception as e:
error("couldn't load profile '{}': {}".format(name, e)) error("couldn't load profile '{}': {}".format(name, e))
exit(1)
for device in profile: for device in profile:
apply_device_profile(profile[device]) apply_device_profile(profile[device])
@ -140,20 +138,20 @@ def iterate_lights():
global device_manager global device_manager
# Turn all lights off try:
for device in device_manager.devices:
device.fx.none()
device.fx.advanced.draw()
# Iterate through all devices # Turn all lights off
for device in device_manager.devices: for device in device_manager.devices:
device.fx.none()
device.fx.advanced.draw()
# Wait five seconds # Iterate through all devices
for i in range(5, 0, -1): for device in device_manager.devices:
print("{} will be iterated in {} seconds".format(device.name, i))
time.sleep(1)
try: # Wait five seconds
for i in range(5, 0, -1):
print("{} will be iterated in {} seconds".format(device.name, i))
time.sleep(1)
# Turn on one light every second # Turn on one light every second
for i in range(device.fx.advanced.rows): for i in range(device.fx.advanced.rows):
@ -163,9 +161,8 @@ def iterate_lights():
print("{}: [{}, {}]".format(device.name, i, j)) print("{}: [{}, {}]".format(device.name, i, j))
time.sleep(1) time.sleep(1)
except Exception as e: except Exception as e:
error("failed to iterate device '{}': {}".format(device.name, e)) error("failed to iterate device '{}': {}".format(device.name, e))
exit(1)
def list_devices(): def list_devices():
@ -236,7 +233,7 @@ if __name__ == "__main__":
try: try:
device_manager = DeviceManager() device_manager = DeviceManager()
except Exception as e: except Exception as e:
error("failed to load device manager: {}".format(e)) error("failed to load device manager: {}".format(e), False)
print("Is the openrazer-daemon running?") print("Is the openrazer-daemon running?")
exit(1) exit(1)
@ -246,14 +243,14 @@ if __name__ == "__main__":
profile_directory = args.profile_directory profile_directory = args.profile_directory
if not os.path.exists(lightmap_directory): if not os.path.exists(lightmap_directory):
error("lightmap directory '{}' doesn't exist".format(lightmap_directory)) error("lightmap directory '{}' doesn't exist".format(lightmap_directory), False)
create = input("Create the directory? [y/N] ") create = input("Create the directory? [y/N] ")
if create in ["y", "Y"]: if create in ["y", "Y"]:
Path(lightmap_directory).mkdir(parents=True, exist_ok=True) Path(lightmap_directory).mkdir(parents=True, exist_ok=True)
else: else:
directories_available = False directories_available = False
if not os.path.exists(profile_directory): if not os.path.exists(profile_directory):
error("profile directory '{}' doesn't exist".format(profile_directory)) error("profile directory '{}' doesn't exist".format(profile_directory), False)
create = input("Create the directory? [y/N] ") create = input("Create the directory? [y/N] ")
if create in ["y", "Y"]: if create in ["y", "Y"]:
Path(profile_directory).mkdir(parents=True, exist_ok=True) Path(profile_directory).mkdir(parents=True, exist_ok=True)