Path: blob/master/Tools/autotest/param_metadata/ednemit.py
9717 views
"""1Emits parameters as an EDN file, does some small remapping of names2"""34from emit import Emit5import edn_format6import datetime7import pytz8import subprocess91011class EDNEmit(Emit):12def __init__(self, *args, **kwargs):13Emit.__init__(self, *args, **kwargs)14self.output = "{:date " + edn_format.dumps(datetime.datetime.now(pytz.utc)) + " "15git = subprocess.Popen(["git log --pretty=format:'%h' -n 1"], shell=True, stdout=subprocess.PIPE).communicate()[0]16self.output += ":git-hash \"" + git.decode("ascii") + "\" "17self.remove_keys = ["real_path"]18self.explict_remap = [["displayname", "display-name"]]19self.vehicle_name = None2021def close(self):22if self.vehicle_name is not None:23self.output += ":vehicle \"" + self.vehicle_name + "\" "24else:25raise Exception('Vehicle name never found')26self.output += "}"27f = open("parameters.edn", mode='w')28f.write(self.output)29f.close()3031def start_libraries(self):32pass3334def emit(self, g):35for param in g.params:36if not self.should_emit_param(param):37continue38output_dict = dict()39# lowercase all keywords40for key in param.__dict__.keys():41output_dict[key.lower()] = param.__dict__[key]4243# strip off any leading sillyness on the param name44split_name = param.__dict__["name"].split(":")45if len(split_name) == 2:46self.vehicle_name = split_name[0]47name = param.__dict__["name"].split(":")[-1]48output_dict["name"] = name4950# remove any keys we don't really care to share51for key in self.remove_keys:52output_dict.pop(key, None)53for key in list(output_dict.keys()):54if not self.should_emit_field(param, key):55output_dict.pop(key, None)5657# rearrange bitmasks to be a vector with nil's if the bit doesn't have meaning58if "bitmask" in output_dict:59highest_set_bit = 060bits = []61for bit in output_dict["bitmask"].split(","):62bit_parts = bit.split(":")63bit_number = int(bit_parts[0])64bit_parts[0] = bit_number65bits.append(bit_parts)66if bit_number > highest_set_bit:67highest_set_bit = bit_number68output_bits = (highest_set_bit+1)*[None]69for bit in bits:70output_bits[bit[0]] = bit[1]71output_dict["bitmask"] = output_bits7273# rearrange values into a float indexed map74if "values" in output_dict:75values = dict()76for value in output_dict["values"].split(","):77index, description = value.split(":")78values[float(index)] = description79output_dict["values"] = values8081# remap range to be a map of floats82if "range" in output_dict:83low, high = output_dict["range"].split()84output_dict["range"] = {"low": float(low), "high": float(high)}8586# remap the string to a float87if "increment" in output_dict:88output_dict["increment"] = float(output_dict["increment"])8990# do any name changing desired91for remap in self.explict_remap:92output_dict[remap[1]] = output_dict.pop(remap[0])9394self.output += "\"" + name + "\" " + edn_format.dumps(output_dict, keyword_keys=True)959697