diff --git a/qutebrowser/utils/standarddir.py b/qutebrowser/utils/standarddir.py index b82845a96..0d64452d0 100644 --- a/qutebrowser/utils/standarddir.py +++ b/qutebrowser/utils/standarddir.py @@ -38,6 +38,7 @@ class _Location(enum.Enum): APPNAME = 'qutebrowser' +CONFIG_LOCATIONS = [os.path.normpath(os.getenv('XDG_CONFIG_HOME', '~/.config')) + '/', '~/.'] class EmptyValueError(Exception): @@ -82,7 +83,7 @@ def _init_config(args: Optional[argparse.Namespace]) -> None: if utils.is_mac: path = _from_args(typ, args) if path is None: # pragma: no branch - path = os.path.expanduser('~/.' + APPNAME) + path = _get_config_location() _create(path) _locations[_Location.config] = path @@ -93,6 +94,18 @@ def _init_config(args: Optional[argparse.Namespace]) -> None: _locations[_Location.config_py] = config_py_file +def _get_config_location() -> str: + """Get the optimal location for the config directory on disk. + + Searches CONFIG_LOCATIONS in order for existing config folder, defaults to + CONFIG_LOCATIONS[0] + """ + it = (p + APPNAME for p in CONFIG_LOCATIONS if + os.path.exists(os.path.expanduser(p + APPNAME))) + path = next(it, CONFIG_LOCATIONS[0] + APPNAME) + return os.path.expanduser(path) + + def config(auto: bool = False) -> str: """Get the location for the config directory. diff --git a/tests/unit/utils/test_standarddir.py b/tests/unit/utils/test_standarddir.py index 96bcbcf4c..d21ecc808 100644 --- a/tests/unit/utils/test_standarddir.py +++ b/tests/unit/utils/test_standarddir.py @@ -74,7 +74,7 @@ def test_unset_organization_no_qapp(monkeypatch): @pytest.mark.posix def test_fake_mac_config(tmp_path, fake_home_envvar): """Test standardir.config on a fake Mac.""" - expected = str(tmp_path) + '/.qute_test' # always with / + expected = str(tmp_path) + '/.config/qute_test' # always with / standarddir._init_config(args=None) assert standarddir.config() == expected @@ -220,7 +220,7 @@ class TestStandardDir: (standarddir.data, 2, ['Application Support', APPNAME]), (lambda: standarddir.config(auto=True), 1, [APPNAME]), (standarddir.config, 0, - os.path.expanduser('~').split(os.sep) + ['.qute_test']), + os.path.expanduser('~').split(os.sep) + ['.config'] + ['qute_test']), (standarddir.cache, 2, ['Caches', APPNAME]), (standarddir.download, 1, ['Downloads']), ])