Пример #1
0
def _MakeClass(name):
    """Generates a dynamic API class for a given name."""
    def init(self, *args):
        """Initializer for dynamically created classes.

    Args:
      self: The instance of this class.  Listed to make the linter hush.
      *args: Either a ComputedObject to be promoted to this type, or
             arguments to an algorithm with the same name as this class.

    Returns:
      The new class.
    """
        klass = globals()[name]
        onlyOneArg = (len(args) == 1)
        # Are we trying to cast something that's already of the right class?
        if onlyOneArg and isinstance(args[0], klass):
            result = args[0]
        else:
            # Decide whether to call a server-side constructor or just do a
            # client-side cast.
            ctor = ApiFunction.lookupInternal(name)
            firstArgIsPrimitive = not isinstance(args[0], ComputedObject)
            shouldUseConstructor = False
            if ctor:
                if not onlyOneArg:
                    # Can't client-cast multiple arguments.
                    shouldUseConstructor = True
                elif firstArgIsPrimitive:
                    # Can't cast a primitive.
                    shouldUseConstructor = True
                elif args[0].func != ctor:
                    # We haven't already called the constructor on this object.
                    shouldUseConstructor = True

        # Apply our decision.
        if shouldUseConstructor:
            # Call ctor manually to avoid having promote() called on the output.
            ComputedObject.__init__(self, ctor,
                                    ctor.promoteArgs(ctor.nameArgs(args)))
        else:
            # Just cast and hope for the best.
            if not onlyOneArg:
                # We don't know what to do with multiple args.
                raise EEException('Too many arguments for ee.%s(): %s' %
                                  (name, args))
            elif firstArgIsPrimitive:
                # Can't cast a primitive.
                raise EEException(
                    'Invalid argument for ee.%s(): %s.  Must be a ComputedObject.'
                    % (name, args))
            else:
                result = args[0]
            ComputedObject.__init__(self, result.func, result.args,
                                    result.varName)

    properties = {'__init__': init, 'name': lambda self: name}
    new_class = type(str(name), (ComputedObject, ), properties)
    ApiFunction.importApi(new_class, name, name)
    return new_class
Пример #2
0
def _MakeClass(name):
  """Generates a dynamic API class for a given name."""

  def init(self, *args):
    """Initializer for dynamically created classes.

    Args:
      self: The instance of this class.  Listed to make the linter hush.
      *args: Either a ComputedObject to be promoted to this type, or
             arguments to an algorithm with the same name as this class.

    Returns:
      The new class.
    """
    if isinstance(args[0], ComputedObject) and len(args) == 1:
      result = args[0]
    else:
      result = ApiFunction.call_(name, *args)

    ComputedObject.__init__(self, result.func, result.args)

  properties = {'__init__': init, 'name': lambda self: name}
  new_class = type(str(name), (ComputedObject,), properties)
  ApiFunction.importApi(new_class, name, name)
  return new_class
Пример #3
0
def _MakeClass(name):
  """Generates a dynamic API class for a given name."""

  def init(self, *args):
    """Initializer for dynamically created classes.

    Args:
      self: The instance of this class.  Listed to make the linter hush.
      *args: Either a ComputedObject to be promoted to this type, or
             arguments to an algorithm with the same name as this class.

    Returns:
      The new class.
    """
    klass = globals()[name]
    onlyOneArg = (len(args) == 1)
    # Are we trying to cast something that's already of the right class?
    if onlyOneArg and isinstance(args[0], klass):
      result = args[0]
    else:
      # Decide whether to call a server-side constructor or just do a
      # client-side cast.
      ctor = ApiFunction.lookupInternal(name)
      firstArgIsPrimitive = not isinstance(args[0], ComputedObject)
      shouldUseConstructor = False
      if ctor:
        if not onlyOneArg:
          # Can't client-cast multiple arguments.
          shouldUseConstructor = True
        elif firstArgIsPrimitive:
          # Can't cast a primitive.
          shouldUseConstructor = True
        elif args[0].func != ctor:
          # We haven't already called the constructor on this object.
          shouldUseConstructor = True

    # Apply our decision.
    if shouldUseConstructor:
      # Call ctor manually to avoid having promote() called on the output.
      ComputedObject.__init__(self, ctor, ctor.promoteArgs(ctor.nameArgs(args)))
    else:
      # Just cast and hope for the best.
      if not onlyOneArg:
        # We don't know what to do with multiple args.
        raise EEException(
            'Too many arguments for ee.%s(): %s' % (name, args))
      elif firstArgIsPrimitive:
        # Can't cast a primitive.
        raise EEException(
            'Invalid argument for ee.%s(): %s.  Must be a ComputedObject.' %
            (name, args))
      else:
        result = args[0]
      ComputedObject.__init__(self, result.func, result.args, result.varName)

  properties = {'__init__': init, 'name': lambda self: name}
  new_class = type(str(name), (ComputedObject,), properties)
  ApiFunction.importApi(new_class, name, name)
  return new_class
Пример #4
0
def _MakeClass(name):
  """Generates a dynamic API class for a given name."""

  def init(self, *args):
    """Initializer for dynamically created classes.

    Args:
      self: The instance of this class.  Listed to make the linter hush.
      *args: Either a ComputedObject to be promoted to this type, or
             arguments to an algorithm with the same name as this class.

    Returns:
      The new class.
    """
    if isinstance(args[0], ComputedObject) and len(args) == 1:
      result = args[0]
    else:
      result = ApiFunction.call_(name, *args)

    ComputedObject.__init__(self, result.func, result.args)

  new_class = type(str(name), (ComputedObject,), {'__init__': init})
  ApiFunction.importApi(new_class, name, name)
  return new_class