6565True. The regular "for...in" syntax does not support sending the "skip" flag
6666back to the iterator. Instead, use a pattern like:
6767
68- itr = file_iterator.file_iter(..., return_dir_paths=True) try: path = itr.next()
69- while True: skip_dir = determine_if_dir_should_be_skipped(path) file_path =
70- itr.send(skip_dir) except KeyboardInterrupt: raise StopIteration except
71- StopIteration: pass
68+ itr = file_iterator.file_iter(..., return_dir_paths=True)
69+ try:
70+ path = itr.next()
71+ while True:
72+ skip_dir = determine_if_dir_should_be_skipped(path)
73+ file_path = itr.send(skip_dir)
74+ except KeyboardInterrupt:
75+ raise StopIteration
76+ except StopIteration:
77+ pass
7278
7379{path_list} does not accept glob patterns, as it's more convenient to let the
7480shell expand glob patterns to directly specified files and dirs. E.g., to use a
@@ -150,50 +156,38 @@ def dir_iter(
150156
151157 for path in path_list :
152158 path = os .path .expanduser (path )
153- # if not isinstance(path, str):
154- # path = path.decode('utf-8')
155- if not ignore_invalid :
156- if not (os .path .isfile (path ) or os .path .isdir (path )):
157- raise EnvironmentError (0 , 'Not a valid file or dir path' , path )
159+
158160 # Return file
159161 if os .path .isfile (path ):
160162 file_name = os .path .split (path )[1 ]
161163 if not _is_filtered (
162164 file_name , include_file_glob_list , exclude_file_glob_list
163165 ):
164166 yield path
167+
165168 # Search directory
166- if os .path .isdir (path ):
167- if recursive :
168- # Recursive directory search
169- file_path_iter = _filtered_walk (
170- path , include_dir_glob_list , exclude_dir_glob_list , return_dir_paths
171- )
172- else :
173- # Single directory search
174- file_path_iter = os .listdir (path )
175-
176- skip_dir = None
177-
178- while True :
179- file_or_dir_path = file_path_iter .send (skip_dir )
180- file_or_dir_name = os .path .split (file_or_dir_path )[1 ]
181- skip_dir = False
182- if not _is_filtered (
183- file_or_dir_name , include_file_glob_list , exclude_file_glob_list
184- ):
185- skip_dir = yield file_or_dir_path
186-
187- # skip_dir = yield next(file_path_iter)
169+ elif os .path .isdir (path ):
170+ yield from _filtered_walk (
171+ path ,
172+ include_dir_glob_list , exclude_dir_glob_list ,
173+ include_file_glob_list , exclude_file_glob_list ,
174+ return_dir_paths , recursive
175+ )
176+ # else:
177+ # # Single directory search
178+ # file_path_iter = os.listdir(path)
179+ #
180+ # skip_dir = None
181+ #
188182 # while True:
189183 # file_or_dir_path = file_path_iter.send(skip_dir)
190184 # file_or_dir_name = os.path.split(file_or_dir_path)[1]
191- # if not _is_filtered(
192- # file_or_dir_name, include_file_glob_list, exclude_file_glob_list
193- # ):
185+ # skip_dir = False
194186 # skip_dir = yield file_or_dir_path
195- # else:
196- # skip_dir = False
187+
188+ else :
189+ if not ignore_invalid :
190+ raise EnvironmentError (0 , 'Not a valid file or dir path' , path )
197191
198192
199193def _is_filtered (name , include_glob_list , exclude_glob_list ):
@@ -206,28 +200,40 @@ def _is_filtered(name, include_glob_list, exclude_glob_list):
206200
207201
208202def _filtered_walk (
209- root_dir_path , include_dir_glob_list , exclude_dir_glob_list ,
210- return_dir_paths
203+ root_dir_path ,
204+ include_dir_glob_list , exclude_dir_glob_list ,
205+ include_file_glob_list , exclude_file_glob_list ,
206+ return_dir_paths , recursive
211207):
212208 skip_dir_path_list = []
209+
213210 for dir_path , dir_list , file_list in os .walk (root_dir_path ):
211+ if not recursive and dir_path != root_dir_path :
212+ return
213+
214214 if any (dir_path .startswith (d ) for d in skip_dir_path_list ):
215- logging .debug ('Skipped dir tree. root ="{}"' .format (dir_path ))
215+ logging .debug ('Skipped dir branch. branch ="{}"' .format (dir_path ))
216216 continue
217+
217218 dir_list [:] = [
218219 d for d in dir_list
219220 if not _is_filtered (
220221 os .path .split (d )[1 ] + '/' , include_dir_glob_list , exclude_dir_glob_list
221222 )
222223 ]
224+
223225 if return_dir_paths :
224226 for dir_name in dir_list :
225227 this_dir_path = os .path .join (dir_path , dir_name )
226228 skip_dir = yield this_dir_path
227229 if skip_dir :
228230 logging .debug (
229- 'Client requested skip. root ="{}"' .format (this_dir_path )
231+ 'Client requested branch. branch ="{}"' .format (this_dir_path )
230232 )
231233 skip_dir_path_list .append (this_dir_path )
234+
232235 for file_name in file_list :
233- yield os .path .join (dir_path , file_name )
236+ if not _is_filtered (
237+ file_name , include_file_glob_list , exclude_file_glob_list
238+ ):
239+ yield os .path .join (dir_path , file_name )
0 commit comments