Coverage for /home/runner/.local/share/hatch/env/virtual/importnb/KA2AwMZG/test.stdlib/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:02 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-02 04:02 +0000
1# coding: utf-8
2"""# `sys.path_hook` modifiers 1hqidr
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 1hqidr
8import sys 1hqidr
9from importlib._bootstrap_external import FileFinder 1hqidr
10from importlib.machinery import ModuleSpec 1hqidr
11from pathlib import Path 1hqidr
14class FileModuleSpec(ModuleSpec): 1hqidr
15 def __init__(self, *args, **kwargs): 1hqidr
16 super().__init__(*args, **kwargs) 1xhiabvyc
17 self._set_fileattr = True 1xhiabvyc
20class FuzzySpec(FileModuleSpec): 1hqidr
21 def __init__( 1hqidr
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): 1hqidr
35 new = "" 1fabgec
36 for chr in str: 1fabgec
37 new += (not new.endswith("__") or chr != "_") and chr or "" 1fabgec
38 return new.replace("__", "*").replace("_", "?") 1fabgec
41def fuzzy_file_search(path, fullname): 1hqidr
42 results = [] 1fabgec
43 id, details = get_loader_details() 1fabgec
44 for ext in sum((list(object[1]) for object in details), []): 1fabgec
45 results.extend(Path(path).glob(fullname + ext)) 1fabgec
46 "_" in fullname and results.extend(Path(path).glob(fuzzy_query(fullname) + ext)) 1fabgec
47 return results 1fabgec
50class FuzzyFinder(FileFinder): 1hqidr
51 """Adds the ability to open file names with special characters using underscores."""
53 def find_spec(self, fullname, target=None): 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) 1djfsktablvgmnopeuc
58 raw = fullname 1djfsktablvgmnopeuc
59 if spec is None: 1djfsktablvgmnopeuc
60 original = fullname 1djfkablvgmnopec
62 if "." in fullname: 1djfkablvgmnopec
63 original, fullname = fullname.rsplit(".", 1) 1e
64 else:
65 original, fullname = "", original 1djfkablvgmnopec
67 if "_" in fullname: 1djfkablvgmnopec
68 # find any files using the fuzzy convention
69 files = fuzzy_file_search(self.path, fullname) 1fabgec
70 if files: 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] 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 1djfsktablvgmnopeuc
89def get_loader_details(): 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 complete1djfwsktablgmnopeuc
91 try: 1djfwsktablgmnopeuc
92 return ( 1djfwsktablgmnopeuc
93 id,
94 list(inspect.getclosurevars(path_hook).nonlocals["loader_details"]),
95 )
96 except: 1djfwsktablgmnopeuc
97 continue 1djfwsktablgmnopeuc
100def get_loader_index(ext): 1hqidr
101 path_id, details = get_loader_details() 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 complete1djfwsktablgmnopeuc
103 if ext in exts: 1djfwsktablgmnopeuc
104 return path_id, i, details 1djfwsktablgmnopeuc