source: utilitiespy/tudelft/utilities/tools/classloader.py@ 1554

Last change on this file since 1554 was 1537, checked in by wouter, 2 weeks ago

#2132 try make classloader robust against bad modules

File size: 972 bytes
Line 
1from importlib import import_module
2from typing import Type
3
4class 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.