Last active
August 29, 2015 14:25
-
-
Save allcaps/a2d5001499e894001bfb to your computer and use it in GitHub Desktop.
Translate *.po file to asterisk strings.
This file contains hidden or 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
#!/usr/bin/python | |
import sys, getopt | |
import re | |
from string import maketrans | |
intab = "mnbvcxzlkjhgfdsapoiuytrewq0987654321MNBVCXZLKJHGFDSAPOIUYTREWQ" | |
outtab = len(intab) * "*" | |
trantab = maketrans(intab, outtab) | |
pattern = re.compile('\${.*}') | |
def postarify(inputfile, outputfile): | |
infile = file(inputfile, 'r') | |
outfile = file(outputfile, 'w') | |
with infile as lines: | |
prev_line = "" | |
for line in lines: | |
if prev_line.startswith('msgid'): | |
current_line = prev_line.translate(trantab) | |
# Copy the `${...}` parts. Because we don't want those translated. | |
iterator = pattern.finditer(prev_line) | |
for match in iterator: | |
start, end = match.span() | |
var = prev_line[start:end] | |
current_line = current_line[:start] + var + current_line[start+len(var):] | |
current_line = 'msgstr' + current_line[5:] | |
outfile.write(current_line) | |
else: | |
outfile.write(line) | |
prev_line = line | |
outfile.close() | |
def main(argv): | |
inputfile = '' | |
outputfile = '' | |
try: | |
opts, args = getopt.getopt(argv,"i:o:",["ifile=","ofile="]) | |
except getopt.GetoptError: | |
print('postar.py -i <inputfile> -o <outputfile>') | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print('postar.py -i <inputfile> -o <outputfile>') | |
sys.exit() | |
elif opt in ("-i", "--infile"): | |
inputfile = arg | |
elif opt in ("-o", "--outfile"): | |
outputfile = arg | |
if inputfile and outputfile: | |
if inputfile == outputfile: | |
print("Input file and output file may not be the same file.") | |
else: | |
postarify(inputfile, outputfile) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment