def save_settings(self): """ Save the current configuration to a user config file """ def _save_displayed_setting(conf, submenu): items = [] if (submenu == "Graphs"): items = self.view.graphs_menu.active_sensors.items() elif (submenu == "Summaries"): items = self.view.summary_menu.active_sensors.items() for source, visible_sensors in items: section = source + "," + submenu conf.add_section(section) sources = self.sources logging.debug("Saving settings for %s", source) logging.debug("Visible sensors %s", visible_sensors) # TODO: consider changing sensors_list to dict curr_sensor = [ x for x in sources if x.get_source_name() == source ][0] sensor_list = curr_sensor.get_sensor_list() for sensor_id, sensor in enumerate(sensor_list): try: conf.set(section, sensor, str(visible_sensors[sensor_id])) except IndexError: conf.set(section, sensor, str(True)) if not user_config_dir_exists(): make_user_config_dir() conf = configparser.ConfigParser() config_file = get_user_config_file() with open(config_file, 'w') as cfgfile: conf.add_section('GraphControll') # Save the configured refresh rete conf.set('GraphControll', 'refresh', str(self.refresh_rate)) # Save the configured UTF8 setting conf.set('GraphControll', 'UTF8', str(self.smooth_graph_mode)) # Save the configured t_thresh if self.temp_thresh: conf.set('GraphControll', 'TTHRESH', str(self.temp_thresh)) _save_displayed_setting(conf, "Graphs") _save_displayed_setting(conf, "Summaries") conf.write(cfgfile)
def _load_config(self, t_thresh): """ Uses configurations defined by user to configure sources for display. This should be the only place where sources are initiated This returns a list of sources after configurations are applied """ # Load and configure user config dir when controller starts if not user_config_dir_exists(): user_config_dir = make_user_config_dir() else: user_config_dir = get_user_config_dir() if user_config_dir is None: logging.warning("Failed to find or create scripts directory,\ proceeding without scripting support") self.script_hooks_enabled = False else: self.script_loader = ScriptHookLoader(user_config_dir) # Use user config file if one was saved before self.conf = None if user_config_file_exists(): self.conf = configparser.ConfigParser() self.conf.read(get_user_config_file()) else: logging.debug("Config file not found") # Load refresh refresh rate from config try: self.refresh_rate = str( self.conf.getfloat('GraphControll', 'refresh')) logging.debug("User refresh rate: %s", self.refresh_rate) except (AttributeError, ValueError, configparser.NoOptionError, configparser.NoSectionError): logging.debug("No refresh rate configed") # Change UTF8 setting from config try: if self.conf.getboolean('GraphControll', 'UTF8'): self.smooth_graph_mode = True else: logging.debug("UTF8 selected as %s", self.conf.get('GraphControll', 'UTF8')) except (AttributeError, ValueError, configparser.NoOptionError, configparser.NoSectionError): logging.debug("No user config for utf8") # Try to load high temperature threshold if configured if t_thresh is None: try: self.temp_thresh = self.conf.get('GraphControll', 'TTHRESH') logging.debug("Temperature threshold set to %s", self.temp_thresh) except (AttributeError, ValueError, configparser.NoOptionError, configparser.NoSectionError): logging.debug("No user config for temp threshold") else: self.temp_thresh = t_thresh # This should be the only place where sources are configured possible_sources = [ TempSource(self.temp_thresh), FreqSource(), UtilSource(), RaplPowerSource(), FanSource() ] # Load sensors config if available sources = [ x.get_source_name() for x in possible_sources if x.get_is_available() ] for source in sources: try: options = list(self.conf.items(source + ",Graphs")) for option in options: # Returns tuples of values in order self.graphs_default_conf[source].append( str_to_bool(option[1])) options = list(self.conf.items(source + ",Summaries")) for option in options: # Returns tuples of values in order self.summary_default_conf[source].append( str_to_bool(option[1])) except (AttributeError, ValueError, configparser.NoOptionError, configparser.NoSectionError): logging.debug("Error reading sensors config") return possible_sources