izimobil / polib

Pure python library to manipulate, create, modify gettext files (pot, po and mo files).
MIT License
100 stars 28 forks source link

Add support for pathlib #114

Open pauloxnet opened 2 years ago

pauloxnet commented 2 years ago

It would be great to add the ability to read file path as pathlib.Path.

kthy commented 2 years ago

I just tried this:

import pathlib
import polib
pth = pathlib.Path(r"C:\whatever\sample.po")
pofile = polib.pofile(pth)
for entry in pofile:
    print(entry.msgid, entry.msgstr)

Worked fine in Python 3.9. Is there anything you've found to not Just Work™ with pathlib?

pauloxnet commented 2 years ago

I just tried this:

import pathlib
import polib
pth = pathlib.Path(r"C:\whatever\sample.po")
pofile = polib.pofile(pth)
for entry in pofile:
    print(entry.msgid, entry.msgstr)

Good to know, this can be the first test to add.

Worked fine in Python 3.9. Is there anything you've found to not Just Work™ with pathlib?

There's no info about pathlib in the README or in the docs and there aren't tests that can guarantee that polib will works with pathlib in the future.

izimobil commented 2 years ago

AFAIK, pathlib is just for manipulating pathes in an object oriented way, in the end it is evaluated as a string, and it is backward compatible with the os.path module.

So:

tosky commented 2 years ago

Hi, I think there is just a use case why to officially document the support: right now the (3rd-party, but very useful) types-polib library (https://pypi.org/project/types-polib/) follows the official definition and when a type checker like mypy is executed against a a simple code like potfile.save(pathlib_obj) where pathlib_obj is a pathlib.Path, the check fails. If the pathlib support was officially documented, it would be possible to ask types-polib maintainers to declare the support without having to exclude polib+pathlib usages from type checkers. I'm not sure types-polib would accept a change like this without official support, but to be honest I haven't asked.