Example #1
0
    def executeMethod(self, interfaceObj, methodName, methodArguments, sender):
        m = getattr(self, 'dbus_' + methodName, None)

        iname = interfaceObj.name
        
        if m is None:
            m = self._getDecoratedMethod( iname, methodName )
            if m is None:
                raise NotImplementedError

        if hasattr(m, '_dbusInterface') and m._dbusInterface != iname:
            m = self._getDecoratedMethod( iname, methodName )
            if m is None:
                raise NotImplementedError

        if not hasattr(six.get_method_function(m), '_dbusCaller'):
            self._set_method_flags(m)

        if six.get_method_function(m)._dbusCaller:
            if methodArguments:
                return m( *methodArguments, dbusCaller = sender )
            else:
                return m( dbusCaller = sender )
        else:
            if methodArguments:
                return m( *methodArguments )
            else:
                return m()
Example #2
0
File: dill.py Project: wxiang7/dill
def save_instancemethod0(pickler, obj):  # example: cStringIO.StringI
    log.info("Me: %s" % obj)             # XXX: obj.__dict__ handled elsewhere?

    args = (get_method_function(obj), get_method_self(obj)) if PY3 \
        else (get_method_function(obj), get_method_self(obj), obj.im_class)

    pickler.save_reduce(MethodType, args, obj=obj)

    log.info("# Me")
Example #3
0
    def __eq__(self, other):
        r"""
        Return whether ``self == other``.

        EXAMPLES::

            sage: C = IntegerListsLex(2, length=3)
            sage: D = IntegerListsLex(2, length=3); L = D.list();
            sage: E = IntegerListsLex(2, min_length=3)
            sage: F = IntegerListsLex(2, length=3, element_constructor=list)
            sage: G = IntegerListsLex(4, length=3)
            sage: C == C
            True
            sage: C == D
            True
            sage: C == E
            False
            sage: C == F
            False
            sage: C == None
            False
            sage: C == G
            False

        This is a minimal implementation enabling pickling tests. It
        is safe, but one would want the two following objects to be
        detected as equal::

            sage: C = IntegerListsLex(2, ceiling=[1,1,1])
            sage: D = IntegerListsLex(2, ceiling=[1,1,1])
            sage: C == D
            False

        TESTS:

        This used to fail due to poor equality testing. See
        :trac:`17979`, comment 433::

            sage: DisjointUnionEnumeratedSets(Family([2,2],
            ....:     lambda n: IntegerListsLex(n, length=2))).list()
            [[2, 0], [1, 1], [0, 2], [2, 0], [1, 1], [0, 2]]
            sage: DisjointUnionEnumeratedSets(Family([2,2],
            ....:     lambda n: IntegerListsLex(n, length=1))).list()
            [[2], [2]]
        """
        if self.__class__ != other.__class__:
            return False
        if self.backend != other.backend:
            return False
        a = self._element_constructor
        b = other._element_constructor
        if ismethod(a):
            a = get_method_function(a)
        if ismethod(b):
            b = get_method_function(b)
        return a == b
Example #4
0
    def __eq__(self, other):
        r"""
        Return whether ``self == other``.

        EXAMPLES::

            sage: C = IntegerListsLex(2, length=3)
            sage: D = IntegerListsLex(2, length=3); L = D.list();
            sage: E = IntegerListsLex(2, min_length=3)
            sage: F = IntegerListsLex(2, length=3, element_constructor=list)
            sage: G = IntegerListsLex(4, length=3)
            sage: C == C
            True
            sage: C == D
            True
            sage: C == E
            False
            sage: C == F
            False
            sage: C == None
            False
            sage: C == G
            False

        This is a minimal implementation enabling pickling tests. It
        is safe, but one would want the two following objects to be
        detected as equal::

            sage: C = IntegerListsLex(2, ceiling=[1,1,1])
            sage: D = IntegerListsLex(2, ceiling=[1,1,1])
            sage: C == D
            False

        TESTS:

        This used to fail due to poor equality testing. See
        :trac:`17979`, comment 433::

            sage: DisjointUnionEnumeratedSets(Family([2,2],
            ....:     lambda n: IntegerListsLex(n, length=2))).list()
            [[2, 0], [1, 1], [0, 2], [2, 0], [1, 1], [0, 2]]
            sage: DisjointUnionEnumeratedSets(Family([2,2],
            ....:     lambda n: IntegerListsLex(n, length=1))).list()
            [[2], [2]]
        """
        if self.__class__ != other.__class__:
            return False
        if self.backend != other.backend:
            return False
        a = self._element_constructor_
        b = other._element_constructor_
        if ismethod(a):
            a = get_method_function(a)
        if ismethod(b):
            b = get_method_function(b)
        return a == b
Example #5
0
def test_replace():
    """Replaced methods replace the original one, but are restored after the with."""
    class SomethingElse(object):
        def foo(self, n, y=None):
            assert None, 'This should never be reached in this test'

    # Case: bound method
    s = SomethingElse()

    def replacement(n, y=None):
        return y

    original_method = six.get_method_function(s.foo)

    with replaced(s.foo, replacement):
        assert s.foo(1, y='a') == 'a'
        assert s.foo(2) == None

    assert six.get_method_function(s.foo) is original_method

    # Case: unbound method
    """Python 3 does not support the concept of unbound methods, they are
    just plain functions without an im_class pointing back to their class.
    See https://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods,
    and https://mail.python.org/pipermail/python-dev/2005-January/050625.html
    for the rationale.

    To be able to support them under Python3, on= is mandatory.
    """

    s = SomethingElse()

    def replacement(self, n, y=None):
        return y

    original_method = six.get_unbound_function(SomethingElse.foo)

    with replaced(SomethingElse.foo, replacement, on=SomethingElse):
        assert s.foo(1, y='a') == 'a'
        assert s.foo(2) == None

    restored_method = six.get_unbound_function(SomethingElse.foo)
    assert restored_method is original_method

    # Case: unbound method (no on= given)
    s = SomethingElse()

    def replacement(self, n, y=None):
        return y

    with pytest.raises(
            ValueError,
            match='You have to supply a on= when stubbing an unbound method'):
        with replaced(SomethingElse.foo, replacement):
            pass
