Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/util/walk.py @ 281:16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 23 Feb 2025 12:37:34 +0100 |
| parents | f3e0b479928c |
| children | d507ae4943d5 |
| rev | line source |
|---|---|
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
2 # :- |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
3 # :Copyright: (c) 2020-2025 Franz Glasner |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
4 # :License: BSD-3-Clause |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
5 # :- |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
6 r"""Utility sub-module to implement a heavily customized :func:`os.walk`. |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
7 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
8 """ |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
9 |
|
196
0f4febf646f5
Prepare for more Python2/3 compatibility: everywhere import print_function and absolute_import
Franz Glasner <fzglas.hg@dom66.de>
parents:
195
diff
changeset
|
10 from __future__ import print_function, absolute_import |
|
0f4febf646f5
Prepare for more Python2/3 compatibility: everywhere import print_function and absolute_import
Franz Glasner <fzglas.hg@dom66.de>
parents:
195
diff
changeset
|
11 |
|
0f4febf646f5
Prepare for more Python2/3 compatibility: everywhere import print_function and absolute_import
Franz Glasner <fzglas.hg@dom66.de>
parents:
195
diff
changeset
|
12 |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
13 __all__ = ["WalkDirEntry", "ScanDir", "getfsencoding"] |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
14 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
15 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
16 import os |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
17 try: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
18 from os import scandir |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
19 except ImportError: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
20 try: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
21 from scandir import scandir |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
22 except ImportError: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
23 scandir = None |
|
273
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
24 import stat |
|
198
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
25 import sys |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
26 |
|
164
a813094ae4f5
Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents:
162
diff
changeset
|
27 from . import PY2 |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
28 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
29 |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
30 _notset = object() |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
31 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
32 |
|
198
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
33 _FSENCODING = sys.getfilesystemencoding() |
|
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
34 |
|
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
35 |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
36 if PY2: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
37 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
38 def _unix_path(s): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
39 if isinstance(s, bytes): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
40 return s.replace(b"\\", b"/") |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
41 return s.replace(u"\\", u"/") |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
42 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
43 else: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
44 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
45 def _unix_path(s): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
46 return s.replace("\\", "/") |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
47 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
48 |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
49 class WalkDirEntry(object): |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
50 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
51 """A :class:`os.DirEntry` alike to be used in :func:`walk` and for |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
52 its results. |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
53 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
54 """ |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
55 |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
56 __slots__ = ("_name", "_path", # encoded as given in the ctor |
|
273
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
57 "_is_symlink", "_is_reg", "_is_dir", "_stat_result", |
|
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
58 "_stat_errno", "_stat_errstr", |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
59 "_alt_fsname", "_alt_u8name") |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
60 |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
61 def __init__(self, name, path): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
62 self._name = name # the name as given in the constructor |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
63 """The name exactly as given in the ctor""" |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
64 self._path = _unix_path(path) |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
65 """The path as given in the ctor -- but normalized to have slashes""" |
|
273
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
66 self._is_symlink = self._is_reg = self._is_dir = self._stat_result = \ |
|
266
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
67 self._stat_errno = self._stat_errstr = None |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
68 self._alt_fsname = self._alt_u8name = _notset |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
69 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
70 @property |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
71 def name(self): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
72 """The original name exactly as given in the ctor""" |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
73 return self._name |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
74 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
75 @property |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
76 def path(self): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
77 """The original path exactly as given in the ctor.""" |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
78 return self._path |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
79 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
80 @property |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
81 def fsname(self): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
82 """The name as bytes for the filesystem. |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
83 |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
84 Also do not allow CR of LF in the name. |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
85 |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
86 :rtype: bytes or None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
87 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
88 """ |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
89 if PY2: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
90 if isinstance(self._name, bytes): |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
91 s = self._name |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
92 try: |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
93 s = self._name.encode(_FSENCODING, "strict") |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
94 except UnicodeError: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
95 return None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
96 else: |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
97 s = os.fsencode(self._name) |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
98 if (b'\n' in s) or (b'\r' in s) or (b'\\' in s): |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
99 return None |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
100 return s |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
101 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
102 @property |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
103 def alt_fsname(self): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
104 """Alternative and "escaped" filesystem name -- always bytes. |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
105 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
106 :rtype: bytes |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
107 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
108 """ |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
109 if self._alt_fsname is _notset: |
|
203
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
110 self._alt_fsname = WalkDirEntry.alt_fs(self._name) |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
111 return self._alt_fsname |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
112 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
113 @property |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
114 def fspath(self): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
115 """Always bytes. |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
116 |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
117 Also do not allow CR of LF in the path. |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
118 |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
119 :rtype: bytes or None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
120 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
121 """ |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
122 if PY2: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
123 if isinstance(self._path, bytes): |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
124 p = self._path |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
125 try: |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
126 p = self._path.encode(_FSENCODING, "strict") |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
127 except UnicodeError: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
128 return None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
129 else: |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
130 p = os.fsencode(self._path) |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
131 if (b'\n' in p) or (b'\r' in p) or (b'\\' in p): |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
132 return None |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
133 return p |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
134 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
135 @property |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
136 def alt_fspath(self): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
137 """Alternative and "escaped" filesystem path -- always bytes. |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
138 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
139 :rtype: bytes |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
140 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
141 """ |
|
203
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
142 return WalkDirEntry.alt_fs(self._path) |
|
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
143 |
|
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
144 @staticmethod |
|
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
145 def alt_fs(what): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
146 if PY2: |
|
203
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
147 if isinstance(what, bytes): |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
148 s = what |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
149 else: |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
150 # |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
151 # Prevent double encoding ... |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
152 # ... and hope that the current FS encoding is compatible |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
153 # with it |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
154 # |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
155 s = what.replace(u'\\', u"\\x5c") |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
156 s = s.encode(_FSENCODING, "backslashreplace") |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
157 return s.replace(b'\n', b"\\x0a").replace(b'\r', b"\\x0d") |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
158 else: |
|
281
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
159 s = os.fsencode(what) |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
160 return (s.replace(b'\\', b"\\x5c") |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
161 .replace(b'\n', b"\\x0a") |
|
16507317e834
treesum: FIX: Also do not allow CR and/or LF and/or backslashes in strictly FS encoded names
Franz Glasner <fzglas.hg@dom66.de>
parents:
280
diff
changeset
|
162 .replace(b'\r', b"\\x0d")) |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
163 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
164 @property |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
165 def uname(self): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
166 """Always "real", strictly encoded Unicode or `None` if this is not |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
167 possible. |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
168 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
169 :rtype: text or None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
170 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
171 """ |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
172 if PY2: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
173 if isinstance(self._name, bytes): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
174 try: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
175 return self._name.decode(_FSENCODING, "strict") |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
176 except UnicodeError: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
177 return None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
178 else: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
179 return self._name |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
180 else: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
181 try: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
182 self._name.encode("utf-8", "strict") |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
183 except UnicodeError: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
184 return None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
185 return self._name |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
186 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
187 @property |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
188 def upath(self): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
189 """Always "real", strictly encoded Unicode or `None` if this is not |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
190 possible. |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
191 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
192 :rtype: text or None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
193 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
194 """ |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
195 if PY2: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
196 if isinstance(self._path, bytes): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
197 try: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
198 return self._path.decode(_FSENCODING, "strict") |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
199 except UnicodeError: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
200 return None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
201 else: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
202 return self._path |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
203 else: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
204 try: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
205 self._path.encode("utf-8", "strict") |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
206 except UnicodeError: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
207 return None |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
208 return self._path |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
209 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
210 @property |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
211 def u8name(self): |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
212 """`.uname` as UTF-8 or `None` (as strict as `uname`). |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
213 |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
214 Also do not allow CR of LF in the name. |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
215 |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
216 """ |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
217 n = self.uname |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
218 if n is None: |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
219 return None |
|
280
f3e0b479928c
treesum: FIX: Also check for that backslashes are nor in u8name or u8path and encode them
Franz Glasner <fzglas.hg@dom66.de>
parents:
276
diff
changeset
|
220 if (u'\n' in n) or (u'\r' in n) or (u'\\' in n): |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
221 return None |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
222 return n.encode("utf-8", "strict") |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
223 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
224 @property |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
225 def u8path(self): |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
226 """`.upath` as UTF-8 or `None` (as strict as `upath`. |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
227 |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
228 Also do not allow CR or LF in the path. |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
229 |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
230 """ |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
231 p = self.upath |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
232 if p is None: |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
233 return None |
|
280
f3e0b479928c
treesum: FIX: Also check for that backslashes are nor in u8name or u8path and encode them
Franz Glasner <fzglas.hg@dom66.de>
parents:
276
diff
changeset
|
234 if (u'\n' in p) or (u'\r' in p) or (u'\\' in p): |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
235 return None |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
236 return p.encode("utf-8", "strict") |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
237 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
238 @property |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
239 def alt_u8name(self): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
240 if self._alt_u8name is _notset: |
|
203
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
241 self._alt_u8name = WalkDirEntry.alt_u8(self._name) |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
242 return self._alt_u8name |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
243 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
244 @property |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
245 def alt_u8path(self): |
|
203
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
246 return WalkDirEntry.alt_u8(self._path) |
|
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
247 |
|
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
248 @staticmethod |
|
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
249 def alt_u8(what): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
250 if PY2: |
|
203
3a85f7bbe0b1
Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents:
201
diff
changeset
|
251 if isinstance(what, bytes): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
252 try: |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
253 s = (what.decode(_FSENCODING, "strict") |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
254 .encode("utf-8", "strict")) |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
255 except UnicodeError: |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
256 s = (WalkDirEntry.surrogate_decode(what) |
|
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
257 .encode("ascii", "backslashreplace")) |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
258 else: |
|
280
f3e0b479928c
treesum: FIX: Also check for that backslashes are nor in u8name or u8path and encode them
Franz Glasner <fzglas.hg@dom66.de>
parents:
276
diff
changeset
|
259 s = what.encode("ascii", "backslashreplace") |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
260 else: |
|
276
f7850ff5cbe0
treesum: when walking: if filenames contain CR and/or LF pretend that this names are not properly encoded.
Franz Glasner <fzglas.hg@dom66.de>
parents:
274
diff
changeset
|
261 s = what.encode("utf-8", "backslashreplace") |
|
280
f3e0b479928c
treesum: FIX: Also check for that backslashes are nor in u8name or u8path and encode them
Franz Glasner <fzglas.hg@dom66.de>
parents:
276
diff
changeset
|
262 return (s.replace(b'\\', b"\\x5c") |
|
f3e0b479928c
treesum: FIX: Also check for that backslashes are nor in u8name or u8path and encode them
Franz Glasner <fzglas.hg@dom66.de>
parents:
276
diff
changeset
|
263 .replace(b'\n', b"\\x0a") |
|
f3e0b479928c
treesum: FIX: Also check for that backslashes are nor in u8name or u8path and encode them
Franz Glasner <fzglas.hg@dom66.de>
parents:
276
diff
changeset
|
264 .replace(b'\r', b"\\x0d")) |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
265 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
266 @property |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
267 def is_symlink(self): |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
268 return self._is_symlink |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
269 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
270 @property |
|
273
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
271 def is_reg(self): |
|
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
272 return self._is_reg |
|
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
273 |
|
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
274 @property |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
275 def is_dir(self): |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
276 return self._is_dir |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
277 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
278 @property |
|
274
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
279 def is_chr(self): |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
280 return (stat.S_ISCHR(self._stat_result.st_mode) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
281 if self._stat_result is not None |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
282 else False) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
283 |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
284 @property |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
285 def is_blk(self): |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
286 return (stat.S_ISBLK(self._stat_result.st_mode) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
287 if self._stat_result is not None |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
288 else False) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
289 |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
290 @property |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
291 def is_fifo(self): |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
292 return (stat.S_ISFIFO(self._stat_result.st_mode) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
293 if self._stat_result is not None |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
294 else False) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
295 |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
296 @property |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
297 def is_socket(self): |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
298 return (stat.S_ISSOCK(self._stat_result.st_mode) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
299 if self._stat_result is not None |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
300 else False) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
301 |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
302 @property |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
303 def is_door(self): |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
304 test = getattr(stat, "S_ISDOOR", None) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
305 return (test(self._stat_result.st_mode) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
306 if test and (self._stat_result is not None) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
307 else False) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
308 |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
309 @property |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
310 def is_eventport(self): |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
311 test = getattr(stat, "S_ISPORT", None) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
312 return (test(self._stat_result.st_mode) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
313 if test and (self._stat_result is not None) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
314 else False) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
315 |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
316 @property |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
317 def is_whiteout(self): |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
318 test = getattr(stat, "S_ISWHT", None) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
319 return (test(self._stat_result.st_mode) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
320 if test and (self._stat_result is not None) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
321 else False) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
322 |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
323 @property |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
324 def is_special(self): |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
325 """Anything besides a regular file and a directory""" |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
326 if self._stat_result is None: |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
327 return False |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
328 return not (self.is_reg or self.is_dir) |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
329 |
|
224725fd9f2f
WalkDirEntry: add support for special files (character and block special, FIFO, socket, door, whiteout, event port, ...)
Franz Glasner <fzglas.hg@dom66.de>
parents:
273
diff
changeset
|
330 @property |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
331 def stat(self): |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
332 return self._stat_result |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
333 |
|
266
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
334 @property |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
335 def stat_errno(self): |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
336 return self._stat_errno |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
337 |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
338 @property |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
339 def stat_errstr(self): |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
340 return self._stat_errstr |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
341 |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
342 def __repr__(self): |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
343 tag = "" |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
344 if self._is_symlink: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
345 tag += "l" |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
346 if self._is_dir: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
347 tag += "d" |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
348 if tag: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
349 return "<WalkDirEntry %r (%s)>" % (self._name, tag) |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
350 return "<WalkDirEntry %r>" % (self._name,) |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
351 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
352 @classmethod |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
353 def from_direntry(cls_, entry): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
354 w = cls_(entry.name, entry.path) |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
355 try: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
356 w._is_dir = entry.is_dir(follow_symlinks=True) |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
357 except OSError: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
358 # |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
359 # If is_dir() raises an OSError, consider that the entry |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
360 # is not a directory, same behaviour than os.path.isdir(). |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
361 # |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
362 w._is_dir = False |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
363 try: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
364 w._is_symlink = entry.is_symlink() |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
365 except OSError: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
366 # |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
367 # If is_symlink() raises an OSError, consider that the entry |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
368 # is not a symbolic link, same behaviour than os.path.islink(). |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
369 # |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
370 w._is_symlink = False |
|
266
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
371 # Consistently follow symlinks |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
372 try: |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
373 w._stat_result = entry.stat(follow_symlinks=True) |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
374 except OSError as e: |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
375 w._stat_result = None |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
376 w._stat_errno = e.errno |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
377 w._stat_errstr = e.strerror |
|
273
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
378 w._is_reg = False |
|
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
379 else: |
|
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
380 w._is_reg = stat.S_ISREG(w._stat_result.st_mode) |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
381 return w |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
382 |
|
162
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
383 @classmethod |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
384 def from_path_name(cls_, path, name, _do_stat=True): |
| 265 | 385 """`_do_stat` is to be used only for testing purposes""" |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
386 w = cls_(name, os.path.join(path, name)) |
|
162
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
387 try: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
388 w._is_dir = os.path.isdir(w._path) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
389 except OSError: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
390 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
391 # If is_dir() raises an OSError, consider that the entry |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
392 # is not a directory, same behaviour than os.path.isdir(). |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
393 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
394 w._is_dir = False |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
395 try: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
396 w._is_symlink = os.path.islink(w._path) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
397 except OSError: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
398 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
399 # If is_symlink() raises an OSError, consider that the entry |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
400 # is not a symbolic link, same behaviour than os.path.islink(). |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
401 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
402 w._is_symlink = False |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
403 if _do_stat: |
|
266
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
404 try: |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
405 w._stat_result = os.stat(w._path) |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
406 except OSError as e: |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
407 w._stat_result = None |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
408 w._stat_errno = e.errno |
|
0add8276e6b8
treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
265
diff
changeset
|
409 w._stat_errstr = e.strerror |
|
273
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
410 w._is_reg = False |
|
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
411 else: |
|
c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents:
266
diff
changeset
|
412 w._is_reg = stat.S_ISREG(w._stat_result.st_mode) |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
413 return w |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
414 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
415 @classmethod |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
416 def from_readlink(cls_, path): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
417 w = cls_(os.path.basename(path), path) |
|
162
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
418 return w |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
419 |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
420 @staticmethod |
|
221
ca9d5a0dc9bb
Rename WalkDirEntry.sort_key and WalkDirEntry.alt_sort_key
Franz Glasner <fzglas.hg@dom66.de>
parents:
203
diff
changeset
|
421 def sort_key_fs(entry): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
422 return entry.alt_fsname # because it should never throw |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
423 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
424 @staticmethod |
|
221
ca9d5a0dc9bb
Rename WalkDirEntry.sort_key and WalkDirEntry.alt_sort_key
Franz Glasner <fzglas.hg@dom66.de>
parents:
203
diff
changeset
|
425 def sort_key_u8(entry): |
|
201
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
426 return entry.alt_u8name # because it should never throw |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
427 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
428 if PY2: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
429 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
430 @staticmethod |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
431 def surrogate_decode(what): |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
432 """Decode the bytes object `what` using surrogates from :pep:`383` |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
433 for all non-ASCII octets. |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
434 |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
435 """ |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
436 uwhat = [] |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
437 assert isinstance(what, bytes) |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
438 for ch in what: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
439 chcode = ord(ch) |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
440 if chcode <= 0x7f: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
441 uwhat.append(unichr(chcode)) # noqa: F821 unichr |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
442 else: |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
443 uwhat.append(unichr(0xDC00 + chcode)) # noqa: F821 unichr |
|
58d93453c307
Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
199
diff
changeset
|
444 return u"".join(uwhat) |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
445 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
446 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
447 if scandir: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
448 |
|
178
dac26a2d9de5
Cleanup: remove non used walk-code
Franz Glasner <fzglas.hg@dom66.de>
parents:
177
diff
changeset
|
449 class ScanDir(object): |
|
175
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
450 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
451 """An :func:`os.scandir` wrapper that is always an iterator and |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
452 a context manager. |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
453 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
454 """ |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
455 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
456 __slots__ = ("_scandir_it", ) |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
457 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
458 def __init__(self, path): |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
459 super(ScanDir, self).__init__() |
|
195
e5f6f4db9157
Use the imported "scandir()" implementation instead always the "os.scandir"
Franz Glasner <fzglas.hg@dom66.de>
parents:
192
diff
changeset
|
460 self._scandir_it = scandir(path) |
|
175
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
461 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
462 def __iter__(self): |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
463 return self |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
464 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
465 def __next__(self): |
|
199
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
466 if self._scandir_it is None: |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
467 raise StopIteration("closed") |
|
175
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
468 return WalkDirEntry.from_direntry(next(self._scandir_it)) |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
469 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
470 if PY2: |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
471 next = __next__ |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
472 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
473 def __enter__(self): |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
474 return self |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
475 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
476 def __exit__(self, *args, **kwds): |
|
199
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
477 self.close() |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
478 |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
479 def close(self): |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
480 if self._scandir_it is not None: |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
481 if hasattr(self._scandir_it, "close"): |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
482 self._scandir_it.close() |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
483 self._scandir_it = None |
|
175
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
484 |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
485 else: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
486 |
|
178
dac26a2d9de5
Cleanup: remove non used walk-code
Franz Glasner <fzglas.hg@dom66.de>
parents:
177
diff
changeset
|
487 class ScanDir(object): |
|
175
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
488 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
489 """An :func:`os.scandir` wrapper that is always an iterator and |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
490 a context manager. |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
491 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
492 """ |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
493 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
494 __slots__ = ("_listdir_it", "_path") |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
495 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
496 def __init__(self, path): |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
497 super(ScanDir, self).__init__() |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
498 self._listdir_it = iter(os.listdir(path)) |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
499 self._path = path |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
500 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
501 def __iter__(self): |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
502 return self |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
503 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
504 def __next__(self): |
|
199
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
505 if self._listdir_it is None: |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
506 raise StopIteration("closed") |
|
175
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
507 return WalkDirEntry.from_path_name(self._path, |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
508 next(self._listdir_it)) |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
509 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
510 if PY2: |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
511 next = __next__ |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
512 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
513 def __enter__(self): |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
514 return self |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
515 |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
516 def __exit__(self, *args, **kwds): |
|
506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
517 pass |
|
198
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
518 |
|
199
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
519 def close(self): |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
520 pass |
|
b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents:
198
diff
changeset
|
521 |
|
198
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
522 |
|
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
523 def getfsencoding(): |
|
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
524 """Return the stored _FSENCODING of this module""" |
|
c1e875ba4bdc
Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents:
196
diff
changeset
|
525 return _FSENCODING |
