Consistent Case: A Simple Linter
Moshe Zadka
moshez at math.huji.ac.il
Wed May 24 01:23:26 EDT 2000
# Released to the public domain by Moshe Zadka
# use like: get_identifiers <module1> <module2> ....
import parser, keyword
def get_from_tuple(l, so_far = None):
if so_far is None:
so_far = []
if l[0] == 1 and not keyword.iskeyword(l[1]):
so_far.append(l[1:])
else:
for t in l:
if type(t) is type(()):
get_from_tuple(t, so_far)
return so_far
def get_from_file(file, res=None):
tup = parser.suite(file.read()).totuple(1)
return get_from_tuple(tup, res)
def by_case(words_by_file):
import string
res = {}
for file, words in words_by_file.items():
for word, lineno in words:
lword = string.lower(word)
if not res.has_key(lword):
res[lword] = []
res[lword].append((lineno, file, word))
return res
def get_suspicious(words_by_case):
susp = []
for word, cases in words_by_case.items():
c = cases[0][2]
for tup in cases[1:]:
if tup[2] != c:
susp.append(word)
break
return susp
def main():
import sys
res = {}
for file in sys.argv[1:]:
f = open(file)
try:
res[file] = get_from_file(f)
finally:
f.close()
res = by_case(res)
susp = get_suspicious(res)
susp.sort()
for word in susp:
print word, 'is used with incosistent case:'
for line, file, case in res[word]:
print '\t%(file)s[%(line)d]: %(case)s' % vars()
if __name__=='__main__':
main()
--
Moshe Zadka <moshez at math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com
More information about the Python-list
mailing list