| 1 |
"""Utility functions for finding modules |
| 2 |
|
| 3 |
Utility functions for finding modules on sys.path. |
| 4 |
|
| 5 |
""" |
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
#----------------------------------------------------------------------------- |
| 9 |
# Copyright (c) 2011, the IPython Development Team. |
| 10 |
# |
| 11 |
# Distributed under the terms of the Modified BSD License. |
| 12 |
# |
| 13 |
# The full license is in the file COPYING.txt, distributed with this software. |
| 14 |
#----------------------------------------------------------------------------- |
| 15 |
|
| 16 |
#----------------------------------------------------------------------------- |
| 17 |
# Imports |
| 18 |
#----------------------------------------------------------------------------- |
| 19 |
|
| 20 |
# Stdlib imports |
| 21 |
import importlib |
| 22 |
import importlib.abc |
| 23 |
import importlib.util |
| 24 |
import sys |
| 25 |
|
| 26 |
# Third-party imports |
| 27 |
|
| 28 |
# Our own imports |
| 29 |
|
| 30 |
|
| 31 |
#----------------------------------------------------------------------------- |
| 32 |
# Globals and constants |
| 33 |
#----------------------------------------------------------------------------- |
| 34 |
|
| 35 |
#----------------------------------------------------------------------------- |
| 36 |
# Local utilities |
| 37 |
#----------------------------------------------------------------------------- |
| 38 |
|
| 39 |
#----------------------------------------------------------------------------- |
| 40 |
# Classes and functions |
| 41 |
#----------------------------------------------------------------------------- |
| 42 |
|
| 43 |
def find_mod(module_name: str) -> str | None | importlib.abc.Loader: |
| 44 |
""" |
| 45 |
Find module `module_name` on sys.path, and return the path to module `module_name`. |
| 46 |
|
| 47 |
* If `module_name` refers to a module directory, then return path to `__init__` file. |
| 48 |
* If `module_name` is a directory without an __init__file, return None. |
| 49 |
|
| 50 |
* If module is missing or does not have a `.py` or `.pyw` extension, return None. |
| 51 |
* Note that we are not interested in running bytecode. |
| 52 |
|
| 53 |
* Otherwise, return the fill path of the module. |
| 54 |
|
| 55 |
Parameters |
| 56 |
---------- |
| 57 |
module_name : str |
| 58 |
|
| 59 |
Returns |
| 60 |
------- |
| 61 |
module_path : str |
| 62 |
Path to module `module_name`, its __init__.py, or None, |
| 63 |
depending on above conditions. |
| 64 |
""" |
| 65 |
spec = importlib.util.find_spec(module_name) |
| 66 |
if spec is None: |
| 67 |
return None |
| 68 |
module_path = spec.origin |
| 69 |
if module_path is None: |
| 70 |
if spec.loader is not None and spec.loader in sys.meta_path: |
| 71 |
return spec.loader |
| 72 |
return None |
| 73 |
else: |
| 74 |
split_path = module_path.split(".") |
| 75 |
if split_path[-1] in ["py", "pyw"]: |
| 76 |
return module_path |
| 77 |
else: |
| 78 |
return None |