Some extra checks to make sure only useful strings are written.

This commit is contained in:
Jeremy Fincher 2004-08-19 12:21:25 +00:00
parent 501b642796
commit 753a410e57

View File

@ -1,5 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
"""
This script exists mostly to pull out literal strings in a program in order to
spell-check them.
"""
import os import os
import sys import sys
import symbol import symbol
@ -22,19 +27,26 @@ def getText(filename):
finally: finally:
fd.close() fd.close()
goodChars = string.letters + string.whitespace
prefixes = ('r"', "r'", '$Id')
def main(): def main():
out = file('strings.txt', 'w')
for filename in sys.argv[1:]: for filename in sys.argv[1:]:
s = getText(filename) s = getText(filename)
node = parser.ast2list(parser.suite(s), True) node = parser.ast2list(parser.suite(s), True)
for (lineno, string) in strings(node): for (lineno, s) in strings(node):
if string.startswith("r'") or string.startswith('r"'): if s.translate(string.ascii, string.printable):
continue continue
string = eval(string) for prefix in prefixes:
if len(string) <= 3: if s.startswith(prefix):
continue continue
out.write('%s: %s: %s\n' % (filename, lineno, string)) s = eval(s)
out.close() s = s.replace('%s', ' ')
s = s.replace('%r', ' ')
if len(s.translate(string.ascii, goodChars))/len(s) < 0.2:
continue
if len(s) <= 3:
continue
print '%s: %s: %s' % (filename, lineno, s)
if __name__ == '__main__': if __name__ == '__main__':
main() main()