Example #6
0
    def _validate_args(self, swfuncs, tailargs):
        if six.get_method_function(self.help) in swfuncs:
            raise ShowHelp()
        if six.get_method_function(self.version) in swfuncs:
            raise ShowVersion()

        requirements = {}
        exclusions = {}
        for swinfo in self._switches_by_func.values():
            if swinfo.mandatory and not swinfo.func in swfuncs:
                raise MissingMandatorySwitch(
                    "Switch %s is mandatory" % ("/".join(
                        ("-" if len(n) == 1 else "--") + n
                        for n in swinfo.names), ))
            requirements[swinfo.func] = set(self._switches_by_name[req]
                                            for req in swinfo.requires)
            exclusions[swinfo.func] = set(self._switches_by_name[exc]
                                          for exc in swinfo.excludes)

        # TODO: compute topological order

        gotten = set(swfuncs.keys())
        for func in gotten:
            missing = set(f.func for f in requirements[func]) - gotten
            if missing:
                raise SwitchCombinationError(
                    "Given %s, the following are missing %r" %
                    (swfuncs[func].swname,
                     [self._switches_by_func[f].names[0] for f in missing]))
            invalid = set(f.func for f in exclusions[func]) & gotten
            if invalid:
                raise SwitchCombinationError(
                    "Given %s, the following are invalid %r" %
                    (swfuncs[func].swname,
                     [swfuncs[f].swname for f in invalid]))

        m_args, m_varargs, _, m_defaults = inspect.getargspec(self.main)
        max_args = six.MAXSIZE if m_varargs else len(m_args) - 1
        min_args = len(m_args) - 1 - (len(m_defaults) if m_defaults else 0)
        if len(tailargs) < min_args:
            raise PositionalArgumentsError(
                "Expected at least %d positional arguments, got %r" %
                (min_args, tailargs))
        elif len(tailargs) > max_args:
            raise PositionalArgumentsError(
                "Expected at most %d positional arguments, got %r" %
                (max_args, tailargs))

        ordered = [(f, a)
                   for _, f, a in sorted([(sf.index, f, sf.val)
                                          for f, sf in swfuncs.items()])]
        return ordered, tailargs
Example #7
0
    def __init__(self):
        # defiain main tasklet
        self._main_coroutine = _coroutine_getmain()

        self._main_tasklet = _coroutine_getcurrent()
        self._main_tasklet.__class__ = tasklet
        six.get_method_function(self._main_tasklet._init)(self._main_tasklet,
                label='main')
        self._last_task = self._main_tasklet

        self.thread_id = thread.get_ident()
        self._callback = None
        self._run_calls = []
        self._squeue = deque()
        self.append(self._main_tasklet)
Example #8
0
    def __findPrivateMethod(self):
        """Finds and returns the bound method associated with the encapsulated
        function.
        """

        obj = self.obj()
        func = self.func()
        methName = self.funcName

        # Find all attributes on the object which end with
        # the method name - there will be more than one of
        # these if the object has base classes which have
        # private methods of the same name.
        attNames = dir(obj)
        attNames = [a for a in attNames if a.endswith(methName)]

        # Find the attribute with the correct name, which
        # is a method, and has the correct function.
        for name in attNames:

            att = getattr(obj, name)

            if isinstance(att, types.MethodType) and \
               six.get_method_function(att) is func:
                return att

        return None
Example #9
0
    def __init__(self, func):
        """Create a new ``WeakFunctionRef`` to encapsulate the given
        function or bound/unbound method.
        """

        # Bound method
        if self.__isMethod(func):

            boundMeth = six.get_method_function(func)
            boundSelf = six.get_method_self(func)

            # We can't take a weakref of the method
            # object, so we have to weakref the object
            # and the unbound class function. The
            # function method will search for and
            # return the bound method, though.
            self.obj = weakref.ref(boundSelf)
            self.func = weakref.ref(boundMeth)

            self.objType = type(boundSelf).__name__
            self.funcName = boundMeth.__name__

        # Unbound/class method or function
        else:

            self.obj = None
            self.objType = None
            self.func = weakref.ref(func)
            self.funcName = func.__name__
Example #10
0
    def __call__(self, *args, **kwargs):
        """
        Call a function, injecting the keyword arguments desired by the
        function from the keys defined in this mapping.  The first
        positional parameter must be the function to invoke.
        Additional positional arguments are passed directly to the
        function, and additional keyword arguments override values
        from this mapping.  This is the entrypoint for dependency
        injection.
        """

        if len(args) < 1:
            raise TypeError('call requires at least one positional argument')

        # Split the function and arguments
        func = args[0]
        args = args[1:]

        # Unwrap class and instance methods
        if inspect.ismethod(func):
            obj = six.get_method_self(func)
            func = six.get_method_function(func)

            # Update the args
            args = (obj,) + args

        # Get the function's injection signature
        sig = WantSignature.from_func(func)

        # Call the function
        return sig(args, self, kwargs)
Example #11
0
 def __details__(self):
     with utils.patch(
         six.get_method_function(self.tasks.service.destroy),
         '__doc__',
         self.__doc__ + (self.tasks.service.destroy.__doc__ or ''),
     ):
         return get_task_details(self.tasks.service.destroy)
Example #12
0
    def _get_es_body(self, for_count=False):
        # If to_es has been overridden, call it and raise a deprecation warning
        if isinstance(self.query, ElasticSearchQuery) and six.get_method_function(self.query.to_es) != ElasticSearchQuery.to_es:
            warnings.warn(
                "The .to_es() method on Elasticsearch query classes is deprecated. "
                "Please rename {class_name}.to_es() to {class_name}.get_query()".format(
                    class_name=self.query.__class__.__name__
                ),
                RemovedInWagtail14Warning, stacklevel=2)

            body = {
                'query': self.query.to_es(),
            }
        else:
            body = {
                'query': self.query.get_query()
            }

        if not for_count:
            sort = self.query.get_sort()

            if sort is not None:
                body['sort'] = sort

        return body
Example #13
0
    def get_op_handler(self, operation, operation_group=None):
        """ Import and load the operation handler """
        # Patch the unversioned sdk path to include the appropriate API version for the
        # resource type in question.
        from importlib import import_module
        import types

        from azure.cli.core.profiles import AZURE_API_PROFILES
        from azure.cli.core.profiles._shared import get_versioned_sdk_path

        for rt in AZURE_API_PROFILES[self.cli_ctx.cloud.profile]:
            if operation.startswith(rt.import_prefix + '.'):
                operation = operation.replace(
                    rt.import_prefix,
                    get_versioned_sdk_path(self.cli_ctx.cloud.profile,
                                           rt,
                                           operation_group=operation_group))

        try:
            mod_to_import, attr_path = operation.split('#')
            op = import_module(mod_to_import)
            for part in attr_path.split('.'):
                op = getattr(op, part)
            if isinstance(op, types.FunctionType):
                return op
            return six.get_method_function(op)
        except (ValueError, AttributeError):
            raise ValueError(
                "The operation '{}' is invalid.".format(operation))
Example #14
0
 def __details__(self):
     with utils.patch(
             six.get_method_function(self.tasks.service.destroy),
             '__doc__',
             self.__doc__ + (self.tasks.service.destroy.__doc__ or ''),
     ):
         return get_task_details(self.tasks.service.destroy)
Example #15
0
def test_get_method_function():
    class X(object):
        def m(self):
            pass
    x = X()
    assert six.get_method_function(x.m) is X.__dict__["m"]
    py.test.raises(AttributeError, six.get_method_function, hasattr)
