mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-01 19:07:45 +08:00
Validate meta data
This commit is contained in:
@@ -101,6 +101,7 @@ class SourceParser(object):
|
|||||||
re_cut_type_specifier = re.compile(r'[a-z]+$')
|
re_cut_type_specifier = re.compile(r'[a-z]+$')
|
||||||
re_is_a_number = re.compile(r'^-?[0-9\.]')
|
re_is_a_number = re.compile(r'^-?[0-9\.]')
|
||||||
re_remove_dots = re.compile(r'\.+$')
|
re_remove_dots = re.compile(r'\.+$')
|
||||||
|
re_remove_carriage_return = re.compile('\n+')
|
||||||
|
|
||||||
valid_tags = set(["group", "board", "min", "max", "unit"])
|
valid_tags = set(["group", "board", "min", "max", "unit"])
|
||||||
|
|
||||||
@@ -188,12 +189,20 @@ class SourceParser(object):
|
|||||||
if last_comment_line:
|
if last_comment_line:
|
||||||
state = "comment-processed"
|
state = "comment-processed"
|
||||||
else:
|
else:
|
||||||
|
tp = None
|
||||||
|
name = None
|
||||||
|
defval = ""
|
||||||
# Non-empty line outside the comment
|
# Non-empty line outside the comment
|
||||||
m = self.re_parameter_definition.match(line)
|
m = self.re_parameter_definition.match(line)
|
||||||
if m:
|
if m:
|
||||||
tp, name, defval = m.group(1, 2, 3)
|
tp, name, defval = m.group(1, 2, 3)
|
||||||
|
else:
|
||||||
|
m = self.re_px4_parameter_definition.match(line)
|
||||||
|
if m:
|
||||||
|
tp, name = m.group(1, 2)
|
||||||
|
if tp is not None:
|
||||||
# Remove trailing type specifier from numbers: 0.1f => 0.1
|
# Remove trailing type specifier from numbers: 0.1f => 0.1
|
||||||
if self.re_is_a_number.match(defval):
|
if defval != "" and self.re_is_a_number.match(defval):
|
||||||
defval = self.re_cut_type_specifier.sub('', defval)
|
defval = self.re_cut_type_specifier.sub('', defval)
|
||||||
param = Parameter(name, tp, defval)
|
param = Parameter(name, tp, defval)
|
||||||
param.SetField("short_desc", name)
|
param.SetField("short_desc", name)
|
||||||
@@ -202,52 +211,61 @@ class SourceParser(object):
|
|||||||
group = "Miscellaneous"
|
group = "Miscellaneous"
|
||||||
if state == "comment-processed":
|
if state == "comment-processed":
|
||||||
if short_desc is not None:
|
if short_desc is not None:
|
||||||
param.SetField("short_desc",
|
param.SetField("short_desc", self.re_remove_dots.sub('', short_desc))
|
||||||
self.re_remove_dots.sub('', short_desc))
|
|
||||||
if long_desc is not None:
|
if long_desc is not None:
|
||||||
|
long_desc = self.re_remove_carriage_return.sub(' ', long_desc)
|
||||||
param.SetField("long_desc", long_desc)
|
param.SetField("long_desc", long_desc)
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
if tag == "group":
|
if tag == "group":
|
||||||
group = tags[tag]
|
group = tags[tag]
|
||||||
elif tag not in self.valid_tags:
|
elif tag not in self.valid_tags:
|
||||||
sys.stderr.write("Skipping invalid "
|
sys.stderr.write("Skipping invalid documentation tag: '%s'\n" % tag)
|
||||||
"documentation tag: '%s'\n" % tag)
|
return False
|
||||||
else:
|
else:
|
||||||
param.SetField(tag, tags[tag])
|
param.SetField(tag, tags[tag])
|
||||||
# Store the parameter
|
# Store the parameter
|
||||||
if group not in self.param_groups:
|
if group not in self.param_groups:
|
||||||
self.param_groups[group] = ParameterGroup(group)
|
self.param_groups[group] = ParameterGroup(group)
|
||||||
self.param_groups[group].AddParameter(param)
|
self.param_groups[group].AddParameter(param)
|
||||||
else:
|
state = None
|
||||||
# Nasty code dup, but this will all go away soon, so quick and dirty (DonLakeFlyer)
|
return True
|
||||||
m = self.re_px4_parameter_definition.match(line)
|
|
||||||
if m:
|
def IsNumber(self, numberString):
|
||||||
tp, name = m.group(1, 2)
|
try:
|
||||||
param = Parameter(name, tp)
|
float(numberString)
|
||||||
param.SetField("short_desc", name)
|
return True
|
||||||
# If comment was found before the parameter declaration,
|
except ValueError:
|
||||||
# inject its data into the newly created parameter.
|
return False
|
||||||
group = "Miscellaneous"
|
|
||||||
if state == "comment-processed":
|
def Validate(self):
|
||||||
if short_desc is not None:
|
"""
|
||||||
param.SetField("short_desc",
|
Validates the parameter meta data.
|
||||||
self.re_remove_dots.sub('', short_desc))
|
"""
|
||||||
if long_desc is not None:
|
for group in self.GetParamGroups():
|
||||||
param.SetField("long_desc", long_desc)
|
for param in group.GetParams():
|
||||||
for tag in tags:
|
name = param.GetName()
|
||||||
if tag == "group":
|
default = param.GetDefault()
|
||||||
group = tags[tag]
|
min = param.GetFieldValue("min")
|
||||||
elif tag not in self.valid_tags:
|
max = param.GetFieldValue("max")
|
||||||
sys.stderr.write("Skipping invalid "
|
sys.stderr.write("{0} default:{1} min:{2} max:{3}\n".format(name, default, min, max))
|
||||||
"documentation tag: '%s'\n" % tag)
|
if default != "" and not self.IsNumber(default):
|
||||||
else:
|
sys.stderr.write("Default value not number: {0} {1}\n".format(name, default))
|
||||||
param.SetField(tag, tags[tag])
|
return False
|
||||||
# Store the parameter
|
if min != "":
|
||||||
if group not in self.param_groups:
|
if not self.IsNumber(min):
|
||||||
self.param_groups[group] = ParameterGroup(group)
|
sys.stderr.write("Min value not number: {0} {1}\n".format(name, min))
|
||||||
self.param_groups[group].AddParameter(param)
|
return False
|
||||||
# Reset parsed comment.
|
if default != "" and float(default) < float(min):
|
||||||
state = None
|
sys.stderr.write("Default value is smaller than min: {0} default:{1} min:{2}\n".format(name, default, min))
|
||||||
|
return False
|
||||||
|
if max != "":
|
||||||
|
if not self.IsNumber(max):
|
||||||
|
sys.stderr.write("Max value not number: {0} {1}\n".format(name, max))
|
||||||
|
return False
|
||||||
|
if default != "" and float(default) > float(max):
|
||||||
|
sys.stderr.write("Default value is larger than max: {0} default:{1} max:{2}\n".format(name, default, max))
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def GetParamGroups(self):
|
def GetParamGroups(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -17,8 +17,10 @@ class SourceScanner(object):
|
|||||||
for dirname, dirnames, filenames in os.walk(srcdir):
|
for dirname, dirnames, filenames in os.walk(srcdir):
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
if filename.endswith(extensions):
|
if filename.endswith(extensions):
|
||||||
path = os.path.join(dirname, filename)
|
path = os.path.join(dirname, filename)
|
||||||
self.ScanFile(path, parser)
|
if not self.ScanFile(path, parser):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def ScanFile(self, path, parser):
|
def ScanFile(self, path, parser):
|
||||||
"""
|
"""
|
||||||
@@ -32,4 +34,4 @@ class SourceScanner(object):
|
|||||||
contents = ''
|
contents = ''
|
||||||
print('Failed reading file: %s, skipping content.' % path)
|
print('Failed reading file: %s, skipping content.' % path)
|
||||||
pass
|
pass
|
||||||
parser.Parse(contents)
|
return parser.Parse(contents)
|
||||||
|
|||||||
@@ -115,7 +115,10 @@ def main():
|
|||||||
|
|
||||||
# Scan directories, and parse the files
|
# Scan directories, and parse the files
|
||||||
print("Scanning source path " + args.src_path)
|
print("Scanning source path " + args.src_path)
|
||||||
scanner.ScanDir(args.src_path, parser)
|
if not scanner.ScanDir(args.src_path, parser):
|
||||||
|
sys.exit(1)
|
||||||
|
if not parser.Validate():
|
||||||
|
sys.exit(1)
|
||||||
param_groups = parser.GetParamGroups()
|
param_groups = parser.GetParamGroups()
|
||||||
|
|
||||||
# Output to XML file
|
# Output to XML file
|
||||||
|
|||||||
Reference in New Issue
Block a user