mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-23 05:53:33 +08:00
Tools: add parser to extract event definitions in source & generate json output
Example definition:
/* EVENT
* @description
* test description
* @arg1: test
*/
events::send<uint8_t>(events::ID("test_event"),
"test message", events::Log::Error, 0);
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import codecs
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
class JsonOutput():
|
||||
def __init__(self, groups):
|
||||
all_json = {}
|
||||
all_json['version'] = 1
|
||||
component = {}
|
||||
all_json['components'] = {1: component} #1: autopilot component
|
||||
|
||||
all_events = {}
|
||||
component['namespace'] = "px4"
|
||||
component['event_groups'] = all_events
|
||||
|
||||
for group in groups:
|
||||
current_group = {}
|
||||
current_events = {}
|
||||
current_group['events'] = current_events
|
||||
all_events[group] = current_group
|
||||
|
||||
for e in groups[group]:
|
||||
event_obj = {}
|
||||
event_obj['name'] = e.name
|
||||
event_obj['message'] = e.message
|
||||
if e.description is not None:
|
||||
event_obj['description'] = e.description
|
||||
args = []
|
||||
for i in range(len(e.arguments)):
|
||||
arg = {}
|
||||
arg['type'] = e.arguments[i][0]
|
||||
arg['name'] = e.arguments[i][1]
|
||||
args.append(arg)
|
||||
if len(args) > 0:
|
||||
event_obj['arguments'] = args
|
||||
sub_id = e.sub_id
|
||||
assert sub_id not in current_events, \
|
||||
"Duplicate event ID for {0} (message: '{1}'), other event message: '{2}'".format(
|
||||
e.name, e.message, current_events[sub_id]['message'])
|
||||
current_events[sub_id] = event_obj
|
||||
|
||||
self.json = all_json
|
||||
|
||||
def save(self, filename):
|
||||
need_to_write = True
|
||||
# only write if current file is not the same, to avoid updating the file
|
||||
# timestamp
|
||||
if os.path.isfile(filename):
|
||||
with open(filename, 'rb') as json_file:
|
||||
existing_data = json.load(json_file)
|
||||
if existing_data == self.json:
|
||||
need_to_write = False
|
||||
if need_to_write:
|
||||
with codecs.open(filename, 'w', 'utf-8') as f:
|
||||
f.write(json.dumps(self.json,indent=2))
|
||||
|
||||
Reference in New Issue
Block a user