annotate cutils/util/walk.py @ 344:0a58948df713

Move the computation of the special tag string marker for special files into a property
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 01 Apr 2025 12:45:56 +0200
parents b3931b511ed0
children b256ae4f4bc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 # :-
323
48430941c18c Adopt copyright and license wordings from https://reuse.software/faq/.
Franz Glasner <fzglas.hg@dom66.de>
parents: 283
diff changeset
3 # SPDX-FileCopyrightText: © 2025 Franz Glasner
48430941c18c Adopt copyright and license wordings from https://reuse.software/faq/.
Franz Glasner <fzglas.hg@dom66.de>
parents: 283
diff changeset
4 # SPDX-License-Identifier: BSD-3-Clause
121
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
344
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
16 import logging
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
17 import os
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
18 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
19 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
20 except ImportError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
21 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
22 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
23 except ImportError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
24 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
25 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
26 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
27
164
a813094ae4f5 Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents: 162
diff changeset
28 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
29
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
30
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
31 _notset = object()
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
32
344
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
33 _logger = logging.getLogger(__name__)
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
34
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
35 _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
36
c1e875ba4bdc Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents: 196
diff changeset
37
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
38 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
39
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
40 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
41 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
42 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
43 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
44
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
45 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
46
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
47 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
48 return s.replace("\\", "/")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
49
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
50
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
51 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
52
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
53 """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
54 its results.
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
55
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
56 """
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
57
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
58 __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
59 "_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
60 "_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
61 "_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
62
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
63 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
64 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
65 """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
66 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
67 """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
68 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
69 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
70 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
71
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
72 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
73 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
74 """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
75 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
76
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
77 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
78 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
79 """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
80 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
81
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
82 @property
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
83 def fsname(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
84 """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
85
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
86 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
87
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
88 :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
89
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
90 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
91 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
92 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
93 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
94 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
95 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
96 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
97 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
98 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
99 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
100 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
101 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
102 return s
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
103
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
104 @property
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
105 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
106 """Always bytes.
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
107
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
108 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
109
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
110 :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
111
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 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
114 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
115 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
116 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
117 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
118 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
119 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
120 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
121 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
122 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
123 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
124 return p
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
125
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
126 @property
282
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
127 def alt_fsname(self):
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
128 """Alternative and "escaped" filesystem name -- always bytes.
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
129
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
130 :rtype: bytes
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
131
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
132 """
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
133 if self._alt_fsname is _notset:
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
134 self._alt_fsname = WalkDirEntry.alt_fs(self._name)
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
135 return self._alt_fsname
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
136
d507ae4943d5 Reordering of methods: make it a little bit more consistent
Franz Glasner <fzglas.hg@dom66.de>
parents: 281
diff changeset
137 @property
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
138 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
139 """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
140
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
141 :rtype: bytes
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
142
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
143 """
203
3a85f7bbe0b1 Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents: 201
diff changeset
144 return WalkDirEntry.alt_fs(self._path)
3a85f7bbe0b1 Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents: 201
diff changeset
145
3a85f7bbe0b1 Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents: 201
diff changeset
146 @staticmethod
3a85f7bbe0b1 Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents: 201
diff changeset
147 def alt_fs(what):
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
148 #
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
149 # Prevent double encoding ...
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
150 # ... and hope that the current FS encoding is compatible
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
151 # with it
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
152 #
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
153 if isinstance(what, bytes):
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
154 s = (what.replace(b'\\', b"\\x5c")
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
155 .replace(b'\n', b"\\x0a")
343
b3931b511ed0 Also encode TAB characters specially for output in digest files.
Franz Glasner <fzglas.hg@dom66.de>
parents: 323
diff changeset
156 .replace(b'\r', b"\\x0d")
b3931b511ed0 Also encode TAB characters specially for output in digest files.
Franz Glasner <fzglas.hg@dom66.de>
parents: 323
diff changeset
157 .replace(b'\t', b"\\x09"))
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
158 else:
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
159 s = (what.replace(u'\\', u"\\x5c")
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
160 .replace(u'\n', u"\\x0a")
343
b3931b511ed0 Also encode TAB characters specially for output in digest files.
Franz Glasner <fzglas.hg@dom66.de>
parents: 323
diff changeset
161 .replace(u'\r', u"\\x0d")
b3931b511ed0 Also encode TAB characters specially for output in digest files.
Franz Glasner <fzglas.hg@dom66.de>
parents: 323
diff changeset
162 .replace(u'\t', u"\\x09"))
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
163 if PY2:
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
164 if isinstance(s, bytes):
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
165 return s
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
166 else:
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
167 return s.encode(_FSENCODING, "backslashreplace")
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
168 else:
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
169 return os.fsencode(s)
201
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 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
172 def uname(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
173 """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
174 possible.
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
175
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
176 :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
177
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
178 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
179 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
180 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
181 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
182 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
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 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
186 return self._name
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
187 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
188 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
189 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
190 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
191 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
192 return self._name
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 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
195 def upath(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
196 """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
197 possible.
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
198
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
199 :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
200
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
201 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
202 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
203 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
204 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
205 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
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 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
209 return self._path
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
210 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
211 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
212 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
213 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
214 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
215 return self._path
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
216
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
217 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
218 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
219 """`.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
220
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 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
222
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
223 """
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
224 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
225 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
226 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
227 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
228 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
229 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
230
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
231 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
232 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
233 """`.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
234
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 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
236
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
237 """
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
238 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
239 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
240 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
241 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
242 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
243 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
244
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
245 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
246 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
247 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
248 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
249 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
250
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
251 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
252 def alt_u8path(self):
203
3a85f7bbe0b1 Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents: 201
diff changeset
253 return WalkDirEntry.alt_u8(self._path)
3a85f7bbe0b1 Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents: 201
diff changeset
254
3a85f7bbe0b1 Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents: 201
diff changeset
255 @staticmethod
3a85f7bbe0b1 Common static method for some alternative encodings
Franz Glasner <fzglas.hg@dom66.de>
parents: 201
diff changeset
256 def alt_u8(what):
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
257 #
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
258 # Prevent double encoding ...
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
259 # ... and hope that the current UTF-8 is compatible
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
260 # with it
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
261 #
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
262 if isinstance(what, bytes):
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
263 s = (what.replace(b'\\', b"\\x5c")
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
264 .replace(b'\n', b"\\x0a")
343
b3931b511ed0 Also encode TAB characters specially for output in digest files.
Franz Glasner <fzglas.hg@dom66.de>
parents: 323
diff changeset
265 .replace(b'\r', b"\\x0d")
b3931b511ed0 Also encode TAB characters specially for output in digest files.
Franz Glasner <fzglas.hg@dom66.de>
parents: 323
diff changeset
266 .replace(b'\t', b"\\x09"))
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
267 else:
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
268 s = (what.replace(u'\\', u"\\x5c")
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
269 .replace(u'\n', u"\\x0a")
343
b3931b511ed0 Also encode TAB characters specially for output in digest files.
Franz Glasner <fzglas.hg@dom66.de>
parents: 323
diff changeset
270 .replace(u'\r', u"\\x0d")
b3931b511ed0 Also encode TAB characters specially for output in digest files.
Franz Glasner <fzglas.hg@dom66.de>
parents: 323
diff changeset
271 .replace(u'\t', u"\\x09"))
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
272 if PY2:
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
273 if isinstance(s, bytes):
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
274 try:
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
275 return (s.decode(_FSENCODING, "strict")
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
276 .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
277 except UnicodeError:
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
278 return (WalkDirEntry.surrogate_decode(s)
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
279 .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
280 else:
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
281 return s.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
282 else:
283
99b78fa04bc1 FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
Franz Glasner <fzglas.hg@dom66.de>
parents: 282
diff changeset
283 return s.encode("utf-8", "backslashreplace")
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
284
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
285 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
286 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
287 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
288
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
289 @property
273
c02a57df2a29 treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents: 266
diff changeset
290 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
291 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
292
c02a57df2a29 treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents: 266
diff changeset
293 @property
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
294 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
295 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
296
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
297 @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
298 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
299 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
300 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
301 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
302
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 @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
304 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
305 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
306 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
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_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
311 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
312 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
313 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
314
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 @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
316 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
317 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
318 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
319 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
320
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 @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
322 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
323 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
324 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
325 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
326 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
327
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 @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
329 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
330 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
331 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
332 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
333 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
334
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
335 @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
336 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
337 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
338 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
339 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
340 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
341
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
342 @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
343 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
344 """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
345 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
346 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
347 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
348
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
349 @property
344
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
350 def special_tag(self):
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
351 """Return a special tag (string) for a special file"""
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
352 assert self.is_special
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
353 if self.is_chr:
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
354 return ':'
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
355 elif self.is_blk:
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
356 return ';'
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
357 elif self.is_fifo:
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
358 return '|'
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
359 elif self.is_socket:
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
360 return '='
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
361 elif self.is_door:
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
362 return '>'
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
363 elif self.is_whiteout:
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
364 return '%'
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
365 elif self.is_eventport:
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
366 return '+'
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
367 _logger.warning(
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
368 "unknown special file type: 0x%X",
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
369 stat.S_IFMT(self._stat_result.st_mode))
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
370 return '?'
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
371
0a58948df713 Move the computation of the special tag string marker for special files into a property
Franz Glasner <fzglas.hg@dom66.de>
parents: 343
diff changeset
372 @property
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
373 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
374 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
375
266
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
376 @property
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
377 def stat_errno(self):
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
378 return self._stat_errno
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
379
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
380 @property
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
381 def stat_errstr(self):
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
382 return self._stat_errstr
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
383
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
384 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
385 tag = ""
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
386 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
387 tag += "l"
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
388 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
389 tag += "d"
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
390 if tag:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
391 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
392 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
393
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
394 @classmethod
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
395 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
396 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
397 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
398 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
399 except OSError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
400 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
401 # 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
402 # 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
403 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
404 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
405 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
406 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
407 except OSError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
408 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
409 # 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
410 # 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
411 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
412 w._is_symlink = False
266
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
413 # Consistently follow symlinks
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
414 try:
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
415 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
416 except OSError as e:
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
417 w._stat_result = None
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
418 w._stat_errno = e.errno
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
419 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
420 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
421 else:
c02a57df2a29 treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents: 266
diff changeset
422 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
423 return w
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
424
162
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
425 @classmethod
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
426 def from_path_name(cls_, path, name, _do_stat=True):
265
188f448ab5e9 FIX: typo in docs
Franz Glasner <fzglas.hg@dom66.de>
parents: 221
diff changeset
427 """`_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
428 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
429 try:
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
430 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
431 except OSError:
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
432 #
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
433 # 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
434 # 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
435 #
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
436 w._is_dir = False
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
437 try:
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
438 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
439 except OSError:
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
440 #
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
441 # 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
442 # 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
443 #
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
444 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
445 if _do_stat:
266
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
446 try:
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
447 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
448 except OSError as e:
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
449 w._stat_result = None
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
450 w._stat_errno = e.errno
0add8276e6b8 treesum: Handle errors like broken symlinks properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
451 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
452 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
453 else:
c02a57df2a29 treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Franz Glasner <fzglas.hg@dom66.de>
parents: 266
diff changeset
454 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
455 return w
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
456
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
457 @classmethod
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
458 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
459 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
460 return w
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
461
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
462 @staticmethod
221
ca9d5a0dc9bb Rename WalkDirEntry.sort_key and WalkDirEntry.alt_sort_key
Franz Glasner <fzglas.hg@dom66.de>
parents: 203
diff changeset
463 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
464 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
465
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
466 @staticmethod
221
ca9d5a0dc9bb Rename WalkDirEntry.sort_key and WalkDirEntry.alt_sort_key
Franz Glasner <fzglas.hg@dom66.de>
parents: 203
diff changeset
467 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
468 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
469
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
470 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
471
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
472 @staticmethod
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
473 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
474 """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
475 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
476
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
477 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
478 uwhat = []
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
479 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
480 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
481 chcode = ord(ch)
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
482 if chcode <= 0x7f:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
483 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
484 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
485 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
486 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
487
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
488
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
489 if scandir:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
490
178
dac26a2d9de5 Cleanup: remove non used walk-code
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
491 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
492
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
493 """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
494 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
495
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
496 """
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
497
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
498 __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
499
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
500 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
501 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
502 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
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 __iter__(self):
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
505 return self
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
506
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
507 def __next__(self):
199
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
508 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
509 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
510 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
511
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
512 if PY2:
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
513 next = __next__
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
514
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
515 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
516 return self
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
517
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
518 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
519 self.close()
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
520
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
521 def close(self):
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
522 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
523 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
524 self._scandir_it.close()
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
525 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
526
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
527 else:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
528
178
dac26a2d9de5 Cleanup: remove non used walk-code
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
529 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
530
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
531 """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
532 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
533
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
534 """
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
535
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
536 __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
537
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
538 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
539 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
540 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
541 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
542
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
543 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
544 return self
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
545
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
546 def __next__(self):
199
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
547 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
548 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
549 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
550 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
551
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
552 if PY2:
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
553 next = __next__
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
554
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
555 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
556 return self
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
557
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
558 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
559 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
560
199
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
561 def close(self):
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
562 pass
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
563
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
564
c1e875ba4bdc Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents: 196
diff changeset
565 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
566 """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
567 return _FSENCODING