1 # The MIT License (MIT)
3 # Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 from barectf
.config_parse_common
import _ConfigurationParseError
25 from barectf
.config_parse_common
import _append_error_ctx
26 import barectf
.config_parse_common
as config_parse_common
27 from barectf
.config_parse_common
import _MapNode
30 from barectf
.typing
import VersionNumber
, _OptStr
31 from typing
import Optional
, List
, Dict
, TextIO
, Union
, Callable
35 def _del_prop_if_exists(node
: _MapNode
, prop_name
: str):
40 def _rename_prop(node
: _MapNode
, old_prop_name
: str, new_prop_name
: str):
41 if old_prop_name
in node
:
42 node
[new_prop_name
] = node
[old_prop_name
]
43 del node
[old_prop_name
]
46 def _copy_prop_if_exists(dst_node
: _MapNode
, src_node
: _MapNode
, src_prop_name
: str,
47 dst_prop_name
: _OptStr
= None):
48 if dst_prop_name
is None:
49 dst_prop_name
= src_prop_name
51 if src_prop_name
in src_node
:
52 dst_node
[dst_prop_name
] = copy
.deepcopy(src_node
[src_prop_name
])
55 # A barectf 2 YAML configuration parser.
57 # The only purpose of such a parser is to transform the passed root
58 # configuration node so that it's a valid barectf 3 configuration node.
60 # The parser's `config_node` property is the equivalent barectf 3
63 # See the comments of _parse() for more implementation details about the
64 # parsing stages and general strategy.
65 class _Parser(config_parse_common
._Parser
):
66 # Builds a barectf 2 YAML configuration parser and parses the root
67 # configuration node `node` (already loaded from the file-like
68 # object `root_file`).
69 def __init__(self
, root_file
: TextIO
, node
: _MapNode
, with_pkg_include_dir
: bool,
70 include_dirs
: Optional
[List
[str]], ignore_include_not_found
: bool):
71 super().__init
__(root_file
, node
, with_pkg_include_dir
, include_dirs
,
72 ignore_include_not_found
, VersionNumber(2))
73 self
._ft
_cls
_name
_to
_conv
_method
: Dict
[str, Callable
[[_MapNode
], _MapNode
]] = {
74 'int': self
._conv
_int
_ft
_node
,
75 'integer': self
._conv
_int
_ft
_node
,
76 'enum': self
._conv
_enum
_ft
_node
,
77 'enumeration': self
._conv
_enum
_ft
_node
,
78 'flt': self
._conv
_real
_ft
_node
,
79 'float': self
._conv
_real
_ft
_node
,
80 'floating-point': self
._conv
_real
_ft
_node
,
81 'str': self
._conv
_string
_ft
_node
,
82 'string': self
._conv
_string
_ft
_node
,
83 'array': self
._conv
_array
_ft
_node
,
84 'struct': self
._conv
_struct
_ft
_node
,
85 'structure': self
._conv
_struct
_ft
_node
,
89 # Converts a v2 field type node to a v3 field type node and returns
91 def _conv_ft_node(self
, v2_ft_node
: _MapNode
) -> _MapNode
:
92 assert 'class' in v2_ft_node
93 cls
= v2_ft_node
['class']
94 assert cls
in self
._ft
_cls
_name
_to
_conv
_method
95 return self
._ft
_cls
_name
_to
_conv
_method
[cls
](v2_ft_node
)
97 def _conv_ft_node_if_exists(self
, v2_parent_node
: Optional
[_MapNode
], key
: str) -> Optional
[_MapNode
]:
98 if v2_parent_node
is None:
101 if key
not in v2_parent_node
:
104 return self
._conv
_ft
_node
(v2_parent_node
[key
])
106 # Converts a v2 integer field type node to a v3 integer field type
107 # node and returns it.
108 def _conv_int_ft_node(self
, v2_ft_node
: _MapNode
) -> _MapNode
:
109 # copy v2 integer field type node
110 v3_ft_node
= copy
.deepcopy(v2_ft_node
)
112 # signedness depends on the class, not a property
115 is_signed_node
= v3_ft_node
.get(prop_name
)
117 if is_signed_node
is True:
120 v3_ft_node
['class'] = cls_name
121 _del_prop_if_exists(v3_ft_node
, prop_name
)
123 # rename `align` property to `alignment`
124 _rename_prop(v3_ft_node
, 'align', 'alignment')
126 # rename `base` property to `preferred-display-base`
127 _rename_prop(v3_ft_node
, 'base', 'preferred-display-base')
129 # remove `encoding` property
130 _del_prop_if_exists(v3_ft_node
, 'encoding')
132 # remove `byte-order` property (always target BO in v3)
133 _del_prop_if_exists(v3_ft_node
, 'byte-order')
135 # remove `property-mappings` property
136 _del_prop_if_exists(v3_ft_node
, 'property-mappings')
140 # Converts a v2 enumeration field type node to a v3 enumeration
141 # field type node and returns it.
142 def _conv_enum_ft_node(self
, v2_ft_node
: _MapNode
) -> _MapNode
:
143 # An enumeration field type _is_ an integer field type, so use a
144 # copy of the converted v2 value field type node.
145 v3_ft_node
= copy
.deepcopy(self
._conv
_ft
_node
(v2_ft_node
['value-type']))
147 # transform class name accordingly
151 if v3_ft_node
[prop_name
] == 'sint':
154 v3_ft_node
[prop_name
] = cls_name
156 # convert members to mappings
157 prop_name
= 'members'
158 members_node
= v2_ft_node
.get(prop_name
)
160 if members_node
is not None:
161 mappings_node
: _MapNode
= collections
.OrderedDict()
164 for member_node
in members_node
:
165 v3_value_node
: Union
[int, List
[int]]
167 if type(member_node
) is str:
172 assert type(member_node
) is collections
.OrderedDict
173 label
= member_node
['label']
174 v2_value_node
= member_node
['value']
176 if type(v2_value_node
) is int:
177 cur
= v2_value_node
+ 1
178 v3_value_node
= v2_value_node
180 assert type(v2_value_node
) is list
181 assert len(v2_value_node
) == 2
182 v3_value_node
= list(v2_value_node
)
183 cur
= v2_value_node
[1] + 1
185 if label
not in mappings_node
:
186 mappings_node
[label
] = []
188 mappings_node
[label
].append(v3_value_node
)
190 v3_ft_node
['mappings'] = mappings_node
194 # Converts a v2 real field type node to a v3 real field type node
196 def _conv_real_ft_node(self
, v2_ft_node
: _MapNode
) -> _MapNode
:
197 # copy v2 real field type node
198 v3_ft_node
= copy
.deepcopy(v2_ft_node
)
200 # set class to `real`
201 v3_ft_node
['class'] = 'real'
203 # rename `align` property to `alignment`
204 _rename_prop(v3_ft_node
, 'align', 'alignment')
206 # set `size` property to a single integer (total size, in bits)
208 v3_ft_node
[prop_name
] = v3_ft_node
[prop_name
]['exp'] + v3_ft_node
[prop_name
]['mant']
212 # Converts a v2 string field type node to a v3 string field type
213 # node and returns it.
214 def _conv_string_ft_node(self
, v2_ft_node
: _MapNode
) -> _MapNode
:
215 # copy v2 string field type node
216 v3_ft_node
= copy
.deepcopy(v2_ft_node
)
218 # remove `encoding` property
219 _del_prop_if_exists(v3_ft_node
, 'encoding')
223 # Converts a v2 array field type node to a v3 (static) array field
224 # type node and returns it.
225 def _conv_array_ft_node(self
, v2_ft_node
: _MapNode
) -> _MapNode
:
226 # class renamed to `static-array` or `dynamic-array`
227 is_dynamic
= v2_ft_node
['length'] == 'dynamic'
228 array_type
= 'dynamic' if is_dynamic
else 'static'
229 v3_ft_node
: _MapNode
= collections
.OrderedDict({'class': f
'{array_type}-array'})
231 # copy `length` property if it's a static array field type
233 _copy_prop_if_exists(v3_ft_node
, v2_ft_node
, 'length')
235 # convert element field type
236 v3_ft_node
['element-field-type'] = self
._conv
_ft
_node
(v2_ft_node
['element-type'])
240 # Converts a v2 structure field type node to a v3 structure field
241 # type node and returns it.
242 def _conv_struct_ft_node(self
, v2_ft_node
: _MapNode
) -> _MapNode
:
243 # Create fresh v3 structure field type node, reusing the class
245 v3_ft_node
= collections
.OrderedDict({'class': v2_ft_node
['class']})
247 # rename `min-align` property to `minimum-alignment`
248 _copy_prop_if_exists(v3_ft_node
, v2_ft_node
, 'min-align', 'minimum-alignment')
250 # convert fields to members
253 if prop_name
in v2_ft_node
:
256 for member_name
, v2_member_ft_node
in v2_ft_node
[prop_name
].items():
257 members_node
.append(collections
.OrderedDict({
258 member_name
: collections
.OrderedDict({
259 'field-type': self
._conv
_ft
_node
(v2_member_ft_node
)
263 v3_ft_node
['members'] = members_node
267 # Converts a v2 clock type node to a v3 clock type node and returns
269 def _conv_clk_type_node(self
, v2_clk_type_node
: _MapNode
) -> _MapNode
:
270 # copy v2 clock type node
271 v3_clk_type_node
= copy
.deepcopy(v2_clk_type_node
)
273 # rename `freq` property to `frequency`
274 _rename_prop(v3_clk_type_node
, 'freq', 'frequency')
276 # rename `error-cycles` property to `precision`
277 _rename_prop(v3_clk_type_node
, 'error-cycles', 'precision')
279 # rename `absolute` property to `origin-is-unix-epoch`
280 _rename_prop(v3_clk_type_node
, 'absolute', 'origin-is-unix-epoch')
282 # rename `$return-ctype`/`return-ctype` property to `$c-type`
283 new_prop_name
= '$c-type'
284 _rename_prop(v3_clk_type_node
, 'return-ctype', new_prop_name
)
285 _rename_prop(v3_clk_type_node
, '$return-ctype', new_prop_name
)
287 return v3_clk_type_node
289 # Converts a v2 event type node to a v3 event type node and returns
291 def _conv_ev_type_node(self
, v2_ev_type_node
: _MapNode
) -> _MapNode
:
292 # create empty v3 event type node
293 v3_ev_type_node
: _MapNode
= collections
.OrderedDict()
295 # copy `log-level` property
296 _copy_prop_if_exists(v3_ev_type_node
, v2_ev_type_node
, 'log-level')
298 # convert specific context field type node
299 v2_ft_node
= v2_ev_type_node
.get('context-type')
301 if v2_ft_node
is not None:
302 v3_ev_type_node
['specific-context-field-type'] = self
._conv
_ft
_node
(v2_ft_node
)
304 # convert payload field type node
305 v2_ft_node
= v2_ev_type_node
.get('payload-type')
307 if v2_ft_node
is not None:
308 v3_ev_type_node
['payload-field-type'] = self
._conv
_ft
_node
(v2_ft_node
)
310 return v3_ev_type_node
313 def _set_v3_feature_ft_if_exists(v3_features_node
: _MapNode
, key
: str,
314 node
: Union
[Optional
[_MapNode
], bool]):
320 v3_features_node
[key
] = val
322 # Converts a v2 stream type node to a v3 stream type node and
324 def _conv_stream_type_node(self
, v2_stream_type_node
: _MapNode
) -> _MapNode
:
325 # This function creates a v3 stream type features node from the
326 # packet context and event header field type nodes of a
327 # v2 stream type node.
328 def v3_features_node_from_v2_ft_nodes(v2_pkt_ctx_ft_fields_node
: _MapNode
,
329 v2_ev_header_ft_fields_node
: Optional
[_MapNode
]) -> _MapNode
:
330 if v2_ev_header_ft_fields_node
is None:
331 v2_ev_header_ft_fields_node
= collections
.OrderedDict()
333 v3_pkt_total_size_ft_node
= self
._conv
_ft
_node
(v2_pkt_ctx_ft_fields_node
['packet_size'])
334 v3_pkt_content_size_ft_node
= self
._conv
_ft
_node
(v2_pkt_ctx_ft_fields_node
['content_size'])
335 v3_pkt_beg_time_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_ctx_ft_fields_node
,
337 v3_pkt_end_time_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_ctx_ft_fields_node
,
339 v3_pkt_disc_ev_counter_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_ctx_ft_fields_node
,
341 v3_ev_type_id_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_ev_header_ft_fields_node
, 'id')
342 v3_ev_time_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_ev_header_ft_fields_node
,
344 v3_features_node
: _MapNode
= collections
.OrderedDict()
345 v3_pkt_node
: _MapNode
= collections
.OrderedDict()
346 v3_ev_node
: _MapNode
= collections
.OrderedDict()
347 v3_pkt_node
['total-size-field-type'] = v3_pkt_total_size_ft_node
348 v3_pkt_node
['content-size-field-type'] = v3_pkt_content_size_ft_node
349 self
._set
_v
3_feature
_ft
_if
_exists
(v3_pkt_node
, 'beginning-time-field-type',
350 v3_pkt_beg_time_ft_node
)
351 self
._set
_v
3_feature
_ft
_if
_exists
(v3_pkt_node
, 'end-time-field-type',
352 v3_pkt_end_time_ft_node
)
353 self
._set
_v
3_feature
_ft
_if
_exists
(v3_pkt_node
, 'discarded-events-counter-field-type',
354 v3_pkt_disc_ev_counter_ft_node
)
355 self
._set
_v
3_feature
_ft
_if
_exists
(v3_ev_node
, 'type-id-field-type',
356 v3_ev_type_id_ft_node
)
357 self
._set
_v
3_feature
_ft
_if
_exists
(v3_ev_node
, 'time-field-type', v3_ev_time_ft_node
)
358 v3_features_node
['packet'] = v3_pkt_node
359 v3_features_node
['event'] = v3_ev_node
360 return v3_features_node
362 def clk_type_name_from_v2_int_ft_node(v2_int_ft_node
: Optional
[_MapNode
]) -> _OptStr
:
363 if v2_int_ft_node
is None:
366 assert v2_int_ft_node
['class'] in ('int', 'integer')
367 prop_mappings_node
= v2_int_ft_node
.get('property-mappings')
369 if prop_mappings_node
is not None and len(prop_mappings_node
) > 0:
370 return prop_mappings_node
[0]['name']
374 # create empty v3 stream type node
375 v3_stream_type_node
: _MapNode
= collections
.OrderedDict()
377 # rename `$default` property to `$is-default`
378 _copy_prop_if_exists(v3_stream_type_node
, v2_stream_type_node
, '$default', '$is-default')
380 # set default clock type node
381 pct_prop_name
= 'packet-context-type'
382 v2_pkt_ctx_ft_fields_node
= v2_stream_type_node
[pct_prop_name
]['fields']
383 eht_prop_name
= 'event-header-type'
384 v2_ev_header_ft_fields_node
= None
385 v2_ev_header_ft_node
= v2_stream_type_node
.get(eht_prop_name
)
387 if v2_ev_header_ft_node
is not None:
388 v2_ev_header_ft_fields_node
= v2_ev_header_ft_node
['fields']
390 def_clk_type_name
= None
393 ts_begin_prop_name
= 'timestamp_begin'
394 ts_begin_clk_type_name
= clk_type_name_from_v2_int_ft_node(v2_pkt_ctx_ft_fields_node
.get(ts_begin_prop_name
))
395 ts_end_prop_name
= 'timestamp_end'
396 ts_end_clk_type_name
= clk_type_name_from_v2_int_ft_node(v2_pkt_ctx_ft_fields_node
.get(ts_end_prop_name
))
398 if ts_begin_clk_type_name
is not None and ts_end_clk_type_name
is not None:
399 if ts_begin_clk_type_name
!= ts_end_clk_type_name
:
400 raise _ConfigurationParseError(f
'`{ts_begin_prop_name}`/`{ts_end_prop_name}` properties',
401 'Field types are not mapped to the same clock type')
402 except _ConfigurationParseError
as exc
:
403 _append_error_ctx(exc
, f
'`{pct_prop_name}` property')
406 if def_clk_type_name
is None and v2_ev_header_ft_fields_node
is not None:
407 def_clk_type_name
= clk_type_name_from_v2_int_ft_node(v2_ev_header_ft_fields_node
.get('timestamp'))
409 if def_clk_type_name
is None and ts_begin_clk_type_name
is not None:
410 def_clk_type_name
= ts_begin_clk_type_name
412 if def_clk_type_name
is None and ts_end_clk_type_name
is not None:
413 def_clk_type_name
= ts_end_clk_type_name
414 except _ConfigurationParseError
as exc
:
415 _append_error_ctx(exc
, f
'`{eht_prop_name}` property')
417 if def_clk_type_name
is not None:
418 v3_stream_type_node
['$default-clock-type-name'] = def_clk_type_name
421 v3_stream_type_node
['$features'] = v3_features_node_from_v2_ft_nodes(v2_pkt_ctx_ft_fields_node
,
422 v2_ev_header_ft_fields_node
)
424 # set extra packet context field type members node
425 pkt_ctx_ft_extra_members
= []
435 for member_name
, v2_ft_node
in v2_pkt_ctx_ft_fields_node
.items():
436 if member_name
in ctf_member_names
:
439 pkt_ctx_ft_extra_members
.append(collections
.OrderedDict({
440 member_name
: collections
.OrderedDict({
441 'field-type': self
._conv
_ft
_node
(v2_ft_node
)
445 if len(pkt_ctx_ft_extra_members
) > 0:
446 v3_stream_type_node
['packet-context-field-type-extra-members'] = pkt_ctx_ft_extra_members
448 # convert event common context field type node
449 v2_ft_node
= v2_stream_type_node
.get('event-context-type')
451 if v2_ft_node
is not None:
452 v3_stream_type_node
['event-common-context-field-type'] = self
._conv
_ft
_node
(v2_ft_node
)
454 # convert event type nodes
455 v3_event_types_node
= collections
.OrderedDict()
457 for ev_type_name
, v2_ev_type_node
in v2_stream_type_node
['events'].items():
459 v3_event_types_node
[ev_type_name
] = self
._conv
_ev
_type
_node
(v2_ev_type_node
)
460 except _ConfigurationParseError
as exc
:
461 _append_error_ctx(exc
, f
'Event type `{ev_type_name}`')
463 v3_stream_type_node
['event-types'] = v3_event_types_node
465 return v3_stream_type_node
467 # Converts a v2 metadata node to a v3 trace node and returns it.
468 def _conv_meta_node(self
, v2_meta_node
: _MapNode
) -> _MapNode
:
469 def v3_features_node_from_v2_ft_node(v2_pkt_header_ft_node
: Optional
[_MapNode
]) -> _MapNode
:
470 def set_if_exists(key
, node
):
471 return self
._set
_v
3_feature
_ft
_if
_exists
(v3_features_node
, key
, node
)
473 v2_pkt_header_ft_fields_node
= collections
.OrderedDict()
475 if v2_pkt_header_ft_node
is not None:
476 v2_pkt_header_ft_fields_node
= v2_pkt_header_ft_node
['fields']
478 v3_magic_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_header_ft_fields_node
, 'magic')
479 v3_uuid_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_header_ft_fields_node
, 'uuid')
480 v3_stream_type_id_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_header_ft_fields_node
,
482 v3_features_node
: _MapNode
= collections
.OrderedDict()
483 set_if_exists('magic-field-type', v3_magic_ft_node
)
484 set_if_exists('uuid-field-type', v3_uuid_ft_node
)
485 set_if_exists('stream-type-id-field-type', v3_stream_type_id_ft_node
)
486 return v3_features_node
488 v3_trace_node
: _MapNode
= collections
.OrderedDict()
489 v3_trace_type_node
: _MapNode
= collections
.OrderedDict()
490 v2_trace_node
= v2_meta_node
['trace']
492 # Move `byte-order` property to root node's `target-byte-order`
494 typing
.cast(_MapNode
, self
._root
_node
)['target-byte-order'] = v2_trace_node
['byte-order']
496 # copy `uuid` property
497 _copy_prop_if_exists(v3_trace_type_node
, v2_trace_node
, 'uuid')
499 # copy `$log-levels`/`log-levels` property
500 new_prop_name
= '$log-level-aliases'
501 _copy_prop_if_exists(v3_trace_type_node
, v2_meta_node
, 'log-levels', new_prop_name
)
502 _copy_prop_if_exists(v3_trace_type_node
, v2_meta_node
, '$log-levels', new_prop_name
)
504 # copy `clocks` property, converting clock type nodes
505 v2_clk_types_node
= v2_meta_node
.get('clocks')
507 if v2_clk_types_node
is not None:
508 v3_clk_types_node
= collections
.OrderedDict()
510 for name
, v2_clk_type_node
in v2_clk_types_node
.items():
511 v3_clk_types_node
[name
] = self
._conv
_clk
_type
_node
(v2_clk_type_node
)
513 v3_trace_type_node
['clock-types'] = v3_clk_types_node
516 v2_pkt_header_ft_node
= v2_trace_node
.get('packet-header-type')
517 v3_trace_type_node
['$features'] = v3_features_node_from_v2_ft_node(v2_pkt_header_ft_node
)
519 # convert stream type nodes
520 v3_stream_types_node
= collections
.OrderedDict()
522 for stream_type_name
, v2_stream_type_node
in v2_meta_node
['streams'].items():
524 v3_stream_types_node
[stream_type_name
] = self
._conv
_stream
_type
_node
(v2_stream_type_node
)
525 except _ConfigurationParseError
as exc
:
526 _append_error_ctx(exc
, f
'Stream type `{stream_type_name}`')
528 v3_trace_type_node
['stream-types'] = v3_stream_types_node
530 # If `v2_meta_node` has a `$default-stream` property, find the
531 # corresponding v3 stream type node and set its `$is-default`
532 # property to `True`.
533 prop_name
= '$default-stream'
534 v2_def_stream_type_node
= v2_meta_node
.get(prop_name
)
536 if v2_def_stream_type_node
is not None:
539 for stream_type_name
, v3_stream_type_node
in v3_stream_types_node
.items():
540 if stream_type_name
== v2_def_stream_type_node
:
541 v3_stream_type_node
['$is-default'] = True
546 raise _ConfigurationParseError(f
'`{prop_name}` property',
547 f
'Stream type `{v2_def_stream_type_node}` does not exist')
549 # set environment node
550 v2_env_node
= v2_meta_node
.get('env')
552 if v2_env_node
is not None:
553 v3_trace_node
['environment'] = copy
.deepcopy(v2_env_node
)
555 # set v3 trace node's type node
556 v3_trace_node
['type'] = v3_trace_type_node
560 # Transforms the root configuration node into a valid v3
561 # configuration node.
562 def _transform_config_node(self
):
563 # remove the `version` property
564 del self
._root
_node
['version']
566 # relocate prefix and option nodes
567 prefix_prop_name
= 'prefix'
568 v2_prefix_node
= self
._root
_node
.get(prefix_prop_name
, 'barectf_')
569 _del_prop_if_exists(self
._root
_node
, prefix_prop_name
)
570 opt_prop_name
= 'options'
571 v2_options_node
= self
._root
_node
.get(opt_prop_name
)
572 _del_prop_if_exists(self
._root
_node
, opt_prop_name
)
573 code_gen_node
= collections
.OrderedDict()
574 v3_prefixes
= config_parse_common
._v
3_prefixes
_from
_v
2_prefix
(v2_prefix_node
)
575 v3_prefix_node
= collections
.OrderedDict([
576 ('identifier', v3_prefixes
.identifier
),
577 ('file-name', v3_prefixes
.file_name
),
579 code_gen_node
[prefix_prop_name
] = v3_prefix_node
581 if v2_options_node
is not None:
582 header_node
= collections
.OrderedDict()
583 _copy_prop_if_exists(header_node
, v2_options_node
, 'gen-prefix-def',
584 'identifier-prefix-definition')
585 _copy_prop_if_exists(header_node
, v2_options_node
, 'gen-default-stream-def',
586 'default-stream-type-name-definition')
587 code_gen_node
['header'] = header_node
589 self
._root
_node
[opt_prop_name
] = collections
.OrderedDict({
590 'code-generation': code_gen_node
,
593 # convert the v2 metadata node into a v3 trace node
595 self
._root
_node
['trace'] = self
._conv
_meta
_node
(self
._root
_node
['metadata'])
596 except _ConfigurationParseError
as exc
:
597 _append_error_ctx(exc
, 'Metadata object')
599 del self
._root
_node
['metadata']
601 # Expands the field type aliases found in the metadata node.
603 # This method modifies the metadata node.
605 # When this method returns:
607 # * Any field type alias is replaced with its full field type node
610 # * The `type-aliases` property of metadata node is removed.
611 def _expand_ft_aliases(self
):
612 meta_node
= self
._root
_node
['metadata']
613 ft_aliases_node
= meta_node
['type-aliases']
615 # Expand field type aliases within trace, stream, and event
618 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, meta_node
['trace'], 'packet-header-type')
619 except _ConfigurationParseError
as exc
:
620 _append_error_ctx(exc
, 'Trace type')
622 for stream_type_name
, stream_type_node
in meta_node
['streams'].items():
624 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, stream_type_node
,
625 'packet-context-type')
626 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, stream_type_node
, 'event-header-type')
627 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, stream_type_node
,
628 'event-context-type')
630 for ev_type_name
, ev_type_node
in stream_type_node
['events'].items():
632 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, ev_type_node
, 'context-type')
633 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, ev_type_node
, 'payload-type')
634 except _ConfigurationParseError
as exc
:
635 _append_error_ctx(exc
, f
'Event type `{ev_type_name}`')
636 except _ConfigurationParseError
as exc
:
637 _append_error_ctx(exc
, f
'Stream type `{stream_type_name}`')
639 # remove the (now unneeded) `type-aliases` node
640 del meta_node
['type-aliases']
642 # Applies field type inheritance to all field type nodes found in
645 # This method modifies the metadata node.
647 # When this method returns, no field type node has an `$inherit` or
648 # `inherit` property.
649 def _apply_fts_inheritance(self
):
650 meta_node
= self
._root
_node
['metadata']
651 self
._apply
_ft
_inheritance
(meta_node
['trace'], 'packet-header-type')
653 for stream_type_node
in meta_node
['streams'].values():
654 self
._apply
_ft
_inheritance
(stream_type_node
, 'packet-context-type')
655 self
._apply
_ft
_inheritance
(stream_type_node
, 'event-header-type')
656 self
._apply
_ft
_inheritance
(stream_type_node
, 'event-context-type')
658 for ev_type_node
in stream_type_node
['events'].values():
659 self
._apply
_ft
_inheritance
(ev_type_node
, 'context-type')
660 self
._apply
_ft
_inheritance
(ev_type_node
, 'payload-type')
662 # Calls _expand_ft_aliases() and _apply_fts_inheritance() if the
663 # metadata node has a `type-aliases` property.
664 def _expand_fts(self
):
665 # Make sure that the current configuration node is valid
666 # considering field types are not expanded yet.
667 self
._schema
_validator
.validate(self
._root
_node
,
668 'config/2/config-pre-field-type-expansion')
670 meta_node
= self
._root
_node
['metadata']
671 ft_aliases_node
= meta_node
.get('type-aliases')
673 if ft_aliases_node
is None:
674 # If there's no `type-aliases` node, then there's no field
675 # type aliases and therefore no possible inheritance.
678 # first, expand field type aliases
679 self
._expand
_ft
_aliases
()
681 # next, apply inheritance to create effective field types
682 self
._apply
_fts
_inheritance
()
684 # Processes the inclusions of the event type node `ev_type_node`,
685 # returning the effective node.
686 def _process_ev_type_node_include(self
, ev_type_node
: _MapNode
) -> _MapNode
:
687 # Make sure the event type node is valid for the inclusion
689 self
._schema
_validator
.validate(ev_type_node
, 'config/2/event-type-pre-include')
692 return self
._process
_node
_include
(ev_type_node
, self
._process
_ev
_type
_node
_include
)
694 # Processes the inclusions of the stream type node
695 # `stream_type_node`, returning the effective node.
696 def _process_stream_type_node_include(self
, stream_type_node
: _MapNode
) -> _MapNode
:
697 def process_children_include(stream_type_node
):
700 if prop_name
in stream_type_node
:
701 ev_types_node
= stream_type_node
[prop_name
]
703 for key
in list(ev_types_node
):
704 ev_types_node
[key
] = self
._process
_ev
_type
_node
_include
(ev_types_node
[key
])
706 # Make sure the stream type node is valid for the inclusion
708 self
._schema
_validator
.validate(stream_type_node
, 'config/2/stream-type-pre-include')
711 return self
._process
_node
_include
(stream_type_node
, self
._process
_stream
_type
_node
_include
,
712 process_children_include
)
714 # Processes the inclusions of the trace type node `trace_type_node`,
715 # returning the effective node.
716 def _process_trace_type_node_include(self
, trace_type_node
: _MapNode
) -> _MapNode
:
717 # Make sure the trace type node is valid for the inclusion
719 self
._schema
_validator
.validate(trace_type_node
, 'config/2/trace-type-pre-include')
722 return self
._process
_node
_include
(trace_type_node
, self
._process
_trace
_type
_node
_include
)
724 # Processes the inclusions of the clock type node `clk_type_node`,
725 # returning the effective node.
726 def _process_clk_type_node_include(self
, clk_type_node
: _MapNode
) -> _MapNode
:
727 # Make sure the clock type node is valid for the inclusion
729 self
._schema
_validator
.validate(clk_type_node
, 'config/2/clock-type-pre-include')
732 return self
._process
_node
_include
(clk_type_node
, self
._process
_clk
_type
_node
_include
)
734 # Processes the inclusions of the metadata node `meta_node`,
735 # returning the effective node.
736 def _process_meta_node_include(self
, meta_node
: _MapNode
) -> _MapNode
:
737 def process_children_include(meta_node
: _MapNode
):
740 if prop_name
in meta_node
:
741 meta_node
[prop_name
] = self
._process
_trace
_type
_node
_include
(meta_node
[prop_name
])
745 if prop_name
in meta_node
:
746 clk_types_node
= meta_node
[prop_name
]
748 for key
in list(clk_types_node
):
749 clk_types_node
[key
] = self
._process
_clk
_type
_node
_include
(clk_types_node
[key
])
751 prop_name
= 'streams'
753 if prop_name
in meta_node
:
754 stream_types_node
= meta_node
[prop_name
]
756 for key
in list(stream_types_node
):
757 stream_types_node
[key
] = self
._process
_stream
_type
_node
_include
(stream_types_node
[key
])
759 # Make sure the metadata node is valid for the inclusion
761 self
._schema
_validator
.validate(meta_node
, 'config/2/metadata-pre-include')
764 return self
._process
_node
_include
(meta_node
, self
._process
_meta
_node
_include
,
765 process_children_include
)
767 # Processes the inclusions of the configuration node, modifying it
768 # during the process.
769 def _process_config_includes(self
):
770 # Process inclusions in this order:
772 # 1. Clock type node, event type nodes, and trace type nodes
773 # (the order between those is not important).
775 # 2. Stream type nodes.
781 # * A metadata node can include clock type nodes, a trace type
782 # node, stream type nodes, and event type nodes (indirectly).
784 # * A stream type node can include event type nodes.
786 # First, make sure the configuration node itself is valid for
787 # the inclusion processing stage.
788 self
._schema
_validator
.validate(self
._root
_node
,
789 'config/2/config-pre-include')
791 # Process metadata node inclusions.
793 # self._process_meta_node_include() returns a new (or the same)
794 # metadata node without any `$include` property in it,
796 prop_name
= 'metadata'
797 self
._root
_node
[prop_name
] = self
._process
_meta
_node
_include
(self
._root
_node
[prop_name
])
800 # Make sure the configuration node is minimally valid, that is,
801 # it contains a valid `version` property.
803 # This step does not validate the whole configuration node yet
804 # because we don't have an effective configuration node; we
807 # * Process inclusions.
808 # * Expand field types (aliases and inheritance).
809 self
._schema
_validator
.validate(self
._root
_node
, 'config/2/config-min')
811 # process configuration node inclusions
812 self
._process
_config
_includes
()
814 # Expand field type nodes.
818 # 1. Replaces field type aliases with "effective" field type
819 # nodes, recursively.
821 # After this step, the `type-aliases` property of the
822 # metadata node is gone.
824 # 2. Applies inheritance, following the `$inherit`/`inherit`
827 # After this step, field type nodes do not contain `$inherit`
828 # or `inherit` properties.
830 # This is done blindly, in that the process _doesn't_ validate
831 # field type nodes at this point.
833 # The reason we must do this here for a barectf 2 configuration,
834 # considering that barectf 3 also supports field type node
835 # aliases and inheritance, is that we need to find specific
836 # packet header and packet context field type member nodes (for
837 # example, `stream_id`, `packet_size`, or `timestamp_end`) to
838 # set the `$features` properties of barectf 3 trace type and
839 # stream type nodes. Those field type nodes can be aliases,
840 # contain aliases, or inherit from other nodes.
843 # Validate the whole, (almost) effective configuration node.
845 # It's almost effective because the `log-level` property of
846 # event type nodes can be log level aliases. Log level aliases
847 # are also a feature of a barectf 3 configuration node,
848 # therefore this is compatible.
849 self
._schema
_validator
.validate(self
._root
_node
, 'config/2/config')
851 # Transform the current configuration node into a valid v3
852 # configuration node.
853 self
._transform
_config
_node
()
856 def config_node(self
) -> config_parse_common
._ConfigNodeV
3:
857 return config_parse_common
._ConfigNodeV
3(typing
.cast(_MapNode
, self
._root
_node
))