Example #16
0
def test_get_method_function():
    class X(object):
        def m(self):
            pass
    x = X()
    assert six.get_method_function(x.m) is X.__dict__["m"]
    py.test.raises(AttributeError, six.get_method_function, hasattr)
Example #17
0
def normal_residual_wrapper(model):
    model_call = six.get_method_function(model.call)

    def call_wrapper(object, inputs):
        def add_normal(dist_a, dist_b):
            if isinstance(dist_a, tf.Tensor):
                return tfp.distributions.Normal(
                    loc=dist_a + dist_b.loc,
                    scale=dist_b.scale)
            else:
                variance = tf.square(dist_a.scale) + tf.square(dist_b.scale)
                return tfp.distributions.Normal(
                    loc=dist_a.loc + dist_b.loc,
                    scale=tf.sqrt(variance))
        inputs, state = inputs
        outputs = model_call(object, [inputs, state])
        if isinstance(outputs, tuple):
            outputs, next_state = outputs
            return outputs, add_normal(state, next_state)
        else:
            next_state = outputs
            return add_normal(state, next_state)

    if isinstance(model, tdl.core.Layer):
        model.call = types.MethodType(call_wrapper, model)
        return model
    else:
        raise ValueError('concat wrapper does not work for type {}.'
                         ''.format(type(model)))
Example #18
0
def get_op_handler(operation):
    """ Import and load the operation handler """
    # Patch the unversioned sdk path to include the appropriate API version for the
    # resource type in question.
    from azure.cli.core._profile import CLOUD
    import types

    for rt in ResourceType:
        if operation.startswith(rt.import_prefix + ".operations."):
            subs = operation[len(rt.import_prefix + ".operations."):]
            operation_group = subs[:subs.index('_operations')]
            operation = operation.replace(
                rt.import_prefix,
                get_versioned_sdk_path(CLOUD.profile,
                                       rt,
                                       operation_group=operation_group))
        elif operation.startswith(rt.import_prefix):
            operation = operation.replace(
                rt.import_prefix, get_versioned_sdk_path(CLOUD.profile, rt))
    try:
        mod_to_import, attr_path = operation.split('#')
        op = import_module(mod_to_import)
        for part in attr_path.split('.'):
            op = getattr(op, part)
        if isinstance(op, types.FunctionType):
            return op
        return six.get_method_function(op)
    except (ValueError, AttributeError):
        raise ValueError("The operation '{}' is invalid.".format(operation))
Example #19
0
    def _get_es_body(self, for_count=False):
        # If to_es has been overridden, call it and raise a deprecation warning
        if isinstance(self.query,
                      ElasticSearchQuery) and six.get_method_function(
                          self.query.to_es) != ElasticSearchQuery.to_es:
            warnings.warn(
                "The .to_es() method on Elasticsearch query classes is deprecated. "
                "Please rename {class_name}.to_es() to {class_name}.get_query()"
                .format(class_name=self.query.__class__.__name__),
                RemovedInWagtail14Warning,
                stacklevel=2)

            body = {
                'query': self.query.to_es(),
            }
        else:
            body = {'query': self.query.get_query()}

        if not for_count:
            sort = self.query.get_sort()

            if sort is not None:
                body['sort'] = sort

        return body
Example #20
0
def monkey_patch():
    """
    Monkey patch in our own, instrumented, get_templates_hierarchy. Make
    sure to keep it a classmethod.
    """
    from widgy.models import Content
    # We need to get the unbound class method here. The bound method will use
    # the MRO of Content, which might not be the same as subclasses of Content.
    # The MRO above Content doesn't actually matter (it currently doesn't call
    # super), but if it did, using the bound classmethod here would mean that
    # the MRO stops at Content.
    old_get_templates_hierarchy_unbound = six.get_method_function(
        Content.get_templates_hierarchy)

    def new_get_templates_hierarchy(cls, **kwargs):
        res = old_get_templates_hierarchy_unbound(cls, **kwargs)
        res = unique_list(res)
        try:
            name = select_template(res).origin.name
        except TemplateDoesNotExist:
            name = [i for i in res if finders.find(i)]
        template_hierarchy_called.send(sender=cls,
                                       cls=cls,
                                       kwargs=kwargs,
                                       templates=res,
                                       used=name)
        return res

    Content.get_templates_hierarchy = classmethod(new_get_templates_hierarchy)
Example #21
0
    def get_op_handler(self, operation):
        """ Import and load the operation handler """
        # Patch the unversioned sdk path to include the appropriate API version for the
        # resource type in question.
        from importlib import import_module
        import types

        from azure.cli.core.profiles import AZURE_API_PROFILES
        from azure.cli.core.profiles._shared import get_versioned_sdk_path

        for rt in AZURE_API_PROFILES[self.cli_ctx.cloud.profile]:
            if operation.startswith(rt.import_prefix + ".operations."):
                subs = operation[len(rt.import_prefix + ".operations."):]
                operation_group = subs[:subs.index('_operations')]
                operation = operation.replace(
                    rt.import_prefix,
                    get_versioned_sdk_path(self.cli_ctx.cloud.profile, rt, operation_group=operation_group))
            elif operation.startswith(rt.import_prefix):
                operation = operation.replace(rt.import_prefix,
                                              get_versioned_sdk_path(self.cli_ctx.cloud.profile, rt))

        try:
            mod_to_import, attr_path = operation.split('#')
            op = import_module(mod_to_import)
            for part in attr_path.split('.'):
                op = getattr(op, part)
            if isinstance(op, types.FunctionType):
                return op
            return six.get_method_function(op)
        except (ValueError, AttributeError):
            raise ValueError("The operation '{}' is invalid.".format(operation))
Example #22
0
    def _init_function(self, r):
        if isinstance(self.function, str):
            self.function = self.function.lower()
            _mapped = {'inverse': 'inverse_multiquadric',
                       'inverse multiquadric': 'inverse_multiquadric',
                       'thin-plate': 'thin_plate'}
            if self.function in _mapped:
                self.function = _mapped[self.function]

            func_name = "_h_" + self.function
            if hasattr(self, func_name):
                self._function = getattr(self, func_name)
            else:
                functionlist = [x[3:] for x in dir(self) if x.startswith('_h_')]
                raise ValueError("function must be a callable or one of " +
                                     ", ".join(functionlist))
            self._function = getattr(self, "_h_"+self.function)
        elif callable(self.function):
            allow_one = False
            if hasattr(self.function, 'func_code') or \
                   hasattr(self.function, '__code__'):
                val = self.function
                allow_one = True
            elif hasattr(self.function, "im_func"):
                val = get_method_function(self.function)
            elif hasattr(self.function, "__call__"):
                val = get_method_function(self.function.__call__)
            else:
                raise ValueError("Cannot determine number of arguments to function")

            argcount = get_function_code(val).co_argcount
            if allow_one and argcount == 1:
                self._function = self.function
            elif argcount == 2:
                if sys.version_info[0] >= 3:
                    self._function = self.function.__get__(self, Rbf)
                else:
                    import new
                    self._function = new.instancemethod(self.function, self,
                                                        Rbf)
            else:
                raise ValueError("Function argument must take 1 or 2 arguments.")

        a0 = self._function(r)
        if a0.shape != r.shape:
            raise ValueError("Callable must take array and return array of the same shape")
        return a0
