コード例 #1
0
ファイル: _impl.py プロジェクト: NRTnarathip/vphack
from contextvars import ContextVar
import sys

current_async_library_cvar = ContextVar("current_async_library_cvar",
                                        default=None)


class AsyncLibraryNotFoundError(RuntimeError):
    pass


def current_async_library():
    """Detect which async library is currently running.

    The following libraries are currently supported:

    ================   ===========  ============================
    Library             Requires     Magic string
    ================   ===========  ============================
    **Trio**            Trio v0.6+   ``"trio"``
    **Curio**           -            ``"curio"``
    **asyncio**                      ``"asyncio"``
    **Trio-asyncio**    v0.8.2+     ``"trio"`` or ``"asyncio"``,
                                    depending on current mode
    ================   ===========  ============================

    Returns:
      A string like ``"trio"``.

    Raises:
      AsyncLibraryNotFoundError: if called from synchronous context,
コード例 #2
0
from nonebot.log import logger
from nonebot.matcher import Matcher
from nonebot.permission import Permission
from nonebot.typing import T_State, T_StateFactory, T_Handler, T_RuleChecker
from nonebot.rule import Rule, startswith, endswith, keyword, command, shell_command, ArgumentParser, regex

if TYPE_CHECKING:
    from nonebot.adapters import Bot, Event, MessageSegment

plugins: Dict[str, "Plugin"] = {}
"""
:类型: ``Dict[str, Plugin]``
:说明: 已加载的插件
"""

_export: ContextVar["Export"] = ContextVar("_export")
_tmp_matchers: ContextVar[Set[Type[Matcher]]] = ContextVar("_tmp_matchers")


class Export(dict):
    """
    :说明:

      插件导出内容以使得其他插件可以获得。

    :示例:

    .. code-block:: python

        nonebot.export().default = "bar"
コード例 #3
0
class HandlerTimeoutError(PermanentError):
    """ An error for the handler's timeout (if set). """


class HandlerRetriesError(PermanentError):
    """ An error for the handler's retries exceeded (if set). """


class HandlerChildrenRetry(TemporaryError):
    """ An internal pseudo-error to retry for the next sub-handlers attempt. """


# The task-local context; propagated down the stack instead of multiple kwargs.
# Used in `@kopf.on.this` and `kopf.execute()` to add/get the sub-handlers.
sublifecycle_var: ContextVar[Optional[lifecycles.LifeCycleFn]] = ContextVar(
    'sublifecycle_var')
subregistry_var: ContextVar[registries.ResourceChangingRegistry] = ContextVar(
    'subregistry_var')
subsettings_var: ContextVar[configuration.OperatorSettings] = ContextVar(
    'subsettings_var')
subexecuted_var: ContextVar[bool] = ContextVar('subexecuted_var')
subrefs_var: ContextVar[Iterable[Set[handlers_.HandlerId]]] = ContextVar(
    'subrefs_var')
handler_var: ContextVar[handlers_.BaseHandler] = ContextVar('handler_var')
cause_var: ContextVar[causation.BaseCause] = ContextVar('cause_var')


