Page MenuHomeDevCentral

Provide configuration paths
ClosedPublic

Authored by dereckson on Sun, Apr 19, 09:07.

Details

Summary

A common question is how to parse a configuration file
in /usr/local/etc vs /etc, or in current vs home vs system dir.

The function get_configuration_paths is intended to offer the four
solutions. Script is expected to remove the non relevant ones.

The try/except block and the or {} allows to silently ignore
configuration error. They need to be removed when config is mandatory.

Test Plan

With a .foo.conf file containing alpha:\n foo: 42:

$ python3
>>> import config
>>> config.parse_configuration()
{'alpha': {'foo': 42}}

Diff Detail

Repository
rSNIPPETS Snippets
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

dereckson created this revision.
This revision is now accepted and ready to land.Sun, Apr 19, 13:53
yousra requested changes to this revision.Sun, Apr 19, 14:17

@dereckson I think it is better to use os.path.expanduser to resolve the user configuration path instead of relying on $HOME, as it provides a more robust and portable way to determine the user’s home directory across environments.

https://www.geeksforgeeks.org/python/python-os-path-expanduser-method/

This revision now requires changes to proceed.Sun, Apr 19, 14:17

Use os.path.expanduser

>>> import os
>>> get_configuration_paths()
['.foo.conf', '/home/dereckson/.config/foo.conf', '/usr/local/etc/foo.conf', '/etc/foo.conf']
This revision is now accepted and ready to land.Sun, Apr 19, 17:12
This revision was automatically updated to reflect the committed changes.