Exemple #1
0
 def __and__(self, other: Union["Rule", RuleChecker]) -> "Rule":
     checkers = self.checkers.copy()
     if isinstance(other, Rule):
         checkers |= other.checkers
     elif asyncio.iscoroutinefunction(other):
         checkers.add(other)  # type: ignore
     else:
         checkers.add(run_sync(other))
     return Rule(*checkers)
Exemple #2
0
 def __and__(self, other: Union["Rule", RuleChecker]) -> "Rule":
     checkers = [*self.checkers]
     if isinstance(other, Rule):
         checkers.extend(other.checkers)
     elif asyncio.iscoroutinefunction(other):
         checkers.append(other)
     else:
         checkers.append(run_sync(other))
     return Rule(*checkers)
Exemple #3
0
 def __or__(self, other: Union["Permission",
                               PermissionChecker]) -> "Permission":
     checkers = [*self.checkers]
     if isinstance(other, Permission):
         checkers.extend(other.checkers)
     elif asyncio.iscoroutinefunction(other):
         checkers.append(other)
     else:
         checkers.append(run_sync(other))
     return Permission(*checkers)
Exemple #4
0
 def __or__(self, other: Union["Permission",
                               PermissionChecker]) -> "Permission":
     checkers = self.checkers.copy()
     if isinstance(other, Permission):
         checkers |= other.checkers
     elif asyncio.iscoroutinefunction(other):
         checkers.add(other)  # type: ignore
     else:
         checkers.add(run_sync(other))
     return Permission(*checkers)
Exemple #5
0
    async def _solve(
        self,
        stack: Optional[AsyncExitStack] = None,
        dependency_cache: Optional[T_DependencyCache] = None,
        **kwargs: Any,
    ) -> Any:
        use_cache: bool = self.extra["use_cache"]
        dependency_cache = {} if dependency_cache is None else dependency_cache

        sub_dependent: Dependent = self.extra["dependent"]
        sub_dependent.call = cast(Callable[..., Any], sub_dependent.call)
        call = sub_dependent.call

        # solve sub dependency with current cache
        sub_values = await sub_dependent.solve(
            stack=stack,
            dependency_cache=dependency_cache,
            **kwargs,
        )

        # run dependency function
        task: asyncio.Task[Any]
        if use_cache and call in dependency_cache:
            solved = await dependency_cache[call]
        elif is_gen_callable(call) or is_async_gen_callable(call):
            assert isinstance(
                stack, AsyncExitStack
            ), "Generator dependency should be called in context"
            if is_gen_callable(call):
                cm = run_sync_ctx_manager(contextmanager(call)(**sub_values))
            else:
                cm = asynccontextmanager(call)(**sub_values)
            task = asyncio.create_task(stack.enter_async_context(cm))
            dependency_cache[call] = task
            solved = await task
        elif is_coroutine_callable(call):
            task = asyncio.create_task(call(**sub_values))
            dependency_cache[call] = task
            solved = await task
        else:
            task = asyncio.create_task(run_sync(call)(**sub_values))
            dependency_cache[call] = task
            solved = await task

        return solved