def get_computers(): computers = [] path = Paths()._computers_configuration_dir for file_name in os.listdir(path): if file_name.endswith(".ini"): file_path = path + "/" + file_name computer = get_computer_by_path(file_path) if not computer is None: add = True for added_computer in computers: if computer.NAME == added_computer: print_warning("Computer name already exists={}".format( computer.NAME)) add = False break if add: computers.append(computer) computers.sort(key=lambda computer: computer.NAME) return computers
def set_color(self, color, side): if side == 'left': self._left_color = color elif side == 'right': self._right_color = color else: print_warning("wrong side=`{}`", side)
def indicator_init(self, uri): try: self._indicator_pyro = Pyro4.Proxy(str(uri)) self.reload_configurations(self._user) except Exception: print_warning("Failed initialization") print(format_exc()) self._indicator_pyro = False
def get_computers(): computers = [] path = Paths().COMPUTERS_CONFIGURATION_FOLDER for file_name in os.listdir(path): if file_name.endswith(".ini"): file_path = path + "/" + file_name print_debug("Reading {}".format(file_path)) computer = Computer() config = ConfigParser() config.optionxform = str config.read(file_path) for key in config["COMMON"]: if hasattr(computer, key): value = config["COMMON"][key] if value != "": if key in ("NAME", "DEFAULT_COLOR", "DEFAULT_MODE"): setattr(computer, key, value) else: setattr(computer, key, int(value)) for section in config.sections(): if section.startswith("REGION"): region = Region(config[section]["ID"], config[section]["DESCRIPTION"], int(config[section]["BLOCK"]), int(config[section]["SUPPORTED_COMMANDS"]), config[section]["CAN_BLINK"] == "True", config[section]["CAN_MORPH"] == "True", config[section]["CAN_LIGHT"] == "True") computer.add_region(region) add = True for added_computer in computers: if computer.NAME == added_computer: print_warning("Computer name already exists={}".format( computer.NAME)) add = False break if add: computers.append(computer) computers.sort(key=lambda computer: computer.NAME) return computers
def set_mode(self, mode): if mode == 'fixed': self._mode = 'fixed' elif mode == 'morph': self._mode = 'morph' elif mode == 'blink': self._mode = 'blink' else: print_warning('wrong mode=`{}`'.format(mode))
def set_speed(self, speed): speed = int(speed) if speed > 255 or speed <= 0: print_warning( 'Wrong speed={}, the speed must be >= 0 and < 256.'.format( speed)) else: self._speed = speed
def set_color(self, hex_color, widget_zone): gdk_color = Gdk.RGBA() if gdk_color.parse(hex_color): if widget_zone == 'left': self._left_color = gdk_color elif widget_zone == 'right': self._right_color = gdk_color else: print_warning('wrong zone name='.format(widget_zone))
def set_default_computer(computer_name): for computer in get_computers(): if computer.NAME == computer_name: with open(_SOFTWARE_PATHS._default_computer_file, 'w') as fw: with open(computer.configuration_path, 'r') as fr: fw.write(fr.read()) print("Default configuration updated to: {}".format(computer_name)) return print_warning("Computer model '{}' not found.".format(computer_name))
def set_mode(self, mode): if mode == 'fixed': self._mode = 'fixed' self._on_command_button_click(self._commands_buttons_events[1], True) elif mode == 'morph': self._mode = 'morph' self._on_command_button_click(self._commands_buttons_events[2], True) elif mode == 'blink': self._mode = 'blink' self._on_command_button_click(self._commands_buttons_events[3], True) else: print_warning('wrong mode={}'.format(mode))
def add_region(self, new_region): for region in self.__regions: if region.name == new_region.name: print_warning("Duplicated region id={}".format(region.name)) return elif region.hex_id == new_region.hex_id: print_warning("Duplicated region block={}".format(region.hex_id)) return self.__regions.append(new_region)
def set_lights(self, user_name, state): """ Turn the lights on or off, 'state' can be a boolean or a string. """ if user_name != self._user: self.reload_configurations(user_name) print_warning(user_name) if state in (False, 'False', 'false'): areas_to_keep_on = self._ccp.get_str_defval('areas_to_keep_on', '') print_warning("AREAS TO KEEP ON") print_warning(areas_to_keep_on) if areas_to_keep_on == '': self._controller.erase_config() for save, block in self._COMPUTER_BLOCKS_TO_SAVE: self._controller.add_block_line(save, block) self._controller.add_reset_line(self._computer.RESET_ALL_LIGHTS_OFF) self._controller.apply_config() else: """ To turn off the lights but let some areas on, instead of sending the command "all the lights off", some areas are set to black color. """ areas_to_keep_on = areas_to_keep_on.split('|') print_warning(areas_to_keep_on) self._controller.erase_config() for save, block in self._COMPUTER_BLOCKS_TO_SAVE: self._controller.add_block_line(save, block) self._controller.add_reset_line(self._computer.RESET_ALL_LIGHTS_ON) self._controller.add_speed_line(1) for area in self._theme.get_areas(): if not area.name in areas_to_keep_on: for zone in area.get_zones(): self._controller.add_color_line(zone.get_hex_id(), 'fixed', '#000000', '#000000') self._controller.end_colors_line() self._controller.end_block_line() self._controller.apply_config() self._lights_state = False self._indicator_send_code(150) else: self._iluminate_keyboard()
def _command(self, command, *args): """ Send a command to the daemon and return a tuple containing (boolean, response). The boolean indicates weather the command was succesfull or not, and the response contains the information returned by the command. """ if command in ('set_profile', 'set_lights', 'switch_lights', 'reload_configurations'): args = [self._user] + list(args) if self._address is None: self.reload_address() if not self._address is None and not self._pyro is None: try: response = getattr(self._pyro, command)(*args) return response except Exception: try: # # I ignore why this has to be inside an exception. Its like if there was a problem printing format_exc(). # The bug happens when repducing the following scenario: # 1) There is a launched indicator. # 2) The software is re-installed. # 3) The user tries to close the indicator by usig the "Exit" button. # if len(args) > 0: print_error("Command={}, arguments=[{}]\n{}\n".format( command, ','.join((str(arg) for arg in args)), str(format_exc()))) else: print_error("Command={}\n{}\n".format( command, str(format_exc()))) except Exception: print_error( "NO TRACEBACK: Command={}\n{}\n".format(command)) return False else: print_warning("The daemon is off.") return False
def add_color_line(self, area_hex_id, mode, left_color, right_color=None): if mode == 'fixed': self._constructor.set_fixed_color(area_hex_id, left_color) elif mode == 'blink': self._constructor.set_blink_color(area_hex_id, left_color) elif mode == 'morph': if right_color is None: print_warning( 'trying to set `morph` mode without a `right_color`.The `fixed` mode will be used instead.' ) self._constructor.set_fixed_color(area_hex_id, left_color) else: self._constructor.set_color_morph(area_hex_id, left_color, right_color) else: print_warning('wrong mode=`{}`'.format(mode))
def set_speed(self, speed): speed = int(speed) if speed > 255: speed = 255 print_warning( "the speed can not be > 255, it will be set equal to 255.") elif speed <= 0: speed = 1 print_warning( "The speed can not be =< 0, it will be set equal to 1.") self._save_line() legend = "set_speed, speed={}\n".format(speed) cmd = copy(self._void) cmd[0] = self.computer.START_BYTE cmd[1] = self.computer.COMMAND_SET_SPEED cmd[3] = int(speed) self.append(Request(legend, cmd))
def set_speed(self, speed): speed = int(speed) if speed > 255: speed = 255 print_warning( "the speed can not be > 255, it will be set equal to 255.") elif speed < 0: speed = 0 print_warning( "The speed can not be < 0, it will be set equal to 0.") self.__save_line() legend = "set_speed, speed={}\n".format(speed) cmd = self.__get_cmd() cmd[0] = self.__computer.START_BYTE cmd[1] = self.__computer.COMMAND_SET_SPEED cmd[3] = int(speed) self.__commands.append(Command(legend, cmd))
def add_area(self, area): if not area.name in self.__areas.keys(): self.__areas[area.name] = area else: print_warning("Duplicated area `{}`, `{}`".format(area.name, self.__areas.keys()))
def __init__(self): self._paths = Paths() # Glade # builder = Gtk.Builder() builder.add_from_file(self._paths.GLADE_FILE) builder.connect_signals(self) glade_object_names = ( 'window_root', 'menubar', 'menuitem_profile', 'menuitem_options', 'checkbutton_autosave', 'checkbutton_profile_buttons', 'checkbutton_delete_warning', 'menuitem_off_areas', 'liststore_profiles', 'combobox_profiles', 'tempobutton', 'box_profile_buttons', 'horizontal_main_box', 'box_area_labels', 'box_areas', 'label_user_message', 'scrolledwindow_no_computer', 'window_new_profile', 'entry_new_profile', 'button_new_profile_create', 'window_about', 'window_computer_data', 'textbuffer_computer_data') for glade_object in glade_object_names: setattr(self, glade_object, builder.get_object(glade_object)) """ Add the accel groups """ for _id, shorcut in (('imagemenuitem_apply_configuration', 'a'), ('imagemenuitem_save', 's'), ('imagemenuitem_delete', 'd'), ('imagemenuitem_new', 'n'), ('imagemenuitem_quit', 'q'), ('button_lights_on', 'o'), ('button_lights_off', 'f'), ('imagemenuitem_export', 'e'), ('imagemenuitem_import', 'i')): imagemenuitem_apply_configuration = builder.get_object(_id) accel_group = Gtk.AccelGroup() self.window_root.add_accel_group(accel_group) imagemenuitem_apply_configuration.add_accelerator( 'activate', accel_group, ord(shorcut), Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE) """ Ask to the user if he wants to import its global configuration (this is a support for older versions of alienware-kbl) """ if (not os.path.exists(self._paths.CONFIGURATION_PATH) and os.path.exists(self._paths.BACKUP_CONFIG)) or \ (not os.path.exists(self._paths.PROFILES_PATH) and os.path.exists(self._paths.BACKUP_PROFILES)): self.window_root.hide() if gtk_dialog_question(self.window_root, TEXT_COPY_CONFIG, icon=self._paths.SMALL_ICON): from distutils.dir_util import copy_tree if not os.path.exists( os.path.dirname(self._paths.CONFIGURATION_PATH)): print_warning('Adding the configuration {}'.format( self._paths.CONFIGURATION_PATH)) os.makedirs(os.path.dirname( self._paths.CONFIGURATION_PATH)) if not os.path.exists(self._paths.PROFILES_PATH): os.makedirs(self._paths.PROFILES_PATH) shutil.copyfile(self._paths.BACKUP_CONFIG, self._paths.CONFIGURATION_PATH) copy_tree(self._paths.BACKUP_PROFILES, self._paths.PROFILES_PATH) self.window_root.show() self.apply_configuration = False self.queue_zones = [] self.ccp = CCParser(self._paths.CONFIGURATION_PATH, 'GUI Configuration') # # self.color_chooser_toolbar = ColorChooserToolbar(self) self.color_chooser_toolbar.set_orientation(Gtk.Orientation.VERTICAL) self.color_chooser_toolbar.connect("colorlist-changed", self.on_toolbar_colorlist_changed) default_toolbar_colors = self.ccp.get_list('toolbar_colors') if len(default_toolbar_colors) > 0: self.color_chooser_toolbar.set_colors(default_toolbar_colors) # Load a configuration # computer_name = AKBLConnection._command('get_computer_name') self.computer = get_computer(computer_name) Theme.LOAD_profiles(self.computer, self._paths.PROFILES_PATH) self.POPULATE_liststore_profiles() """ Extra GUI initialization """ computer_data = AKBLConnection._command('get_computer_info') self.textbuffer_computer_data.set_text( TEXT_COMPUTER_DATA.format(*computer_data[0:5])) # Add the areas to the "menuitem_off_areas" # self.menu_turn_off_areas = Gtk.Menu() self.areas_description_dict = dict( (area.description, area.name) for area in self.theme.get_areas()) active_configuration_areas = self.ccp.get_str_defval( 'areas_to_keep_on', '').split('|') for description, area in sorted(self.areas_description_dict.items(), key=lambda x: x[0]): checkbox = Gtk.CheckMenuItem(label=description) if area in active_configuration_areas: checkbox.set_active(True) checkbox.connect('activate', self.on_checkbox_turnoff_areas_changed) self.menu_turn_off_areas.append(checkbox) self.menuitem_off_areas.set_submenu(self.menu_turn_off_areas) # Extra stuff # self.horizontal_main_box.add(self.color_chooser_toolbar) self.horizontal_main_box.reorder_child(self.color_chooser_toolbar, 0) self.checkbutton_autosave.set_active( self.ccp.get_bool_defval('auto_save', True)) self.checkbutton_profile_buttons.set_active( self.ccp.get_bool_defval('profile_buttons', False)) self.checkbutton_delete_warning.set_active( self.ccp.get_bool_defval('delete_warning', True)) self.POPULATE_box_areas() self.window_root.show_all() if not self.checkbutton_profile_buttons.get_active(): self.box_profile_buttons.hide() self.scrolledwindow_no_computer.hide()
def load(self, path): print_debug('path=`{}`'.format(path)) with open(path, encoding='utf-8', mode='rt') as f: lines = f.readlines() area_found, left_color, right_color, mode, = False, False, False, False imported_areas = [] supported_region_names = self._computer.get_supported_regions_name() #print_debug('supported_region_names=`{}`'.format(supported_region_names)) # Parse the configuration file # for line in lines: if line.strip(): variables = line.strip().split('=') if len(variables) == 2: var_name = variables[0] var_arg = variables[1] if var_name == 'name': if var_arg == '': name = os.path.basename(path) else: name = var_arg if name.endswith('.cfg'): name = name[:-4] self.name = name elif var_name == 'speed': self.set_speed(var_arg) elif var_name == 'area': area_name = var_arg if area_name in supported_region_names: area_found = True imported_areas.append(area_name) region = self._computer.get_region_by_name( area_name) area = Area() area.init_from_region(region) self.add_area(area) else: area_found = False print_warning( "area.name `{}` not listed on computer regions names" .format(area_name)) elif var_name in ( 'type', 'mode'): # `type`is to give support to old themes mode = var_arg if mode not in ('fixed', 'morph', 'blink'): default_mode = self._computer.DEFAULT_MODE print_warning( 'wrong mode=`{}` when importing theme. Using default mode=`{}`' .format(mode, default_mode)) mode = default_mode elif var_name in ( 'color', 'color1', 'left_color' ): # `color` & `color1`are to give support to old themes if string_is_hex_color(var_arg): left_color = var_arg else: if area_found: area_name = area.name else: area_name = "?" print_warning( "Wrong left hex_color={} for area={}, the default hex_color= will be used instead." .format(left_color, area_name, self._computer.DEFAULT_COLOR)) left_color = self._computer.DEFAULT_COLOR elif var_name in ( 'color2', 'right_color' ): # `color2` is to give support to old themes if string_is_hex_color(var_arg): right_color = var_arg else: if area_found: area_name = area.name else: area_name = "?" print_warning( "Wrong left hex_color={} for area={}, the default hex_color={} will be used instead." .format(right_color, area_name, self._computer.DEFAULT_COLOR)) right_color = self._computer.DEFAULT_COLOR if area_found and left_color and right_color and mode: #print_debug('adding Zone to Area, mode=`{}`, left_color=`{}`, right_color=`{}`'.format(mode, left_color, right_color)) zone = Zone(mode=mode, left_color=left_color, right_color=right_color) area.add_zone(zone) left_color, right_color, mode = False, False, False # Add areas in case they be missing # for area_name in supported_region_names: if area_name not in imported_areas: region = self._computer.get_region_by_name(area_name) area = Area() area.init_from_region(region) zone = Zone(mode=self._computer.DEFAULT_MODE, left_color=self._computer.DEFAULT_COLOR, right_color=self._computer.DEFAULT_COLOR) area.add_zone(zone) self.add_area(area) print_warning("missing area.name=`{}` on theme=`{}`".format( area_name, self.name)) print_debug( 'adding Zone to the missing Area, mode=`{}`, left_color=`{}`, right_color=`{}`' .format(zone.get_mode(), zone.get_left_color(), zone.get_right_color())) print_debug(self) # # Add the configuration # AVAILABLE_THEMES[self.name] = self
def set_colors(self, mode, speed, left_colors, right_colors=None): """ Change the colors and the mode of the keyboard. + The available modes are: 'fixed', 'morph', 'blink' 'fixed' and 'blink' only takes left_colors + Speed must be an integer. 1 =< speed =< 256 + left_colors and right_colors can be a single hex color or a list of hex_colors. If both arguments are used, they must have the same number of items. """ if mode not in ('fixed', 'morph', 'blink'): print_warning("Wrong mode" + str(mode)) return elif not isinstance(speed, int): print_warning("The speed argument must be an integer.") return elif speed > 255: print_warning("The speed argument exeeds the limit > 255.") return elif speed < 1: print_warning("The speed argument must be >= 1.") return if not isinstance(left_colors, list) and not isinstance( left_colors, tuple): left_colors = [left_colors] if right_colors is None: right_colors = left_colors elif not isinstance(right_colors, list) and not isinstance( right_colors, tuple): right_colors = [right_colors] if len(left_colors) != len(right_colors): print_warning("The colors lists must have the same lenght.") return for color_list in (left_colors, right_colors): for color in color_list: if not string_is_hex_color(color): print_warning( "The colors argument must only contain hex colors. The color={} is not valid." .format(color)) return self._controller.erase_config() for save, block in self._COMPUTER_BLOCKS_TO_SAVE: self._controller.add_block_line(save, block) self._controller.add_reset_line(self._computer.RESET_ALL_LIGHTS_ON) self._controller.add_speed_line(speed) for region in self._computer.get_regions(): for i, (left_color, right_color) in enumerate( zip(left_colors, right_colors)): if i + 1 > region.max_commands: print_warning( "The number of maximum commands for the region={} have been exeed. The loop was stopped at {}." .format(region.name, i + 1)) break if mode == 'blink': if region.can_blink: self._controller.add_color_line( region.hex_id, 'blink', left_color, right_color) else: self._controller.add_color_line( region.hex_id, 'fixed', left_color) print_warning( "The mode=blink is not supported for the region={}, the mode=fixed will be used instead." .format(region.name)) elif mode == 'morph': if region.can_morph: self._controller.add_color_line( region.hex_id, 'morph', left_color, right_color) else: self._controller.add_color_line( region.hex_id, 'fixed', left_color) print_warning( "The mode=morph is not supported for the region={}, the mode=fixed will be used instead." .format(region.name)) else: self._controller.add_color_line( region.hex_id, 'fixed', left_color) self._controller.end_colors_line() self._controller.end_block_line() self._controller.apply_config() self._lights_state = True