Esempio n. 1
0
def DictToProtoMessage(values, out_message):
    for name, field in out_message.DESCRIPTOR.fields_by_name.iteritems():
        if name not in values:
            if field.label == field.LABEL_REQUIRED:
                raise ValueError, "Missing required field %s" % name
            continue

        value = values.get(name)
        if field.type == field.TYPE_MESSAGE:
            inner_message = getattr(out_message, name)
            if field.label == field.LABEL_REPEATED:
                for subval in value:
                    DictToProtoMessage(subval, inner_message.add())
            else:
                DictToProtoMessage(value, inner_message)
        else:
            if field.label == field.LABEL_REPEATED:
                out = getattr(out_message, name)
                for v in value:
                    if isinstance(v, datetime.datetime):
                        v = util.datetime_to_iso8601str(v, TIME_ZONE)
                    out.append(v)
            else:
                if isinstance(value, datetime.datetime):
                    value = util.datetime_to_iso8601str(value, TIME_ZONE)
                setattr(out_message, name, value)
    return out_message
Esempio n. 2
0
def DictToProtoMessage(values, out_message):
  for name, field in out_message.DESCRIPTOR.fields_by_name.iteritems():
    if name not in values:
      if field.label == field.LABEL_REQUIRED:
        raise ValueError, "Missing required field %s" % name
      continue

    value = values.get(name)
    if field.type == field.TYPE_MESSAGE:
      inner_message = getattr(out_message, name)
      if field.label == field.LABEL_REPEATED:
        for subval in value:
          DictToProtoMessage(subval, inner_message.add())
      else:
        DictToProtoMessage(value, inner_message)
    else:
      if field.label == field.LABEL_REPEATED:
        out = getattr(out_message, name)
        for v in value:
          if isinstance(v, datetime.datetime):
            v = util.datetime_to_iso8601str(v, TIME_ZONE)
          out.append(v)
      else:
        if isinstance(value, datetime.datetime):
          value = util.datetime_to_iso8601str(value, TIME_ZONE)
        setattr(out_message, name, value)
  return out_message
Esempio n. 3
0
 def default(self, obj):
   if isinstance(obj, datetime.datetime):
     return util.datetime_to_iso8601str(obj, TIME_ZONE)
   return json.JSONEncoder.default(self, obj)
Esempio n. 4
0
 def default(self, obj):
     if isinstance(obj, datetime.datetime):
         return util.datetime_to_iso8601str(obj, TIME_ZONE)
     return json.JSONEncoder.default(self, obj)