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