def save_settings(self, w=None): """ Save the current configuration to a user config file """ 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') conf.set('GraphControll', 'refresh', str(self.controller.refresh_rate)) conf.set('GraphControll', 'UTF8', str(self.controller.smooth_graph_mode)) for graph_name in self.available_graphs.keys(): try: if graph_name in self.visible_graphs: conf.set('GraphControll', graph_name, 'True') else: conf.set('GraphControll', graph_name, 'False') except (AttributeError, configparser.NoOptionError, configparser.NoSectionError): pass # Writing temp sensor conf.add_section('TempControll') if self.controller.custom_temp is not None: logging.debug("Custom temp sensor is " + self.controller.custom_temp) try: conf.set('TempControll', 'sensor', self.controller.custom_temp) except (AttributeError, configparser.NoOptionError, configparser.NoSectionError): pass if self.controller.temp_thresh is not None: logging.debug("Custom temp threshold set to " + str(self.controller.temp_thresh)) try: conf.set('TempControll', 'threshold', self.controller.temp_thresh) except (AttributeError, configparser.NoOptionError, configparser.NoSectionError): pass conf.write(cfgfile)
def __init__(self, args): # 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() self.script_hooks_enabled = True 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") # Set refresh rate accorrding to user config self.refresh_rate = '2.0' try: self.refresh_rate = str(self.conf.getfloat( 'GraphControll', 'refresh')) logging.debug("User refresh rate: " + str(self.refresh_rate)) except (AttributeError, ValueError, configparser.NoOptionError, configparser.NoSectionError): logging.debug("No refresh rate configed") # Set initial smooth graph state according to user config self.smooth_graph_mode = False try: if self.conf.getboolean('GraphControll', 'UTF8'): self.smooth_graph_mode = True else: logging.debug("UTF8 selected as " + self.conf.get('GraphControll', 'UTF8')) except (AttributeError, ValueError, configparser.NoOptionError, configparser.NoSectionError): logging.debug("No user config for utf8") self.temp_thresh = args.t_thresh # Try to load high temperature threshold if configured if args.t_thresh is None: try: self.temp_thresh = self.conf.get('TempControll', 'threshold') logging.debug("Temperature threshold set to " + str(self.temp_thresh)) except (AttributeError, ValueError, configparser.NoOptionError, configparser.NoSectionError): logging.debug("No user config for temp threshold") # Needed for use in view self.args = args self.animate_alarm = None self.terminal = args.terminal self.json = args.json self.mode = GraphMode() self.handle_mouse = not args.no_mouse self.stress_start_time = 0 self.stress_time = 0 self.view = GraphView(self) # use the first mode (no stress) as the default mode = self.get_modes()[0] self.mode.set_mode(mode) # update the view self.view.on_mode_change(mode) self.view.update_displayed_information() # Update csv file to save self.csv_file = None self.save_csv = args.csv if args.csv_file is not None: self.csv_file = args.csv_file logging.info("Printing output to csv " + self.csv_file) elif args.csv_file is None and args.csv: self.csv_file = DEFAULT_CSV_FILE