Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/autotest/param_metadata/jsonemit.py
Views: 1799
import json1import copy2from emit import Emit345# Emit ArduPilot documentation in JSON format6class JSONEmit(Emit):7def __init__(self, *args, **kwargs):8Emit.__init__(self, *args, **kwargs)9json_fname = 'apm.pdef.json'10self.f = open(json_fname, mode='w')11self.content = {"json": {"version": 0}}1213def close(self):14json.dump(self.content, self.f, indent=2, sort_keys=True)15self.f.close()1617def jsonFromKeyList(self, main_key, dictionary):18json_object = {}19if main_key in dictionary:20values = dictionary[main_key]21for value in values.split(','):22key, description = value.split(":")23json_object[key.strip()] = description.strip()24return json_object2526def emit(self, g):27content = {}2829# Copy content to avoid any modification30g = copy.deepcopy(g)3132self.content[g.name] = {}3334# Check all params available35for param in g.params:36param_json = {}3738# Get display name39if hasattr(param, 'DisplayName'):40# i.e. ArduPlane (ArduPlane:FOOPARM)41param_json['displayName'] = param.DisplayName4243# Get description44if hasattr(param, 'Description'):45param_json['description'] = param.Description4647# Get user type48if hasattr(param, 'User'):49# i.e. Standard or Advanced50param_json['user'] = param.User5152# Get param name and and remove key53name = param.__dict__.pop('name')54if ':' in name:55name = name.split(':')[1]5657# Remove various unwanted keys58for key in list(param.__dict__.keys()):59if not self.should_emit_field(param, key):60param.__dict__.pop(key)61for key in 'real_path', 'SortValues', '__field_text':62try:63param.__dict__.pop(key)64except KeyError:65pass6667# Remove __field_text key68if '__field_text' in param.__dict__:69param.__dict__.pop('__field_text')7071# Get range section if available72range_json = {}73if 'Range' in param.__dict__:74range = param.__dict__['Range'].split(' ')75range_json['low'] = range[0]76range_json['high'] = range[1]77param.__dict__.pop('Range')7879# Get bitmask section if available80bitmask_json = self.jsonFromKeyList('Bitmask', param.__dict__)81if(bitmask_json):82param.__dict__.pop('Bitmask')8384# get value section if availables85values_json = self.jsonFromKeyList('Values', param.__dict__)86if(values_json):87param.__dict__.pop('Values')8889# Set actual content90content[name] = param.__dict__9192# Set range if available93if(range_json):94content[name]['Range'] = range_json9596# Set bitmask if available97if(bitmask_json):98content[name]['Bitmask'] = bitmask_json99100# Set values if available101if(values_json):102content[name]['Values'] = values_json103104# Update main content with actual content105for key in content:106self.content[g.name][key] = content[key]107108109