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

1# coding: utf-8

2"""# `sys.path_hook` modifiers 6 ctx 1ikjfld

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 6 ctx 1ikjfld

8 import sys 6 ctx 1ikjfld

9 from importlib._bootstrap_external import FileFinder 6 ctx 1ikjfld

10 from importlib.machinery import ModuleSpec 6 ctx 1ikjfld

11 from pathlib import Path 6 ctx 1ikjfld

12

13

14 class FileModuleSpec(ModuleSpec): 6 ctx 1ikjfld

15 def __init__(self, *args, **kwargs): 6 ctx 1ikjfld

16 super().__init__(*args, **kwargs) 9 ctx 1yijdabzAc

17 self._set_fileattr = True 9 ctx 1yijdabzAc

18

19

20 class FuzzySpec(FileModuleSpec): 6 ctx 1ikjfld

21 def __init__( 6 ctx 1ikjfld

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): 6 ctx 1ikjfld

35 new = "" 6 ctx 1gabhec

36 for chr in str: 6 ctx 1gabhec

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

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

39

40

41 def fuzzy_file_search(path, fullname): 6 ctx 1ikjfld

42 results = [] 6 ctx 1gabhec

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

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

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

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

47 return results 6 ctx 1gabhec

48

49

50 class FuzzyFinder(FileFinder): 6 ctx 1ikjfld

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

52

53 def find_spec(self, fullname, target=None): 6 ctx 1ikjfld

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) 19 ctx 1fmdgnopabqrhstuvewc

58 raw = fullname 19 ctx 1fmdgnopabqrhstuvewc

59 if spec is None: 19 ctx 1fmdgnopabqrhstuvewc

60 original = fullname 6 ctx 1gabhec

61

62 if "." in fullname: 6 ctx 1gabhec

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

64 else:

65 original, fullname = "", original 6 ctx 1gabhec

66

67 if "_" in fullname: 6 ctx 1gabhec

68 # find any files using the fuzzy convention

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

70 if files: 6 ctx 1gabhec

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 19 ctx 1fmdgnopabqrhstuvewc

87

88

89 def get_loader_details(): 6 ctx 1ikjfld

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 20 ctx 1fmdgxnopabqrhstuvewc

91 try: 20 ctx 1fmdgxnopabqrhstuvewc

92 return ( 20 ctx 1fmdgxnopabqrhstuvewc

93 id,

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

95 )

96 except: 20 ctx 1fmdgxnopabqrhstuvewc

97 continue 20 ctx 1fmdgxnopabqrhstuvewc

98

99

100 def get_loader_index(ext): 6 ctx 1ikjfld

101 path_id, details = get_loader_details() 20 ctx 1fmdgxnopabqrhstuvewc

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 20 ctx 1fmdgxnopabqrhstuvewc

103 if ext in exts: 20 ctx 1fmdgxnopabqrhstuvewc

104 return path_id, i, details 20 ctx 1fmdgxnopabqrhstuvewc