Add the option to auto reload config file

This commit is contained in:
Angel-Karasu 2024-07-22 16:52:05 +02:00
parent c787ff4020
commit d2ca7f20c1
2 changed files with 24 additions and 20 deletions

View File

@ -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("--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("--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("--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("--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("--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("--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("--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("--version", is_flag=True, help="Show currently installed version")
@click.option("--donate", is_flag=True, help="Support the project") @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): def main(monitor, live, daemon, install, update, remove, force, config, reload, stats, get_state, completions, debug, version, donate):
# display info if config file is used CONFIG.setup(config, reload)
CONFIG.set_path(config)
if len(sys.argv) == 1: if len(sys.argv) == 1:
print("\n" + "-" * 32 + " auto-cpufreq " + "-" * 33 + "\n") 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"') print('\nNote: You can quit monitor mode by pressing "ctrl+c"')
battery_setup() battery_setup()
battery_get_thresholds() battery_get_thresholds()
CONFIG.notifier.start()
if IS_INSTALLED_WITH_SNAP: if IS_INSTALLED_WITH_SNAP:
gnome_power_detect_snap() gnome_power_detect_snap()
tlp_service_detect_snap() tlp_service_detect_snap()
@ -73,14 +72,13 @@ def main(monitor, live, daemon, install, update, remove, force, config, stats, g
mon_autofreq() mon_autofreq()
countdown(2) countdown(2)
except KeyboardInterrupt: break except KeyboardInterrupt: break
CONFIG.notifier.stop() CONFIG.stop_notifier()
elif live: elif live:
root_check() root_check()
print('\nNote: You can quit live mode by pressing "ctrl+c"') print('\nNote: You can quit live mode by pressing "ctrl+c"')
time.sleep(1) time.sleep(1)
battery_setup() battery_setup()
battery_get_thresholds() battery_get_thresholds()
CONFIG.notifier.start()
if IS_INSTALLED_WITH_SNAP: if IS_INSTALLED_WITH_SNAP:
gnome_power_detect_snap() gnome_power_detect_snap()
tlp_service_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() gnome_power_start_live()
print() print()
break break
CONFIG.notifier.stop() CONFIG.stop_notifier()
elif daemon: elif daemon:
root_check() root_check()
file_stats() file_stats()
@ -113,7 +111,6 @@ def main(monitor, live, daemon, install, update, remove, force, config, stats, g
gnome_power_detect() gnome_power_detect()
tlp_service_detect() tlp_service_detect()
battery_setup() battery_setup()
CONFIG.notifier.start()
while True: while True:
try: try:
footer() footer()
@ -124,7 +121,7 @@ def main(monitor, live, daemon, install, update, remove, force, config, stats, g
set_autofreq() set_autofreq()
countdown(2) countdown(2)
except KeyboardInterrupt: break except KeyboardInterrupt: break
CONFIG.notifier.stop() CONFIG.stop_notifier()
elif install: elif install:
root_check() root_check()
if IS_INSTALLED_WITH_SNAP: if IS_INSTALLED_WITH_SNAP:

View File

@ -25,20 +25,27 @@ class Config:
conf:ConfigParser = None conf:ConfigParser = None
file:str = "" 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 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 has_option(self, section:str, option:str) -> bool: return self.conf.has_option(section, option)
def set_path(self, args_config_file:str) -> None: def set_file(self, file:str):
self.file = find_config_file(args_config_file) self.file = file
print(f"Info: Using settings defined in {self.file} file") print(f"Info: Using settings defined in {file} file")
self.watch_manager.add_watch(path.dirname(self.file), mask=MASK)
self.update_config() 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: def update_config(self) -> None:
self.conf = ConfigParser() # create new ConfigParser to prevent old data from remaining self.conf = ConfigParser() # create new ConfigParser to prevent old data from remaining