Loop over lines in wmllint using enumerate()

This commit is contained in:
Kevin Yap 2015-07-28 00:56:05 -07:00
parent 2e30e0e7fe
commit a3fd3c71e1

View File

@ -1234,8 +1234,7 @@ def global_sanity_check(filename, lines):
sidecount = 0 sidecount = 0
recruitment_pattern = {} recruitment_pattern = {}
ifdef_stack = [None] ifdef_stack = [None]
for i in xrange(len(lines)): for num, line in enumerate((l.strip() for l in lines), start=1):
line = lines[i].strip()
if line.startswith("#ifdef") or line.startswith("#ifhave") or line.startswith("#ifver"): if line.startswith("#ifdef") or line.startswith("#ifhave") or line.startswith("#ifver"):
ifdef_stack.append(line.split()[1]) ifdef_stack.append(line.split()[1])
continue continue
@ -1251,54 +1250,54 @@ def global_sanity_check(filename, lines):
if line.startswith("#endif"): if line.startswith("#endif"):
ifdef_stack.pop() ifdef_stack.pop()
continue continue
if "[generator]" in lines[i]: if "[generator]" in line:
in_generator = True in_generator = True
continue continue
elif "[/generator]" in lines[i]: elif "[/generator]" in line:
in_generator = False in_generator = False
continue continue
elif "[side]" in lines[i]: elif "[side]" in line:
in_side = True in_side = True
sidecount += 1 sidecount += 1
continue continue
elif "[/side]" in lines[i]: elif "[/side]" in line:
if recruit or recruitment_pattern: if recruit or recruitment_pattern:
sides.append((filename, recruit, recruitment_pattern)) sides.append((filename, recruit, recruitment_pattern))
in_side = False in_side = False
recruit = {} recruit = {}
recruitment_pattern = {} recruitment_pattern = {}
continue continue
elif in_side and "[ai]" in lines[i]: elif in_side and "[ai]" in line:
in_ai = True in_ai = True
continue continue
elif in_side and "[unit]" in lines[i]: elif in_side and "[unit]" in line:
in_subunit = True in_subunit = True
continue continue
elif in_side and "[/ai]" in lines[i]: elif in_side and "[/ai]" in line:
in_ai = False in_ai = False
continue continue
elif in_side and "[/unit]" in lines[i]: elif in_side and "[/unit]" in line:
in_subunit = False in_subunit = False
continue continue
if "wmllint: skip-side" in lines[i]: if "wmllint: skip-side" in line:
sidecount += 1 sidecount += 1
if not in_side or in_subunit or '=' not in lines[i]: if not in_side or in_subunit or '=' not in line:
continue continue
try: try:
(key, prefix, value, comment) = parse_attribute(lines[i]) (key, prefix, value, comment) = parse_attribute(line)
if key in ("recruit", "extra_recruit") and value: if key in ("recruit", "extra_recruit") and value:
recruit[ifdef_stack[-1]] = (i+1, [x.strip() for x in value.split(",")]) recruit[ifdef_stack[-1]] = (num, [x.strip() for x in value.split(",")])
elif key == "recruitment_pattern" and value: elif key == "recruitment_pattern" and value:
if not in_ai: if not in_ai:
print '"%s", line %d: recruitment_pattern outside [ai]' \ print '"%s", line %d: recruitment_pattern outside [ai]' \
% (filename, i+1) % (filename, num)
else: else:
recruitment_pattern[ifdef_stack[-1]] = (i+1, [x.strip() for x in value.split(",")]) recruitment_pattern[ifdef_stack[-1]] = (num, [x.strip() for x in value.split(",")])
elif key == "side" and not in_ai: elif key == "side" and not in_ai:
try: try:
if not in_generator and sidecount != int(value): if not in_generator and sidecount != int(value):
print '"%s", line %d: side number %s is out of sequence (%d expected)' \ print '"%s", line %d: side number %s is out of sequence (%d expected)' \
% (filename, i+1, value, sidecount) % (filename, num, value, sidecount)
except ValueError: except ValueError:
pass # Ignore ill-formed integer literals pass # Ignore ill-formed integer literals
except TypeError: except TypeError:
@ -1314,64 +1313,64 @@ def global_sanity_check(filename, lines):
in_unit = False in_unit = False
in_side = False in_side = False
in_unit_type = False in_unit_type = False
for i in xrange(len(lines)): for num, line in enumerate(lines, start=1):
if "[effect]" in lines[i]: if "[effect]" in line:
in_effect = True in_effect = True
elif "[/effect]" in lines[i]: elif "[/effect]" in line:
in_effect = False in_effect = False
elif "[unit]" in lines[i]: elif "[unit]" in line:
in_unit = True in_unit = True
elif "[/unit]" in lines[i]: elif "[/unit]" in line:
in_unit = False in_unit = False
elif "[side]" in lines[i]: elif "[side]" in line:
in_side = True in_side = True
elif "[/side]" in lines[i]: elif "[/side]" in line:
in_side = False in_side = False
elif "[unit_type]" in lines[i]: elif "[unit_type]" in line:
in_unit_type = True in_unit_type = True
elif "[/unit_type]" in lines[i]: elif "[/unit_type]" in line:
in_unit_type = False in_unit_type = False
# ellipsecheck magic comment allows to deactivate the ellipse sanity check # ellipsecheck magic comment allows to deactivate the ellipse sanity check
if "wmllint: no ellipsecheck" not in lines[i]: if "wmllint: no ellipsecheck" not in line:
if in_effect: if in_effect:
try: try:
(key, prefix, value, comment) = parse_attribute(lines[i]) (key, prefix, value, comment) = parse_attribute(line)
if key == "ellipse" and value in ("misc/ellipse-nozoc", "misc/ellipse-leader"): if key == "ellipse" and value in ("misc/ellipse-nozoc", "misc/ellipse-leader"):
print '"%s", line %d: [effect] apply_to=ellipse needs to be removed' % (filename, i+1) print '"%s", line %d: [effect] apply_to=ellipse needs to be removed' % (filename, num)
elif key == "ellipse" and value not in ("none", "misc/ellipse", "misc/ellipse-hero"): elif key == "ellipse" and value not in ("none", "misc/ellipse", "misc/ellipse-hero"):
print '"%s", line %d: custom ellipse %s may need to be updated' % (filename, i+1, value) print '"%s", line %d: custom ellipse %s may need to be updated' % (filename, num, value)
except TypeError: # this is needed to handle tags, that parse_attribute cannot split except TypeError: # this is needed to handle tags, that parse_attribute cannot split
pass pass
elif in_unit or in_side or in_unit_type: elif in_unit or in_side or in_unit_type:
try: try:
(key, prefix, value, comment) = parse_attribute(lines[i]) (key, prefix, value, comment) = parse_attribute(line)
if key == "ellipse" and value in ("misc/ellipse-nozoc","misc/ellipse-leader"): if key == "ellipse" and value in ("misc/ellipse-nozoc","misc/ellipse-leader"):
print '"%s", line %d: %s=%s needs to be removed' % (filename, i+1, key, value) print '"%s", line %d: %s=%s needs to be removed' % (filename, num, key, value)
elif key == "ellipse" and value not in ("none","misc/ellipse","misc/ellipse-hero"): elif key == "ellipse" and value not in ("none","misc/ellipse","misc/ellipse-hero"):
print '"%s", line %d: custom ellipse %s may need to be updated' % (filename, i+1, value) print '"%s", line %d: custom ellipse %s may need to be updated' % (filename, num, value)
except TypeError: # this is needed to handle tags, that parse_attribute cannot split except TypeError: # this is needed to handle tags, that parse_attribute cannot split
pass pass
# Interpret various magic comments # Interpret various magic comments
for i in xrange(len(lines)): for line in lines:
# Interpret magic comments for setting the usage pattern of units. # Interpret magic comments for setting the usage pattern of units.
# This coped with some wacky UtBS units that were defined with # This coped with some wacky UtBS units that were defined with
# variant-spawning macros. The prototype comment looks like this: # variant-spawning macros. The prototype comment looks like this:
#wmllint: usage of "Desert Fighter" is fighter #wmllint: usage of "Desert Fighter" is fighter
m = re.search('# *wmllint: usage of "([^"]*)" is +(.*)', lines[i]) m = re.search('# *wmllint: usage of "([^"]*)" is +(.*)', line)
if m: if m:
usage[m.group(1)] = m.group(2).strip() usage[m.group(1)] = m.group(2).strip()
unit_types.append(m.group(1)) unit_types.append(m.group(1))
# Magic comment for adding non-standard usage types # Magic comment for adding non-standard usage types
m = re.search('# *wmllint: usagetypes? +(.*)', lines[i]) m = re.search('# *wmllint: usagetypes? +(.*)', line)
if m: if m:
for newusage in m.group(1).split(","): for newusage in m.group(1).split(","):
usage_types.append(newusage.strip()) usage_types.append(newusage.strip())
# Accumulate global spelling exceptions # Accumulate global spelling exceptions
words = re.search("wmllint: general spellings? (.*)", lines[i]) words = re.search("wmllint: general spellings? (.*)", line)
if words: if words:
for word in words.group(1).split(): for word in words.group(1).split():
declared_spellings["GLOBAL"].append(word.lower()) declared_spellings["GLOBAL"].append(word.lower())
words = re.search("wmllint: directory spellings? (.*)", lines[i]) words = re.search("wmllint: directory spellings? (.*)", line)
if words: if words:
fdir = os.path.dirname(filename) fdir = os.path.dirname(filename)
if fdir not in declared_spellings: if fdir not in declared_spellings:
@ -1731,9 +1730,9 @@ def global_sanity_check(filename, lines):
# Check for textdomain strings; should be exactly one, on line 1 # Check for textdomain strings; should be exactly one, on line 1
textdomains = [] textdomains = []
no_text = False no_text = False
for i in xrange(len(lines)): for num, line in enumerate(lines, start=1):
if "#textdomain" in lines[i]: if "#textdomain" in line:
textdomains.append(i+1) textdomains.append(num)
elif "wmllint: no translatables": elif "wmllint: no translatables":
no_text = True no_text = True
if not no_text: if not no_text:
@ -1987,7 +1986,7 @@ def hack_syntax(filename, lines):
elif lines[i].count("'") % 2 == 1: elif lines[i].count("'") % 2 == 1:
try: try:
(key, prefix, value, comment) = parse_attribute(lines[i]) (key, prefix, value, comment) = parse_attribute(lines[i])
if "'" in value and value[0].isalpha() and value[-1].isalpha() and '"'+value+'"' not in line: if "'" in value and value[0].isalpha() and value[-1].isalpha() and '"'+value+'"' not in lines[i]:
newtext = prefix + '"' + value + '"' + comment + "\n" newtext = prefix + '"' + value + '"' + comment + "\n"
if lines[i] != newtext: if lines[i] != newtext:
lines[i] = newtext lines[i] = newtext
@ -2105,8 +2104,8 @@ def hack_syntax(filename, lines):
outside_of_theme_wml = True outside_of_theme_wml = True
if outside_of_theme_wml: if outside_of_theme_wml:
if not in_side_one_tag: if not in_side_one_tag:
for j in range(len(side_one_tags_allowing_filter_side)): for tag in side_one_tags_allowing_filter_side:
if "[" + side_one_tags_allowing_filter_side[j] + "]" in precomment: if "[" + tag + "]" in precomment:
in_side_one_tag = True in_side_one_tag = True
else: else:
if side_one_tag_needs_side_one: if side_one_tag_needs_side_one:
@ -2114,11 +2113,11 @@ def hack_syntax(filename, lines):
side_one_tag_needs_side_one = False side_one_tag_needs_side_one = False
if "[filter_side]" in precomment: if "[filter_side]" in precomment:
side_one_tag_needs_side_one = False side_one_tag_needs_side_one = False
for j in range(len(side_one_tags_allowing_filter_side)): for tag in side_one_tags_allowing_filter_side:
if "[/" + side_one_tags_allowing_filter_side[j] + "]" in precomment: if "[/" + tag + "]" in precomment:
if side_one_tag_needs_side_one: if side_one_tag_needs_side_one:
if verbose: if verbose:
print '"%s", line %d: [%s] without "side" attribute is now applied to all sides'%(filename, i+1, side_one_tags_allowing_filter_side[j]) print '"%s", line %d: [%s] without "side" attribute is now applied to all sides'%(filename, i+1, tag)
#lines.insert(i, leader(precomment) + baseindent + "side=1\n") #lines.insert(i, leader(precomment) + baseindent + "side=1\n")
in_side_one_tag = False in_side_one_tag = False
side_one_tag_needs_side_one = True side_one_tag_needs_side_one = True