# HG changeset patch # User Franz Glasner # Date 1737482222 -3600 # Node ID 3a85f7bbe0b11a9890c98ef7531304295f2ae4c3 # Parent b9b38584919b31307f9b127d01ac1937265ee1c9 Common static method for some alternative encodings diff -r b9b38584919b -r 3a85f7bbe0b1 cutils/util/walk.py --- a/cutils/util/walk.py Tue Jan 21 14:51:58 2025 +0100 +++ b/cutils/util/walk.py Tue Jan 21 18:57:02 2025 +0100 @@ -99,14 +99,7 @@ """ if self._alt_fsname is _notset: - if PY2: - if isinstance(self._name, bytes): - self._alt_fsname = self._name - else: - self._alt_fsname = self._name.encode( - _FSENCODING, "backslashreplace") - else: - self._alt_fsname = os.fsencode(self._name) + self._alt_fsname = WalkDirEntry.alt_fs(self._name) return self._alt_fsname @property @@ -133,12 +126,16 @@ :rtype: bytes """ + return WalkDirEntry.alt_fs(self._path) + + @staticmethod + def alt_fs(what): if PY2: - if isinstance(self._path, bytes): - return self._path - return self._path.encode(_FSENCODING, "backslashreplace") + if isinstance(what, bytes): + return what + return what.encode(_FSENCODING, "backslashreplace") else: - return os.fsencode(self._path) + return os.fsencode(what) @property def uname(self): @@ -201,39 +198,27 @@ @property def alt_u8name(self): if self._alt_u8name is _notset: - if PY2: - if isinstance(self._name, bytes): - try: - self._alt_u8name = ( - self._name - .decode(_FSENCODING, "strict") - .encode("utf-8", "strict")) - except UnicodeError: - self._alt_u8name = ( - self.surrogate_decode(self._name) - .encode("ascii", "backslashreplace")) - else: - self._alt_u8name = self._name.encode( - "ascii", "backslashreplace") - else: - self._alt_u8name = self._name.encode( - "utf-8", "backslashreplace") + self._alt_u8name = WalkDirEntry.alt_u8(self._name) return self._alt_u8name @property def alt_u8path(self): + return WalkDirEntry.alt_u8(self._path) + + @staticmethod + def alt_u8(what): if PY2: - if isinstance(self._path, bytes): + if isinstance(what, bytes): try: - return (self._path.decode(_FSENCODING, "strict") + return (what.decode(_FSENCODING, "strict") .encode("utf-8", "strict")) except UnicodeError: - return (self.surrogate_decode(self._path) + return (WalkDirEntry.surrogate_decode(what) .encode("ascii", "backslashreplace")) else: - return self._path.encode("ascii", "backslashreplace") + return what.encode("ascii", "backslashreplace") else: - return self._path.encode("utf-8", "backslashreplace") + return what.encode("utf-8", "backslashreplace") @property def is_symlink(self):