polib
polib copied to clipboard
Add support for pathlib
It would be great to add the ability to read file path as pathlib.Path
.
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
?
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.
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:
- I don't think there's any need to document this (I doubt every single python library that uses builtin
open
function, will mentionpathlib
support, asopen
already supportspathlib
) - A unit test wouldn't hurt maybe (for example if in the future polib makes decision based on the type of given path) but it's not really necessary as for now.
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.