Python google.protobuf.descriptor.FieldDescriptor.TYPE_STRING() Examples

The following are code examples for showing how to use google.protobuf.descriptor.FieldDescriptor.TYPE_STRING(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pbtk   Author: marin-m   File:    GNU General Public License v3.0 6 votes vote down vote up
def produce(obj, pb, sep):
    for ds, val in pb.ListFields():
        for val in (val if ds.label == ds.LABEL_REPEATED else [val]):
            if ds.cpp_type == ds.CPPTYPE_MESSAGE:
                origlen = len(obj)
                produce(obj, val, sep)
                obj.insert(origlen, '%dm%d' % (ds.number, len(obj) - origlen))
            elif ds.type == ds.TYPE_STRING:
                if sep == '!':
                    val = val.replace('*', '*2A').replace('!', '*21')
                    val = quote(val, safe='~()*!.\'')
            elif ds.type == ds.TYPE_BYTES:
                val = urlsafe_b64encode(val).decode('ascii').strip('=')
            elif ds.type == ds.TYPE_BOOL:
                val = int(val)
            obj.append('%d%s%s' % (ds.number, types_enc[ds.type], val))
    return obj 
Example 2
Project: felix   Author: axbaretto   File:    Apache License 2.0 5 votes vote down vote up
def convert_pb_rules(pb_rules):
    dict_rules = []
    for pb_rule in pb_rules:
        _log.debug("Converting protobuf rule: %r type: %s",
                   pb_rule, pb_rule.__class__)
        d_rule = {}
        for fd, value in pb_rule.ListFields():
            if value is None:
            if fd.type == FieldDescriptor.TYPE_STRING and value == "":
            if fd.type in (FieldDescriptor.TYPE_INT32,
                           FieldDescriptor.TYPE_INT64) and value == 0:
            _log.debug("Field %s = %s",, value)
            negated ="not_")
            stem = if not negated else[4:]
            dict_name = "!" + stem if negated else stem

            if stem.endswith("_ports"):
                value = convert_pb_ports(value)
            elif stem.endswith("protocol"):
                value = convert_pb_protocol(value)
            elif stem.endswith("ip_set_ids"):
                value = list(value)

            if stem == "icmp_type_code":
                # Special case: ICMP is represented by an object, unpack it.
                d_rule[("!" if negated else "") + "icmp_type"] = value.type
                d_rule[("!" if negated else "") + "icmp_code"] = value.code
                d_rule[dict_name] = value

    return dict_rules 
Example 3
Project: pbtk   Author: marin-m   File:    GNU General Public License v3.0 4 votes vote down vote up
def consume(obj, pb, sep):
    while obj:
        field = obj.pop(0)
        index, type_, val = match('(\d+)(\w)(.*)', field).groups()
        type_ = types_dec[type_]
        if int(index) not in pb.DESCRIPTOR.fields_by_number:
            warn('Unknown index: !' + field)
            if type_ == fd.TYPE_MESSAGE:
                del obj[:int(val)]
        field = pb.DESCRIPTOR.fields_by_number[int(index)]
        repeated = field.label == field.LABEL_REPEATED
        field =
        if type_ == fd.TYPE_MESSAGE:
            if not repeated:
                getattr(pb, field).SetInParent()
                consume(obj[:int(val)], getattr(pb, field), sep)
                consume(obj[:int(val)], getattr(pb, field).add(), sep)
            del obj[:int(val)]
        elif type_ == fd.TYPE_STRING:
            if sep == '!':
                val = val.replace('*21', '!').replace('*2A', '*')
                val = unquote(val)
        elif type_ == fd.TYPE_BYTES:
            val = urlsafe_b64decode(val + '=' * (-len(val) % 4))
        elif type_ == "base64_string":
            val = urlsafe_b64decode(val + '=' * (-len(val) % 4)).decode('utf8')
        elif type_ == fd.TYPE_BOOL:
            val = bool(int(val))
        elif type_ in (fd.TYPE_DOUBLE, fd.TYPE_FLOAT):
            val = float(val)
            val = int(val)
        if not repeated:
            setattr(pb, field, val)
            getattr(pb, field).append(val)