Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys, getopt
  3. import re
  4. from string import maketrans
  5.  
  6. intab = "mnbvcxzlkjhgfdsapoiuytrewq0987654321MNBVCXZLKJHGFDSAPOIUYTREWQ"
  7. outtab = len(intab) * "*"
  8. trantab = maketrans(intab, outtab)
  9. pattern = re.compile('\${.*}')
  10.  
  11. def postarify(inputfile, outputfile):
  12. infile = file(inputfile, 'r')
  13. outfile = file(outputfile, 'w')
  14.  
  15. with infile as lines:
  16. prev_line = ""
  17. for line in lines:
  18. if prev_line.startswith('msgid'):
  19. current_line = prev_line.translate(trantab)
  20. # Copy the `${...}` parts. Because we don't want those translated.
  21. iterator = pattern.finditer(prev_line)
  22. for match in iterator:
  23. start, end = match.span()
  24. var = prev_line[start:end]
  25. current_line = current_line[:start] + var + current_line[start+len(var):]
  26. current_line = 'msgstr' + current_line[5:]
  27. outfile.write(current_line)
  28. else:
  29. outfile.write(line)
  30. prev_line = line
  31. outfile.close()
  32.  
  33. def main(argv):
  34. inputfile = ''
  35. outputfile = ''
  36. try:
  37. opts, args = getopt.getopt(argv,"i:o:",["ifile=","ofile="])
  38. except getopt.GetoptError:
  39. print('postar.py -i <inputfile> -o <outputfile>')
  40. sys.exit(2)
  41. for opt, arg in opts:
  42. if opt == '-h':
  43. print('postar.py -i <inputfile> -o <outputfile>')
  44. sys.exit()
  45. elif opt in ("-i", "--infile"):
  46. inputfile = arg
  47. elif opt in ("-o", "--outfile"):
  48. outputfile = arg
  49. if inputfile and outputfile:
  50. if inputfile == outputfile:
  51. print("Input file and output file may not be the same file.")
  52. else:
  53. postarify(inputfile, outputfile)
  54.  
  55. if __name__ == "__main__":
  56. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement