1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2023 Olivier Dion <odion@efficios.com>
4 # Copyright (c) 2024 Philippe Proulx <pproulx@efficios.com>
11 # Recursively prints the contents of `field` with the indentation level
12 # `indent` and the introduction `intro`.
17 # No introduction (root field).
20 # Structure field member name.
23 # `field` is an array field element.
24 def _print_field(intro
, field
, indent
=0):
25 indent_str
= " " * indent
* 2
28 if intro
is _array_elem
:
30 elif intro
is not None:
31 intro_str
= "{}: ".format(intro
)
33 if isinstance(field
, bt2
._StringFieldConst
):
34 print('{}{}"{}"'.format(indent_str
, intro_str
, field
))
35 elif isinstance(field
, bt2
._StructureFieldConst
):
36 print(indent_str
+ intro_str
, end
="")
39 # Special case for an empty structure field
42 if intro
is _array_elem
:
43 # Structure field is an array field element itself:
44 # print the first element first, and then print the
45 # remaining ones indented.
52 sub_field_names
= list(field
)
53 _print_field(sub_field_names
[0], field
[sub_field_names
[0]], 0)
55 for sub_field_name
in sub_field_names
[1:]:
56 _print_field(sub_field_name
, field
[sub_field_name
], indent
+ 1)
61 # Structure field has a name (already printed at
62 # this point): print a newline, and then print all
63 # the members indented (one more level):
74 for sub_field_name
in field
:
77 field
[sub_field_name
],
80 elif isinstance(field
, bt2
._ArrayFieldConst
):
84 # Array field has an intro: print it, then print a newline,
85 # and then print all the elements indented (one more level).
87 # Example 1 (parent is an structure field):
94 # Example 2 (parent is an array field):
101 print(indent_str
+ intro_str
.rstrip())
103 for sub_field
in field
:
104 _print_field(_array_elem
, sub_field
, indent
+ add_indent
)
105 elif isinstance(field
.cls
, bt2
._IntegerFieldClassConst
):
106 # Honor the preferred display base
107 base
= field
.cls
.preferred_display_base
108 print(indent_str
+ intro_str
, end
="")
118 elif isinstance(field
, bt2
._BitArrayFieldConst
):
119 print(indent_str
+ intro_str
+ bin(field
))
120 elif isinstance(field
, bt2
._BoolFieldConst
):
121 print(indent_str
+ intro_str
+ ("yes" if field
else "no"))
122 elif isinstance(field
, bt2
._RealFieldConst
):
123 print("{}{}{:.6f}".format(indent_str
, intro_str
, float(field
)))
124 elif isinstance(field
, bt2
._OptionFieldConst
):
126 _print_field(intro
, field
.field
, indent
)
128 # Special case for an option field without a field
129 print("{}{}~".format(indent_str
, intro_str
))
130 elif isinstance(field
, bt2
._VariantFieldConst
):
131 _print_field(intro
, field
.selected_option
, indent
)
133 print(indent_str
+ intro_str
+ field
)
136 @bt2.plugin_component_class
137 class _SingleSinkComponent(bt2
._UserSinkComponent
, name
="single"):
138 def __init__(self
, config
, params
, obj
):
139 self
._input
= self
._add
_input
_port
("input")
140 self
._field
_name
= str(params
.get("field-name", "root"))
142 def _user_graph_is_configured(self
):
143 self
._it
= self
._create
_message
_iterator
(self
._input
)
145 def _user_consume(self
):
148 if type(msg
) is bt2
._EventMessageConst
:
149 assert self
._field
_name
in msg
.event
.payload_field
150 assert len(msg
.event
.payload_field
) == 1
151 _print_field(None, msg
.event
.payload_field
[self
._field
_name
])
154 bt2
.register_plugin(__name__
, "test-text")
This page took 0.031678 seconds and 4 git commands to generate.