Example #23
0
    def __init__(self):
        # define the main tasklet
        self._main_coroutine = _coroutine_getmain()
        self._main_tasklet = _coroutine_getcurrent()
        self._main_tasklet.__class__ = tasklet
        six.get_method_function(self._main_tasklet._init)(self._main_tasklet,
                label='main')
        self._last_task = self._main_tasklet

        self.thread_id = thread_ident() # the scheduler thread id
        self._lock = threading.Lock() # global scheduler lock

        self._callback = None # scheduler callback
        self._run_calls = [] # runcalls. (tasks where run apply
        self.runnable = deque() # runnable tasks
        self.blocked = 0 # number of blocked/sleeping tasks
        self.append(self._main_tasklet)
Example #24
0
    def __init__(self):
        # define the main tasklet
        self._main_coroutine = _coroutine_getmain()
        self._main_tasklet = _coroutine_getcurrent()
        self._main_tasklet.__class__ = tasklet
        six.get_method_function(self._main_tasklet._init)(self._main_tasklet,
                                                          label='main')
        self._last_task = self._main_tasklet

        self.thread_id = thread_ident()  # the scheduler thread id
        self._lock = threading.Lock()  # global scheduler lock

        self._callback = None  # scheduler callback
        self._run_calls = []  # runcalls. (tasks where run apply
        self.runnable = deque()  # runnable tasks
        self.blocked = 0  # number of blocked/sleeping tasks
        self.append(self._main_tasklet)
Example #25
0
def unpickleMethod(im_name, im_self, im_class):
    'support function for copyreg to unpickle method refs'
    try:
        unbound = getattr(im_class, im_name)
        if im_self is None:
            return unbound
        bound = types.MethodType(get_method_function(unbound), im_self)
        return bound
    except AttributeError:
        # assert im_self is not None,"No recourse: no instance to guess from."
        # Attempt a common fix before bailing -- if classes have
        # changed around since we pickled this method, we may still be
        # able to get it by looking on the instance's current class.
        unbound = getattr(im_self.__class__, im_name)
        if im_self is None:
            return unbound
        bound = types.MethodType(get_method_function(unbound), im_self)
        return bound
Example #26
0
def _instance_overrides_method(base, instance, method_name):
    """
    Returns True if instance overrides a method (method_name)
    inherited from base.
    """
    bound_method = getattr(instance, method_name)
    unbound_method = getattr(base, method_name)
    return get_unbound_function(unbound_method) != get_method_function(
        bound_method)
Example #27
0
 def object_build(self, node, obj):
     """recursive method which create a partial ast from real objects
      (only function, class, and method are handled)
     """
     if obj in self._done:
         return self._done[obj]
     self._done[obj] = node
     for name in dir(obj):
         try:
             member = getattr(obj, name)
         except AttributeError:
             # damned ExtensionClass.Base, I know you're there !
             attach_dummy_node(node, name)
             continue
         if ismethod(member):
             member = six.get_method_function(member)
         if isfunction(member):
             # verify this is not an imported function
             filename = getattr(six.get_function_code(member),
                                'co_filename', None)
             if filename is None:
                 assert isinstance(member, object)
                 object_build_methoddescriptor(node, member, name)
             elif filename != getattr(self._module, '__file__', None):
                 attach_dummy_node(node, name, member)
             else:
                 object_build_function(node, member, name)
         elif isbuiltin(member):
             if (not _io_discrepancy(member) and
                     self.imported_member(node, member, name)):
                 continue
             object_build_methoddescriptor(node, member, name)
         elif isclass(member):
             if self.imported_member(node, member, name):
                 continue
             if member in self._done:
                 class_node = self._done[member]
                 if not class_node in node.locals.get(name, ()):
                     node.add_local_node(class_node, name)
             else:
                 class_node = object_build_class(node, member, name)
                 # recursion
                 self.object_build(class_node, member)
             if name == '__class__' and class_node.parent is None:
                 class_node.parent = self._done[self._module]
         elif ismethoddescriptor(member):
             assert isinstance(member, object)
             object_build_methoddescriptor(node, member, name)
         elif isdatadescriptor(member):
             assert isinstance(member, object)
             object_build_datadescriptor(node, member, name)
         elif type(member) in _CONSTANTS:
             attach_const_node(node, name, member)
         else:
             # create an empty node so that the name is actually defined
             attach_dummy_node(node, name, member)
Example #28
0
    def get_local_annotations(cls,
                              target,
                              exclude=None,
                              ctx=None,
                              select=lambda *p: True):
        """Get a list of local target annotations in the order of their
            definition.

        :param type cls: type of annotation to get from target.
        :param target: target from where get annotations.
        :param tuple/type exclude: annotation types to exclude from selection.
        :param ctx: target ctx.
        :param select: selection function which takes in parameters a target,
            a ctx and an annotation and returns True if the annotation has to
            be selected. True by default.

        :return: target local annotations.
        :rtype: list
        """

        result = []

        # initialize exclude
        exclude = () if exclude is None else exclude

        try:
            # get local annotations
            local_annotations = get_local_property(
                target, Annotation.__ANNOTATIONS_KEY__, result, ctx=ctx)
            if not local_annotations:
                if ismethod(target):
                    func = get_method_function(target)
                    local_annotations = get_local_property(
                        func, Annotation.__ANNOTATIONS_KEY__, result, ctx=ctx)
                    if not local_annotations:
                        local_annotations = get_local_property(
                            func, Annotation.__ANNOTATIONS_KEY__, result)
                elif isfunction(target):
                    local_annotations = get_local_property(
                        target, Annotation.__ANNOTATIONS_KEY__, result)

        except TypeError:
            raise TypeError('target {0} must be hashable'.format(target))

        for local_annotation in local_annotations:
            # check if local annotation inherits from cls
            inherited = isinstance(local_annotation, cls)
            # and if not excluded
            not_excluded = not isinstance(local_annotation, exclude)
            # and if selected
            selected = select(target, ctx, local_annotation)
            # if three conditions, add local annotation to the result
            if inherited and not_excluded and selected:
                result.append(local_annotation)

        return result
