Limnoria/tools/find-strings.py

56 lines
1.3 KiB
Python
Raw Normal View History

2004-08-19 00:23:58 +02:00
#!/usr/bin/env python
"""
This script exists mostly to pull out literal strings in a program in order to
spell-check them.
"""
2004-08-19 00:23:58 +02:00
import os
import sys
import parser
2004-08-19 14:23:36 +02:00
import string
import symbol
2004-08-19 00:23:58 +02:00
import supybot.utils as utils
def strings(node):
if node[0] == 3:
yield (node[2], node[1])
for x in node:
if isinstance(x, list):
for t in strings(x):
yield t
def getText(filename):
try:
fd = file(filename)
return fd.read()
finally:
fd.close()
goodChars = string.letters + string.whitespace
prefixes = ('r"', "r'", '$Id')
2004-08-19 00:23:58 +02:00
def main():
for filename in sys.argv[1:]:
s = getText(filename)
node = parser.ast2list(parser.suite(s), True)
for (lineno, s) in strings(node):
if s.translate(string.ascii, string.printable):
continue
for prefix in prefixes:
if s.startswith(prefix):
continue
s = eval(s)
s = s.replace('%s', ' ')
s = s.replace('%r', ' ')
2004-08-19 14:26:11 +02:00
if not s:
continue
2004-08-19 14:28:51 +02:00
if len(s.translate(string.ascii, goodChars))/len(s) > 0.8:
continue
if len(s) <= 3:
continue
print '%s: %s: %s' % (filename, lineno, s)
2004-08-19 00:23:58 +02:00
if __name__ == '__main__':
main()