Skip to content

Commit

Permalink
Add load from user config in XDG_CONFIG_HOME if available (#672)
Browse files Browse the repository at this point in the history
* 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: #672 (comment)

* Update configuration options in README

See also: #672
  • Loading branch information
braun-steven committed Apr 3, 2024
1 parent 215026a commit 4cdcf74
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@ See [`--force` flag](#overriding-governor) for more info.
You can configure separate profiles for the battery and power supply. These profiles will let you pick which governor to use, as well as how and when turbo boost is enabled. The possible values for turbo boost behavior are `always`, `auto`, and `never`. The default behavior is `auto`, which only activates turbo during high load.
By default, auto-cpufreq does not use the config file! If you wish to use it, the location where it needs to be placed to be read automatically is: `/etc/auto-cpufreq.conf`
By default, auto-cpufreq does not use a config file. If you wish to configure auto-cpufreq statically, we look for a configuration file in the following order:
1. Commandline argument: `--config <FILE>` if passed as commandline argument to `auto-cpufreq`
2. User-specific configuration: `$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`
3. System-wide configuration: `/etc/auto-cpufreq.conf`
#### Example config file contents
```python
Expand Down
8 changes: 5 additions & 3 deletions auto_cpufreq/bin/auto_cpufreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@click.option(
"--config",
is_flag=False,
default="/etc/auto-cpufreq.conf",
required=False,
help="Use config file at defined path",
)
@click.option("--debug", is_flag=True, help="Show debug info (include when submitting bugs)")
Expand All @@ -41,8 +41,10 @@ def main(config, daemon, debug, update, install, remove, live, log, monitor, sta

# display info if config file is used
def config_info_dialog():
if get_config(config) and hasattr(get_config, "using_cfg_file"):
print("\nUsing settings defined in " + config + " file")

config_file = find_config_file(config)
if get_config(config_file) and hasattr(get_config, "using_cfg_file"):
print("\nUsing settings defined in " + config_file + " file")

# set governor override unless None or invalid
if force is not None:
Expand Down
34 changes: 34 additions & 0 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,40 @@ def file_stats():
auto_cpufreq_stats_file = open(auto_cpufreq_stats_path, "w")
sys.stdout = auto_cpufreq_stats_file


def find_config_file(args_config_file):
"""
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
home = os.getenv("HOME")
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
# Check if the config file path points to a valid file
if os.path.isfile(args_config_file):
return args_config_file
else:
# Not a valid file
print(f"Config file specified with '--config {args_config_file}' not found.")
sys.exit(1)
elif os.path.isfile(os.path.join(user_config_file)): # (2) User config file
return user_config_file
else: # (3) System config file (default if nothing else is found)
return system_config_file


def get_config(config_file=""):
if not hasattr(get_config, "config"):
get_config.config = configparser.ConfigParser()
Expand Down

0 comments on commit 4cdcf74

Please sign in to comment.