sloria / konch

Configures your Python shell.
https://konch.readthedocs.io/
MIT License
406 stars 18 forks source link

Follow freedesktop.org spec when discovering the default configuration file #177

Closed rpdelaney closed 3 years ago

rpdelaney commented 3 years ago

From the XDG base directory spec:

$XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored. If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.

Rather than force anyone to migrate, a fallback strategy would be an option:

  1. Check $XDG_CONFIG_HOME/konchrc.default
  2. If not found, check $HOME/.config/konchrc.default
  3. If not found, check $HOME/.konchrc.default

If you would be open to a PR on this, I could maybe put something together.

sloria commented 3 years ago

I don't have any strong objections to this. Taking a peek at what I have on my macOS machine, there doesn't seem to be a lot of consistency in where config files for CLIs live:

Interestingly, click's get_app_dir utility seems to prefer ~/ to ~/.config for macOS: https://github.com/pallets/click/blob/9d8ad23f8d9f80464c6566db449611c9e9d7571a/src/click/utils.py#L375-L390

All that said, since this can be implemented without adding too much maintenance overhead, I'd review and merge a PR.

rpdelaney commented 3 years ago

appdirs handles a lot of this automagically, including cross-platform standards. The code would be simpler, since it's just user_config_dir("konch"). Is that an option, or would you prefer to keep the dependencies minimal?

there doesn't seem to be a lot of consistency

XKCD 927 comes to mind :-)

sloria commented 3 years ago

appdirs doesn't look it'll do the right thing on macOS:

https://github.com/ActiveState/appdirs/blob/1e126c5f96e688dd89454ea6463de93e2f65cacb/appdirs.py#L187-L189

We almost certainly don't want to use ~/Library/Preferences/, right? IIUC that's more for GUI Mac apps.

Weirdly enough, click's get_app_dir also won't use XDG_CONFIG_HOME or ~/.config/ on macOS: https://github.com/pallets/click/blob/a4763ef93734ccfea497ea10bf0870d588132432/src/click/utils.py#L375-L378

🤔 Is it just me, or do both those implementations look incorrect for CLIs?

rpdelaney commented 3 years ago

I can't give an informed opinion about what is appropriate in the macOS ecosystem, unfortunately. What history I can find on how appdirs came to be in its current state is a bit labyrithine as well -- there doesn't seem to be a comprehensive statement on their current understanding of the state of the world from their POV that explains the rationale behind these decisions. At least, not that I can find. But using $HOME/.config on macOS seems somehow wrong in an intangible way that I can't clearly articulate. (Maybe that means it's not really a problem though.)

Regardless, I have all my own stuff in $HOME/.config on macOS too just because so many tools I use presume you're on a *NIX type system and put things there without thinking twice about it. So for my selfish purposes, doing that here is fine with me, if that's the way you want to go.

sloria commented 3 years ago

Well, your original proposal is better than what appdirs and click are doing. Preferring $HOME/.config across platforms seems a lot better than using ~/Library/Preferences/ on macOS and C:\Documents and Settings\<username>\Application Data\<AppAuthor>\<AppName> on Windows.

Here's what flake8 does: https://github.com/PyCQA/flake8/blob/a42d8cbed40dda51c22ee80ee558b2ef60eb3806/src/flake8/options/config.py#L58-L69

Relevant docs

IIUC, your proposal is to do the same except with a fallback to ~/.konchrc.default to maintain backwards compatibility.

rpdelaney commented 3 years ago

IIUC, your proposal is to do the same except with a fallback to ~/.konchrc.default to maintain backwards compatibility.

That's the spirit anyway. Roughly:

if $XDG_CONFIG_HOME/konchrc.default exists:
    use that
else if ~/.config/konchrc.default exists
    use that
else if ~/.konchrc.default exists
    use that

If none are found and we want to create one, create it in env.get(XDG_CONFIG_HOME, ~/.config)

Edit: re- the above, I don't remember if konch creates the config for the user at any point. If not, the last bit wouldn't be needed

sloria commented 3 years ago

Yep, that logic looks right.

I don't remember if konch creates the config for the user at any point. If not, the last bit wouldn't be needed

IIRC that functionality to create the config doesn't exist.