Пример #1
0
    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)
Пример #2
0
  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)
Пример #3
0
def _Promote(arg, klass):
    """Wrap an argument in an object of the specified class.

  This is used to e.g.: promote numbers or strings to Images and arrays
  to Collections.

  Args:
    arg: The object to promote.
    klass: The expected type.

  Returns:
    The argument promoted if the class is recognized, otherwise the
    original argument.
  """
    if arg is None:
        return arg

    if klass == 'Image':
        return Image(arg)
    elif klass == 'Feature':
        if isinstance(arg, Collection):
            # TODO(user): Decide whether we want to leave this in. It can be
            #              quite dangerous on large collections.
            return ApiFunction.call_(
                'Feature', ApiFunction.call_('Collection.geometry', arg))
        else:
            return Feature(arg)
    elif klass == 'Element':
        if isinstance(arg, Element):
            # Already an Element.
            return arg
        elif isinstance(arg, Geometry):
            # Geometries get promoted to Features.
            return Feature(arg)
        elif isinstance(arg, ComputedObject):
            # Try a cast.
            return Element(arg.func, arg.args, arg.varName)
        else:
            # No way to convert.
            raise EEException('Cannot convert %s to Element.' % arg)
    elif klass == 'Geometry':
        if isinstance(arg, Collection):
            return ApiFunction.call_('Collection.geometry', arg)
        else:
            return Geometry(arg)
    elif klass in ('FeatureCollection', 'Collection'):
        # For now Collection is synonymous with FeatureCollection.
        if isinstance(arg, Collection):
            return arg
        else:
            return FeatureCollection(arg)
    elif klass == 'ImageCollection':
        return ImageCollection(arg)
    elif klass == 'Filter':
        return Filter(arg)
    elif klass == 'Algorithm':
        if isinstance(arg, basestring):
            # An API function name.
            return ApiFunction.lookup(arg)
        elif callable(arg):
            # A native function that needs to be wrapped.
            args_count = len(inspect.getargspec(arg).args)
            return CustomFunction.create(arg, 'Object',
                                         ['Object'] * args_count)
        elif isinstance(arg, Encodable):
            # An ee.Function or a computed function like the return value of
            # Image.parseExpression().
            return arg
        else:
            raise EEException('Argument is not a function: %s' % arg)
    elif klass == 'Dictionary':
        if isinstance(arg, dict):
            return arg
        else:
            return Dictionary(arg)
    elif klass == 'String':
        if (types.isString(arg) or isinstance(arg, ComputedObject)
                or isinstance(arg, String)):
            return String(arg)
        else:
            return arg
    elif klass == 'List':
        return List(arg)
    elif klass in ('Number', 'Float', 'Long', 'Integer', 'Short', 'Byte'):
        return Number(arg)
    elif klass in globals():
        cls = globals()[klass]
        ctor = ApiFunction.lookupInternal(klass)
        # Handle dynamically created classes.
        if isinstance(arg, cls):
            # Return unchanged.
            return arg
        elif ctor:
            # The client-side constructor will call the server-side constructor.
            return cls(arg)
        elif isinstance(arg, basestring):
            if hasattr(cls, arg):
                # arg is the name of a method in klass.
                return getattr(cls, arg)()
            else:
                raise EEException('Unknown algorithm: %s.%s' % (klass, arg))
        else:
            # Client-side cast.
            return cls(arg)
    else:
        return arg
