https://github.com/ProgVal/Limnoria
Raw File
Tip revision: 39855b85c86e660cfcf075ccee32379785a0c90d authored by Jeremy Fincher on 31 August 2004, 20:23:35 UTC
Updated for the new release.
Tip revision: 39855b8
find-strings.py
#!/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 sys
import parser
import string
import symbol

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')
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', ' ')
            if not s:
                continue
            if len(s.translate(string.ascii, goodChars))/len(s) > 0.8:
                continue
            if len(s) <= 3:
                continue
            print '%s: %s: %s' % (filename, lineno, s)

if __name__ == '__main__':
    main()
back to top