https://github.com/ProgVal/Limnoria
Raw File
Tip revision: 534313a165a8c662771ddfa092ed29d835b58582 authored by James Vega on 11 January 2005, 14:40:36 UTC
Updated to 0.80.0rc2.
Tip revision: 534313a
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