@@ -28,11 +28,7 @@ def initialize(self, bot_handler: BotHandler) -> None:
28
28
handler_location = config .get ("handler_location" )
29
29
if not handler_location :
30
30
raise KeyError ("No `handler_location` was specified" )
31
- handle = get_handle (handler_location )
32
- if handle is None :
33
- raise Exception ("Could not get handler from handler_location." )
34
- else :
35
- self .handle = handle
31
+ self .handle = get_handle (handler_location )
36
32
37
33
help_message = config .get ("help_message" )
38
34
if not help_message :
@@ -63,7 +59,7 @@ def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> No
63
59
handler_class = WitaiHandler
64
60
65
61
66
- def get_handle (location : str ) -> Optional [ Callable [[Dict [str , Any ]], Optional [str ] ]]:
62
+ def get_handle (location : str ) -> Callable [[Dict [str , Any ]], Optional [str ]]:
67
63
"""Returns a function to be used when generating a response from Wit.ai
68
64
bot. This function is the function named `handle` in the module at the
69
65
given `location`. For an example of a `handle` function, see `doc.md`.
@@ -78,16 +74,9 @@ def get_handle(location: str) -> Optional[Callable[[Dict[str, Any]], Optional[st
78
74
Parameters:
79
75
- location: The absolute path to the module to look for `handle` in.
80
76
"""
81
- try :
82
- spec = importlib .util .spec_from_file_location ("module.name" , location )
83
- if spec is None :
84
- return None
85
- handler = importlib .util .module_from_spec (spec )
86
- loader = spec .loader
87
- if not isinstance (loader , importlib .abc .Loader ):
88
- return None
89
- loader .exec_module (handler )
90
- return handler .handle
91
- except Exception as e :
92
- print (e )
93
- return None
77
+ spec = importlib .util .spec_from_file_location ("module.name" , location )
78
+ if spec is None or spec .loader is None :
79
+ raise RuntimeError (f"Could not get handler from { location !r} ." )
80
+ handler = importlib .util .module_from_spec (spec )
81
+ spec .loader .exec_module (handler )
82
+ return handler .handle
0 commit comments