Example #29
0
 def object_build(self, node, obj):
     """recursive method which create a partial ast from real objects
      (only function, class, and method are handled)
     """
     if obj in self._done:
         return self._done[obj]
     self._done[obj] = node
     for name in dir(obj):
         try:
             member = getattr(obj, name)
         except AttributeError:
             # damned ExtensionClass.Base, I know you're there !
             attach_dummy_node(node, name)
             continue
         if ismethod(member):
             member = six.get_method_function(member)
         if isfunction(member):
             # verify this is not an imported function
             filename = getattr(six.get_function_code(member),
                                'co_filename', None)
             if filename is None:
                 assert isinstance(member, object)
                 object_build_methoddescriptor(node, member, name)
             elif filename != getattr(self._module, '__file__', None):
                 attach_dummy_node(node, name, member)
             else:
                 object_build_function(node, member, name)
         elif isbuiltin(member):
             if (not _io_discrepancy(member)
                     and self.imported_member(node, member, name)):
                 continue
             object_build_methoddescriptor(node, member, name)
         elif isclass(member):
             if self.imported_member(node, member, name):
                 continue
             if member in self._done:
                 class_node = self._done[member]
                 if not class_node in node.locals.get(name, ()):
                     node.add_local_node(class_node, name)
             else:
                 class_node = object_build_class(node, member, name)
                 # recursion
                 self.object_build(class_node, member)
             if name == '__class__' and class_node.parent is None:
                 class_node.parent = self._done[self._module]
         elif ismethoddescriptor(member):
             assert isinstance(member, object)
             object_build_methoddescriptor(node, member, name)
         elif isdatadescriptor(member):
             assert isinstance(member, object)
             object_build_datadescriptor(node, member, name)
         elif type(member) in _CONSTANTS:
             attach_const_node(node, name, member)
         else:
             # create an empty node so that the name is actually defined
             attach_dummy_node(node, name, member)
def _find_method(obj, func):
    if obj:
        try:
            func_self = six.get_method_self(func)
        except AttributeError:  # func has no __self__
            pass
        else:
            if func_self is obj:
                return six.get_method_function(func).__name__
    raise ValueError("Function %s is not a method of: %s" % (func, obj))
Example #31
0
def _find_method(obj, func):
    if obj:
        try:
            func_self = six.get_method_self(func)
        except AttributeError:  # func has no __self__
            pass
        else:
            if func_self is obj:
                return six.get_method_function(func).__name__
    raise ValueError("Function %s is not a method of: %s" % (func, obj))
Example #32
0
    def _validate_args(self, swfuncs, tailargs):
        if six.get_method_function(self.help) in swfuncs:
            raise ShowHelp()
        if six.get_method_function(self.version) in swfuncs:
            raise ShowVersion()

        requirements = {}
        exclusions = {}
        for swinfo in self._switches_by_func.values():
            if swinfo.mandatory and not swinfo.func in swfuncs:
                raise MissingMandatorySwitch(
                    "Switch %s is mandatory" % ("/".join(("-" if len(n) == 1 else "--") + n for n in swinfo.names),)
                )
            requirements[swinfo.func] = set(self._switches_by_name[req] for req in swinfo.requires)
            exclusions[swinfo.func] = set(self._switches_by_name[exc] for exc in swinfo.excludes)

        # TODO: compute topological order

        gotten = set(swfuncs.keys())
        for func in gotten:
            missing = set(f.func for f in requirements[func]) - gotten
            if missing:
                raise SwitchCombinationError(
                    "Given %s, the following are missing %r"
                    % (swfuncs[func].swname, [self._switches_by_func[f].names[0] for f in missing])
                )
            invalid = set(f.func for f in exclusions[func]) & gotten
            if invalid:
                raise SwitchCombinationError(
                    "Given %s, the following are invalid %r"
                    % (swfuncs[func].swname, [swfuncs[f].swname for f in invalid])
                )

        m_args, m_varargs, _, m_defaults = inspect.getargspec(self.main)
        max_args = six.MAXSIZE if m_varargs else len(m_args) - 1
        min_args = len(m_args) - 1 - (len(m_defaults) if m_defaults else 0)
        if len(tailargs) < min_args:
            raise PositionalArgumentsError("Expected at least %d positional arguments, got %r" % (min_args, tailargs))
        elif len(tailargs) > max_args:
            raise PositionalArgumentsError("Expected at most %d positional arguments, got %r" % (max_args, tailargs))

        ordered = [(f, a) for _, f, a in sorted([(sf.index, f, sf.val) for f, sf in swfuncs.items()])]
        return ordered, tailargs
 def test_contribute_to_class(self, dummy_property, model_instance):
     descriptor = getattr(model_instance.__class__, dummy_property.name)
     assert isinstance(descriptor, QueryablePropertyDescriptor)
     assert descriptor.prop is dummy_property
     assert isinstance(dummy_property, QueryableProperty)
     assert dummy_property.name == 'dummy'
     assert dummy_property.verbose_name == 'Dummy'
     assert dummy_property.model is model_instance.__class__
     assert six.get_method_function(
         model_instance.reset_property) is reset_queryable_property
Example #34
0
    def _init_pyhooks(self):
        hooks = log.PyHooks.get('DFT', None)
        if hooks is None:
            return

        for label, hook in hooks.items():
            name   = "{}_hook".format(label)
            _hook = six.get_method_function(hook) if six.get_method_self(hook) else hook
            method = six.create_bound_method(_hook, DFT)
            setattr(self, name, method)
Example #35
0
def _find_method(obj, func):
    if obj:
        try:
            fun_self = six.get_method_self(func)
        except:  # func has no __self__
            pass
        else:
            if fun_self is obj:
                return six.get_method_function(func).__name__
    raise AttributeError('Function {} is not a method of {}'.format(func, obj))
Example #36
0
    def _get_subscriber_key_and_value(subscriber):
        """
        Allow Subscribers to be garbage collected while still subscribed inside Publisher.

        Subscribing methods of objects is tricky::

            class TheObserver(object):
                def __init__(self):
                    self.received_data = []

                def on_new_data(self, data):
                    self.received_data.append(data)

            observer1 = TheObserver()
            observer2 = TheObserver()

            subscribe(observer1.on_new_data)
            subscribe(observer2.on_new_data)
            subscribe(observer2.on_new_data)

        Even if it looks like 2 different subscriptions they all
        pass 3 different bound-method objects (different id()).
        This is so since access via observer1.on_new_data
        creates new object (bound method) on the fly.

        We want to use weakref but weakref to bound method doesn't work
        see: http://code.activestate.com/recipes/81253/
        and : https://stackoverflow.com/questions/599430/why-doesnt-the-weakref-work-on-this-bound-method
        When we wrap bound-method into weakref it may quickly disappear
        if that is only reference to bound method.
        So, we need to unbind it to have access to real method + self instance

        Unbinding above 3 examples of on_new_data will give:
        1) self                      - 2 different id()
        2) function object of class  - all 3 have same id()

        Observer key is pair: (self-id, function-id)
        """
        try:
            self_or_none = six.get_method_self(subscriber)
            self_id = instance_id(self_or_none)
            self_or_none = weakref.proxy(self_or_none)
        except AttributeError:
            self_id = 0  # default for not bound methods
            self_or_none = None

        try:
            func = six.get_method_function(subscriber)
        except AttributeError:
            func = subscriber
        function_id = instance_id(func)

        subscription_key = (self_id, function_id)
        subscription_value = (self_or_none, weakref.proxy(func))
        return subscription_key, subscription_value
