diff cutils/util/walk.py @ 203:3a85f7bbe0b1

Common static method for some alternative encodings
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 21 Jan 2025 18:57:02 +0100
parents 58d93453c307
children ca9d5a0dc9bb
line wrap: on
line diff
--- 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):