Пример #1
0
  def get(cls, queue_names=frozenset()):
    """Returns a 2-tuple: (list of push _QueueInfo, list of pull _QueueInfo)."""
    fetch_queue_request = taskqueue_service_pb.TaskQueueFetchQueuesRequest()
    fetch_queue_request.set_max_rows(1000)
    fetch_queue_response = taskqueue_service_pb.TaskQueueFetchQueuesResponse()
    apiproxy_stub_map.MakeSyncCall('taskqueue',
                                   'FetchQueues',
                                   fetch_queue_request,
                                   fetch_queue_response)

    queue_stats_request = taskqueue_service_pb.TaskQueueFetchQueueStatsRequest()
    for queue in fetch_queue_response.queue_list():
      queue_stats_request.add_queue_name(queue.queue_name())
    queue_stats_response = (
        taskqueue_service_pb.TaskQueueFetchQueueStatsResponse())
    apiproxy_stub_map.MakeSyncCall('taskqueue',
                                   'FetchQueueStats',
                                   queue_stats_request,
                                   queue_stats_response)

    for queue, queue_stats in zip(
        fetch_queue_response.queue_list(),
        queue_stats_response.queuestats_list()):
      if queue_names and queue.queue_name() not in queue_names:
        continue
      yield cls._from_queue_and_stats(queue, queue_stats)
Пример #2
0
  def _Dynamic_Transaction(self, request, response):
    """Handle a Transaction request.

    We handle transactions by accumulating Put requests on the client end, as
    well as recording the key and hash of Get requests. When Commit is called,
    Transaction is invoked, which verifies that all the entities in the
    precondition list still exist and their hashes match, then performs a
    transaction of its own to make the updates.
    """
    tx = datastore_pb.Transaction()
    apiproxy_stub_map.MakeSyncCall('datastore_v3', 'BeginTransaction',
                                   api_base_pb.VoidProto(), tx)

    preconditions = request.precondition_list()
    if preconditions:
      get_request = datastore_pb.GetRequest()
      get_request.mutable_transaction().CopyFrom(tx)
      for precondition in preconditions:
        key = get_request.add_key()
        key.CopyFrom(precondition.key())
      get_response = datastore_pb.GetResponse()
      apiproxy_stub_map.MakeSyncCall('datastore_v3', 'Get', get_request,
                                     get_response)
      entities = get_response.entity_list()
      assert len(entities) == request.precondition_size()
      for precondition, entity in zip(preconditions, entities):
        if precondition.has_hash() != entity.has_entity():
          raise apiproxy_errors.ApplicationError(
              datastore_pb.Error.CONCURRENT_TRANSACTION,
              "Transaction precondition failed.")
        elif entity.has_entity():
          entity_hash = sha.new(entity.entity().Encode()).digest()
          if precondition.hash() != entity_hash:
            raise apiproxy_errors.ApplicationError(
                datastore_pb.Error.CONCURRENT_TRANSACTION,
                "Transaction precondition failed.")

    if request.has_puts():
      put_request = request.puts()
      put_request.mutable_transaction().CopyFrom(tx)
      apiproxy_stub_map.MakeSyncCall('datastore_v3', 'Put',
                                     put_request, response)

    if request.has_deletes():
      delete_request = request.deletes()
      delete_request.mutable_transaction().CopyFrom(tx)
      apiproxy_stub_map.MakeSyncCall('datastore_v3', 'Delete',
                                     delete_request, api_base_pb.VoidProto())

    apiproxy_stub_map.MakeSyncCall('datastore_v3', 'Commit', tx,
                                   api_base_pb.VoidProto())
Пример #3
0
  def _Dynamic_RunQuery(self, request, response):
    """Handle a RunQuery request.

    We handle RunQuery by executing a Query and a Next and returning the result
    of the Next request.
    """
    runquery_response = datastore_pb.QueryResult()
    apiproxy_stub_map.MakeSyncCall('datastore_v3', 'RunQuery',
                                   request, runquery_response)
    next_request = datastore_pb.NextRequest()
    next_request.mutable_cursor().CopyFrom(runquery_response.cursor())
    next_request.set_count(request.limit())
    apiproxy_stub_map.MakeSyncCall('datastore_v3', 'Next',
                                   next_request, response)
