Coverage for /home/runner/.local/share/hatch/env/virtual/importnb/KA2AwMZG/test.stdlib/lib/python3.9/site-packages/importnb/finder.py: 95%

1# coding: utf-8

2"""# `sys.path_hook` modifiers 5 ctx 1hqidr

3

4 Many suggestions for importing notebooks use `sys.meta_paths`, but `importnb` relies on the `sys.path_hooks` to load any notebook in the path. `PathHooksContext` is a base class for the `importnb.Notebook` `SourceFileLoader`.

5"""

6

7 import inspect 5 ctx 1hqidr

8 import sys 5 ctx 1hqidr

9 from importlib._bootstrap_external import FileFinder 5 ctx 1hqidr

10 from importlib.machinery import ModuleSpec 5 ctx 1hqidr

11 from pathlib import Path 5 ctx 1hqidr

12

13

14 class FileModuleSpec(ModuleSpec): 5 ctx 1hqidr

15 def __init__(self, *args, **kwargs): 5 ctx 1hqidr

16 super().__init__(*args, **kwargs) 8 ctx 1xhiabvyc

17 self._set_fileattr = True 8 ctx 1xhiabvyc

18

19

20 class FuzzySpec(FileModuleSpec): 5 ctx 1hqidr

21 def __init__( 5 ctx 1hqidr

22 self, name, loader, *, alias=None, origin=None, loader_state=None, is_package=None

23 ):

24 super().__init__( 3 ctx 1abc

25 name,

26 loader,

27 origin=origin,

28 loader_state=loader_state,

29 is_package=is_package,

30 )

31 self.alias = alias 3 ctx 1abc

32

33

34 def fuzzy_query(str): 5 ctx 1hqidr

35 new = "" 6 ctx 1fabgec

36 for chr in str: 6 ctx 1fabgec

37 new += (not new.endswith("__") or chr != "_") and chr or "" 6 ctx 1fabgec

38 return new.replace("__", "*").replace("_", "?") 6 ctx 1fabgec

39

40

41 def fuzzy_file_search(path, fullname): 5 ctx 1hqidr

42 results = [] 6 ctx 1fabgec

43 id, details = get_loader_details() 6 ctx 1fabgec

44 for ext in sum((list(object[1]) for object in details), []): 6 ctx 1fabgec

45 results.extend(Path(path).glob(fullname + ext)) 6 ctx 1fabgec

46 "_" in fullname and results.extend(Path(path).glob(fuzzy_query(fullname) + ext)) 6 ctx 1fabgec

47 return results 6 ctx 1fabgec

48

49

50 class FuzzyFinder(FileFinder): 5 ctx 1hqidr

51 """Adds the ability to open file names with special characters using underscores."""

52

53 def find_spec(self, fullname, target=None): 5 ctx 1hqidr

54 """Try to finder the spec and if it cannot be found, use the underscore starring syntax

55 to identify potential matches.

56 """

57 spec = super().find_spec(fullname, target=target) 18 ctx 1djfsktablvgmnopeuc

58 raw = fullname 18 ctx 1djfsktablvgmnopeuc

59 if spec is None: 18 ctx 1djfsktablvgmnopeuc

60 original = fullname 15 ctx 1djfkablvgmnopec

61

62 if "." in fullname: 15 ctx 1djfkablvgmnopec

63 original, fullname = fullname.rsplit(".", 1) 1 ctx 1e

64 else:

65 original, fullname = "", original 15 ctx 1djfkablvgmnopec

66

67 if "_" in fullname: 15 ctx 1djfkablvgmnopec

68 # find any files using the fuzzy convention

69 files = fuzzy_file_search(self.path, fullname) 6 ctx 1fabgec

70 if files: 6 ctx 1fabgec

71 # sort and create of a path of the chosen file

72 file = sorted(files, key=lambda x: x.stat().st_mtime, reverse=True)[0] 3 ctx 1abc

73 name = file.stem 3 ctx 1abc

74 if original: 74 ↛ 75line 74 didn't jump to line 75, because the condition on line 74 was never true 3 ctx 1abc

75 name = ".".join((original, name))

76 name = (original + "." + file.stem).lstrip(".") 3 ctx 1abc

77 spec = super().find_spec(name, target=target) 3 ctx 1abc

78 spec = spec and FuzzySpec( 3 ctx 1abc

79 spec.name,

80 spec.loader,

81 origin=spec.origin,

82 loader_state=spec.loader_state,

83 alias=raw,

84 is_package=bool(spec.submodule_search_locations),

85 )

86 return spec 18 ctx 1djfsktablvgmnopeuc

87

88

89 def get_loader_details(): 5 ctx 1hqidr

90 for id, path_hook in enumerate(sys.path_hooks): 90 ↛ exitline 90 didn't return from function 'get_loader_details', because the loop on line 90 didn't complete 18 ctx 1djfwsktablgmnopeuc

91 try: 18 ctx 1djfwsktablgmnopeuc

92 return ( 18 ctx 1djfwsktablgmnopeuc

93 id,

94 list(inspect.getclosurevars(path_hook).nonlocals["loader_details"]),

95 )

96 except: 18 ctx 1djfwsktablgmnopeuc

97 continue 18 ctx 1djfwsktablgmnopeuc

98

99

100 def get_loader_index(ext): 5 ctx 1hqidr

101 path_id, details = get_loader_details() 18 ctx 1djfwsktablgmnopeuc

102 for i, (loader, exts) in enumerate(details): 102 ↛ exitline 102 didn't return from function 'get_loader_index', because the loop on line 102 didn't complete 18 ctx 1djfwsktablgmnopeuc

103 if ext in exts: 18 ctx 1djfwsktablgmnopeuc

104 return path_id, i, details 18 ctx 1djfwsktablgmnopeuc