def FromData(cls, stream, json_data, http, auto_transfer=None, **kwds):
     """Create a new Upload of stream from serialized json_data using http."""
     info = json.loads(json_data)
     missing_keys = cls._REQUIRED_SERIALIZATION_KEYS - set(info.keys())
     if missing_keys:
         raise exceptions.InvalidDataError(
             'Invalid serialization data, missing keys: %s' %
             (', '.join(missing_keys)))
     if 'total_size' in kwds:
         raise exceptions.InvalidUserInputError(
             'Cannot override total_size on serialized Upload')
     upload = cls.FromStream(stream,
                             info['mime_type'],
                             total_size=info.get('total_size'),
                             **kwds)
     if isinstance(stream, io.IOBase) and not stream.seekable():
         raise exceptions.InvalidUserInputError(
             'Cannot restart resumable upload on non-seekable stream')
     if auto_transfer is not None:
         upload.auto_transfer = auto_transfer
     else:
         upload.auto_transfer = info['auto_transfer']
     upload.strategy = RESUMABLE_UPLOAD
     upload._Initialize(http, info['url'])  # pylint: disable=protected-access
     upload.RefreshResumableUploadState()
     upload.EnsureInitialized()
     if upload.auto_transfer:
         upload.StreamInChunks()
     return upload
示例#2
0
def _ValidateJsonValue(json_value):
    entries = [(f, json_value.get_assigned_value(f.name))
               for f in json_value.all_fields()]
    assigned_entries = [(f, value) for f, value in entries
                        if value is not None]
    if len(assigned_entries) != 1:
        raise exceptions.InvalidDataError('Malformed JsonValue: %s' %
                                          json_value)
 def serialization_data(self):
     self.EnsureInitialized()
     if self.strategy != RESUMABLE_UPLOAD:
         raise exceptions.InvalidDataError(
             'Serialization only supported for resumable uploads')
     return {
         'auto_transfer': self.auto_transfer,
         'mime_type': self.mime_type,
         'total_size': self.total_size,
         'url': self.url,
     }
示例#4
0
def _IncludeFields(encoded_message, message, include_fields):
    """Add the requested fields to the encoded message."""
    if include_fields is None:
        return encoded_message
    result = json.loads(encoded_message)
    for field_name in include_fields:
        try:
            message.field_by_name(field_name)
        except KeyError:
            raise exceptions.InvalidDataError(
                'No field named %s in message of type %s' %
                (field_name, type(message)))
        result[field_name] = None
    return json.dumps(result)
示例#5
0
 def FromData(cls, stream, json_data, http=None, auto_transfer=None):
     """Create a new Download object from a stream and serialized data."""
     info = json.loads(json_data)
     missing_keys = cls._REQUIRED_SERIALIZATION_KEYS - set(info.keys())
     if missing_keys:
         raise exceptions.InvalidDataError(
             'Invalid serialization data, missing keys: %s' %
             (', '.join(missing_keys)))
     download = cls.FromStream(stream)
     if auto_transfer is not None:
         download.auto_transfer = auto_transfer
     else:
         download.auto_transfer = info['auto_transfer']
     setattr(download, '_Download__progress', info['progress'])
     setattr(download, '_Download__total_size', info['total_size'])
     download._Initialize(http, info['url'])  # pylint: disable=protected-access
     return download
示例#6
0
def _PythonValueToJsonValue(py_value):
    """Convert the given python value to a JsonValue."""
    if py_value is None:
        return JsonValue(is_null=True)
    if isinstance(py_value, bool):
        return JsonValue(boolean_value=py_value)
    if isinstance(py_value, basestring):
        return JsonValue(string_value=py_value)
    if isinstance(py_value, numbers.Number):
        if isinstance(py_value, (int, long)):
            if _MININT64 < py_value < _MAXINT64:
                return JsonValue(integer_value=py_value)
        return JsonValue(double_value=float(py_value))
    if isinstance(py_value, dict):
        return JsonValue(object_value=_PythonValueToJsonObject(py_value))
    if isinstance(py_value, collections.Iterable):
        return JsonValue(array_value=_PythonValueToJsonArray(py_value))
    raise exceptions.InvalidDataError('Cannot convert "%s" to JsonValue' %
                                      py_value)
示例#7
0
 def num_retries(self, value):
     util.Typecheck(value, (int, long))
     if value < 0:
         raise exceptions.InvalidDataError(
             'Cannot have negative value for num_retries')
     self.__num_retries = value