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
recruitment_pattern = {}
ifdef_stack = [None]
for i in xrange(len(lines)):
line = lines[i].strip()
for num, line in enumerate((l.strip() for l in lines), start=1):
if line.startswith("#ifdef") or line.startswith("#ifhave") or line.startswith("#ifver"):
ifdef_stack.append(line.split()[1])
continue
@ -1251,54 +1250,54 @@ def global_sanity_check(filename, lines):
if line.startswith("#endif"):
ifdef_stack.pop()
continue
if "[generator]" in lines[i]:
if "[generator]" in line:
in_generator = True
continue
elif "[/generator]" in lines[i]:
elif "[/generator]" in line:
in_generator = False
continue
elif "[side]" in lines[i]:
elif "[side]" in line:
in_side = True
sidecount += 1
continue
elif "[/side]" in lines[i]:
elif "[/side]" in line:
if recruit or recruitment_pattern:
sides.append((filename, recruit, recruitment_pattern))
in_side = False
recruit = {}
recruitment_pattern = {}
continue
elif in_side and "[ai]" in lines[i]:
elif in_side and "[ai]" in line:
in_ai = True
continue
elif in_side and "[unit]" in lines[i]:
elif in_side and "[unit]" in line:
in_subunit = True
continue
elif in_side and "[/ai]" in lines[i]:
elif in_side and "[/ai]" in line:
in_ai = False
continue
elif in_side and "[/unit]" in lines[i]:
elif in_side and "[/unit]" in line:
in_subunit = False
continue
if "wmllint: skip-side" in lines[i]:
if "wmllint: skip-side" in line:
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
try:
(key, prefix, value, comment) = parse_attribute(lines[i])
(key, prefix, value, comment) = parse_attribute(line)
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:
if not in_ai:
print '"%s", line %d: recruitment_pattern outside [ai]' \
% (filename, i+1)
% (filename, num)
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:
try:
if not in_generator and sidecount != int(value):
print '"%s", line %d: side number %s is out of sequence (%d expected)' \
% (filename, i+1, value, sidecount)
% (filename, num, value, sidecount)
except ValueError:
pass # Ignore ill-formed integer literals
except TypeError:
@ -1314,64 +1313,64 @@ def global_sanity_check(filename, lines):
in_unit = False
in_side = False
in_unit_type = False
for i in xrange(len(lines)):
if "[effect]" in lines[i]:
for num, line in enumerate(lines, start=1):
if "[effect]" in line:
in_effect = True
elif "[/effect]" in lines[i]:
elif "[/effect]" in line:
in_effect = False
elif "[unit]" in lines[i]:
elif "[unit]" in line:
in_unit = True
elif "[/unit]" in lines[i]:
elif "[/unit]" in line:
in_unit = False
elif "[side]" in lines[i]:
elif "[side]" in line:
in_side = True
elif "[/side]" in lines[i]:
elif "[/side]" in line:
in_side = False
elif "[unit_type]" in lines[i]:
elif "[unit_type]" in line:
in_unit_type = True
elif "[/unit_type]" in lines[i]:
elif "[/unit_type]" in line:
in_unit_type = False
# 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:
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"):
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"):
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
pass
elif in_unit or in_side or in_unit_type:
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"):
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"):
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
pass
# Interpret various magic comments
for i in xrange(len(lines)):
for line in lines:
# Interpret magic comments for setting the usage pattern of units.
# This coped with some wacky UtBS units that were defined with
# variant-spawning macros. The prototype comment looks like this:
#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:
usage[m.group(1)] = m.group(2).strip()
unit_types.append(m.group(1))
# Magic comment for adding non-standard usage types
m = re.search('# *wmllint: usagetypes? +(.*)', lines[i])
m = re.search('# *wmllint: usagetypes? +(.*)', line)
if m:
for newusage in m.group(1).split(","):
usage_types.append(newusage.strip())
# Accumulate global spelling exceptions
words = re.search("wmllint: general spellings? (.*)", lines[i])
words = re.search("wmllint: general spellings? (.*)", line)
if words:
for word in words.group(1).split():
declared_spellings["GLOBAL"].append(word.lower())
words = re.search("wmllint: directory spellings? (.*)", lines[i])
words = re.search("wmllint: directory spellings? (.*)", line)
if words:
fdir = os.path.dirname(filename)
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
textdomains = []
no_text = False
for i in xrange(len(lines)):
if "#textdomain" in lines[i]:
textdomains.append(i+1)
for num, line in enumerate(lines, start=1):
if "#textdomain" in line:
textdomains.append(num)
elif "wmllint: no translatables":
no_text = True
if not no_text:
@ -1987,7 +1986,7 @@ def hack_syntax(filename, lines):
elif lines[i].count("'") % 2 == 1:
try:
(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"
if lines[i] != newtext:
lines[i] = newtext
@ -2105,8 +2104,8 @@ def hack_syntax(filename, lines):
outside_of_theme_wml = True
if outside_of_theme_wml:
if not in_side_one_tag:
for j in range(len(side_one_tags_allowing_filter_side)):
if "[" + side_one_tags_allowing_filter_side[j] + "]" in precomment:
for tag in side_one_tags_allowing_filter_side:
if "[" + tag + "]" in precomment:
in_side_one_tag = True
else:
if side_one_tag_needs_side_one:
@ -2114,11 +2113,11 @@ def hack_syntax(filename, lines):
side_one_tag_needs_side_one = False
if "[filter_side]" in precomment:
side_one_tag_needs_side_one = False
for j in range(len(side_one_tags_allowing_filter_side)):
if "[/" + side_one_tags_allowing_filter_side[j] + "]" in precomment:
for tag in side_one_tags_allowing_filter_side:
if "[/" + tag + "]" in precomment:
if side_one_tag_needs_side_one:
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")
in_side_one_tag = False
side_one_tag_needs_side_one = True