auto-cpufreq/auto_cpufreq/config/config.py

68 lines
2.8 KiB
Python
Raw Normal View History

import os, pyinotify, sys
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
from configparser import ConfigParser, ParsingError
from subprocess import run, PIPE
from auto_cpufreq.config.config_event_handler import ConfigEventHandler
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
def find_config_file(args_config_file) -> str:
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
"""
Find the config file to use.
Look for a config file in the following priorization order:
1. Command line argument
2. User config file
3. System config file
:param args_config_file: Path to the config file provided as a command line argument
:return: The path to the config file to use
"""
# Prepare paths
# use $SUDO_USER or $USER to get home dir since sudo can't access
# user env vars
home = run(["getent passwd ${SUDO_USER:-$USER} | cut -d: -f6"],
shell=True,
stdout=PIPE,
universal_newlines=True
).stdout.rstrip()
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
user_config_dir = os.getenv("XDG_CONFIG_HOME", default=os.path.join(home, ".config"))
user_config_file = os.path.join(user_config_dir, "auto-cpufreq/auto-cpufreq.conf")
system_config_file = "/etc/auto-cpufreq.conf"
if args_config_file is not None: # (1) Command line argument was specified
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
# Check if the config file path points to a valid file
if os.path.isfile(args_config_file): return args_config_file
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
else:
# Not a valid file
print(f"Config file specified with '--config {args_config_file}' not found.")
sys.exit(1)
elif os.path.isfile(user_config_file): return user_config_file # (2) User config file
else: return system_config_file # (3) System config file (default if nothing else is found)
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
class _Config:
def __init__(self) -> None:
self.path: str = ""
self._config: ConfigParser = ConfigParser()
self.watch_manager: pyinotify.WatchManager = pyinotify.WatchManager()
self.config_handler = ConfigEventHandler(self)
# check for file changes using threading
self.notifier: pyinotify.ThreadedNotifier = pyinotify.ThreadedNotifier(self.watch_manager, self.config_handler)
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
def set_path(self, path: str) -> None:
self.path = path
mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
self.watch_manager.add_watch(os.path.dirname(path), mask=mask)
if os.path.isfile(path): self.update_config()
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
def has_config(self) -> bool: return os.path.isfile(self.path)
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
def get_config(self) -> ConfigParser: return self._config
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
def update_config(self) -> None:
# create new ConfigParser to prevent old data from remaining
self._config = ConfigParser()
try: self._config.read(self.path)
except ParsingError as e: print(f"The following error occured while parsing the config file: \n{repr(e)}")
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
config = _Config()