예제 #1
0
    def importApi(cls, target, prefix, type_name, opt_prepend=None):
        """Adds all API functions that begin with a given prefix to a target class.

    Args:
      target: The class to add to.
      prefix: The prefix to search for in the signatures.
      type_name: The name of the object's type. Functions whose
          first argument matches this type are bound as instance methods, and
          those whose first argument doesn't match are bound as static methods.
      opt_prepend: An optional string to prepend to the names of the
          added functions.
    """
        cls.initialize()
        prepend = opt_prepend or ''
        for name, api_func in cls._api.iteritems():
            parts = name.split('.')
            if len(parts) == 2 and parts[0] == prefix:
                fname = prepend + parts[1]
                signature = api_func.getSignature()

                cls._bound_signatures.add(name)

                # Specifically handle the function names that are illegal in python.
                if keyword.iskeyword(fname):
                    fname = fname.title()

                # Don't overwrite existing versions of this function.
                if (hasattr(target, fname)
                        and not hasattr(getattr(target, fname), 'signature')):
                    continue

                # Create a new function so we can attach properties to it.
                def MakeBoundFunction(func):
                    # We need the lambda to capture "func" from the enclosing scope.
                    return lambda *args, **kwargs: func.call(*args, **kwargs)  # pylint: disable=unnecessary-lambda

                bound_function = MakeBoundFunction(api_func)

                # Add docs.
                setattr(bound_function, '__name__', name.encode('utf8'))
                bound_function.__doc__ = str(api_func)
                # Attach the signature object for documentation generators.
                bound_function.signature = signature

                # Mark as deprecated if needed.
                if signature.get('deprecated'):
                    deprecated_decorator = deprecation.Deprecated(
                        signature['deprecated'])
                    bound_function = deprecated_decorator(bound_function)

                # Decide whether this is a static or an instance function.
                is_instance = (signature['args'] and ee_types.isSubtype(
                    signature['args'][0]['type'], type_name))
                if not is_instance:
                    bound_function = staticmethod(bound_function)

                # Attach the function as a method.
                setattr(target, fname, bound_function)
예제 #2
0
  def importApi(cls, target, prefix, type_name, opt_prepend=None):
    """Adds all API functions that begin with a given prefix to a target class.

    Args:
      target: The class to add to.
      prefix: The prefix to search for in the signatures.
      type_name: The name of the object's type. Functions whose
          first argument matches this type are bound as instance methods, and
          those whose first argument doesn't match are bound as static methods.
      opt_prepend: An optional string to prepend to the names of the
          added functions.
    """
    cls.initialize()
    prepend = opt_prepend or ''
    for name, api_func in cls._api.iteritems():
      parts = name.split('.')
      if len(parts) == 2 and parts[0] == prefix:
        fname = prepend + parts[1]
        signature = api_func.getSignature()

        cls._bound_signatures.add(name)

        # Specifically handle the function names that are illegal in python.
        if keyword.iskeyword(fname):
          fname = fname.title()

        # Don't overwrite existing versions of this function.
        if (hasattr(target, fname) and
            not hasattr(getattr(target, fname), 'signature')):
          continue

        # Create a new function so we can attach properties to it.
        def MakeBoundFunction(func):
          # We need the lambda to capture "func" from the enclosing scope.
          return lambda *args, **kwargs: func.call(*args, **kwargs)  # pylint: disable=unnecessary-lambda
        bound_function = MakeBoundFunction(api_func)

        # Add docs.
        setattr(bound_function, '__name__', name.encode('utf8'))
        bound_function.__doc__ = str(api_func)
        # Attach the signature object for documentation generators.
        bound_function.signature = signature

        # Mark as deprecated if needed.
        if signature.get('deprecated'):
          deprecated_decorator = deprecation.Deprecated(signature['deprecated'])
          bound_function = deprecated_decorator(bound_function)

        # Decide whether this is a static or an instance function.
        is_instance = (signature['args'] and
                       ee_types.isSubtype(signature['args'][0]['type'],
                                          type_name))
        if not is_instance:
          bound_function = staticmethod(bound_function)

        # Attach the function as a method.
        setattr(target, fname, bound_function)