Пример #4
0
    def _Dynamic_Commit(self,
                        transaction,
                        transaction_response,
                        request_id=None):
        """ Send a transaction request to commit a transaction to the 
        datastore server. """
        transaction.set_app(self.__app_id)

        self._RemoteSend(transaction, transaction_response, "Commit",
                         request_id)

        response = taskqueue_service_pb.TaskQueueAddResponse()
        try:
            for action in self.__tx_actions[transaction.handle()]:
                try:
                    apiproxy_stub_map.MakeSyncCall('taskqueue', 'Add', action,
                                                   response)
                except apiproxy_errors.ApplicationError, e:
                    logging.warning(
                        'Transactional task %s has been dropped, %s', action,
                        e)

        finally:
            try:
                del self.__tx_actions[transaction.handle()]
            except KeyError:
                pass
Пример #5
0
    def _flush(self):
        """Internal version of flush() with no locking."""
        logs = self.parse_logs()
        self._clear()

        first_iteration = True
        while logs or first_iteration:
            first_iteration = False
            request = log_service_pb.FlushRequest()
            group = log_service_pb.UserAppLogGroup()
            byte_size = 0
            n = 0
            for entry in logs:

                if len(entry[2]) > LogsBuffer._MAX_LINE_SIZE:
                    entry = list(entry)
                    entry[2] = self._truncate(entry[2],
                                              LogsBuffer._MAX_LINE_SIZE)

                if byte_size + len(entry[2]) > LogsBuffer._MAX_FLUSH_SIZE:
                    break
                line = group.add_log_line()
                line.set_timestamp_usec(entry[0])
                line.set_level(entry[1])
                line.set_message(entry[2])
                byte_size += 1 + group.lengthString(line.ByteSize())
                n += 1
            assert n > 0 or not logs
            logs = logs[n:]
            request.set_logs(group.Encode())
            response = api_base_pb.VoidProto()
            apiproxy_stub_map.MakeSyncCall('logservice', 'Flush', request,
                                           response)
Пример #6
0
def create_logout_url(dest_url, _auth_domain=None):
  """Computes the logout URL for this request and specified destination URL,
     for both federated login App and Google Accounts App.

  Args:
    dest_url: String that is the desired final destination URL for the user
              once logout is complete. If 'dest_url' does not have a host
              specified, we will use the host from the current request.

  Returns:
    Logout URL as a string
  """
  req = user_service_pb.CreateLogoutURLRequest()
  resp = user_service_pb.CreateLogoutURLResponse()
  req.set_destination_url(dest_url)
  if _auth_domain:
    req.set_auth_domain(_auth_domain)

  try:
    apiproxy_stub_map.MakeSyncCall('user', 'CreateLogoutURL', req, resp)
  except apiproxy_errors.ApplicationError, e:
    if (e.application_error ==
        user_service_pb.UserServiceError.REDIRECT_URL_TOO_LONG):
      raise RedirectTooLongError
    else:
      raise e
Пример #7
0
  def recvfrom(self, buffersize, flags=0):
    """recvfrom(buffersize[, flags]) -> (data, address info)

    Like recv(buffersize, flags) but also return the sender's address info.
    """
    if not self._created:
      self._CreateSocket()
    if not self._socket_descriptor:
      raise error(errno.EBADF, os.strerror(errno.EBADF))

    request = remote_socket_service_pb.ReceiveRequest()
    request.set_socket_descriptor(self._socket_descriptor)
    request.set_data_size(buffersize)
    request.set_flags(flags)
    if self.type == SOCK_STREAM:
      if not (self._connected or self._connect_in_progress):
        raise error(errno.ENOTCONN, os.strerror(errno.ENOTCONN))
    if self._shutdown_read:
      request.set_timeout_seconds(0.0)
    elif self.gettimeout() is not None:
      request.set_timeout_seconds(self.gettimeout())

    reply = remote_socket_service_pb.ReceiveReply()

    try:
      apiproxy_stub_map.MakeSyncCall('remote_socket', 'Receive', request, reply)
    except apiproxy_errors.ApplicationError, e:
      e = _SystemExceptionFromAppError(e)
      if not self._shutdown_read or e.errno != errno.EAGAIN:
        raise e
