Chromium Code Reviews| Index: infra_libs/ts_mon/common/pb_to_popo.py |
| diff --git a/infra_libs/ts_mon/common/pb_to_popo.py b/infra_libs/ts_mon/common/pb_to_popo.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c732ff453710e121d3a35b0d6a9c23b1b67a9238 |
| --- /dev/null |
| +++ b/infra_libs/ts_mon/common/pb_to_popo.py |
| @@ -0,0 +1,35 @@ |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +from google.protobuf.descriptor import FieldDescriptor as fd |
| + |
| +def convert(pb): |
| + """Convert protobuf to plain-old-python-object""" |
| + obj = {} |
| + for field, value in pb.ListFields(): |
| + if field.label == fd.LABEL_REPEATED: |
| + obj[field.name] = list(_FD_TO_JSON[field.type](v) for v in value) |
|
Sergey Berezin
2016/07/01 22:05:21
nit: just to be on the safe side, I'd use _FD_TO_J
tnn
2016/07/01 22:31:39
If a proto changes, crashing an application is bet
Sergey Berezin
2016/07/06 17:18:39
For application code a crash may be preferred in s
tnn
2016/07/06 19:28:06
Fair enough. I'll use unicode and log something he
|
| + else: |
| + obj[field.name] = _FD_TO_JSON[field.type](value) |
| + return obj |
| + |
| + |
| +_FD_TO_JSON = { |
| + fd.TYPE_BOOL: bool, |
| + fd.TYPE_DOUBLE: float, |
| + fd.TYPE_ENUM: int, |
| + fd.TYPE_FIXED32: float, |
| + fd.TYPE_FIXED64: float, |
| + fd.TYPE_FLOAT: float, |
| + fd.TYPE_INT32: int, |
| + fd.TYPE_INT64: long, |
| + fd.TYPE_SFIXED32: float, |
| + fd.TYPE_SFIXED64: float, |
| + fd.TYPE_SINT32: int, |
| + fd.TYPE_SINT64: long, |
| + fd.TYPE_STRING: unicode, |
| + fd.TYPE_UINT32: int, |
| + fd.TYPE_UINT64: long, |
| + fd.TYPE_MESSAGE: convert |
| +} |