Coverage for /home/runner/.local/share/hatch/env/virtual/importnb/KA2AwMZG/test.interactive/lib/python3.9/site-packages/importnb/finder.py: 95%
57 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-02 04:03 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-02 04:03 +0000
1# coding: utf-8
2"""# `sys.path_hook` modifiers 1ikjfld
4Many 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"""
7import inspect 1ikjfld
8import sys 1ikjfld
9from importlib._bootstrap_external import FileFinder 1ikjfld
10from importlib.machinery import ModuleSpec 1ikjfld
11from pathlib import Path 1ikjfld
14class FileModuleSpec(ModuleSpec): 1ikjfld
15 def __init__(self, *args, **kwargs): 1ikjfld
16 super().__init__(*args, **kwargs) 1yijdabzAc
17 self._set_fileattr = True 1yijdabzAc
20class FuzzySpec(FileModuleSpec): 1ikjfld
21 def __init__( 1ikjfld
22 self, name, loader, *, alias=None, origin=None, loader_state=None, is_package=None
23 ):
24 super().__init__( 1abc
25 name,
26 loader,
27 origin=origin,
28 loader_state=loader_state,
29 is_package=is_package,
30 )
31 self.alias = alias 1abc
34def fuzzy_query(str): 1ikjfld
35 new = "" 1gabhec
36 for chr in str: 1gabhec
37 new += (not new.endswith("__") or chr != "_") and chr or "" 1gabhec
38 return new.replace("__", "*").replace("_", "?") 1gabhec
41def fuzzy_file_search(path, fullname): 1ikjfld
42 results = [] 1gabhec
43 id, details = get_loader_details() 1gabhec
44 for ext in sum((list(object[1]) for object in details), []): 1gabhec
45 results.extend(Path(path).glob(fullname + ext)) 1gabhec
46 "_" in fullname and results.extend(Path(path).glob(fuzzy_query(fullname) + ext)) 1gabhec
47 return results 1gabhec
50class FuzzyFinder(FileFinder): 1ikjfld
51 """Adds the ability to open file names with special characters using underscores."""
53 def find_spec(self, fullname, target=None): 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) 1fmdgnopabqrhstuvewc
58 raw = fullname 1fmdgnopabqrhstuvewc
59 if spec is None: 1fmdgnopabqrhstuvewc
60 original = fullname 1gabhec
62 if "." in fullname: 1gabhec
63 original, fullname = fullname.rsplit(".", 1) 1e
64 else:
65 original, fullname = "", original 1gabhec
67 if "_" in fullname: 1gabhec
68 # find any files using the fuzzy convention
69 files = fuzzy_file_search(self.path, fullname) 1gabhec
70 if files: 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] 1abc
73 name = file.stem 1abc
74 if original: 74 ↛ 75line 74 didn't jump to line 75, because the condition on line 74 was never true1abc
75 name = ".".join((original, name))
76 name = (original + "." + file.stem).lstrip(".") 1abc
77 spec = super().find_spec(name, target=target) 1abc
78 spec = spec and FuzzySpec( 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 1fmdgnopabqrhstuvewc
89def get_loader_details(): 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 complete1fmdgxnopabqrhstuvewc
91 try: 1fmdgxnopabqrhstuvewc
92 return ( 1fmdgxnopabqrhstuvewc
93 id,
94 list(inspect.getclosurevars(path_hook).nonlocals["loader_details"]),
95 )
96 except: 1fmdgxnopabqrhstuvewc
97 continue 1fmdgxnopabqrhstuvewc
100def get_loader_index(ext): 1ikjfld
101 path_id, details = get_loader_details() 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 complete1fmdgxnopabqrhstuvewc
103 if ext in exts: 1fmdgxnopabqrhstuvewc
104 return path_id, i, details 1fmdgxnopabqrhstuvewc