Skip to content

Instantly share code, notes, and snippets.

@allcaps
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allcaps/a2d5001499e894001bfb to your computer and use it in GitHub Desktop.
Save allcaps/a2d5001499e894001bfb to your computer and use it in GitHub Desktop.
Translate *.po file to asterisk strings.
#!/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