How to use the wrapt.ObjectProxy function in wrapt

To help you get started, we’ve selected a few wrapt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aws / aws-xray-sdk-python / aws_xray_sdk / ext / dbapi2.py View on Github external
import copy
import wrapt

from aws_xray_sdk.core import xray_recorder


class XRayTracedConn(wrapt.ObjectProxy):

    _xray_meta = None

    def __init__(self, conn, meta={}):

        super(XRayTracedConn, self).__init__(conn)
        self._xray_meta = meta

    def cursor(self, *args, **kwargs):

        cursor = self.__wrapped__.cursor(*args, **kwargs)
        return XRayTracedCursor(cursor, self._xray_meta)


class XRayTracedCursor(wrapt.ObjectProxy):
github instana / python-sensor / instana / instrumentation / pep0249.py View on Github external
return self.__wrapped__.execute(proc_name, params)

        with tracer.start_active_span(self._module_name, child_of=parent_span) as scope:
            try:
                self._collect_kvs(scope.span, proc_name)

                result = self.__wrapped__.callproc(proc_name, params)
            except Exception as e:
                if scope.span:
                    scope.span.log_exception(e)
                raise
            else:
                return result


class ConnectionWrapper(wrapt.ObjectProxy):
    __slots__ = ('_module_name', '_connect_params')

    def __init__(self, connection, module_name, connect_params):
        super(ConnectionWrapper, self).__init__(wrapped=connection)
        self._module_name = module_name
        self._connect_params = connect_params

    def cursor(self, *args, **kwargs):
        return CursorWrapper(
            cursor=self.__wrapped__.cursor(*args, **kwargs),
            module_name=self._module_name,
            connect_params=self._connect_params,
            cursor_params=(args, kwargs) if args or kwargs else None)

    def begin(self):
        return self.__wrapped__.begin()
github dany74q / python-apache-ignite / ignite.py View on Github external
def IgniteResponse(resp_dict):
    resp = resp_dict.get('response', u'') or ''
    affinity_node_id = resp_dict.get('affinityNodeId', u'')
    session_token = resp_dict.get('sessionToken', u'')
    error = resp_dict.get('error', u'')
    success_status = resp_dict.get('successStatus', u'')

    class IgniteResponseWrapper(wrapt.ObjectProxy):

        @property
        def affinity_node_id(self):
            return affinity_node_id

        @property
        def session_token(self):
            return session_token

        @property
        def success_status(self):
            return success_status

        def __repr__(self):
            return repr(resp)
github ramses-tech / ra / ra / raml.py View on Github external
if param in params:
            schema[param] = params[param]

    return schema


class RootNode(wrapt.ObjectProxy):
    "Wraps a ``ramlfications.raml.RootNode and its contained objects``"
    def __init__(self, wrapped):
        super(RootNode, self).__init__(wrapped)

        self.resources = _map_resources(ResourceNode(r)
                                       for r in wrapped.resources)


class ResourceNode(wrapt.ObjectProxy):
    """Wraps a ``ramlfications.raml.ResourceNode`` to map parameters, bodies and
    responses by a sensible key.
    """
    def __init__(self, wrapped):
        super(ResourceNode, self).__init__(wrapped)

        self.query_params     =  list_to_dict(wrapped.query_params, by='name')
        self.uri_params       =  list_to_dict(wrapped.uri_params, by='name')
        self.base_uri_params  =  list_to_dict(wrapped.base_uri_params, by='name')
        self.form_params      =  list_to_dict(wrapped.form_params, by='name')
        self.headers          =  list_to_dict(wrapped.headers, by='name')
        self.body             =  list_to_dict(wrapped.body, by='mime_type')
        self.responses        =  list_to_dict((Response(r) for r
                                               in (wrapped.responses or [])),
                                               by='code')
github epsagon / epsagon-python / epsagon / modules / db_wrapper.py View on Github external
epsagon.modules.general_wrapper.wrapper(
            DBAPIEventFactory,
            self.__wrapped__.execute,
            self,
            args,
            kwargs,
        )

    def __enter__(self):
        # raise appropriate error if api not supported (should reach the user)
        self.__wrapped__.__enter__  # pylint: disable=W0104

        return self


class ConnectionWrapper(wrapt.ObjectProxy):
    """
    A dbapi connection wrapper for tracing.
    """
    def __init__(self, connection, args, kwargs):
        super(ConnectionWrapper, self).__init__(connection)
        self._self_args = args
        self._self_kwargs = kwargs

    def cursor(self, *args, **kwargs):
        """
        Return cursor wrapper.
        :param args: args.
        :param kwargs: kwargs.
        :return: Cursorwrapper.
        """
        cursor = self.__wrapped__.cursor(*args, **kwargs)
github airbnb / omniduct / omniduct / utils / proxies.py View on Github external
def __repr__(self):
        if None in self.__tree__:
            return self.__wrapped__.__repr__()
        return ObjectProxy.__repr__(self)
github bmabey / provenance / provenance / repos.py View on Github external
See the documentation for lazy_proxy_dict for more information.
        """
        return lazy_proxy_dict(self.artifact_ids, group_artifacts_of_same_name)


def save_artifact(f, artifact_ids):

    def wrapped(*args, **kargs):
        artifact = f(*args, **kargs)
        artifact_ids.add(artifact.id)
        return artifact

    return wrapped


class RepoSpy(wrapt.ObjectProxy):

    def __init__(self, repo):
        super(RepoSpy, self).__init__(repo)
        self.artifact_ids = set()
        self.put = save_artifact(repo.put, self.artifact_ids)
        self.get_by_id = save_artifact(repo.get_by_id, self.artifact_ids)
        self.get_by_value_id = save_artifact(repo.get_by_value_id, self.artifact_ids)


@contextmanager
def capture_set(labels=None, initial_set=None):
    if initial_set:
        initial = set(t.map(_artifact_id, initial_set))
    else:
        initial = set()
github deepmind / tree / tree / __init__.py View on Github external
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
import functools
import sys
import types

import six
from six.moves import map
from six.moves import zip

try:
  import wrapt  # pylint: disable=g-import-not-at-top
  ObjectProxy = wrapt.ObjectProxy
except ImportError:
  class ObjectProxy(object):
    """Stub-class for `wrapt.ObjectProxy``."""

try:
  # pylint: disable=g-import-not-at-top
  from typing import Any, Mapping, Sequence, Union, Text, TypeVar
  # pylint: enable=g-import-not-at-top
except ImportError:
  typing_available = False
else:
  typing_available = True

try:
  from tree import _tree  # pylint: disable=g-import-not-at-top
except ImportError:
github tensorwerk / hangar-py / src / hangar / utils.py View on Github external
Parameters
    ----------
    obj: Any
        object instance implementing the __enter__ and __exit__ methods which
        should be passed through as a weakref proxy object

    Returns
    -------
    ObjectProxy
        object analogous to a plain weakproxy object.
    """
    wr = weakref.proxy(obj)
    setattr(wr, '__enter__', partial(obj.__class__.__enter__, wr))
    setattr(wr, '__exit__', partial(obj.__class__.__exit__, wr))
    obj_proxy = wrapt.ObjectProxy(wr)
    return obj_proxy