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