Coverage for subtractor/stream.py: 98.31%

31 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 22:33:07 +00:00

1# -*- coding: utf-8 -*- 

2# pylint: disable=c-extension-no-member,expression-not-assigned,line-too-long,logging-fstring-interpolation 

3"""Create the streams.""" 

4import pathlib 

5import typing 

6 

7 

8@typing.no_type_check 

9def final_suffix_in(path, suffixes=('.png',)): 

10 """Simple post filter on the final suffix (including the dots).""" 

11 if path.is_dir(): 

12 return False 

13 final_suffix = '' if not path.suffixes else path.suffixes[-1].lower() 

14 return final_suffix in suffixes 

15 

16 

17@typing.no_type_check 

18def visit(tree_or_file_path, pre_filter=None, pre_filter_options=None, post_filter=None, post_filter_options=None): 

19 """Visit tree and yield the leaves optionally pre and post filtered with corresponding options.""" 

20 thing = pathlib.Path(tree_or_file_path) 

21 if thing.is_file(): 

22 yield thing 

23 elif pre_filter is None: 

24 if post_filter_options is None: 24 ↛ 26line 24 didn't jump to line 26, because the condition on line 24 was never false

25 post_filter_options = {} 

26 for path in thing.rglob('*'): 

27 if post_filter is None: 

28 yield path 

29 else: 

30 if post_filter(path, **post_filter_options): 

31 yield path 

32 else: 

33 if pre_filter_options is None: 

34 pre_filter_options = {} 

35 if post_filter_options is None: 

36 post_filter_options = {} 

37 for path in pre_filter(thing.rglob('*'), **pre_filter_options): 

38 if post_filter is None: 

39 yield path 

40 else: 

41 if post_filter(path, **post_filter_options): 

42 yield path