polib icon indicating copy to clipboard operation
polib copied to clipboard

Add support for pathlib

Open pauloxnet opened this issue 3 years ago • 4 comments

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

pauloxnet avatar Nov 07 '21 10:11 pauloxnet

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?

kthy avatar Nov 08 '21 08:11 kthy

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.

pauloxnet avatar Nov 08 '21 09:11 pauloxnet

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 mention pathlib support, as open already supports pathlib)
  • 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.

izimobil avatar Nov 08 '21 13:11 izimobil

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.

tosky avatar Apr 10 '22 22:04 tosky