Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load from user config in XDG_CONFIG_HOME if available #672

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 config file is provided as a command line argument but is invalid, exit with error message
if args_config_file is not None:
if not os.path.isfile(args_config_file):
print(f"Config file specified with '--config {args_config_file}' not found.")
sys.exit(1)

if args_config_file and os.path.isfile(args_config_file): # (1) Command line argument
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.path.isfile(args_config_file) in line 113 is redundant since it would be caught by line 109

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've merged the two conditions: 2b3e9d9

return args_config_file
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