Example #37
0
        def item_function_wrapper(*args, **kwargs):
            # Extract the function from the expression.
            locals_, globals_ = locals(), item_function_globals
            locals_.update(dict(zip(item_function_argspec.args, args)))
            locals_.update(kwargs)
            six.exec_('_function = %s' % expression, globals_, locals_)
            _function = locals_['_function']

            # Initialize benchmark process.
            props = {'times': list()}

            # Create a wrapper for the method to benchmark.
            @wraps(_function)
            def benchmark(*args, **kwargs):
                # nonlocal elapsed, real_iterations
                if self.config.option.bench_disable_gc:
                    gc.collect()
                    gc.disable()
                start = timer()
                result = _function(*args, **kwargs)
                finish = timer()
                if self.config.option.bench_disable_gc:
                    gc.enable()
                props['times'].append(finish - start)
                return result

            # Replace the function with the wrapped function.
            locals_['benchmark'] = benchmark
            six.exec_('%s = benchmark' % expression, globals_, locals_)

            # Attempt to replace it in global scope as well.
            globals_.update(locals_)

            # Get the (unbound) function.
            try:
                locals_['function'] = six.get_method_function(item_function)

            except AttributeError:
                locals_['function'] = item_function

            # Iterate the set number of iterations.
            item.teardown()
            for _ in range(iterations):
                item.setup()
                locals_['args'] = args
                locals_['kwargs'] = kwargs
                six.exec_('function(*args, **kwargs)', globals_, locals_)
                item.teardown()

            # Restore the benchmarked function.
            six.exec_('%s = _function' % expression, globals_, locals_)

            # Construct a Benchmark instance to store the result.
            self._benchmarks.append(Benchmark(item, **props))
Example #38
0
    def __str__(self):

        d = self.__dict__
        d['sl'] = self.sl

        try:
            fn = six.get_method_function(self.encode)
            kwargs = {k: fn(v) for k, v in d.items()}
            return self.fmt.format(**kwargs)
        except (ValueError, KeyError) as e:
            raise ValueError('Bad value in {} for {}: {}'.format(d, self.fmt, e))
Example #39
0
 def _get_op_handler(operation):
     """ Import and load the operation handler """
     try:
         mod_to_import, attr_path = operation.split('#')
         op = import_module(mod_to_import)
         for part in attr_path.split('.'):
             op = getattr(op, part)
         if isinstance(op, types.FunctionType):
             return op
         return six.get_method_function(op)
     except (ValueError, AttributeError):
         raise ValueError("The operation '{}' is invalid.".format(operation))
Example #40
0
File: proxy.py Project: b3j0f/utils
def proxified_elt(proxy):
    """Get proxified element.

    :param proxy: proxy element from where get proxified element.
    :return: proxified element. None if proxy is not proxified.
    """

    if ismethod(proxy):
        proxy = get_method_function(proxy)
    result = getattr(proxy, __PROXIFIED__, None)

    return result
Example #41
0
    def __str__(self):

        d = self.__dict__
        d['sl'] = self.sl

        try:
            fn = six.get_method_function(self.encode)
            kwargs = {k: fn(v) for k, v in d.items()}
            return self.fmt.format(**kwargs)
        except (ValueError, KeyError) as e:
            raise ValueError('Bad value in {} for {}: {}'.format(
                d, self.fmt, e))
Example #42
0
 def object_build(self, node, obj):
     """recursive method which create a partial ast from real objects
      (only function, class, and method are handled)
     """
     if obj in self._done:
         return self._done[obj]
     self._done[obj] = node
     for name in dir(obj):
         try:
             member = getattr(obj, name)
         except AttributeError:
             # damned ExtensionClass.Base, I know you're there !
             attach_dummy_node(node, name)
             continue
         if inspect.ismethod(member):
             member = six.get_method_function(member)
         if inspect.isfunction(member):
             _build_from_function(node, name, member, self._module)
         elif inspect.isbuiltin(member):
             if (not _io_discrepancy(member)
                     and self.imported_member(node, member, name)):
                 continue
             object_build_methoddescriptor(node, member, name)
         elif inspect.isclass(member):
             if self.imported_member(node, member, name):
                 continue
             if member in self._done:
                 class_node = self._done[member]
                 if class_node not in node.locals.get(name, ()):
                     node.add_local_node(class_node, name)
             else:
                 class_node = object_build_class(node, member, name)
                 # recursion
                 self.object_build(class_node, member)
             if name == '__class__' and class_node.parent is None:
                 class_node.parent = self._done[self._module]
         elif inspect.ismethoddescriptor(member):
             assert isinstance(member, object)
             object_build_methoddescriptor(node, member, name)
         elif inspect.isdatadescriptor(member):
             assert isinstance(member, object)
             object_build_datadescriptor(node, member, name)
         elif isinstance(member, _CONSTANTS):
             attach_const_node(node, name, member)
         elif inspect.isroutine(member):
             # This should be called for Jython, where some builtin
             # methods aren't caught by isbuiltin branch.
             _build_from_function(node, name, member, self._module)
         else:
             # create an empty node so that the name is actually defined
             attach_dummy_node(node, name, member)
     return None
Example #43
0
 def object_build(self, node, obj):
     """recursive method which create a partial ast from real objects
      (only function, class, and method are handled)
     """
     if obj in self._done:
         return self._done[obj]
     self._done[obj] = node
     for name in dir(obj):
         try:
             member = getattr(obj, name)
         except AttributeError:
             # damned ExtensionClass.Base, I know you're there !
             attach_dummy_node(node, name)
             continue
         if inspect.ismethod(member):
             member = six.get_method_function(member)
         if inspect.isfunction(member):
             _build_from_function(node, name, member, self._module)
         elif inspect.isbuiltin(member):
             if (not _io_discrepancy(member) and
                     self.imported_member(node, member, name)):
                 continue
             object_build_methoddescriptor(node, member, name)
         elif inspect.isclass(member):
             if self.imported_member(node, member, name):
                 continue
             if member in self._done:
                 class_node = self._done[member]
                 if class_node not in node.locals.get(name, ()):
                     node.add_local_node(class_node, name)
             else:
                 class_node = object_build_class(node, member, name)
                 # recursion
                 self.object_build(class_node, member)
             if name == '__class__' and class_node.parent is None:
                 class_node.parent = self._done[self._module]
         elif inspect.ismethoddescriptor(member):
             assert isinstance(member, object)
             object_build_methoddescriptor(node, member, name)
         elif inspect.isdatadescriptor(member):
             assert isinstance(member, object)
             object_build_datadescriptor(node, member, name)
         elif isinstance(member, _CONSTANTS):
             attach_const_node(node, name, member)
         elif inspect.isroutine(member):
             # This should be called for Jython, where some builtin
             # methods aren't caught by isbuiltin branch.
             _build_from_function(node, name, member, self._module)
         else:
             # create an empty node so that the name is actually defined
             attach_dummy_node(node, name, member)
     return None
