Skip to content

Commit 07c6e68

Browse files
committed
XDG Base Directory compliance (fixes earwig#12)
1 parent bc80ab6 commit 07c6e68

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

gitup/config.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,31 @@
1313
__all__ = ["get_bookmarks", "add_bookmarks", "delete_bookmarks",
1414
"list_bookmarks"]
1515

16-
CONFIG_FILENAME = os.path.join(os.path.expanduser("~"), ".gitup")
17-
1816
YELLOW = Fore.YELLOW + Style.BRIGHT
1917
RED = Fore.RED + Style.BRIGHT
2018

2119
INDENT1 = " " * 3
2220

21+
def _get_config_path():
22+
"""Return the path to the configuration file."""
23+
xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config")
24+
return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini")
25+
2326
def _load_config_file():
2427
"""Read the config file and return a SafeConfigParser() object."""
2528
config = configparser.SafeConfigParser()
2629
# Don't lowercase option names, because we are storing paths there:
2730
config.optionxform = str
28-
config.read(CONFIG_FILENAME)
31+
config.read(_get_config_path())
2932
return config
3033

3134
def _save_config_file(config):
32-
"""Save config changes to the config file specified by CONFIG_FILENAME."""
33-
with open(CONFIG_FILENAME, "wb") as config_file:
35+
"""Save config changes to the config file returned by _get_config_path."""
36+
cfg_path = _get_config_path()
37+
if not os.path.exists(os.path.dirname(cfg_path)): # Race condition, meh...
38+
os.makedirs(os.path.dirname(cfg_path))
39+
40+
with open(cfg_path, "wb") as config_file:
3441
config.write(config_file)
3542

3643
def get_bookmarks():

0 commit comments

Comments
 (0)