def _install_handler(cls, module, install_me): # Secure the function and ensure dispatch termination when complete def packaged_fn(update, context): try: extra_args = cls._pass[module] if module in cls._pass else None install_me(update, context, extra_args) except DispatcherHandlerContinue: return except DispatcherHandlerStop: raise except Exception as err: msg = 'Error invoking ' + module + '.invoke: ' msg += traceback.format_exc() logging.error(msg) reply(update, 'Internal error.') raise DispatcherHandlerStop() cls._pre_protection_fns[module] = packaged_fn safe = Permissions.secure_module(module, packaged_fn) # Install the handler c_handler = CommandHandler(module, safe) cls._dp.add_handler(c_handler, cls._g) cls._installed[module] = c_handler