Пример #8
0
  def listen(self, backlog):
    """listen(backlog)

    Enable a server to accept connections.  The backlog argument must be at
    least 1; it specifies the number of unaccepted connection that the system
    will allow before refusing new connections.
    """
    if not self._created:
      self._CreateSocket(bind_address=('', 0))
    if not self._socket_descriptor:
      raise error(errno.EBADF, os.strerror(errno.EBADF))
    if self._connected:
      raise error(errno.EINVAL, os.strerror(errno.EINVAL))
    if self.type != SOCK_STREAM:
      raise error(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
    self._bound = True
    self._listen = True

    request = remote_socket_service_pb.ListenRequest()
    request.set_socket_descriptor(self._socket_descriptor)
    request.set_backlog(backlog)

    reply = remote_socket_service_pb.ListenReply()

    try:
      apiproxy_stub_map.MakeSyncCall('remote_socket', 'Listen', request, reply)
    except apiproxy_errors.ApplicationError, e:
      raise _SystemExceptionFromAppError(e)
Пример #9
0
  def shutdown(self, flag):
    """shutdown(flag)

    Shut down the reading side of the socket (flag == SHUT_RD), the writing side
    of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).
    """
    if not flag in (SHUT_RD, SHUT_WR, SHUT_RDWR):
      raise error(errno.EINVAL, os.strerror(errno.EINVAL))
    if not self._created:
      self._CreateSocket()
    if not self._socket_descriptor:
      raise error(errno.EBADF, os.strerror(errno.EBADF))
    if (not self._connected or
        (self._shutdown_read and flag in (SHUT_RD, SHUT_RDWR)) or
        (self._shutdown_write and flag in (SHUT_RD, SHUT_RDWR))):
      raise error(errno.ENOTCONN, os.strerror(errno.ENOTCONN))

    request = remote_socket_service_pb.ShutDownRequest()
    request.set_socket_descriptor(self._socket_descriptor)
    request.set_how(flag)
    request.set_send_offset(self._stream_offset)

    reply = remote_socket_service_pb.ShutDownReply()

    try:
      apiproxy_stub_map.MakeSyncCall(
          'remote_socket', 'ShutDown', request, reply)
    except apiproxy_errors.ApplicationError, e:
      raise _SystemExceptionFromAppError(e)
Пример #10
0
  def _flush(self):
    """Internal version of flush() with no locking."""
    records_to_be_flushed = []
    try:
      while True:
        group = log_service_pb.UserAppLogGroup()
        bytes_left = self._MAX_FLUSH_SIZE
        while self._buffer:
          record = self._get_record()
          if record.IsBlank():
            continue

          message = self._clean(record.message)



          message = self._truncate(message, self._MAX_LINE_SIZE)


          if len(message) > bytes_left:
            self._rollback_record(record)
            break

          records_to_be_flushed.append(record)

          line = group.add_log_line()
          line.set_timestamp_usec(record.created)
          line.set_level(record.level)
          if record.source_location is not None:
            line.mutable_source_location().set_file(record.source_location[0])
            line.mutable_source_location().set_line(record.source_location[1])
            line.mutable_source_location().set_function_name(
                record.source_location[2])
          line.set_message(message)

          bytes_left -= 1 + group.lengthString(line.ByteSize())

        request = log_service_pb.FlushRequest()
        request.set_logs(group.Encode())
        response = api_base_pb.VoidProto()
        apiproxy_stub_map.MakeSyncCall('logservice', 'Flush', request, response)
        if not self._buffer:
          break
    except apiproxy_errors.CancelledError:


      records_to_be_flushed.reverse()
      self._buffer.extendleft(records_to_be_flushed)
    except Exception, e:
      records_to_be_flushed.reverse()
      self._buffer.extendleft(records_to_be_flushed)
      line = '-' * 80
      msg = 'ERROR: Could not flush to log_service (%s)\n%s\n%s\n%s\n'




      _sys_stderr.write(msg % (e, line, self._contents(), line))
      self._clear()
      raise
Пример #11
0
def get_presence(jid, from_jid=None, get_show=False):
    """Gets the presence for a JID.

  Args:
    jid: The JID of the contact whose presence is requested. This may also be a
      list of JIDs, which also implies get_show (below).
    from_jid: The optional custom JID to use for sending. Currently, the default
      is <appid>@appspot.com. This is supported as a value. Custom JIDs can be
      of the form <anything>@<appid>.appspotchat.com.
    get_show: if True, return a tuple of (is_available, show). If a list of jids
      is given, this will always be True.

  Returns:
    At minimum, a boolean is_available representing whether the requested JID
    is available.

    If get_show is specified, a tuple (is_available, show) will be given.

    If a list of JIDs is given, a list of tuples will be returned, including
    is_available, show, and an additional boolean indicating if that JID was
    valid.

  Raises:
    InvalidJidError: Raised if no JID passed in is valid.
    Error: if an unspecified error happens processing the request.
  """
    if not jid:
        raise InvalidJidError()

    request = xmpp_service_pb.BulkPresenceRequest()
    response = xmpp_service_pb.BulkPresenceResponse()

    if isinstance(jid, basestring):
        single_jid = True
        jidlist = [jid]
    else:

        single_jid = False
        get_show = True
        jidlist = jid

    for given_jid in jidlist:
        request.add_jid(_to_str(given_jid))

    if from_jid:
        request.set_from_jid(_to_str(from_jid))

    try:
        apiproxy_stub_map.MakeSyncCall("xmpp", "BulkGetPresence", request,
                                       response)
    except apiproxy_errors.ApplicationError, e:
        if (e.application_error == xmpp_service_pb.XmppServiceError.INVALID_JID
            ):

            raise InvalidJidError()
        elif (e.application_error ==
              xmpp_service_pb.XmppServiceError.NONDEFAULT_MODULE):
            raise NondefaultModuleError()
        else:
            raise Error()
Пример #12
0
    def _flush(self):
        """Internal version of flush() with no locking."""
        if self._stderr:
            sys.stderr.flush()
            return

        lines_to_be_flushed = []
        try:
            while True:
                group = log_service_pb.UserAppLogGroup()
                bytes_left = LogsBufferNew._MAX_FLUSH_SIZE
                while self._buffer:
                    bare_line = self._get_line()

                    timestamp_usec, level, message = logsutil.ParseLogEntry(
                        bare_line)

                    if message[-1] == '\n':
                        message = message[:-1]

                    if not message:
                        continue

                    message = LogsBufferNew._truncate(
                        message, LogsBufferNew._MAX_LINE_SIZE)

                    if len(message) > bytes_left:
                        self._rollback_line(bare_line)
                        break

                    lines_to_be_flushed.append(bare_line)

                    line = group.add_log_line()
                    line.set_timestamp_usec(timestamp_usec)
                    line.set_level(level)
                    line.set_message(message)

                    bytes_left -= 1 + group.lengthString(line.ByteSize())

                request = log_service_pb.FlushRequest()
                request.set_logs(group.Encode())
                response = api_base_pb.VoidProto()
                apiproxy_stub_map.MakeSyncCall('logservice', 'Flush', request,
                                               response)
                if not self._buffer:
                    break
        except apiproxy_errors.CancelledError:

            lines_to_be_flushed.reverse()
            self._buffer.extendleft(lines_to_be_flushed)
        except Exception, e:
            lines_to_be_flushed.reverse()
            self._buffer.extendleft(lines_to_be_flushed)
            if not self._stderr:
                line = '-' * 80
                msg = 'ERROR: Could not flush to log_service (%s)\n%s\n%s\n%s\n'
                sys.stderr.write(msg %
                                 (str(e), line, '\n'.join(self._buffer), line))
            self._clear()
            raise
Пример #13
0
def stop_module(module=None, version=None):
    """Stops all instances for the given version of the module.

  Args:
    module: The module to affect, if None the current module is used.
    version: The version of the given module to affect, if None the current
      version is used.

  Raises:
    InvalidVersionError if the given module version is invalid.
    UnexpectedStateError if the module is already stopped, or cannot be stopped.
    TransientError if there is a problem persisting the change.
  """
    req = modules_service_pb.StopModuleRequest()
    if module:
        req.set_module(module)
    if version:
        req.set_version(version)
    resp = modules_service_pb.StopModuleResponse()
    try:
        apiproxy_stub_map.MakeSyncCall('modules', 'StopModule', req, resp)
    except apiproxy_errors.ApplicationError, e:
        if (e.application_error ==
                modules_service_pb.ModulesServiceError.INVALID_VERSION):
            raise InvalidVersionError()
        elif (e.application_error ==
              modules_service_pb.ModulesServiceError.UNEXPECTED_STATE):
            raise UnexpectedStateError()
        elif (e.application_error ==
              modules_service_pb.ModulesServiceError.TRANSIENT_ERROR):
            raise TransientError()
        else:
            raise Error()
Пример #14
0
def set_num_instances(instances, module=None, version=None):
    """Sets the number of instances on the module and version.

  Args:
    instances: The number of instances to set.
    module: The module to set the number of instances for, if None the current
      module will be used.
    version: The version set the number of instances for, if None the current
      version will be used.

  Raises:
    InvalidVersionError if the given module version isn't valid, TransientError
    if there is an issue persisting the change.
    TypeError if the given instances type is invalid.
  """
    if not isinstance(instances, (long, int)):
        raise TypeError("'instances' arg must be of type long or int.")
    req = modules_service_pb.SetNumInstancesRequest()
    req.set_instances(instances)
    if module:
        req.set_module(module)
    if version:
        req.set_version(version)
    resp = modules_service_pb.SetNumInstancesResponse()
    try:
        apiproxy_stub_map.MakeSyncCall('modules', 'SetNumInstances', req, resp)
    except apiproxy_errors.ApplicationError, e:
        if (e.application_error ==
                modules_service_pb.ModulesServiceError.INVALID_VERSION):
            raise InvalidVersionError()
        elif (e.application_error ==
              modules_service_pb.ModulesServiceError.TRANSIENT_ERROR):
            raise TransientError()
        else:
            raise Error()
Пример #15
0
def get_num_instances(module=None, version=None):
    """Return the number of instances that are set for the given module version.

  This is only valid for fixed modules, an error will be raised for
  automatically-scaled modules.  Support for automatically-scaled modules may be
  supported in the future.

  Args:
    module: String containing the name of the module to fetch this info for, if
      None the module of the current instance will be used.
    version: String containing the name of the version to fetch this info for,
      if None the version of the current instance will be used.  If that version
      does not exist in the other module, then an InvalidVersionError is raised.

  Raises:
    InvalidVersionError on invalid input.
  """
    req = modules_service_pb.GetNumInstancesRequest()
    if module:
        req.set_module(module)
    if version:
        req.set_version(version)
    resp = modules_service_pb.GetNumInstancesResponse()
    try:
        apiproxy_stub_map.MakeSyncCall('modules', 'GetNumInstances', req, resp)
    except apiproxy_errors.ApplicationError, e:
        if (e.application_error ==
                modules_service_pb.ModulesServiceError.INVALID_VERSION):
            raise InvalidVersionError()
        else:
            raise Error()
Пример #16
0
  def setsockopt(self, level, option, value):
    """setsockopt(level, option, value)

    Set a socket option.  See the Unix manual for level and option.
    The value argument can either be an integer or a string.
    """



    if not self._created:
      self._setsockopt.append((level, option, value))
      self._CreateSocket()
      return
    if not self._socket_descriptor:
      raise error(errno.EBADF, os.strerror(errno.EBADF))

    request = remote_socket_service_pb.SetSocketOptionsRequest()
    request.set_socket_descriptor(self._socket_descriptor)

    o = request.add_options()
    o.set_level(level)
    o.set_option(option)
    if isinstance(value, (int, long)):
      o.set_value(struct.pack('=L', value))
    else:
      o.set_value(value)

    reply = remote_socket_service_pb.SetSocketOptionsReply()

    try:
      apiproxy_stub_map.MakeSyncCall(
          'remote_socket', 'SetSocketOptions', request, reply)
    except apiproxy_errors.ApplicationError, e:
      raise _SystemExceptionFromAppError(e)
Пример #17
0
  def getsockopt(self, level, option, buffersize=0):
    """getsockopt(level, option[, buffersize]) -> value

    Get a socket option.  See the Unix manual for level and option.
    If a nonzero buffersize argument is given, the return value is a
    string of that length; otherwise it is an integer.
    """
    if not self._created:
      self._CreateSocket()
    if not self._socket_descriptor:
      raise error(errno.EBADF, os.strerror(errno.EBADF))

    request = remote_socket_service_pb.GetSocketOptionsRequest()
    request.set_socket_descriptor(self._socket_descriptor)
    o = request.add_options()
    o.set_level(level)
    o.set_option(option)
    o.set_value('')

    reply = remote_socket_service_pb.GetSocketOptionsReply()

    try:
      apiproxy_stub_map.MakeSyncCall(
          'remote_socket', 'GetSocketOptions', request, reply)
    except apiproxy_errors.ApplicationError, e:
      raise _SystemExceptionFromAppError(e)
Пример #18
0
def create_logout_url(dest_url, _auth_domain=None):
    """Computes the logout URL and specified destination URL for the request.

  This function works for both federated login applications and Google Accounts
  applications.

  Args:
    dest_url: String that is the desired final destination URL for the user
        after the user has logged out. If `dest_url` does not specify a host,
        the host from the current request is used.

  Returns:
    Logout URL as a string.
  """
    req = user_service_pb.CreateLogoutURLRequest()
    resp = user_service_pb.CreateLogoutURLResponse()
    req.set_destination_url(dest_url)
    if _auth_domain:
        req.set_auth_domain(_auth_domain)

    try:
        apiproxy_stub_map.MakeSyncCall('user', 'CreateLogoutURL', req, resp)
    except apiproxy_errors.ApplicationError, e:
        if (e.application_error ==
                user_service_pb.UserServiceError.REDIRECT_URL_TOO_LONG):
            raise RedirectTooLongError
        else:
            raise e
Пример #19
0
  def bind(self, address):
    """bind(address)

    Bind the socket to a local address.  For IP sockets, the address is a
    pair (host, port); the host must refer to the local host. For raw packet
    sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
    """
    if not self._created:
      self._CreateSocket(bind_address=address)
      return
    if not self._socket_descriptor:
      raise error(errno.EBADF, os.strerror(errno.EBADF))
    if self._bound:
      raise error(errno.EINVAL, os.strerror(errno.EINVAL))

    request = remote_socket_service_pb.BindRequest()
    request.set_socket_descriptor(self._socket_descriptor)
    self._SetProtoFromAddr(request.mutable_proxy_external_ip(), address)

    reply = remote_socket_service_pb.BindReply()

    try:
      apiproxy_stub_map.MakeSyncCall('remote_socket', 'Bind', request, reply)
    except apiproxy_errors.ApplicationError, e:
      raise _SystemExceptionFromAppError(e)
Пример #20
0
def start_new_background_thread(target, args, kwargs=None):
  """Starts a new background thread.

  Creates a new background thread which will call target(*args, **kwargs).

  Args:
    target: A callable for the new thread to run.
    args: Position arguments to be passed to target.
    kwargs: Keyword arguments to be passed to target.

  Returns:
    The thread ID of the background thread.
  """

  if kwargs is None:
    kwargs = {}
  request = system_service_pb.StartBackgroundRequestRequest()
  response = system_service_pb.StartBackgroundRequestResponse()
  try:
    apiproxy_stub_map.MakeSyncCall('system', 'StartBackgroundRequest', request,
                                   response)
  except apiproxy_errors.ApplicationError as error:
    raise ERROR_MAP[error.application_error](error.error_detail)
  else:
    return background.EnqueueBackgroundThread(
        response.request_id(),
        target,
        args,
        kwargs)
Пример #21
0
  def accept(self):
    """accept() -> (socket object, address info)

    Wait for an incoming connection.  Return a new socket representing the
    connection, and the address of the client.  For IP sockets, the address
    info is a pair (hostaddr, port).
    """
    if not self._created:
      self._CreateSocket()
    if not self._socket_descriptor:
      raise error(errno.EBADF, os.strerror(errno.EBADF))
    if not self._listen:
      raise error(errno.EINVAL, os.strerror(errno.EINVAL))

    request = remote_socket_service_pb.AcceptRequest()
    request.set_socket_descriptor(self._socket_descriptor)
    if self.gettimeout() is not None:
      request.set_timeout_seconds(self.gettimeout())

    reply = remote_socket_service_pb.AcceptReply()

    try:
      apiproxy_stub_map.MakeSyncCall('remote_socket', 'Accept', request, reply)
    except apiproxy_errors.ApplicationError, e:
      raise _SystemExceptionFromAppError(e)
def send_invite(jid, from_jid=None):
  """Sends an invitation to chat to a JID.

  Args:
    jid: The JID of the contact to invite.
    from_jid: The optional custom JID to use for sending. Currently, the default
      is <appid>@appspot.com. This is supported as a value. Custom JIDs can be
      of the form <anything>@<appid>.appspotchat.com.

  Raises:
    InvalidJidError if the JID passed is invalid.
    Error if an unspecified error happens processing the request.
  """
  if not jid:
    raise InvalidJidError()

  request = xmpp_service_pb.XmppInviteRequest()
  response = xmpp_service_pb.XmppInviteResponse()

  request.set_jid(_to_str(jid))
  if from_jid:
    request.set_from_jid(_to_str(from_jid))

  try:
    apiproxy_stub_map.MakeSyncCall("xmpp",
                                   "SendInvite",
                                   request,
                                   response)
  except apiproxy_errors.ApplicationError, e:
    if (e.application_error ==
        xmpp_service_pb.XmppServiceError.INVALID_JID):
      raise InvalidJidError()
    else:
      raise Error()
Пример #23
0
def create_channel(client_id, duration_minutes=None):
  """Create a channel.

  Args:
    client_id: A string to identify this channel on the server side.
    duration_minutes: A string, the maximum number of minutes the channel 
      should be open.
  Returns:
    A token that the client can use to connect to the channel.

  Raises:
    InvalidChannelClientIdError: if clientid is not an instance of str or
        unicode, or if the (utf-8 encoded) string is longer than 64 characters.
    Other errors returned by _ToChannelError
  """


  client_id = _ValidateClientId(client_id)

  request = channel_service_pb.CreateChannelRequest()
  response = channel_service_pb.CreateChannelResponse()

  request.set_application_key(client_id)

  try:
    apiproxy_stub_map.MakeSyncCall(_GetService(),
                                   'CreateChannel',
                                   request,
                                   response)
  except apiproxy_errors.ApplicationError, e:
    raise _ToChannelError(e)
Пример #24
0
    def __AddTasks(self, tasks, transactional):
        """Internal implementation of .add() where tasks must be a list."""

        if len(tasks) > MAX_TASKS_PER_ADD:
            raise TooManyTasksError(
                'No more than %d tasks can be added in a single add call' %
                MAX_TASKS_PER_ADD)

        request = taskqueue_service_pb.TaskQueueBulkAddRequest()
        response = taskqueue_service_pb.TaskQueueBulkAddResponse()

        task_names = set()
        for task in tasks:
            if task.name:
                if task.name in task_names:
                    raise DuplicateTaskNameError(
                        'The task name %r is used more than once in the request'
                        % task.name)
                task_names.add(task.name)

            self.__FillAddRequest(task, request.add_add_request(),
                                  transactional)

        try:
            apiproxy_stub_map.MakeSyncCall('taskqueue', 'BulkAdd', request,
                                           response)
        except apiproxy_errors.ApplicationError, e:
            raise self.__TranslateError(e.application_error, e.error_detail)
Пример #25
0
    def _AddTransactionalBulkTask(self, request, response):
        """ Add a transactional task.

    Args:
      request: A taskqueue_service_pb.TaskQueueAddRequest.
      response: A taskqueue_service_pb.TaskQueueAddResponse.
    Returns:
      The taskqueue response.
    """
        for add_request in request.add_request_list():
            task_result = response.add_taskresult()
            task_name = None
            if add_request.has_task_name():
                task_name = add_request.task_name()
            namespaced_name = self._ChooseTaskName(add_request.app_id(),
                                                   add_request.queue_name(),
                                                   user_chosen=task_name)
            add_request.set_task_name(namespaced_name)
            task_result.set_chosen_task_name(namespaced_name)

        for add_request, task_result in zip(request.add_request_list(),
                                            response.taskresult_list()):
            task_result.set_result(
                taskqueue_service_pb.TaskQueueServiceError.OK)

        # All task should have been validated and assigned a unique name by this point.
        try:
            apiproxy_stub_map.MakeSyncCall('datastore_v3', 'AddActions',
                                           request, api_base_pb.VoidProto())
        except apiproxy_errors.ApplicationError, e:
            raise apiproxy_errors.ApplicationError(
                e.application_error +
                taskqueue_service_pb.TaskQueueServiceError.DATASTORE_ERROR,
                e.error_detail)
Пример #26
0
    def execute_transforms(self, output_encoding=PNG):
        """Perform transformations on given image.

    Args:
      output_encoding: A value from OUTPUT_ENCODING_TYPES.

    Returns:
      str, image data after the transformations have been performed on it.

    Raises:
      BadRequestError when there is something wrong with the request
        specifications.
      NotImageError when the image data given is not an image.
      BadImageError when the image data given is corrupt.
      LargeImageError when the image data given is too large to process.
      InvalidBlobKeyError when the blob key provided is invalid.
      TransformtionError when something errors during image manipulation.
      Error when something unknown, but bad, happens.
    """
        if output_encoding not in OUTPUT_ENCODING_TYPES:
            raise BadRequestError("Output encoding type not in recognized set "
                                  "%s" % OUTPUT_ENCODING_TYPES)

        if not self._transforms:
            raise BadRequestError("Must specify at least one transformation.")

        request = images_service_pb.ImagesTransformRequest()
        response = images_service_pb.ImagesTransformResponse()

        self._set_imagedata(request.mutable_image())

        for transform in self._transforms:
            request.add_transform().CopyFrom(transform)

        request.mutable_output().set_mime_type(output_encoding)

        try:
            apiproxy_stub_map.MakeSyncCall("images", "Transform", request,
                                           response)
        except apiproxy_errors.ApplicationError, e:
            if (e.application_error ==
                    images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA):
                raise BadRequestError()
            elif (e.application_error ==
                  images_service_pb.ImagesServiceError.NOT_IMAGE):
                raise NotImageError()
            elif (e.application_error ==
                  images_service_pb.ImagesServiceError.BAD_IMAGE_DATA):
                raise BadImageError()
            elif (e.application_error ==
                  images_service_pb.ImagesServiceError.IMAGE_TOO_LARGE):
                raise LargeImageError()
            elif (e.application_error ==
                  images_service_pb.ImagesServiceError.INVALID_BLOB_KEY):
                raise InvalidBlobKeyError()
            elif (e.application_error ==
                  images_service_pb.ImagesServiceError.UNSPECIFIED_ERROR):
                raise TransformationError()
            else:
                raise Error()
Пример #27
0
    def _flush(self):
        """Internal version of flush() with no locking."""
        logs = self.parse_logs()
        self._clear()

        while True:
            group = log_service_pb.UserAppLogGroup()
            byte_size = 0
            n = 0
            for timestamp_usec, level, message, unused_source_location in logs:

                message = self._truncate(message, self._MAX_LINE_SIZE)

                if byte_size + len(message) > self._MAX_FLUSH_SIZE:
                    break
                line = group.add_log_line()
                line.set_timestamp_usec(timestamp_usec)
                line.set_level(level)
                line.set_message(message)
                byte_size += 1 + group.lengthString(line.ByteSize())
                n += 1
            assert n > 0 or not logs
            logs = logs[n:]

            request = log_service_pb.FlushRequest()
            request.set_logs(group.Encode())
            response = api_base_pb.VoidProto()
            apiproxy_stub_map.MakeSyncCall('logservice', 'Flush', request,
                                           response)
            if not logs:
                break
Пример #28
0
def _maybe_call_get_oauth_user(_scope=None):
    """Makes an GetOAuthUser RPC and stores the results in os.environ.

  This method will only make the RPC if 'OAUTH_ERROR_CODE' has not already
  been set or 'OAUTH_LAST_SCOPE' is different to _scope.
  """

    if ('OAUTH_ERROR_CODE' not in os.environ
            or os.environ.get('OAUTH_LAST_SCOPE', None) != _scope):
        req = user_service_pb.GetOAuthUserRequest()
        if _scope:
            req.set_scope(_scope)

        resp = user_service_pb.GetOAuthUserResponse()
        try:
            apiproxy_stub_map.MakeSyncCall('user', 'GetOAuthUser', req, resp)
            os.environ['OAUTH_EMAIL'] = resp.email()
            os.environ['OAUTH_AUTH_DOMAIN'] = resp.auth_domain()
            os.environ['OAUTH_USER_ID'] = resp.user_id()
            if resp.is_admin():
                os.environ['OAUTH_IS_ADMIN'] = '1'
            else:
                os.environ['OAUTH_IS_ADMIN'] = '0'
            os.environ['OAUTH_ERROR_CODE'] = ''
        except apiproxy_errors.ApplicationError, e:
            os.environ['OAUTH_ERROR_CODE'] = str(e.application_error)
        if _scope:
            os.environ['OAUTH_LAST_SCOPE'] = _scope
        else:
            os.environ.pop('OAUTH_LAST_SCOPE', None)
Пример #29
0
def SetStatus(status):
    """Indicates the process status."""
    request = log_service_pb.SetStatusRequest()
    request.status = status
    response = api_base_pb.VoidProto()
    apiproxy_stub_map.MakeSyncCall('logservice', 'SetStatus', request,
                                   response)
Пример #30
0
def get_oauth_consumer_key():
    """Returns the value of the 'oauth_consumer_key' parameter from the request.

  Returns:
    string: The value of the 'oauth_consumer_key' parameter from the request,
        an identifier for the consumer that signed the request.

  Raises:
    OAuthRequestError: The request was not a valid OAuth request.
    OAuthServiceFailureError: An unknown error occurred.
  """
    req = user_service_pb.CheckOAuthSignatureRequest()
    resp = user_service_pb.CheckOAuthSignatureResponse()
    try:
        apiproxy_stub_map.MakeSyncCall('user', 'CheckOAuthSignature', req,
                                       resp)
    except apiproxy_errors.ApplicationError, e:
        if (e.application_error ==
                user_service_pb.UserServiceError.OAUTH_INVALID_REQUEST):
            raise InvalidOAuthParametersError
        elif (e.application_error ==
              user_service_pb.UserServiceError.OAUTH_ERROR):
            raise OAuthServiceFailureError
        else:
            raise OAuthServiceFailureError