Path: blob/master/Tools/autotest/param_metadata/jsonemit.py
9676 views
# flake8: noqa12import json3import copy4from emit import Emit567# Emit ArduPilot documentation in JSON format8class JSONEmit(Emit):9def __init__(self, *args, **kwargs):10Emit.__init__(self, *args, **kwargs)11json_fname = 'apm.pdef.json'12self.f = open(json_fname, mode='w')13self.content = {"json": {"version": 0}}1415def close(self):16json.dump(self.content, self.f, indent=2, sort_keys=True)17self.f.close()1819def jsonFromKeyList(self, main_key, dictionary):20json_object = {}21if main_key in dictionary:22values = dictionary[main_key]23for value in values.split(','):24key, description = value.split(":")25json_object[key.strip()] = description.strip()26return json_object2728def emit(self, g):29content = {}3031# Copy content to avoid any modification32g = copy.deepcopy(g)3334self.content[g.name] = {}3536# Check all params available37for param in g.params:38if not self.should_emit_param(param):39continue40param_json = {}4142# Get display name43if hasattr(param, 'DisplayName'):44# i.e. ArduPlane (ArduPlane:FOOPARM)45param_json['displayName'] = param.DisplayName4647# Get description48if hasattr(param, 'Description'):49param_json['description'] = param.Description5051# Get user type52if hasattr(param, 'User'):53# i.e. Standard or Advanced54param_json['user'] = param.User5556# Get param name and and remove key57name = param.__dict__.pop('name')58if ':' in name:59name = name.split(':')[1]6061# Remove various unwanted keys62for key in list(param.__dict__.keys()):63if not self.should_emit_field(param, key):64param.__dict__.pop(key)65for key in 'real_path', 'SortValues', '__field_text':66try:67param.__dict__.pop(key)68except KeyError:69pass7071# Remove __field_text key72if '__field_text' in param.__dict__:73param.__dict__.pop('__field_text')7475# Get range section if available76range_json = {}77if 'Range' in param.__dict__:78range = param.__dict__['Range'].split(' ')79range_json['low'] = range[0]80range_json['high'] = range[1]81param.__dict__.pop('Range')8283# Get bitmask section if available84bitmask_json = self.jsonFromKeyList('Bitmask', param.__dict__)85if(bitmask_json):86param.__dict__.pop('Bitmask')8788# get value section if availables89values_json = self.jsonFromKeyList('Values', param.__dict__)90if(values_json):91param.__dict__.pop('Values')9293# Set actual content94content[name] = param.__dict__9596# Set range if available97if(range_json):98content[name]['Range'] = range_json99100# Set bitmask if available101if(bitmask_json):102content[name]['Bitmask'] = bitmask_json103104# Set values if available105if(values_json):106content[name]['Values'] = values_json107108# Update main content with actual content109for key in content:110self.content[g.name][key] = content[key]111112113