Mercurial > hgrepos > Python > apps > py-cutils
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 202:b9b38584919b | 203:3a85f7bbe0b1 |
|---|---|
| 97 | 97 |
| 98 :rtype: bytes | 98 :rtype: bytes |
| 99 | 99 |
| 100 """ | 100 """ |
| 101 if self._alt_fsname is _notset: | 101 if self._alt_fsname is _notset: |
| 102 if PY2: | 102 self._alt_fsname = WalkDirEntry.alt_fs(self._name) |
| 103 if isinstance(self._name, bytes): | |
| 104 self._alt_fsname = self._name | |
| 105 else: | |
| 106 self._alt_fsname = self._name.encode( | |
| 107 _FSENCODING, "backslashreplace") | |
| 108 else: | |
| 109 self._alt_fsname = os.fsencode(self._name) | |
| 110 return self._alt_fsname | 103 return self._alt_fsname |
| 111 | 104 |
| 112 @property | 105 @property |
| 113 def fspath(self): | 106 def fspath(self): |
| 114 """Always bytes. | 107 """Always bytes. |
| 131 """Alternative and "escaped" filesystem path -- always bytes. | 124 """Alternative and "escaped" filesystem path -- always bytes. |
| 132 | 125 |
| 133 :rtype: bytes | 126 :rtype: bytes |
| 134 | 127 |
| 135 """ | 128 """ |
| 136 if PY2: | 129 return WalkDirEntry.alt_fs(self._path) |
| 137 if isinstance(self._path, bytes): | 130 |
| 138 return self._path | 131 @staticmethod |
| 139 return self._path.encode(_FSENCODING, "backslashreplace") | 132 def alt_fs(what): |
| 140 else: | 133 if PY2: |
| 141 return os.fsencode(self._path) | 134 if isinstance(what, bytes): |
| 135 return what | |
| 136 return what.encode(_FSENCODING, "backslashreplace") | |
| 137 else: | |
| 138 return os.fsencode(what) | |
| 142 | 139 |
| 143 @property | 140 @property |
| 144 def uname(self): | 141 def uname(self): |
| 145 """Always "real", strictly encoded Unicode or `None` if this is not | 142 """Always "real", strictly encoded Unicode or `None` if this is not |
| 146 possible. | 143 possible. |
| 199 return p if p is None else p.encode("utf-8", "strict") | 196 return p if p is None else p.encode("utf-8", "strict") |
| 200 | 197 |
| 201 @property | 198 @property |
| 202 def alt_u8name(self): | 199 def alt_u8name(self): |
| 203 if self._alt_u8name is _notset: | 200 if self._alt_u8name is _notset: |
| 204 if PY2: | 201 self._alt_u8name = WalkDirEntry.alt_u8(self._name) |
| 205 if isinstance(self._name, bytes): | |
| 206 try: | |
| 207 self._alt_u8name = ( | |
| 208 self._name | |
| 209 .decode(_FSENCODING, "strict") | |
| 210 .encode("utf-8", "strict")) | |
| 211 except UnicodeError: | |
| 212 self._alt_u8name = ( | |
| 213 self.surrogate_decode(self._name) | |
| 214 .encode("ascii", "backslashreplace")) | |
| 215 else: | |
| 216 self._alt_u8name = self._name.encode( | |
| 217 "ascii", "backslashreplace") | |
| 218 else: | |
| 219 self._alt_u8name = self._name.encode( | |
| 220 "utf-8", "backslashreplace") | |
| 221 return self._alt_u8name | 202 return self._alt_u8name |
| 222 | 203 |
| 223 @property | 204 @property |
| 224 def alt_u8path(self): | 205 def alt_u8path(self): |
| 225 if PY2: | 206 return WalkDirEntry.alt_u8(self._path) |
| 226 if isinstance(self._path, bytes): | 207 |
| 208 @staticmethod | |
| 209 def alt_u8(what): | |
| 210 if PY2: | |
| 211 if isinstance(what, bytes): | |
| 227 try: | 212 try: |
| 228 return (self._path.decode(_FSENCODING, "strict") | 213 return (what.decode(_FSENCODING, "strict") |
| 229 .encode("utf-8", "strict")) | 214 .encode("utf-8", "strict")) |
| 230 except UnicodeError: | 215 except UnicodeError: |
| 231 return (self.surrogate_decode(self._path) | 216 return (WalkDirEntry.surrogate_decode(what) |
| 232 .encode("ascii", "backslashreplace")) | 217 .encode("ascii", "backslashreplace")) |
| 233 else: | 218 else: |
| 234 return self._path.encode("ascii", "backslashreplace") | 219 return what.encode("ascii", "backslashreplace") |
| 235 else: | 220 else: |
| 236 return self._path.encode("utf-8", "backslashreplace") | 221 return what.encode("utf-8", "backslashreplace") |
| 237 | 222 |
| 238 @property | 223 @property |
| 239 def is_symlink(self): | 224 def is_symlink(self): |
| 240 return self._is_symlink | 225 return self._is_symlink |
| 241 | 226 |
