(
cls,
key: str,
path: str,
linemap: Optional[List[Tuple[int, str]]] = None,
attrs: Optional[Dict[str, Any]] = None,
)
| 1868 | |
| 1869 | @classmethod |
| 1870 | def load_by_key_path( |
| 1871 | cls, |
| 1872 | key: str, |
| 1873 | path: str, |
| 1874 | linemap: Optional[List[Tuple[int, str]]] = None, |
| 1875 | attrs: Optional[Dict[str, Any]] = None, |
| 1876 | ) -> ModuleType: |
| 1877 | if linemap is None: |
| 1878 | linemap = [] |
| 1879 | if key not in cls.cache: |
| 1880 | with open(path) as f: |
| 1881 | try: |
| 1882 | code = compile(f.read(), path, "exec") |
| 1883 | except Exception as e: |
| 1884 | raise RuntimeError( |
| 1885 | f"Failed to import {path}\n{type(e).__name__}: {e}" |
| 1886 | ) from None |
| 1887 | mod = ModuleType(f"{__name__}.{key}") |
| 1888 | mod.__file__ = path |
| 1889 | mod.key = key # type: ignore[attr-defined] |
| 1890 | exec(code, mod.__dict__, mod.__dict__) |
| 1891 | sys.modules[mod.__name__] = mod |
| 1892 | # another thread might set this first |
| 1893 | cls.cache.setdefault(key, mod) |
| 1894 | # unzip into separate lines/nodes lists |
| 1895 | cls.linemaps[path] = list(zip(*linemap)) |
| 1896 | |
| 1897 | if attrs is not None: |
| 1898 | for k, v in attrs.items(): |
| 1899 | setattr(mod, k, v) |
| 1900 | |
| 1901 | return cls.cache[key] |
| 1902 | |
| 1903 | @classmethod |
| 1904 | @functools.lru_cache(None) |
no test coverage detected