annotate cutils/util/walk.py @ 201:58d93453c307

Much more encoding-related methods for DirWalkEntry and some unittests
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 21 Jan 2025 14:30:06 +0100
parents b2aba84ca426
children 3a85f7bbe0b1
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 # :-
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
3 # :Copyright: (c) 2020-2025 Franz Glasner
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
4 # :License: BSD-3-Clause
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
5 # :-
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
6 r"""Utility sub-module to implement a heavily customized :func:`os.walk`.
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
7
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
8 """
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
9
196
0f4febf646f5 Prepare for more Python2/3 compatibility: everywhere import print_function and absolute_import
Franz Glasner <fzglas.hg@dom66.de>
parents: 195
diff changeset
10 from __future__ import print_function, absolute_import
0f4febf646f5 Prepare for more Python2/3 compatibility: everywhere import print_function and absolute_import
Franz Glasner <fzglas.hg@dom66.de>
parents: 195
diff changeset
11
0f4febf646f5 Prepare for more Python2/3 compatibility: everywhere import print_function and absolute_import
Franz Glasner <fzglas.hg@dom66.de>
parents: 195
diff changeset
12
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
13 __all__ = ["WalkDirEntry", "ScanDir", "getfsencoding"]
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
14
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
15
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
16 import os
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
17 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
18 from os import scandir
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
19 except ImportError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
20 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
21 from scandir import scandir
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
22 except ImportError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
23 scandir = None
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
24 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
25
164
a813094ae4f5 Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents: 162
diff changeset
26 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
27
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
28
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
29 _notset = object()
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
30
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
31
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
32 _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
33
c1e875ba4bdc Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents: 196
diff changeset
34
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
35 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
36
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
37 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
38 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
39 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
40 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
41
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
42 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
43
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
44 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
45 return s.replace("\\", "/")
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
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
48 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
49
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
50 """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
51 its results.
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 """
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
54
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
55 __slots__ = ("_name", "_path", # encoded 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
56 "_is_symlink", "_is_dir", "_stat_result",
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
57 "_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
58
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
59 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
60 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
61 """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
62 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
63 """The path as given in the ctor -- but normalized to have slashes"""
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
64 self._is_symlink = self._is_dir = self._stat_result = None
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
65 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
66
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
67 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
68 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
69 """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
70 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
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 path(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 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
75 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
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
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
78 def fsname(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
79 """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
80
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
81 :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
82
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
83 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
84 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
85 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
86 return self._name
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
87 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
88 return self._name.encode(_FSENCODING, "strict")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
89 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
90 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
91 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
92 return os.fsencode(self._name)
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
93
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
94 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
95 def alt_fsname(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
96 """Alternative and "escaped" filesystem name -- always bytes.
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
97
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
98 :rtype: bytes
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
99
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
100 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
101 if self._alt_fsname is _notset:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
102 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
103 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
104 self._alt_fsname = self._name
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
105 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
106 self._alt_fsname = self._name.encode(
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
107 _FSENCODING, "backslashreplace")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
108 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
109 self._alt_fsname = os.fsencode(self._name)
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
110 return self._alt_fsname
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
111
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
112 @property
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
113 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
114 """Always bytes.
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
115
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
116 :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
117
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
118 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
119 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
120 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
121 return self._path
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
122 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
123 return self._path.encode(_FSENCODING, "strict")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
124 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
125 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
126 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
127 return os.fsencode(self._path)
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
128
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
129 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
130 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
131 """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
132
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
133 :rtype: bytes
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
134
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
135 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
136 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
137 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
138 return self._path
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
139 return self._path.encode(_FSENCODING, "backslashreplace")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
140 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
141 return os.fsencode(self._path)
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 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
144 def uname(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
145 """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
146 possible.
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
147
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
148 :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
149
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
150 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
151 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
152 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
153 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
154 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
155 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
156 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
157 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
158 return self._name
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
159 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
160 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
161 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
162 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
163 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
164 return self._name
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
165
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
166 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
167 def upath(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
168 """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
169 possible.
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 :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
172
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
173 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
174 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
175 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
176 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
177 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
178 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
179 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
180 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
181 return self._path
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
182 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
183 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
184 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
185 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
186 return None
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
187 return self._path
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
188
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
189 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
190 def u8name(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
191 """`.uname` as UTF-8 or `None` (as strict as `uname`)"""
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
192 n = self.uname
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
193 return n if n is None else n.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
194
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
195 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
196 def u8path(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
197 """`.upath` as UTF-8 or `None` (as strict as `upath`"""
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
198 p = self.upath
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
199 return p if p is None else p.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
200
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
201 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
202 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
203 if self._alt_u8name is _notset:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
204 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
205 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
206 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
207 self._alt_u8name = (
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
208 self._name
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
209 .decode(_FSENCODING, "strict")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
210 .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
211 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
212 self._alt_u8name = (
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
213 self.surrogate_decode(self._name)
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
214 .encode("ascii", "backslashreplace"))
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
215 else:
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
216 self._alt_u8name = self._name.encode(
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
217 "ascii", "backslashreplace")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
218 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
219 self._alt_u8name = self._name.encode(
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
220 "utf-8", "backslashreplace")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
221 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
222
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
223 @property
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
224 def alt_u8path(self):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
225 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
226 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
227 try:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
228 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
229 .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
230 except UnicodeError:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
231 return (self.surrogate_decode(self._path)
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
232 .encode("ascii", "backslashreplace"))
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
233 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
234 return self._path.encode("ascii", "backslashreplace")
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
235 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
236 return self._path.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
237
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
238 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
239 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
240 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
241
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
242 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
243 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
244 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
245
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
246 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
247 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
248 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
249
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
250 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
251 tag = ""
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
252 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
253 tag += "l"
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
254 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
255 tag += "d"
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
256 if tag:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
257 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
258 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
259
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
260 @classmethod
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
261 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
262 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
263 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
264 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
265 except OSError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
266 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
267 # 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
268 # 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
269 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
270 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
271 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
272 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
273 except OSError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
274 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
275 # 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
276 # 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
277 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
278 w._is_symlink = False
156
481cc9b26861 Calculate "stat()" for directories also in a WalkDirEntry
Franz Glasner <fzglas.hg@dom66.de>
parents: 155
diff changeset
279 # Do not supress errors here and (consistently) follow symlinks
481cc9b26861 Calculate "stat()" for directories also in a WalkDirEntry
Franz Glasner <fzglas.hg@dom66.de>
parents: 155
diff changeset
280 w._stat_result = entry.stat(follow_symlinks=True)
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
281 return w
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
282
162
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
283 @classmethod
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
284 def from_path_name(cls_, path, name, _do_stat=True):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
285 """`_nostat` is to be used only for testing purposes"""
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
286 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
287 try:
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
288 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
289 except OSError:
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
290 #
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
291 # 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
292 # 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
293 #
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
294 w._is_dir = False
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
295 try:
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
296 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
297 except OSError:
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
298 #
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
299 # 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
300 # 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
301 #
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
302 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
303 if _do_stat:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
304 w._stat_result = os.stat(w._path)
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
305 return w
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
306
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
307 @classmethod
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
308 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
309 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
310 return w
29dd5528174c Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
311
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
312 @staticmethod
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
313 def sort_key(entry):
201
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
314 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
315
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
316 @staticmethod
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
317 def alt_sort_key(entry):
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
318 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
319
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
320 if PY2:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
321
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
322 @staticmethod
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
323 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
324 """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
325 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
326
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
327 """
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
328 uwhat = []
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
329 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
330 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
331 chcode = ord(ch)
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
332 if chcode <= 0x7f:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
333 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
334 else:
58d93453c307 Much more encoding-related methods for DirWalkEntry and some unittests
Franz Glasner <fzglas.hg@dom66.de>
parents: 199
diff changeset
335 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
336 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
337
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
338
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
339 if scandir:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
340
178
dac26a2d9de5 Cleanup: remove non used walk-code
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
341 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
342
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
343 """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
344 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
345
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
346 """
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
347
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
348 __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
349
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
350 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
351 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
352 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
353
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
354 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
355 return self
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
356
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
357 def __next__(self):
199
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
358 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
359 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
360 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
361
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
362 if PY2:
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
363 next = __next__
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
364
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
365 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
366 return self
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
367
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
368 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
369 self.close()
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
370
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
371 def close(self):
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
372 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
373 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
374 self._scandir_it.close()
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
375 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
376
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
377 else:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
378
178
dac26a2d9de5 Cleanup: remove non used walk-code
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
379 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
380
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
381 """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
382 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
383
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
384 """
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
385
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
386 __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
387
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
388 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
389 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
390 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
391 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
392
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
393 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
394 return self
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
395
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
396 def __next__(self):
199
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
397 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
398 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
399 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
400 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
401
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
402 if PY2:
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
403 next = __next__
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
404
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
405 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
406 return self
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
407
506d895a8500 Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
Franz Glasner <fzglas.hg@dom66.de>
parents: 164
diff changeset
408 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
409 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
410
199
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
411 def close(self):
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
412 pass
b2aba84ca426 Implement a "close()" method for cutils.util.walk.ScanDir.
Franz Glasner <fzglas.hg@dom66.de>
parents: 198
diff changeset
413
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
414
c1e875ba4bdc Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
Franz Glasner <fzglas.hg@dom66.de>
parents: 196
diff changeset
415 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
416 """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
417 return _FSENCODING