Example #44
0
        def item_function_wrapper(*args, **kwargs):
            # Extract the function from the expression.
            locals_, globals_ = locals(), item_function_globals
            locals_.update(dict(zip(item_function_argspec.args, args)))
            locals_.update(kwargs)
            six.exec_('_function = %s' % expression, globals_, locals_)
            _function = locals_['_function']

            # Initialize benchmark process.
            props = {'times': list()}

            # Create a wrapper for the method to benchmark.
            @wraps(_function)
            def benchmark(*args, **kwargs):
                # nonlocal elapsed, real_iterations
                gc.collect()
                gc.disable()
                start = timer()
                result = _function(*args, **kwargs)
                finish = timer()
                gc.enable()
                props['times'].append(finish - start)
                return result

            # Replace the function with the wrapped function.
            locals_['benchmark'] = benchmark
            six.exec_('%s = benchmark' % expression, globals_, locals_)

            # Attempt to replace it in global scope as well.
            globals_.update(locals_)

            # Get the (unbound) function.
            try:
                locals_['function'] = six.get_method_function(item_function)

            except AttributeError:
                locals_['function'] = item_function

            # Iterate the set number of iterations.
            item.teardown()
            for _ in range(iterations):
                item.setup()
                locals_['args'] = args
                locals_['kwargs'] = kwargs
                six.exec_('function(*args, **kwargs)', globals_, locals_)
                item.teardown()

            # Restore the benchmarked function.
            six.exec_('%s = _function' % expression, globals_, locals_)

            # Construct a Benchmark instance to store the result.
            self._benchmarks.append(Benchmark(item, **props))
    def __init__(self, verbose_name=None):
        """
        Initialize a new queryable property.

        :param str verbose_name: An optional verbose name of the property. If
                                 not provided it defaults to a prettified
                                 version of the property's name.
        """
        self.model = None
        self.name = None
        self.setter_cache_behavior = six.get_method_function(
            self.setter_cache_behavior)
        self.verbose_name = verbose_name
Example #46
0
def _find_method(obj, func):
    if obj:
        try:
            func_self = six.get_method_self(func)
        except AttributeError:  # func has no __self__
            pass
        else:
            if func_self is obj:
                name = six.get_method_function(func).__name__
                if _is_private_method(name):
                    return _mangle_private_name(obj, func, name)
                return name
    raise ValueError("Function %s is not a method of: %s" % (func, obj))
Example #47
0
def get_func_name(func):
    """Returns name of passed callable."""
    _, func = tf_decorator.unwrap(func)
    if callable(func):
        if tf_inspect.isfunction(func):
            return func.__name__
        elif tf_inspect.ismethod(func):
            return '%s.%s' % (six.get_method_self(func).__class__.__name__,
                              six.get_method_function(func).__name__)
        else:  # Probably a class instance with __call__
            return type(func)
    else:
        raise ValueError('Argument must be callable')
Example #48
0
def unpickleMethod(im_name,
                   im_self,
                   im_class):
    'support function for copyreg to unpickle method refs'
    try:
        unbound = getattr(im_class, im_name)
        if im_self is None:
            return unbound
        bound = types.MethodType(get_method_function(unbound),
                                 im_self)
        return bound
    except AttributeError:
        # assert im_self is not None,"No recourse: no instance to guess from."
        # Attempt a common fix before bailing -- if classes have
        # changed around since we pickled this method, we may still be
        # able to get it by looking on the instance's current class.
        unbound = getattr(im_self.__class__, im_name)
        if im_self is None:
            return unbound
        bound = types.MethodType(get_method_function(unbound),
                                 im_self)
        return bound
def get_func_name(func):
  """Returns name of passed callable."""
  _, func = tf_decorator.unwrap(func)
  if callable(func):
    if tf_inspect.isfunction(func):
      return func.__name__
    elif tf_inspect.ismethod(func):
      return '%s.%s' % (six.get_method_self(func).__class__.__name__,
                        six.get_method_function(func).__name__)
    else:  # Probably a class instance with __call__
      return str(type(func))
  else:
    raise ValueError('Argument must be callable')
Example #50
0
    def __init__(self):
        self._cache_groups = dict()
        self._diff_running = False

        regex = re.compile(r'^_cache_(.+)$')

        for (_, m) in inspect.getmembers(type(self),
                                         predicate=lambda p:
                                         (inspect.ismethod or
                                         inspect.isdatadescriptor)):

            if hasattr(m, 'fget'):
                f = m.fget
            elif inspect.ismethod(m):
                f = six.get_method_function(m)
            elif inspect.isfunction(m):
                f = m
            else:
                continue

            fv = six.get_function_code(f).co_freevars

            try:
                closure = six.get_function_closure(f)
            except AttributeError:
                continue

            if closure is None:
                continue

            vs = dict(zip(fv, (c.cell_contents for c in closure)))

            # this is used to make sure we are in the right function
            # i'm not proud of that, by the way
            if '_cache_identifier_pj97YCjgnp' not in vs:
                continue

            try:
                groups = vs['groups']
                method_name = re.match(regex, vs['cache_var_name']).group(1)
            except KeyError:
                continue

            for g in groups:
                if g not in self._cache_groups:
                    self._cache_groups[g] = []
                self._cache_groups[g].append(method_name)

            setattr(self, '_cache_' + method_name, None)
            setattr(self, '_cached_' + method_name, False)
            setattr(self, '_cached_args_' + method_name, dict())
Example #51
0
    def _set_method_flags(self, method_obj):
        """
        Sets the \"_dbusCaller\" boolean on the \"dbus_*\" methods. This
        is a one-time operation used to flag each method with a boolean
        indicating whether or not they accept the \"dbusCaller\" keyword
        argument
        """
        args         = inspect.getargspec(method_obj)[0]
        needs_caller = False

        if len(args) >= 1 and args[-1] == 'dbusCaller':
            needs_caller = True

        six.get_method_function(method_obj)._dbusCaller = needs_caller
Example #52
0
File: proxy.py Project: b3j0f/utils
def is_proxy(elt):
    """Return True if elt is a proxy.

    :param elt: elt to check such as a proxy.
    :return: True iif elt is a proxy.
    :rtype: bool
    """

    if ismethod(elt):
        elt = get_method_function(elt)

    result = hasattr(elt, __PROXIFIED__)

    return result
