From 203416f136ec7f6b3ab236d9eef95c4cc00638c8 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sat, 6 Feb 2016 16:11:58 +0100 Subject: [PATCH] PX4 params: Support enum values --- Tools/px4params/srcparser.py | 41 +++++++++++++++++++++++++++++++++--- Tools/px4params/xmlout.py | 7 ++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Tools/px4params/srcparser.py b/Tools/px4params/srcparser.py index 6abdf7e862..f537e6c28d 100644 --- a/Tools/px4params/srcparser.py +++ b/Tools/px4params/srcparser.py @@ -51,6 +51,7 @@ class Parameter(object): def __init__(self, name, type, default = ""): self.fields = {} + self.values = {} self.name = name self.type = type self.default = default @@ -70,6 +71,12 @@ class Parameter(object): """ self.fields[code] = value + def SetEnumValue(self, code, value): + """ + Set named enum value + """ + self.values[code] = value + def GetFieldCodes(self): """ Return list of existing field codes in convenient order @@ -87,7 +94,26 @@ class Parameter(object): if not fv: # required because python 3 sorted does not accept None return "" - return self.fields.get(code) + return fv + + def GetEnumCodes(self): + """ + Return list of existing value codes in convenient order + """ + keys = self.values.keys() + #keys = sorted(keys) + #keys = sorted(keys, key=lambda x: self.priority.get(x, 0), reverse=True) + return keys + + def GetEnumValue(self, code): + """ + Return value of the given enum code or None if not found. + """ + fv = self.values.get(code) + if not fv: + # required because python 3 sorted does not accept None + return "" + return fv class SourceParser(object): """ @@ -107,7 +133,7 @@ class SourceParser(object): re_remove_dots = re.compile(r'\.+$') re_remove_carriage_return = re.compile('\n+') - valid_tags = set(["group", "board", "min", "max", "unit", "decimal", "reboot_required"]) + valid_tags = set(["group", "board", "min", "max", "unit", "decimal", "reboot_required", "value"]) # Order of parameter groups priority = { @@ -137,6 +163,7 @@ class SourceParser(object): short_desc = None long_desc = None tags = {} + def_values = {} elif state is not None and state != "comment-processed": m = self.re_comment_end.search(line) if m: @@ -156,7 +183,12 @@ class SourceParser(object): m = self.re_comment_tag.match(comment_content) if m: tag, desc = m.group(1, 2) - tags[tag] = desc + if (tag == "value"): + # Take the meta info string and split the code and description + metainfo = desc.split(" ", 1) + def_values[metainfo[0]] = metainfo[1] + else: + tags[tag] = desc current_tag = tag state = "wait-tag-end" elif state == "wait-short": @@ -191,6 +223,7 @@ class SourceParser(object): defval = "" # Non-empty line outside the comment m = self.re_px4_param_default_definition.match(line) + # Default value handling if m: name_m, defval_m = m.group(1,2) default_var[name_m] = defval_m @@ -226,6 +259,8 @@ class SourceParser(object): return False else: param.SetField(tag, tags[tag]) + for def_value in def_values: + param.SetEnumValue(def_value, def_values[def_value]) # Store the parameter if group not in self.param_groups: self.param_groups[group] = ParameterGroup(group) diff --git a/Tools/px4params/xmlout.py b/Tools/px4params/xmlout.py index b37cdb0627..10d75fec20 100644 --- a/Tools/px4params/xmlout.py +++ b/Tools/px4params/xmlout.py @@ -52,6 +52,13 @@ class XMLOutput(): xml_field.text = value if last_param_name != param.GetName(): board_specific_param_set = False + + if len(param.GetEnumCodes()) > 0: + xml_values = ET.SubElement(xml_param, "values") + for code in param.GetEnumCodes(): + xml_value = ET.SubElement(xml_values, "value") + xml_value.attrib["code"] = code; + xml_value.text = param.GetEnumValue(code) indent(xml_parameters) self.xml_document = ET.ElementTree(xml_parameters)