CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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