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