ssato / python-anyconfig

Python library provides common APIs to load and dump configuration files in various formats
MIT License
277 stars 31 forks source link

Allow open from a pathlib #84

Closed gsemet closed 5 years ago

gsemet commented 6 years ago

Hello. Can we have support for pathlib in the load and dump api?

from pathlib import Path
import anyconfig
content = anyconfig.load(Path("path") / "to" / "my" / "json.json")
ssato commented 6 years ago

How about this although a little bit long ?

content = anyconfig.load((pathlib.Path("path") / "to" / "my" / "json.json").open(), ac_parser="json")

To support pathlib (direct passing of pathlib.Path objects) needs some work and may not be easy, I think. At least, I have to keep compatibility with older python versions w/o pathlib somehow. (I opened another issue for this enhancement as #85.)

ssato commented 6 years ago

FYI. pathlib support was into the git HEAD (master branch).

ssato commented 6 years ago

FYI. I released 0.9.6 and it includes this feature.

>>> import pathlib
>>> import anyconfig
>>> anyconfig.VERSION
'0.9.6'
>>> p = pathlib.Path("/var/lib/dnf/groups.json")
>>> x = anyconfig.load(p)
>>> x.keys()
dict_keys(['ENVIRONMENTS', 'GROUPS', 'meta'])
>>> x["meta"]
{'version': '0.6.0'}
>>>
ssato commented 5 years ago

I'll close this as the fixes are merged. Please let me know if you have any further issues on this.

Thanks again for your report!