cthoyt / pystow

👜 Easily pick a place to store data for your Python code.
https://pystow.readthedocs.io
MIT License
36 stars 6 forks source link

Need to create a config file before it can be written to using write_config #57

Closed sgbaird closed 1 year ago

sgbaird commented 1 year ago

Related: https://github.com/cthoyt/zenodo-client/issues/6

https://github.com/cthoyt/pystow/blob/ecbe7ea9f044602be12fff1d9574071403bc900c/src/pystow/config_api.py#L151-L164

I needed to call cfp.add_section(module) before I could use cfp.set(module, key, value) given the file doesn't already exist.

cthoyt commented 1 year ago

Good catch, there are a few things we could do here:

  1. Split the logic with a conditional like if path.is_file(): ...
  2. Directly add in cfp.add_section(module) between line 161 and 162
  3. something else?

So you're saying you edited the code and option 2 worked for you?

sgbaird commented 1 year ago

@cthoyt cfp.add_section(module) works if the section didn't already exist, but throws an error if the section already exists (IIRC).

cthoyt commented 1 year ago

I figured this out the hard way ;) so I added a check for non-existence.

sgbaird commented 1 year ago

For provenance, here's some code I used to create the file (and folder) in the correct location if it doesn't exist.

from os import path
from pathlib import Path
ini_dir = path.join(path.abspath(path.expanduser("~")), ".config")
ini_path = path.join(ini_dir, "zenodo.ini")
Path(ini_dir).mkdir(exist_ok=True, parents=True)
Path(ini_path).touch()
cthoyt commented 1 year ago

Are you saying there's still a persisting issue? This issue has been addressed, as far as I remember

sgbaird commented 1 year ago

I was getting the behavior of the files confused with something else. I don't think it's an issue anymore.