Last active
January 8, 2019 21:23
-
-
Save tremby/34e1d0da36496b87a03f80bbc3c687f9 to your computer and use it in GitHub Desktop.
Find and highlight strings in python code, helpful for finding things for internationalization.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Usage: python findstrings.py <file.py> [<file.py> [...]] | |
""" | |
import tokenize | |
import token | |
def findstrings(readline): | |
for tok in tokenize.tokenize(readline): | |
if token.tok_name[tok.type] == "STRING": | |
yield tok | |
if __name__ == "__main__": | |
from termcolor import colored | |
import sys | |
for filename in sys.argv[1:]: | |
with open(filename, "rb") as f: | |
for tok in findstrings(f.readline): | |
line = tok.line.rstrip() | |
head = line[0:tok.start[1]] | |
middle = colored(tok.string, "red") | |
tail = line.splitlines()[-1][tok.end[1]:] | |
print("{}:{}:{}{}{}".format(filename, tok.start[0], head, middle, tail)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment