Skip to content

Instantly share code, notes, and snippets.

@allcaps allcaps/postar.py
Last active Aug 29, 2015

Embed
What would you like to do?
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
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.