Пример #1
0
    def _register_listener(
        self,
        func: Callable[..., BoltResponse],
        primary_matcher: ListenerMatcher,
        matchers: Optional[List[Callable[..., bool]]],
        middleware: Optional[List[Union[Callable, Middleware]]],
        auto_acknowledgement: bool = False,
    ) -> None:

        listener_matchers = [
            CustomListenerMatcher(app_name=self.name, func=f)
            for f in (matchers or [])
        ]
        listener_matchers.insert(0, primary_matcher)
        listener_middleware = []
        for m in middleware or []:
            if isinstance(m, Middleware):
                listener_middleware.append(m)
            elif isinstance(m, Callable):
                listener_middleware.append(
                    CustomMiddleware(app_name=self.name, func=m))
            else:
                raise ValueError(
                    f"Unexpected value for a listener middleware: {type(m)}")

        self._listeners.append(
            CustomListener(
                app_name=self.name,
                func=func,
                matchers=listener_matchers,
                middleware=listener_middleware,
                auto_acknowledgement=auto_acknowledgement,
            ))
Пример #2
0
 def to_listener_matchers(
     app_name: str,
     matchers: Optional[List[Union[Callable[..., bool], ListenerMatcher]]],
 ) -> List[ListenerMatcher]:
     _matchers = []
     if matchers is not None:
         for m in matchers:
             if isinstance(m, ListenerMatcher):
                 _matchers.append(m)
             elif isinstance(m, Callable):
                 _matchers.append(CustomListenerMatcher(app_name=app_name, func=m))
             else:
                 raise ValueError(f"Invalid matcher: {type(m)}")
     return _matchers  # type: ignore
Пример #3
0
    def _register_listener(
        self,
        functions: Sequence[Callable[..., Optional[BoltResponse]]],
        primary_matcher: ListenerMatcher,
        matchers: Optional[Sequence[Callable[..., bool]]],
        middleware: Optional[Sequence[Union[Callable, Middleware]]],
        auto_acknowledgement: bool = False,
    ) -> Optional[Callable[..., Optional[BoltResponse]]]:
        value_to_return = None
        if not isinstance(functions, list):
            functions = list(functions)
        if len(functions) == 1:
            # In the case where the function is registered using decorator,
            # the registration should return the original function.
            value_to_return = functions[0]

        listener_matchers = [
            CustomListenerMatcher(app_name=self.name, func=f)
            for f in (matchers or [])
        ]
        listener_matchers.insert(0, primary_matcher)
        listener_middleware = []
        for m in middleware or []:
            if isinstance(m, Middleware):
                listener_middleware.append(m)
            elif isinstance(m, Callable):
                listener_middleware.append(
                    CustomMiddleware(app_name=self.name, func=m))
            else:
                raise ValueError(error_unexpected_listener_middleware(type(m)))

        self._listeners.append(
            CustomListener(
                app_name=self.name,
                ack_function=functions.pop(0),
                lazy_functions=functions,
                matchers=listener_matchers,
                middleware=listener_middleware,
                auto_acknowledgement=auto_acknowledgement,
            ))
        return value_to_return