Line | |
---|
1 | from importlib import import_module
|
---|
2 | from typing import Type
|
---|
3 |
|
---|
4 | class ClassLoader:
|
---|
5 | def loadClass(self, fullclasspath:str)->Type:
|
---|
6 | '''
|
---|
7 | @param fullclasspath a string of the form full.path.to.pyfile.classname
|
---|
8 | where pyfile is the name of the module (file) without the .py
|
---|
9 | and classname is the actual name of the class inside that file
|
---|
10 | @return Type (=class object)
|
---|
11 | @raise ModuleNotFoundError if module or class not found:
|
---|
12 | '''
|
---|
13 | if not "." in fullclasspath:
|
---|
14 | raise ModuleNotFoundError("classpath misses a '.':"+fullclasspath)
|
---|
15 |
|
---|
16 | mo,cl = fullclasspath.rsplit('.', 1)
|
---|
17 | try:
|
---|
18 | mod = import_module(mo)
|
---|
19 | except ModuleNotFoundError as e:
|
---|
20 | raise e
|
---|
21 | except BaseException as e:
|
---|
22 | raise ModuleNotFoundError("Module crashes on load",e)
|
---|
23 | if hasattr(mod, cl):
|
---|
24 | return getattr(mod, cl)
|
---|
25 | raise ModuleNotFoundError("module "+mo+" does not contain class "+cl)
|
---|
26 | |
---|
Note:
See
TracBrowser
for help on using the repository browser.