Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/autotest/param_metadata/jsonemit.py
9676 views
1
# flake8: noqa
2
3
import json
4
import copy
5
from emit import Emit
6
7
8
# Emit ArduPilot documentation in JSON format
9
class JSONEmit(Emit):
10
def __init__(self, *args, **kwargs):
11
Emit.__init__(self, *args, **kwargs)
12
json_fname = 'apm.pdef.json'
13
self.f = open(json_fname, mode='w')
14
self.content = {"json": {"version": 0}}
15
16
def close(self):
17
json.dump(self.content, self.f, indent=2, sort_keys=True)
18
self.f.close()
19
20
def jsonFromKeyList(self, main_key, dictionary):
21
json_object = {}
22
if main_key in dictionary:
23
values = dictionary[main_key]
24
for value in values.split(','):
25
key, description = value.split(":")
26
json_object[key.strip()] = description.strip()
27
return json_object
28
29
def emit(self, g):
30
content = {}
31
32
# Copy content to avoid any modification
33
g = copy.deepcopy(g)
34
35
self.content[g.name] = {}
36
37
# Check all params available
38
for param in g.params:
39
if not self.should_emit_param(param):
40
continue
41
param_json = {}
42
43
# Get display name
44
if hasattr(param, 'DisplayName'):
45
# i.e. ArduPlane (ArduPlane:FOOPARM)
46
param_json['displayName'] = param.DisplayName
47
48
# Get description
49
if hasattr(param, 'Description'):
50
param_json['description'] = param.Description
51
52
# Get user type
53
if hasattr(param, 'User'):
54
# i.e. Standard or Advanced
55
param_json['user'] = param.User
56
57
# Get param name and and remove key
58
name = param.__dict__.pop('name')
59
if ':' in name:
60
name = name.split(':')[1]
61
62
# Remove various unwanted keys
63
for key in list(param.__dict__.keys()):
64
if not self.should_emit_field(param, key):
65
param.__dict__.pop(key)
66
for key in 'real_path', 'SortValues', '__field_text':
67
try:
68
param.__dict__.pop(key)
69
except KeyError:
70
pass
71
72
# Remove __field_text key
73
if '__field_text' in param.__dict__:
74
param.__dict__.pop('__field_text')
75
76
# Get range section if available
77
range_json = {}
78
if 'Range' in param.__dict__:
79
range = param.__dict__['Range'].split(' ')
80
range_json['low'] = range[0]
81
range_json['high'] = range[1]
82
param.__dict__.pop('Range')
83
84
# Get bitmask section if available
85
bitmask_json = self.jsonFromKeyList('Bitmask', param.__dict__)
86
if(bitmask_json):
87
param.__dict__.pop('Bitmask')
88
89
# get value section if availables
90
values_json = self.jsonFromKeyList('Values', param.__dict__)
91
if(values_json):
92
param.__dict__.pop('Values')
93
94
# Set actual content
95
content[name] = param.__dict__
96
97
# Set range if available
98
if(range_json):
99
content[name]['Range'] = range_json
100
101
# Set bitmask if available
102
if(bitmask_json):
103
content[name]['Bitmask'] = bitmask_json
104
105
# Set values if available
106
if(values_json):
107
content[name]['Values'] = values_json
108
109
# Update main content with actual content
110
for key in content:
111
self.content[g.name][key] = content[key]
112
113