async def execute(
    *,
    fns: Optional[Iterable[callbacks.ResourceChangingFn]] = None,
    handlers: Optional[Iterable[handlers_.ResourceChangingHandler]] = None,
コード例 #4
0
def context_var(name, *, default=None, has_default):
    if has_default:
        return ContextVar(name, default=default)
    else:
        return ContextVar(name)
コード例 #5
0
# -*- coding: utf-8 -*-

import time
from contextvars import ContextVar
from loguru import logger
from uuid import uuid4

from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request

CORRELATION_ID_CTX_KEY = 'correlation_id'
REQUEST_ID_CTX_KEY = 'request_id'

_correlation_id_ctx_var: ContextVar[str] = ContextVar(CORRELATION_ID_CTX_KEY,
                                                      default=None)
_request_id_ctx_var: ContextVar[str] = ContextVar(REQUEST_ID_CTX_KEY,
                                                  default=None)


def get_correlation_id() -> str:
    return _correlation_id_ctx_var.get()


def get_request_id() -> str:
    return _request_id_ctx_var.get()


class RequestContextLogMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request,
                       call_next: RequestResponseEndpoint):
        correlation_id = _correlation_id_ctx_var.set(
コード例 #6
0
from sowba.core import api
from fastapi import APIRouter
from contextvars import ContextVar
from sowba.resources.{{ cookiecutter.resource_name }}.model import (
    Base{{ cookiecutter.resource_name|capitalize }}, {{ cookiecutter.resource_name|capitalize }}Type
)

router = APIRouter()
db_context = ContextVar("{{ cookiecutter.resource_name }}_db_context")


@api(router, db_connector="RocksDBConnector", context=db_context)
class {{ cookiecutter.resource_name|capitalize }}(Base{{ cookiecutter.resource_name|capitalize }}):
    name: str = None
    type: {{ cookiecutter.resource_name|capitalize }}Type = None
コード例 #7
0
ファイル: utils.py プロジェクト: twobombs/wazuh
    Parameters
    ----------
    config_file : str
        Path to configuration file.

    Returns
    -------
    dict
        Dictionary with cluster configuration.
    """
    return read_cluster_config(config_file=config_file)


# Context vars
context_tag: ContextVar[str] = ContextVar('tag', default='')
context_subtag: ContextVar[str] = ContextVar('subtag', default='')


class ClusterFilter(logging.Filter):
    """
    Add cluster related information into cluster logs.
    """
    def __init__(self, tag: str, subtag: str, name: str = ''):
        """Class constructor.

        Parameters
        ----------
        tag : str
            First tag to show in the log - Usually describes class.
        subtag : str
コード例 #8
0
ファイル: handler.py プロジェクト: Allan0101/Telbot
import inspect
from contextvars import ContextVar
from dataclasses import dataclass
from typing import Optional, Iterable, List

ctx_data = ContextVar('ctx_handler_data')
current_handler = ContextVar('current_handler')


@dataclass
class FilterObj:
    filter: callable
    kwargs: dict
    is_async: bool


class SkipHandler(Exception):
    pass


class CancelHandler(Exception):
    pass


def _get_spec(func: callable):
    while hasattr(func, '__wrapped__'):  # Try to resolve decorated callbacks
        func = func.__wrapped__
    spec = inspect.getfullargspec(func)
    return spec

コード例 #9
0
import rx
import rx.operators as op
from rx.subject import Subject
from rx.disposable import Disposable
from contextvars import ContextVar
import wrapt

from .utils import dropargs

action_ctx = ContextVar("action_ctx", default={})

class BufferedObserver():
    def __init__(self, func):
        self.values = Subject()
        self.boundaries = Subject()
        self.grouped_values = self.values.pipe(
            op.buffer(self.boundaries)
        )

        self.sub = self.grouped_values.subscribe(func)

    def deliver_values(self):
        self.boundaries.on_next(True)

    def __call__(self, value):
        self.values.on_next(value)

    def dispose(self):
        self.sub.dispose()

def run_in_action(func):
コード例 #10
0
ファイル: server.py プロジェクト: revan57/ipapp
    max_age: Optional[Union[int, str]] = None
    path: str = "/"
    secure: Optional[bool] = None
    httponly: Optional[bool] = None
    version: Optional[str] = None
    samesite: Optional[str] = None


@dataclass
class _DelCookie:
    name: str
    domain: Optional[str] = None
    path: str = "/"


response_set_headers: ContextVar[CIMultiDict] = ContextVar(
    "response_set_headers", default=CIMultiDict())

response_set_cookies: ContextVar[List[_SetCookie]] = ContextVar(
    "response_set_cookies", default=[])
response_del_cookies: ContextVar[List[_DelCookie]] = ContextVar(
    "response_del_cookies", default=[])


class RestRpcHttpHandlerConfig(BaseModel):
    path: str = Field("/", description="Путь Rest-RPC сервера")
    healthcheck_path: str = Field("/health",
                                  description="Путь health check RPC сервера")
    shield: bool = False
    cors_enabled: bool = True
    cors_origin: str = ""
    openapi_json_url: str = Field(
コード例 #11
0
ファイル: parser.py プロジェクト: brandonwillard/hebigo
            yield f"({group})"
        else:
            yield group


def reads(hebigo):
    res = parse(lex(hebigo))
    return res


def transpile(package: resources.Package, *modules: Union[str, PurePath]):
    for module in modules:
        transpile_module(package, module + ".hebi")


QUALSYMBOL = ContextVar("QUALSYMBOL", default=None)


@contextmanager
def qualify_context(qualname):
    token = QUALSYMBOL.set(qualname)
    try:
        yield
    finally:
        QUALSYMBOL.reset(token)


def transpile_module(
        package: resources.Package,
        resource: Union[str, PurePath],
        out: Union[None, str, bytes, Path] = None,
コード例 #12
0
ファイル: agent.py プロジェクト: nardi/ajay
from contextvars import ContextVar

from .actions import PrintAction, SendAction
from .percepts import MessagePercept, ResultPercept
from .utils import eprint, anext, aiter

from .actions import GenericAction, wrap_coroutine

from .actions import act_context
from .percepts import percepts_context

from .fipa_acl import AgentIdentifier

context = zmq.Context()

start_time = ContextVar("start_time")

current_agent = ContextVar("current_agent")

def local_address(port):
    return f"tcp://localhost:{port}"

def own_address(port):
    # TODO: figure out how to find this when sending over the network.
    return local_address(port)

async def loop_time():
    loop = get_running_loop()
    return loop.time()

async def log_item(item, log):
コード例 #13
0
class BaseBot:
    """
    Base class for bot. It's raw bot.
    """
    _ctx_timeout = ContextVar('TelegramRequestTimeout')
    _ctx_token = ContextVar('BotDifferentToken')

    def __init__(
        self,
        token: base.String,
        loop: Optional[Union[asyncio.BaseEventLoop,
                             asyncio.AbstractEventLoop]] = None,
        connections_limit: Optional[base.Integer] = None,
        proxy: Optional[base.String] = None,
        proxy_auth: Optional[aiohttp.BasicAuth] = None,
        validate_token: Optional[base.Boolean] = True,
        parse_mode: typing.Optional[base.String] = None,
        timeout: typing.Optional[typing.Union[base.Integer, base.Float,
                                              aiohttp.ClientTimeout]] = None):
        """
        Instructions how to get Bot token is found here: https://core.telegram.org/bots#3-how-do-i-create-a-bot

        :param token: token from @BotFather
        :type token: :obj:`str`
        :param loop: event loop
        :type loop: Optional Union :obj:`asyncio.BaseEventLoop`, :obj:`asyncio.AbstractEventLoop`
        :param connections_limit: connections limit for aiohttp.ClientSession
        :type connections_limit: :obj:`int`
        :param proxy: HTTP proxy URL
        :type proxy: :obj:`str`
        :param proxy_auth: Authentication information
        :type proxy_auth: Optional :obj:`aiohttp.BasicAuth`
        :param validate_token: Validate token.
        :type validate_token: :obj:`bool`
        :param parse_mode: You can set default parse mode
        :type parse_mode: :obj:`str`
        :param timeout: Request timeout
        :type timeout: :obj:`typing.Optional[typing.Union[base.Integer, base.Float, aiohttp.ClientTimeout]]`
        :raise: when token is invalid throw an :obj:`aiogram.utils.exceptions.ValidationError`
        """
        # Authentication
        if validate_token:
            api.check_token(token)
        self._token = None
        self.__token = token

        self.proxy = proxy
        self.proxy_auth = proxy_auth

        # Asyncio loop instance
        if loop is None:
            loop = asyncio.get_event_loop()
        self.loop = loop

        # aiohttp main session
        ssl_context = ssl.create_default_context(cafile=certifi.where())

        if isinstance(proxy, str) and (proxy.startswith('socks5://')
                                       or proxy.startswith('socks4://')):
            from aiohttp_socks import SocksConnector
            from aiohttp_socks.helpers import parse_socks_url

            socks_ver, host, port, username, password = parse_socks_url(proxy)
            if proxy_auth:
                if not username:
                    username = proxy_auth.login
                if not password:
                    password = proxy_auth.password

            connector = SocksConnector(socks_ver=socks_ver,
                                       host=host,
                                       port=port,
                                       username=username,
                                       password=password,
                                       limit=connections_limit,
                                       ssl_context=ssl_context,
                                       rdns=True,
                                       loop=self.loop)

            self.proxy = None
            self.proxy_auth = None
        else:
            connector = aiohttp.TCPConnector(limit=connections_limit,
                                             ssl=ssl_context,
                                             loop=self.loop)
        self._timeout = None
        self.timeout = timeout

        self.session = aiohttp.ClientSession(connector=connector,
                                             loop=self.loop,
                                             json_serialize=json.dumps)

        self.parse_mode = parse_mode

    def __del__(self):
        if not hasattr(self, 'loop'):
            return
        if self.loop.is_running():
            self.loop.create_task(self.close())
            return
        loop = asyncio.new_event_loop()
        loop.run_until_complete(self.close())

    @staticmethod
    def _prepare_timeout(
        value: typing.Optional[typing.Union[base.Integer, base.Float,
                                            aiohttp.ClientTimeout]]
    ) -> typing.Optional[aiohttp.ClientTimeout]:
        if value is None or isinstance(value, aiohttp.ClientTimeout):
            return value
        return aiohttp.ClientTimeout(total=value)

    @property
    def timeout(self):
        timeout = self._ctx_timeout.get(self._timeout)
        if timeout is None:
            return sentinel
        return timeout

    @timeout.setter
    def timeout(self, value):
        self._timeout = self._prepare_timeout(value)

    @timeout.deleter
    def timeout(self):
        self.timeout = None

    @contextlib.contextmanager
    def request_timeout(self, timeout: typing.Union[base.Integer, base.Float,
                                                    aiohttp.ClientTimeout]):
        """
        Context manager implements opportunity to change request timeout in current context

        :param timeout: Request timeout
        :type timeout: :obj:`typing.Optional[typing.Union[base.Integer, base.Float, aiohttp.ClientTimeout]]`
        :return:
        """
        timeout = self._prepare_timeout(timeout)
        token = self._ctx_timeout.set(timeout)
        try:
            yield
        finally:
            self._ctx_timeout.reset(token)

    @property
    def __token(self):
        return self._ctx_token.get(self._token)

    @__token.setter
    def __token(self, value):
        self._token = value

    @contextlib.contextmanager
    def with_token(self,
                   bot_token: base.String,
                   validate_token: Optional[base.Boolean] = True):
        if validate_token:
            api.check_token(bot_token)
        token = self._ctx_token.set(bot_token)
        try:
            yield
        finally:
            self._ctx_token.reset(token)

    async def close(self):
        """
        Close all client sessions
        """
        await self.session.close()

    async def request(self,
                      method: base.String,
                      data: Optional[Dict] = None,
                      files: Optional[Dict] = None,
                      **kwargs) -> Union[List, Dict, base.Boolean]:
        """
        Make an request to Telegram Bot API

        https://core.telegram.org/bots/api#making-requests

        :param method: API method
        :type method: :obj:`str`
        :param data: request parameters
        :type data: :obj:`dict`
        :param files: files
        :type files: :obj:`dict`
        :return: result
        :rtype: Union[List, Dict]
        :raise: :obj:`aiogram.exceptions.TelegramApiError`
        """
        return await api.make_request(self.session,
                                      self.__token,
                                      method,
                                      data,
                                      files,
                                      proxy=self.proxy,
                                      proxy_auth=self.proxy_auth,
                                      timeout=self.timeout,
                                      **kwargs)

    async def download_file(
            self,
            file_path: base.String,
            destination: Optional[base.InputFile] = None,
            timeout: Optional[base.Integer] = sentinel,
            chunk_size: Optional[base.Integer] = 65536,
            seek: Optional[base.Boolean] = True
    ) -> Union[io.BytesIO, io.FileIO]:
        """
        Download file by file_path to destination

        if You want to automatically create destination (:class:`io.BytesIO`) use default
        value of destination and handle result of this method.

        :param file_path: file path on telegram server (You can get it from :obj:`aiogram.types.File`)
        :type file_path: :obj:`str`
        :param destination: filename or instance of :class:`io.IOBase`. For e. g. :class:`io.BytesIO`
        :param timeout: Integer
        :param chunk_size: Integer
        :param seek: Boolean - go to start of file when downloading is finished.
        :return: destination
        """
        if destination is None:
            destination = io.BytesIO()

        url = self.get_file_url(file_path)

        dest = destination if isinstance(destination, io.IOBase) else open(
            destination, 'wb')
        async with self.session.get(url,
                                    timeout=timeout,
                                    proxy=self.proxy,
                                    proxy_auth=self.proxy_auth) as response:
            while True:
                chunk = await response.content.read(chunk_size)
                if not chunk:
                    break
                dest.write(chunk)
                dest.flush()
        if seek:
            dest.seek(0)
        return dest

    def get_file_url(self, file_path):
        return api.Methods.file_url(token=self.__token, path=file_path)

    async def send_file(self, file_type, method, file,
                        payload) -> Union[Dict, base.Boolean]:
        """
        Send file

        https://core.telegram.org/bots/api#inputfile

        :param file_type: field name
        :param method: API method
        :param file: String or io.IOBase
        :param payload: request payload
        :return: response
        """
        if file is None:
            files = {}
        elif isinstance(file, str):
            # You can use file ID or URL in the most of requests
            payload[file_type] = file
            files = None
        else:
            files = {file_type: file}

        return await self.request(method, payload, files)

    @property
    def parse_mode(self):
        return getattr(self, '_parse_mode', None)

    @parse_mode.setter
    def parse_mode(self, value):
        if value is None:
            setattr(self, '_parse_mode', None)
        else:
            if not isinstance(value, str):
                raise TypeError(f"Parse mode must be str, not {type(value)}")
            value = value.lower()
            if value not in ParseMode.all():
                raise ValueError(
                    f"Parse mode must be one of {ParseMode.all()}")
            setattr(self, '_parse_mode', value)

    @parse_mode.deleter
    def parse_mode(self):
        self.parse_mode = None

    def check_auth_widget(self, data):
        return check_integrity(self.__token, data)
コード例 #14
0
 def __init__(self, name: str, default: object):
     # pylint: disable=super-init-not-called
     self.name = name
     self.contextvar = ContextVar(name)  # type: ContextVar[object]
     self.default = base_context.wrap_callable(
         default)  # type: typing.Callable[..., object]
コード例 #15
0
    and holds global objects and configuration.
    """
    def __init__(self, app, *args, **kwargs):
        self.app = app
        super().__init__(*args, **kwargs)

    @property
    def database(self):
        return self.app.database

    @property
    def plugins(self):
        return self.app.plugins


_sparrow_context: ContextVar[SparrowContext] = ContextVar("sparrow-context",
                                                          default=None)


def _setup_context(app):
    log.debug("Setting up application context")
    ctx = SparrowContext(app)
    _sparrow_context.set(ctx)


def app_context() -> SparrowContext:
    return _sparrow_context.get()


def get_sparrow_app(create=True):
    from .app.base import Sparrow
コード例 #16
0
ファイル: model.py プロジェクト: richardliaw/thinc
import functools
import threading

from .backends import ParamServer, Ops, NumpyOps, CupyOps, get_current_ops
from .optimizers import Optimizer  # noqa: F401
from .shims import Shim
from .util import convert_recursive, is_xp_array
from .util import partial, validate_fwd_input_output
from .types import FloatsXd


InT = TypeVar("InT")
OutT = TypeVar("OutT")
SelfT = TypeVar("SelfT", bound="Model")

context_operators: ContextVar[dict] = ContextVar("context_operators", default={})
DATA_VALIDATION: ContextVar[bool] = ContextVar("DATA_VALIDATION", default=True)


def empty_init(model: "Model", *args, **kwargs) -> "Model":
    return model


class Model(Generic[InT, OutT]):
    """Class for implementing Thinc models and layers."""

    global_id: int = 0
    global_id_lock: threading.Lock = threading.Lock()
    _context_operators = context_operators

    name: str
コード例 #17
0
from contextvars import ContextVar


class EventContext:
    __slots__ = ["__id", "__flow_id"]

    def __init__(self, id, flow_id):
        self.__id = id
        self.__flow_id = flow_id

    @property
    def id(self):
        return self.__id

    @property
    def flow_id(self):
        return self.__flow_id


_context: ContextVar[EventContext] = ContextVar("event_context", default=None)
コード例 #18
0
_COLLECTABLE_STATE_ATTRIBUTES = {
    "state",
    "attributes",
    "last_changed",
    "last_updated",
    "context",
    "domain",
    "object_id",
    "name",
}

ALL_STATES_RATE_LIMIT = timedelta(minutes=1)
DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1)

template_cv: ContextVar[str | None] = ContextVar("template_cv", default=None)


@bind_opp
def attach(opp: OpenPeerPower, obj: Any) -> None:
    """Recursively attach opp to all template instances in list and dict."""
    if isinstance(obj, list):
        for child in obj:
            attach(opp, child)
    elif isinstance(obj, collections.abc.Mapping):
        for child_key, child_value in obj.items():
            attach(opp, child_key)
            attach(opp, child_value)
    elif isinstance(obj, Template):
        obj.opp = opp
コード例 #19
0
# We can add a sink to process the yielded values.  A sink can be
# 1) another agent, 2) a channel/topic, or 3) or callable accepting
# the value as a single argument (that callable can be async or non-async):
#
# async def my_sink(result: bool) -> None:
#    if result:
#        print('Agent withdraw just sent an alert!')
#
# withdraw.add_sink(my_sink)
#
# TIP: Sinks can also be added as an argument to the ``@agent`` decorator:
#      ``@app.agent(sinks=[other_agent])``.


_current_agent: ContextVar[Optional[AgentT]]
_current_agent = ContextVar('current_agent')


def current_agent() -> Optional[AgentT]:
    return _current_agent.get(None)


class Agent(AgentT, Service):
    """Agent.

    This is the type of object returned by the ``@app.agent`` decorator.
    """

    # supervisor is None until the agent is started so we cast to simplify.
    supervisor: SupervisorStrategyT = cast(SupervisorStrategyT, None)
コード例 #20
0
ファイル: matcher.py プロジェクト: luochann/nonebot2
from nonebot.rule import Rule
from nonebot.log import logger
from nonebot.permission import Permission, USER
from nonebot.typing import T_State, T_StateFactory, T_Handler, T_ArgsParser, T_TypeUpdater, T_PermissionUpdater
from nonebot.exception import PausedException, RejectedException, FinishedException, StopPropagation

if TYPE_CHECKING:
    from nonebot.adapters import Bot, Event, Message, MessageSegment

matchers: Dict[int, List[Type["Matcher"]]] = defaultdict(list)
"""
:类型: ``Dict[int, List[Type[Matcher]]]``
:说明: 用于存储当前所有的事件响应器
"""
current_bot: ContextVar = ContextVar("current_bot")
current_event: ContextVar = ContextVar("current_event")


class MatcherMeta(type):

    def __repr__(self) -> str:
        return (f"<Matcher from {self.module or 'unknow'}, "  # type: ignore
                f"type={self.type}, priority={self.priority}, "  # type: ignore
                f"temp={self.temp}>")  # type: ignore

    def __str__(self) -> str:
        return repr(self)


class Matcher(metaclass=MatcherMeta):
コード例 #21
0
            )
            return

        async with self._process_updates:
            tasks = []
            for entity in self.entities.values():
                if not entity.should_poll:
                    continue
                tasks.append(entity.async_update_ha_state(True))

            if tasks:
                await asyncio.gather(*tasks)


current_platform: ContextVar[EntityPlatform | None] = ContextVar(
    "current_platform", default=None
)


@callback
def async_get_current_platform() -> EntityPlatform:
    """Get the current platform from context."""
    if (platform := current_platform.get()) is None:
        raise RuntimeError("Cannot get non-set current platform")
    return platform


@callback
def async_get_platforms(
    hass: HomeAssistant, integration_name: str
) -> list[EntityPlatform]:
コード例 #22
0
from typing import Tuple, Callable, Optional, cast, Union, Dict, Any

from thinc.initializers import glorot_uniform_init
from thinc.util import partial, get_width

from .chain import chain
from .array_getitem import ints_getitem
from ..types import Ints1d, Floats2d, Ints2d, Floats1d, Unserializable
from ..model import Model
from ..config import registry
from contextvars import ContextVar

InT = Union[Ints1d, Ints2d]
OutT = Floats2d

context_vectors: ContextVar[dict] = ContextVar("context_vectors", default={})


@registry.layers("StaticVectors.v1")
def StaticVectors(
    nO: Optional[int] = None,
    vectors: Optional[Floats2d] = None,
    *,
    column: Optional[int] = None,
    dropout: Optional[float] = None,
    init_W: Callable = glorot_uniform_init,
) -> Model[InT, OutT]:
    attrs: Dict[str, Any] = {
        "column": column,
        "vectors": Unserializable(vectors)
    }
コード例 #23
0
    get = __call__

    def __repr__(self):
        try:
            sol_items = list(self.sol)
        except Exception:
            sol_items = type(self.sol).__name__
        return f"OpTask({self.op}, sol_keys={sol_items!r})"


#: (unstable API) Populated with the :class:`_OpTask` for the currently executing operation.
#: It does not work for (deprecated) :term:`parallel execution`.
#:
#: .. seealso::
#:     The elaborate example in :ref:`hierarchical-data` section
task_context: ContextVar[_OpTask] = ContextVar("task_context")


def _do_task(task):
    """
    Un-dill the *simpler* :class:`_OpTask` & Dill the results, to pass through pool-processes.

    See https://stackoverflow.com/a/24673524/548792
    """
    ## Note, the "else" case is only for debugging aid,
    #  by skipping `_OpTask.marshal()`` call.
    #
    if isinstance(task, bytes):
        import dill

        task = dill.loads(task)
コード例 #24
0
ファイル: local.py プロジェクト: dev-johnlopez/COD-Stats
 def __init__(self) -> None:
     # Note as __setattr__ is overidden below, use the object __setattr__
     object.__setattr__(self, "_storage", ContextVar("storage"))
コード例 #25
0
from .exceptions import InvalidAPIParameters, ServerMisconfiguredError
from .manager import ManagerStatus
from .types import CORSOptions, WebMiddleware
from .utils import chunked, check_api_params

log = BraceStyleAdapter(logging.getLogger('ai.backend.gateway.etcd'))

config_defaults = {
    'volumes/_mount': '/mnt',
    'volumes/_default_host': 'local',
    'volumes/_fsprefix': '/',
    'config/api/allow-origins': '*',
    'config/docker/image/auto_pull': 'digest',
}

current_vfolder_types: ContextVar[List[str]] = ContextVar(
    'current_vfolder_types')


class ConfigServer:
    def __init__(self, app_ctx: Mapping[str, Any], etcd_addr: HostPortPair,
                 etcd_user: Optional[str], etcd_password: Optional[str],
                 namespace: str) -> None:
        # WARNING: importing etcd3/grpc must be done after forks.
        from ai.backend.common.etcd import AsyncEtcd
        self.context = app_ctx
        credentials = None
        if etcd_user:
            credentials = {
                'user': etcd_user,
                'password': etcd_password,
            }
コード例 #26
0
ファイル: streams.py プロジェクト: yunstanford/faust
                         loop: Optional[asyncio.AbstractEventLoop] = None
                         ) -> None:
        # see module: aiocontextvars.inherit
        # this is the backport of the contextvars module added in CPython 3.7.
        # it provides "thread-locals" for async generators, and asyncio.Task
        # will automatically call this stuff in 3.7, but not in 3.6 so we call
        # this when starting to iterate over the stream (Stream.__aiter__).
        task = asyncio.Task.current_task(loop=loop or asyncio.get_event_loop())
        # note: in actual CPython it's task._context, the aiocontextvars
        # backport is a backport of a previous version of the PEP: :pep:`560`
        task.ctx = Context(Context.current())  # type: ignore


if typing.TYPE_CHECKING:  # pragma: no cover
    _current_event: ContextVar[weakref.ReferenceType[EventT]]
_current_event = ContextVar('current_event')


def current_event() -> Optional[EventT]:
    """Return the event currently being processed, or None."""
    eventref = _current_event.get(None)  # type: ignore
    return eventref() if eventref is not None else None


async def maybe_forward(value: Any, channel: ChannelT) -> Any:
    if isinstance(value, EventT):
        await value.forward(channel)
    else:
        await channel.send(value=value)
    return value
コード例 #27
0
ファイル: auth.py プロジェクト: blaisep/kopf
import os
import ssl
import tempfile
import warnings
from contextvars import ContextVar
from typing import Optional, Callable, Any, TypeVar, Dict, Iterator, Mapping, cast

import aiohttp

from kopf.structs import credentials
from kopf.structs import resources

# Per-operator storage and exchange point for authentication methods.
# Used by the client wrappers to retrieve the credentials and report the failures.
# Set by `spawn_tasks`, so that every operator's task has the same vault.
vault_var: ContextVar[credentials.Vault] = ContextVar('vault_var')

# A typevar to show that we return a function with the same signature as given.
_F = TypeVar('_F', bound=Callable[..., Any])


def reauthenticated_request(fn: _F) -> _F:
    """
    A client-specific decorator to re-authenticate a one-time request.

    If a wrapped function fails on the authentication, this will be reported
    back to the credentials container, which will trigger the re-authentication
    activity. Meanwhile, the request-performing function will be awaiting
    for the new credentials, and re-executed once they are available.
    """
    @functools.wraps(fn)
コード例 #28
0
                "item_id": item_id,
                "run_id": str(self._child_run_id),
            }
        if self._variables:
            result["changed_variables"] = self._variables
        if self._error is not None:
            result["error"] = str(self._error)
        if self._result is not None:
            result["result"] = self._result
        return result


# Context variables for tracing
# Current trace
trace_cv: ContextVar[dict[str, deque[TraceElement]] | None] = ContextVar(
    "trace_cv", default=None
)
# Stack of TraceElements
trace_stack_cv: ContextVar[list[TraceElement] | None] = ContextVar(
    "trace_stack_cv", default=None
)
# Current location in config tree
trace_path_stack_cv: ContextVar[list[str] | None] = ContextVar(
    "trace_path_stack_cv", default=None
)
# Copy of last variables
variables_cv: ContextVar[Any | None] = ContextVar("variables_cv", default=None)
# (domain.item_id, Run ID)
trace_id_cv: ContextVar[tuple[str, str] | None] = ContextVar(
    "trace_id_cv", default=None
)
コード例 #29
0
ファイル: utils.py プロジェクト: vaishali4000/SentryDemoTest
    try:
        return "%s.%s.%s" % (
            func.im_class.__module__,  # type: ignore
            func.im_class.__name__,  # type: ignore
            func.__name__,
        )
    except Exception:
        pass

    func_qualname = (
        getattr(func, "__qualname__", None) or getattr(func, "__name__", None) or None
    )  # type: Optional[str]

    if not func_qualname:
        # No idea what it is
        return None

    # Methods in Python 3
    # Functions
    # Classes
    try:
        return "%s.%s" % (func.__module__, func_qualname)
    except Exception:
        pass

    # Possibly a lambda
    return func_qualname


disable_capture_event = ContextVar("disable_capture_event")
コード例 #30
0
import sysconfig
from typing import List, Any, Tuple, Union
import warnings

from bugsnag.sessiontracker import SessionMiddleware
from bugsnag.middleware import DefaultMiddleware, MiddlewareStack
from bugsnag.utils import (fully_qualified_class_name, validate_str_setter,
                           validate_bool_setter, validate_iterable_setter,
                           validate_required_str_setter)
from bugsnag.delivery import (create_default_delivery, DEFAULT_ENDPOINT,
                              DEFAULT_SESSIONS_ENDPOINT)
from bugsnag.uwsgi import warn_if_running_uwsgi_without_threads

try:
    from contextvars import ContextVar
    _request_info = ContextVar('bugsnag-request', default=None)  # type: ignore
except ImportError:
    from bugsnag.utils import ThreadContextVar
    _request_info = ThreadContextVar('bugsnag-request', default=None)  # type: ignore  # noqa: E501


__all__ = ('Configuration', 'RequestConfiguration')


class Configuration:
    """
    Global app-level Bugsnag configuration settings.
    """
    def __init__(self):
        self.api_key = os.environ.get('BUGSNAG_API_KEY', None)
        self.release_stage = os.environ.get("BUGSNAG_RELEASE_STAGE",