def print_gpb_compact_hdr(header):
    """
    Print the compact GPB message header
    """
    print("""
Encoding:{:#x}
Policy Name:{}
Version:{}
Identifier:{}
Start Time:{}
End Time:{}
# Tables: {}""".format(header.encoding, header.policy_name,
                       header.version, header.identifier,
                       timestamp_to_string(header.start_time),
                       timestamp_to_string(header.end_time),
                       len(header.tables)))
def print_gpb_kv_hdr(header):
    """
    Print the key-value GPB message header
    """
    print(
        """
Collection ID:   {}
Base Path:       {}
Subscription ID: {}
Model Version:   {}""".format(
            header.collection_id, header.base_path, header.subscription_identifier, header.model_version
        )
    )
    # start and end time are not always present
    if header.collection_start_time > 0:
        print("Start Time:      {}".format(timestamp_to_string(header.collection_start_time)))
    print("Msg Timestamp:   {}".format(timestamp_to_string(header.msg_timestamp)))
    if header.collection_end_time > 0:
        print("End Time:      {}".format(timestamp_to_string(header.collection_end_time)))
    print("Fields: {}".format(len(header.fields)))
コード例 #3
0
def print_json_hdr(message):
    """
    Print the values in the top level JSON object
    """
    # Print everything except the Data, displaying timestamps in date format
    print("")
    for key, value in message.items():
        if key == "CollectionStartTime" or key == "CollectionEndTime" or key == "Start Time" or key == "End Time":
            print("{}: {}".format(key, timestamp_to_string(value)))
        elif key != "Data":
            print("{}: {}".format(key, value))
def print_json_hdr(message):
    """
    Print the values in the top level JSON object
    """
    # Print everything except the Data, displaying timestamps in date format
    print("")
    for key, value in message.items():
        if (key == "CollectionStartTime" or key == "CollectionEndTime"
                or key == "Start Time" or key == "End Time"):
            print("{}: {}".format(key, timestamp_to_string(value)))
        elif key != "Data":
            print("{}: {}".format(key, value))
def print_gpb_kv_hdr(header):
    """
    Print the key-value GPB message header
    """
    print("""
Collection ID:   {}
Base Path:       {}
Subscription ID: {}
Model Version:   {}""".format(header.collection_id, header.base_path,
                              header.subscription_identifier,
                              header.model_version))
    # start and end time are not always present
    if header.collection_start_time > 0:
        print("Start Time:      {}".format(
            timestamp_to_string(header.collection_start_time)))
    print("Msg Timestamp:   {}".format(
        timestamp_to_string(header.msg_timestamp)))
    if header.collection_end_time > 0:
        print("End Time:      {}".format(
            timestamp_to_string(header.collection_end_time)))
    print("Fields: {}".format(len(header.fields)))
def print_gpb_compact_hdr(header):
    """
    Print the compact GPB message header
    """
    print(
        """
Encoding:{:#x}
Policy Name:{}
Version:{}
Identifier:{}
Start Time:{}
End Time:{}
# Tables: {}""".format(
            header.encoding,
            header.policy_name,
            header.version,
            header.identifier,
            timestamp_to_string(header.start_time),
            timestamp_to_string(header.end_time),
            len(header.tables),
        )
    )
def print_json_data(obj, indent, args):
    """
    Print the body of a JSON message
    """
    if type(obj) == dict:
        for key in list(obj.keys()):
            child = obj[key]
            # If the child object is a list or dictionary then indent using
            # braces. If the child is a leaf then just print on a single line.
            if type(child) == dict:
                print_at_indent("{} {{".format(key), indent)
                print_json_data(obj[key], indent + 1, args)
                print_at_indent("}", indent)
            elif type(child) == list:
                if not args.print_all:
                    warning = " - displaying first entry only"
                else:
                    warning = ""
                print_at_indent(
                    "{} ({} items{}) [".format(key, len(child), warning),
                    indent)
                print_json_data(obj[key], indent + 1, args)
                print_at_indent("]", indent)
            elif key == "CollectionTime":
                # Pretty-print collection timestamp
                print_at_indent(
                    "{}: {}".format(key, timestamp_to_string(child)), indent)
            else:
                # Try printing values as a string and if that fails print
                # it as bytes
                try:
                    print_at_indent("{}: {}".format(key, str(child)), indent)
                except Exception:
                    print_at_indent(
                        "{}: {}".format(key, bytes_to_string(child)), indent)
    elif type(obj) == list:
        for i, item in enumerate(obj):
            print_at_indent("[{}]".format(i), indent)
            print_json_data(item, indent + 1, args)
            if not args.print_all:
                # Stop after first item
                break
    else:
        print_at_indent("{}".format(str(obj)), indent)
