Skip to content

Instantly share code, notes, and snippets.

@allcaps allcaps/
Last active Aug 29, 2015

What would you like to do?
Translate *.po file to asterisk strings.
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:]
prev_line = line
def main(argv):
inputfile = ''
outputfile = ''
opts, args = getopt.getopt(argv,"i:o:",["ifile=","ofile="])
except getopt.GetoptError:
print(' -i <inputfile> -o <outputfile>')
for opt, arg in opts:
if opt == '-h':
print(' -i <inputfile> -o <outputfile>')
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.")
postarify(inputfile, outputfile)
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.