Changeset View
Changeset View
Standalone View
Standalone View
test/lint/lint-python-format.py
Show First 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | def build_replacement(error): | ||||
# Replace the qualifier. | # Replace the qualifier. | ||||
# E.g % 42 => .format(42) | # E.g % 42 => .format(42) | ||||
# E.g. % (42, "my_string") => .format(42, "my_string") | # E.g. % (42, "my_string") => .format(42, "my_string") | ||||
def single_qualifier_sub(match): | def single_qualifier_sub(match): | ||||
qualifier = ".format(" + match.group(1).strip() | qualifier = ".format(" + match.group(1).strip() | ||||
# Where to close the parenthesis if there is a single specifier ? | # Where to close the parenthesis if there is a single specifier ? | ||||
# It is whether at the end or before the first ',', ']', '}' (if | # It is whether at the end or before the first ',', ']', '}' (if | ||||
# enclosed in a function call, a list or a dictionary). | # enclosed in a function call, a list or a dictionary). | ||||
# | |||||
# There is a special case to be handled when the qualifier is an array. | |||||
# In this case, ensure there is one more ']' than '['. | |||||
close_before = [",", "]", "}"] | close_before = [",", "]", "}"] | ||||
opening_count = 0 | |||||
for i, c in enumerate(qualifier): | for i, c in enumerate(qualifier): | ||||
if c == "[": | |||||
opening_count += 1 | |||||
if c in close_before: | if c in close_before: | ||||
if(c == "]" and opening_count > 0): | |||||
opening_count -= 1 | |||||
continue | |||||
return qualifier[:i] + ")" + qualifier[i:] | return qualifier[:i] + ")" + qualifier[i:] | ||||
return qualifier + ")" | return qualifier + ")" | ||||
def multi_qualifier_sub(match): | def multi_qualifier_sub(match): | ||||
# The closing parenthesis is already there as we are replacing a tuple | # The closing parenthesis is already there as we are replacing a tuple | ||||
qualifier = ".format(" + match.group(1).strip() | qualifier = ".format(" + match.group(1).strip() | ||||
return qualifier | return qualifier | ||||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | def main(file): | ||||
=> ("{}".format("string1"), "{} {}".format("string2", "string3") | => ("{}".format("string1"), "{} {}".format("string2", "string3") | ||||
(52) ("%s %s" % ("string1", "string2"), "%s %s" % ("string3", "string4")) | (52) ("%s %s" % ("string1", "string2"), "%s %s" % ("string3", "string4")) | ||||
=> ("{} {}".format("string1", "string2"), "{} {}".format("string3", "string4")) | => ("{} {}".format("string1", "string2"), "{} {}".format("string3", "string4")) | ||||
(55) ["test %05i %% %s" % | (55) ["test %05i %% %s" % | ||||
(len("string1"), | (len("string1"), | ||||
"%d %-10s %%" % (len("string2"), | "%d %-10s %%" % (len("string2"), | ||||
"string2"))] | "string2"))] | ||||
=> ["test {:05d} % {}".format(len("string1"), "{} {:10s} %".format(len("string2"), "string2"))] | => ["test {:05d} % {}".format(len("string1"), "{} {:10s} %".format(len("string2"), "string2"))] | ||||
(73) "test %s" % an_array[0] | |||||
=> "test {}".format(an_array[0]) | |||||
(75) "test %s" % an_array[0][0] | |||||
=> "test {}".format(an_array[0][0]) | |||||
(77) ["test %s" % an_array[0]] | |||||
=> ["test {}".format(an_array[0])] | |||||
(79) {"test":" ["test %s" % an_array[0][0]]} | |||||
=> {"test":" ["test {}".format(an_array[0][0])]} | |||||
""" | """ | ||||
errors = find_errors(file) | errors = find_errors(file) | ||||
# Python dictionnaries do not guarantee ordering, sort by line number | # Python dictionnaries do not guarantee ordering, sort by line number | ||||
for line_number, error in sorted(errors.items(), | for line_number, error in sorted(errors.items(), | ||||
key=lambda pair: int(pair[0])): | key=lambda pair: int(pair[0])): | ||||
replacement = build_replacement(error) | replacement = build_replacement(error) | ||||
print("({}) {}".format(line_number, error.rstrip())) | print("({}) {}".format(line_number, error.rstrip())) | ||||
print("=> " + replacement) | print("=> " + replacement) | ||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
if len(sys.argv) != 2: | if len(sys.argv) != 2: | ||||
sys.exit(testmod()[1]) | sys.exit(testmod()[1]) | ||||
else: | else: | ||||
main(sys.argv[1]) | main(sys.argv[1]) |