コード例 #8
0
def print_json_data(obj, indent, args):
    """
    Print the body of a JSON message
    """
    if type(obj) == dict:
        for key in list(obj.keys()):
            child = obj[key]
            # If the child object is a list or dictionary then indent using
            # braces. If the child is a leaf then just print on a single line.
            if type(child) == dict:
                print_at_indent("{} {{".format(key), indent)
                print_json_data(obj[key], indent + 1, args)
                print_at_indent("}", indent)
            elif type(child) == list:
                if not args.print_all:
                    warning = " - displaying first entry only"
                else:
                    warning = ""
                print_at_indent("{} ({} items{}) [".format(key, len(child), warning), indent)
                print_json_data(obj[key], indent + 1, args)
                print_at_indent("]", indent)
            elif key == "CollectionTime":
                # Pretty-print collection timestamp
                print_at_indent("{}: {}".format(key, timestamp_to_string(child)), indent)
            else:
                # Try printing values as a string and if that fails print
                # it as bytes
                try:
                    print_at_indent("{}: {}".format(key, str(child)), indent)
                except Exception:
                    print_at_indent("{}: {}".format(key, bytes_to_string(child)), indent)
    elif type(obj) == list:
        for i, item in enumerate(obj):
            print_at_indent("[{}]".format(i), indent)
            print_json_data(item, indent + 1, args)
            if not args.print_all:
                # Stop after first item
                break
    else:
        print_at_indent("{}".format(str(obj)), indent)
def print_gpb_kv_field(field, indent):
    """
    Pretty-print a TelemtryField message
    """
    # Decode the timestamp if there is one
    if field.timestamp != 0:
        time = timestamp_to_string(field.timestamp)
    else:
        time = ""

    # Find the datatype and print it
    datatypes = [
        "bytes_value",
        "string_value",
        "bool_value",
        "uint32_value",
        "uint64_value",
        "sint32_value",
        "sint64_value",
        "double_value",
        "float_value",
    ]
    for d in datatypes:
        datatype = d[:-6]
        if field.HasField(d):
            if datatype == "bytes":
                print_gpb_kv_field_data(field.name, bytes_to_string(field.bytes_value), datatype, time, indent)
            else:
                print_gpb_kv_field_data(field.name, getattr(field, d), datatype, time, indent)

    # If 'fields' is used then recursively call this function to decode
    if len(field.fields) > 0:
        print_gpb_kv_field_data(
            field.name, "fields", "items {}".format(len(field.fields)), "{} {{".format(time), indent
        )

        for f in field.fields:
            print_gpb_kv_field(f, indent + 1)
        print_at_indent("}", indent)
def print_gpb_kv_field(field, indent):
    """
    Pretty-print a TelemtryField message
    """
    # Decode the timestamp if there is one
    if field.timestamp != 0:
        time = timestamp_to_string(field.timestamp)
    else:
        time = ""

    # Find the datatype and print it
    datatypes = [
        "bytes_value", "string_value", "bool_value", "uint32_value",
        "uint64_value", "sint32_value", "sint64_value", "double_value",
        "float_value"
    ]
    for d in datatypes:
        datatype = d[:-6]
        if field.HasField(d):
            if datatype == "bytes":
                print_gpb_kv_field_data(field.name,
                                        bytes_to_string(field.bytes_value),
                                        datatype, time, indent)
            else:
                print_gpb_kv_field_data(field.name, getattr(field, d),
                                        datatype, time, indent)

    # If 'fields' is used then recursively call this function to decode
    if len(field.fields) > 0:
        print_gpb_kv_field_data(field.name, "fields",
                                "items {}".format(len(field.fields)),
                                "{} {{".format(time), indent)

        for f in field.fields:
            print_gpb_kv_field(f, indent + 1)
        print_at_indent("}", indent)