diff --git a/auto_cpufreq/bin/auto_cpufreq.py b/auto_cpufreq/bin/auto_cpufreq.py index 2012318..ab4a8ba 100755 --- a/auto_cpufreq/bin/auto_cpufreq.py +++ b/auto_cpufreq/bin/auto_cpufreq.py @@ -22,16 +22,16 @@ from auto_cpufreq.power_helper import * @click.option("--update", is_flag=False, help="Update daemon and package for (permanent) automatic CPU optimizations", flag_value="--update") @click.option("--remove", is_flag=True, help="Remove daemon for (permanent) automatic CPU optimizations") @click.option("--force", is_flag=False, help="Force use of either \"powersave\" or \"performance\" governors. Setting to \"reset\" will go back to normal mode") -@click.option("--config", is_flag=False, required=False, help="Use config file at defined path",) +@click.option("--config", is_flag=False, required=False, help="Use config file at defined path") +@click.option("--reload", is_flag=True, help="Reload when the configuration has been modified") @click.option("--stats", is_flag=True, help="View live stats of CPU optimizations made by daemon") @click.option("--get-state", is_flag=True, hidden=True) @click.option("--completions", is_flag=False, help="Enables shell completions for bash, zsh and fish.\n Possible values bash|zsh|fish") @click.option("--debug", is_flag=True, help="Show debug info (include when submitting bugs)") @click.option("--version", is_flag=True, help="Show currently installed version") @click.option("--donate", is_flag=True, help="Support the project") -def main(monitor, live, daemon, install, update, remove, force, config, stats, get_state, completions, debug, version, donate): - # display info if config file is used - CONFIG.set_path(config) +def main(monitor, live, daemon, install, update, remove, force, config, reload, stats, get_state, completions, debug, version, donate): + CONFIG.setup(config, reload) if len(sys.argv) == 1: print("\n" + "-" * 32 + " auto-cpufreq " + "-" * 33 + "\n") @@ -54,7 +54,6 @@ def main(monitor, live, daemon, install, update, remove, force, config, stats, g print('\nNote: You can quit monitor mode by pressing "ctrl+c"') battery_setup() battery_get_thresholds() - CONFIG.notifier.start() if IS_INSTALLED_WITH_SNAP: gnome_power_detect_snap() tlp_service_detect_snap() @@ -73,14 +72,13 @@ def main(monitor, live, daemon, install, update, remove, force, config, stats, g mon_autofreq() countdown(2) except KeyboardInterrupt: break - CONFIG.notifier.stop() + CONFIG.stop_notifier() elif live: root_check() print('\nNote: You can quit live mode by pressing "ctrl+c"') time.sleep(1) battery_setup() battery_get_thresholds() - CONFIG.notifier.start() if IS_INSTALLED_WITH_SNAP: gnome_power_detect_snap() tlp_service_detect_snap() @@ -102,7 +100,7 @@ def main(monitor, live, daemon, install, update, remove, force, config, stats, g gnome_power_start_live() print() break - CONFIG.notifier.stop() + CONFIG.stop_notifier() elif daemon: root_check() file_stats() @@ -113,7 +111,6 @@ def main(monitor, live, daemon, install, update, remove, force, config, stats, g gnome_power_detect() tlp_service_detect() battery_setup() - CONFIG.notifier.start() while True: try: footer() @@ -124,7 +121,7 @@ def main(monitor, live, daemon, install, update, remove, force, config, stats, g set_autofreq() countdown(2) except KeyboardInterrupt: break - CONFIG.notifier.stop() + CONFIG.stop_notifier() elif install: root_check() if IS_INSTALLED_WITH_SNAP: diff --git a/auto_cpufreq/config/config.py b/auto_cpufreq/config/config.py index 32d2f4b..f715377 100644 --- a/auto_cpufreq/config/config.py +++ b/auto_cpufreq/config/config.py @@ -25,20 +25,27 @@ class Config: conf:ConfigParser = None file:str = "" - def __init__(self) -> None: - # check for file changes using threading - self.watch_manager: WatchManager = WatchManager() - self.notifier: ThreadedNotifier = ThreadedNotifier(self.watch_manager, ConfigEventHandler(self)) - def get_option(self, section:str, option:str) -> str: return self.conf[section][option] def has_option(self, section:str, option:str) -> bool: return self.conf.has_option(section, option) - - def set_path(self, args_config_file:str) -> None: - self.file = find_config_file(args_config_file) - print(f"Info: Using settings defined in {self.file} file") - self.watch_manager.add_watch(path.dirname(self.file), mask=MASK) + + def set_file(self, file:str): + self.file = file + print(f"Info: Using settings defined in {file} file") self.update_config() + if self.auto_reload: self.watch_manager.add_watch(path.dirname(file), mask=MASK) + + def setup(self, args_config_file:str, auto_reload:bool) -> None: + self.auto_reload = auto_reload + if self.auto_reload: # check for file changes using threading + self.watch_manager: WatchManager = WatchManager() + self.notifier: ThreadedNotifier = ThreadedNotifier(self.watch_manager, ConfigEventHandler(self)) + self.notifier.start() + + self.set_file(find_config_file(args_config_file)) + + def stop_notifier(self) -> None: + if self.auto_reload: self.notifier.stop() def update_config(self) -> None: self.conf = ConfigParser() # create new ConfigParser to prevent old data from remaining