Example #53
0
    def __hash__(self):
        """
        Return the hash of ``self``.

        EXAMPLES::

            sage: C = IntegerListsLex(2, length=3)
            sage: D = IntegerListsLex(2, max_length=3)
            sage: hash(C) == hash(C)
            True
        """
        a = self._element_constructor_
        if ismethod(a):
            a = get_method_function(a)
        return hash((self.__class__, a))
Example #54
0
    def runiter(self, slot_name, **kwargs):
        """Return an iterable of PluginHookRunner objects.

        The iterable will return a PluginHookRunner object
        for each plugin hook mapped to slot_name. Multiple plugins
        with hooks for the same slot will result in multiple
        PluginHookRunners in the iterable.

        See run() docs for what to expect from PluginHookRunner.run().
        """
        # slot's called should always exist here, if not
        if slot_name not in self._slot_to_funcs:
            raise SlotNameException(slot_name)

        for func in self._slot_to_funcs[slot_name]:
            module = inspect.getmodule(func)
            func_module_name = getattr(func, '__module__')
            if not func_module_name:
                if module:
                    func_module_name = module.__name__
                else:
                    func_module_name = 'unknown_module'
            func_class_name = six.get_method_self(func).__class__.__name__
            plugin_key = ".".join([func_module_name, func_class_name])
            log.debug("Running %s in %s" % (six.get_method_function(func).__name__, plugin_key))
            # resolve slot_name to conduit
            # FIXME: handle cases where we don't have a conduit for a slot_name
            #   (should be able to handle this since we map those at the same time)
            conduit = self._slot_to_conduit[slot_name]

            try:
                # create a Conduit
                # FIXME: handle cases where we can't create a Conduit()
                conduit_instance = conduit(six.get_method_self(func).__class__, **kwargs)
            # TypeError tends to mean we provided the wrong kwargs for this
            # conduit
            # if we get an Exception above, should we exit early, or
            # continue onto other hooks. A conduit could fail for
            # something specific to func.__class__, but unlikely
            except Exception as e:
                log.exception(e)
                raise

            runner = PluginHookRunner(conduit_instance, func)
            yield runner
Example #55
0
    def __str__(self):

        d = self.__dict__
        d['sl'] = self.sl

        # Hacks for special string cases
        if 'sldu' in d:
            d['sldu'] = str(d['sldu']).zfill(3)

        if 'sldl' in d:
            d['sldl'] = str(d['sldl']).zfill(3)

        try:
            fn = six.get_method_function(self.encode)
            kwargs = {k: fn(v) for k, v in d.items()}
            return self.fmt.format(**kwargs)
        except (ValueError, KeyError) as e:
            raise ValueError("Bad value in {}, data {} for format {}: {}".format(type(self), d, self.fmt, e))
Example #56
0
    def mixin_function_or_method(target, routine, name=None, isbound=False):
        """Mixin a routine into the target.

        :param routine: routine to mix in target.
        :param str name: mixin name. Routine name by default.
        :param bool isbound: If True (False by default), the mixin result is a
            bound method to target.
        """

        function = None

        if isfunction(routine):
            function = routine

        elif ismethod(routine):
            function = get_method_function(routine)

        else:
            raise Mixin.MixInError(
                "{0} must be a function or a method.".format(routine))

        if name is None:
            name = routine.__name__

        if not isclass(target) or isbound:
            _type = type(target)
            method_args = [function, target]

            if PY2:
                method_args += _type

            result = MethodType(*method_args)

        else:

            if PY2:
                result = MethodType(function, None, target)

            else:
                result = function

        Mixin.set_mixin(target, result, name)

        return result
Example #57
0
def get_op_handler(operation):
    """ Import and load the operation handler """
    # Patch the unversioned sdk path to include the appropriate API version for the
    # resource type in question.
    from azure.cli.core._profile import CLOUD
    import types

    for rt in ResourceType:
        if operation.startswith(rt.import_prefix):
            operation = operation.replace(rt.import_prefix,
                                          get_versioned_sdk_path(CLOUD.profile, rt))
    try:
        mod_to_import, attr_path = operation.split('#')
        op = import_module(mod_to_import)
        for part in attr_path.split('.'):
            op = getattr(op, part)
        if isinstance(op, types.FunctionType):
            return op
        return six.get_method_function(op)
    except (ValueError, AttributeError):
        raise ValueError("The operation '{}' is invalid.".format(operation))
Example #58
0
    def scrape_response(self, scrape_func, response, request, spider):
        fname = lambda f:'%s.%s' % (
                six.get_method_self(f).__class__.__name__,
                six.get_method_function(f).__name__)

        def process_spider_input(response):
            for method in self.methods['process_spider_input']:
                try:
                    result = method(response=response, spider=spider)
                    assert result is None, \
                            'Middleware %s must returns None or ' \
                            'raise an exception, got %s ' \
                            % (fname(method), type(result))
                except:
                    return scrape_func(Failure(), request, spider)
            return scrape_func(response, request, spider)

        def process_spider_exception(_failure):
            exception = _failure.value
            for method in self.methods['process_spider_exception']:
                result = method(response=response, exception=exception, spider=spider)
                assert result is None or _isiterable(result), \
                    'Middleware %s must returns None, or an iterable object, got %s ' % \
                    (fname(method), type(result))
                if result is not None:
                    return result
            return _failure

        def process_spider_output(result):
            for method in self.methods['process_spider_output']:
                result = method(response=response, result=result, spider=spider)
                assert _isiterable(result), \
                    'Middleware %s must returns an iterable object, got %s ' % \
                    (fname(method), type(result))
            return result

        dfd = mustbe_deferred(process_spider_input, response)
        dfd.addErrback(process_spider_exception)
        dfd.addCallback(process_spider_output)
        return dfd
Example #59
0
def subscribe(func):
    '''
    Add a subscriber function to option events

    Parameters
    ----------
    func : callable
        A callable object that takes two parameters: key and value.
        This function is called with the name and value of any option
        that is set.

    Returns
    -------
    None

    '''
    if isinstance(func, types.MethodType):
        obj = six.get_method_self(func)
        func = six.get_method_function(func)
        _subscribers[func] = (weakref.ref(func), weakref.ref(obj))
    else:
        _subscribers[func] = (weakref.ref(func), None)
Example #60
0
    def wrapper(self):
        if not self._wrapped:
            if self._instance or self._class:
                wrapped = self._func.__get__(self._instance, self._class)

                if isinstance(self._func, staticmethod):
                    # we don't need instance or class, however we need scope
                    self.cache.scope = self._instance or self._class
                    self._instance = None
                    self._class = None
                else:
                    wrapped = six.get_method_function(wrapped)
            else:
                wrapped = self._func

            update_wrapper(self.cache, wrapped)
            self.cache.function = wrapped
            self.cache.instance = self._instance
            self.cache.klass = self._class
            self._wrapped = True

        return self.cache