Пример #4
0
def _Promote(arg, klass):
    """Wrap an argument in an object of the specified class.

  This is used to e.g.: promote numbers or strings to Images and arrays
  to Collections.

  Args:
    arg: The object to promote.
    klass: The expected type.

  Returns:
    The argument promoted if the class is recognized, otherwise the
    original argument.
  """
    if arg is None:
        return arg

    if klass == "Image":
        return Image(arg)
    elif klass == "Feature":
        if isinstance(arg, Collection):
            # TODO(user): Decide whether we want to leave this in. It can be
            #              quite dangerous on large collections.
            return ApiFunction.call_("Feature", ApiFunction.call_("Collection.geometry", arg))
        else:
            return Feature(arg)
    elif klass == "Element":
        if isinstance(arg, Element):
            # Already an Element.
            return arg
        elif isinstance(arg, Geometry):
            # Geometries get promoted to Features.
            return Feature(arg)
        elif isinstance(arg, ComputedObject):
            # Try a cast.
            return Element(arg.func, arg.args, arg.varName)
        else:
            # No way to convert.
            raise EEException("Cannot convert %s to Element." % arg)
    elif klass == "Geometry":
        if isinstance(arg, Collection):
            return ApiFunction.call_("Collection.geometry", arg)
        else:
            return Geometry(arg)
    elif klass in ("FeatureCollection", "Collection"):
        # For now Collection is synonymous with FeatureCollection.
        if isinstance(arg, Collection):
            return arg
        else:
            return FeatureCollection(arg)
    elif klass == "ImageCollection":
        return ImageCollection(arg)
    elif klass == "Filter":
        return Filter(arg)
    elif klass == "Algorithm":
        if isinstance(arg, basestring):
            # An API function name.
            return ApiFunction.lookup(arg)
        elif callable(arg):
            # A native function that needs to be wrapped.
            args_count = len(inspect.getargspec(arg).args)
            return CustomFunction.create(arg, "Object", ["Object"] * args_count)
        elif isinstance(arg, Encodable):
            # An ee.Function or a computed function like the return value of
            # Image.parseExpression().
            return arg
        else:
            raise EEException("Argument is not a function: %s" % arg)
    elif klass == "Dictionary":
        if isinstance(arg, dict):
            return arg
        else:
            return Dictionary(arg)
    elif klass == "String":
        if types.isString(arg) or isinstance(arg, ComputedObject) or isinstance(arg, String):
            return String(arg)
        else:
            return arg
    elif klass == "List":
        return List(arg)
    elif klass in ("Number", "Float", "Long", "Integer", "Short", "Byte"):
        return Number(arg)
    elif klass in globals():
        cls = globals()[klass]
        ctor = ApiFunction.lookupInternal(klass)
        # Handle dynamically created classes.
        if isinstance(arg, cls):
            # Return unchanged.
            return arg
        elif ctor:
            # The client-side constructor will call the server-side constructor.
            return cls(arg)
        elif isinstance(arg, basestring):
            if hasattr(cls, arg):
                # arg is the name of a method in klass.
                return getattr(cls, arg)()
            else:
                raise EEException("Unknown algorithm: %s.%s" % (klass, arg))
        else:
            # Client-side cast.
            return cls(arg)
    else:
        return arg
Пример #5
0
def _Promote(arg, klass):
  """Wrap an argument in an object of the specified class.

  This is used to e.g.: promote numbers or strings to Images and arrays
  to Collections.

  Args:
    arg: The object to promote.
    klass: The expected type.

  Returns:
    The argument promoted if the class is recognized, otherwise the
    original argument.
  """
  if arg is None:
    return arg

  if klass == 'Image':
    return Image(arg)
  elif klass == 'Feature':
    if isinstance(arg, Collection):
      # TODO(user): Decide whether we want to leave this in. It can be
      #              quite dangerous on large collections.
      return ApiFunction.call_(
          'Feature', ApiFunction.call_('Collection.geometry', arg))
    else:
      return Feature(arg)
  elif klass in ('Element', 'EEObject'):
    # TODO(user): Remove EEObject once the server is updated.
    if isinstance(arg, Element):
      # Already an EEObject.
      return arg
    elif isinstance(arg, ComputedObject):
      # Try a cast.
      return Element(arg.func, arg.args, arg.varName)
    else:
      # No way to convert.
      raise EEException('Cannot convert %s to Element.' % arg)
  elif klass == 'Geometry':
    if isinstance(arg, Collection):
      return ApiFunction.call_('Collection.geometry', arg)
    else:
      return Geometry(arg)
  elif klass in ('FeatureCollection', 'Collection'):
    # For now Collection is synonymous with FeatureCollection.
    if isinstance(arg, Collection):
      return arg
    else:
      return FeatureCollection(arg)
  elif klass == 'ImageCollection':
    return ImageCollection(arg)
  elif klass == 'Filter':
    return Filter(arg)
  elif klass == 'Algorithm' and isinstance(arg, basestring):
    return ApiFunction.lookup(arg)
  elif klass == 'Dictionary':
    if isinstance(arg, dict):
      return arg
    else:
      return Dictionary(arg)
  elif klass == 'String':
    if (types.isString(arg) or
        isinstance(arg, ComputedObject) or
        isinstance(arg, String)):
      return String(arg)
    else:
      return arg
  elif klass == 'List':
    return List(arg)
  elif klass in ('Number', 'Float', 'Long', 'Integer', 'Short', 'Byte'):
    return Number(arg)
  elif klass in globals():
    cls = globals()[klass]
    ctor = ApiFunction.lookupInternal(klass)
    # Handle dynamically created classes.
    if isinstance(arg, cls):
      # Return unchanged.
      return arg
    elif ctor:
      # The client-side constructor will call the server-side constructor.
      return cls(arg)
    elif isinstance(arg, basestring):
      if hasattr(cls, arg):
        # arg is the name of a method in klass.
        return getattr(cls, arg)()
      else:
        raise EEException('Unknown algorithm: %s.%s' % (klass, arg))
    else:
      # Client-side cast.
      return cls(arg)
  else:
    return arg