From 4cd6df86cfddd26fe7783cdc20c3b4b699d42e48 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 19 Apr 2020 21:01:04 +0100 Subject: [PATCH 1/2] Portable CPath fixes https://github.com/karlicoss/HPI/issues/28 --- my/kython/kompress.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/my/kython/kompress.py b/my/kython/kompress.py index 3d43f76..f958cf4 100644 --- a/my/kython/kompress.py +++ b/my/kython/kompress.py @@ -1,7 +1,8 @@ """ Various helpers for compression """ -from pathlib import Path, PosixPath +import pathlib +from pathlib import Path from typing import Union PathIsh = Union[Path, str] @@ -33,8 +34,21 @@ def kopen(path: PathIsh, *args, **kwargs): # TODO is it bytes stream?? return pp.open(*args, **kwargs) -class CPath(PosixPath): +import typing +import os + +if typing.TYPE_CHECKING: + # otherwise mypy can't figure out that BasePath is a type alias.. + BasePath = pathlib.Path +else: + BasePath = pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath + + +class CPath(BasePath): """ + Hacky way to support compressed files. + If you can think of a better way to do this, please let me know! https://github.com/karlicoss/HPI/issues/20 + Ugh. So, can't override Path because of some _flavour thing. Path only has _accessor and _closed slots, so can't directly set .open method _accessor.open has to return file descriptor, doesn't work for compressed stuff. From ab61a957013ca33e0cf9e348b38a9fab7e494921 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 19 Apr 2020 21:48:16 +0100 Subject: [PATCH 2/2] specify encoding for uncompressed files in kompress.kopen --- my/kython/kompress.py | 1 + 1 file changed, 1 insertion(+) diff --git a/my/kython/kompress.py b/my/kython/kompress.py index f958cf4..3077e71 100644 --- a/my/kython/kompress.py +++ b/my/kython/kompress.py @@ -31,6 +31,7 @@ def kopen(path: PathIsh, *args, **kwargs): # TODO is it bytes stream?? elif suf in {'.zstd'}: return _zstd_open(pp) else: + kwargs['encoding'] = 'utf-8' return pp.open(*args, **kwargs)