bobfang1992 / pytomlpp

A python wrapper for tomlplusplus
https://bobfang1992.github.io/pytomlpp/pytomlpp.html
MIT License
86 stars 11 forks source link

Add optional encoding parameter to load/dump functions #76

Closed Backist closed 7 months ago

Backist commented 7 months ago

Hello, I was working with this module and I found that it is not possible to specify neither for load nor for dump with which enconding Python should open a file. In the current version, enconding cannot be specified, forcing the user to open a file with the system specific encoding, in this case, locale.getpreferredencoding(False), which is what builtins.open uses when no encoding is specified. What happens here is that the system encoding is not always used, as was my case.

So, could you optionally add an encoding parameter for both functions? I leave here the modification I had to make to the _io.py file.

def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: Optional[str] = "utf-8") -> None:
    """Serialise data to TOML file.

    Args:
        data (Dict[Any, Any]): input data
        fl (FilePathOrObject): file like object or path
        mode (str, optional): mode to write the file, support "w", "wt" (text) or "wb" (binary). Defaults to "w".
        encoding (str): defaults to utf-8, if None, local encoding is selected.
    """
    data = _impl.dumps(data)
    if mode == "wb":
        data = data.encode("utf-8")
    if hasattr(fl, "write"):
        fl.write(data)
        return
    with open(fl, mode=mode, encoding=encoding or None) as fh:
        fh.write(data)
def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = "utf-8") -> Dict[Any, Any]:
    """Deserialise from TOML file to python dict.

    Args:
        fl (FilePathOrObject): file like object or path.
        mode (str, optional): mode to read the file, support "r", "rt" (text) or "rb" (binary). Defaults to "r".

    Returns:
        Dict[Any, Any]: deserialised data.
    """

    if hasattr(fl, "read"):
        data = fl.read()
    else:
        with open(fl, mode=mode, encoding=encoding or None) as fh:
            data = fh.read()
    if isinstance(data, bytes):
        return _impl.loads(data.decode("utf-8"))
    return _impl.loads(data)

It is a simple and easy modification but very necessary to be able to open files with the desired encoding, which is not always the one used by the system by default.