Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement