Debian Codegraph @

tests/auto/protobuf/wellknown/proto/generate_anymessages_data.py

raw
1 #!/usr/bin/env python3
2 # Copyright (C) 2022 The Qt Company Ltd.
3 # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
4
5 # This script is run to generate test data for the Any message tests,
6 # transcribed to suitable parts of tst_protobuf_any.cpp
7
8 # NOTE: "protoc" must be installed and in path, this script takes care of
9 # invoking the tool to generate the generated python output.
10 # ALSO NOTE: paths assume it is run from the directory in which it is stored.
11
12 import os, os.path
13 import shutil
14
15 proto_path = '../../../shared/data/proto/anymessages.proto'
16 assert os.path.exists(proto_path), \
17 "I need to be run from the directory that generate_anymessages_data.py lives in"
18 # Generates anymessages_pb2...
19 os.system(f'protoc --python_out=. --proto_path={os.path.dirname(proto_path)} {proto_path}')
20 # ... which we subsequently import:
21 import anymessages_pb2
22
23 msg = anymessages_pb2.AnyMessage()
24 print("empty")
25 print(msg.SerializeToString().hex())
26
27 ex = anymessages_pb2.Example()
28 msg = anymessages_pb2.AnyMessage()
29 msg.field.Pack(ex)
30 print("defaulted 'Example' message")
31 print(msg.SerializeToString().hex())
32
33 ex = anymessages_pb2.Example()
34 ex.str = "Hello"
35 ex.i = 1
36 ex.j = 2
37 ex.h = 3
38 ex.str2 = "World!"
39
40 msg = anymessages_pb2.AnyMessage()
41 msg.field.Pack(ex)
42 print("hello 1 2 3 world:")
43 print(msg.SerializeToString().hex())
44
45 ex = anymessages_pb2.Example()
46 ex.str = "No"
47 ex.str2 = "Numbers!"
48
49 msg = anymessages_pb2.AnyMessage()
50 msg.field.Pack(ex)
51 print("no 0 0 0 numbers")
52 print(msg.SerializeToString().hex())
53
54 ex = anymessages_pb2.Example()
55 ex.str = "Hello"
56 ex.str2 = "World!"
57
58 anymsg = anymessages_pb2.google_dot_protobuf_dot_any__pb2.Any()
59 anymsg.Pack(ex)
60
61 msg = anymessages_pb2.RepeatedAnyMessage()
62 msg.anys.append(anymsg)
63 msg.anys.append(anymsg)
64 msg.anys.append(anymsg)
65 print("[Hello 0 0 0 World] * 3")
66 print(msg.SerializeToString().hex())
67
68 msg = anymessages_pb2.TwoAnyMessage()
69 msg.two.Pack(ex)
70 print("[Empty] and [Hello 0 0 0 World!]")
71 print(msg.SerializeToString().hex())
72
73 msg = anymessages_pb2.TwoAnyMessage()
74 anymsg = anymessages_pb2.google_dot_protobuf_dot_any__pb2.Any()
75 msg.one.Pack(anymsg)
76 msg.two.Pack(ex)
77 print("[Nested empty] and [Hello 0 0 0 World!]")
78 print(msg.SerializeToString().hex())
79
80 os.remove("anymessages_pb2.py")
81 shutil.rmtree("__pycache__")