예제 #1
0
파일: s_tui.py 프로젝트: tim77/s-tui
def main():
    args = get_args()
    # Print version and exit
    if args.version:
        print(VERSION_MESSAGE)
        sys.exit(0)

    # Setup logging util
    log_file = DEFAULT_LOG_FILE
    if args.debug_run:
        args.debug = True
    if args.debug or args.debug_file is not None:
        level = logging.DEBUG
        if args.debug_file is not None:
            log_file = args.debug_file
        log_formatter = logging.Formatter(
            "%(asctime)s [%(funcName)s()] [%(levelname)-5.5s]  %(message)s")
        root_logger = logging.getLogger()
        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(log_formatter)
        root_logger.addHandler(file_handler)
        root_logger.setLevel(level)
    else:
        level = logging.ERROR
        log_formatter = logging.Formatter(
            "%(asctime)s [%(funcName)s()] [%(levelname)-5.5s]  %(message)s")
        root_logger = logging.getLogger()
        root_logger.setLevel(level)

    if args.terminal or args.json:
        logging.info("Printing single line to terminal")
        sources = [
            FreqSource(),
            TempSource(),
            UtilSource(),
            RaplPowerSource(),
            FanSource()
        ]
        if args.terminal:
            output_to_terminal(sources)
        elif args.json:
            output_to_json(sources)

    global graph_controller
    graph_controller = GraphController(args)
    graph_controller.main()
예제 #2
0
    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