PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/convert.py

https://bitbucket.org/ggozad/underi18n
Python | 41 lines | 37 code | 3 blank | 1 comment | 0 complexity | 7ff094114980727458cd9d9a8765c41c MD5 | raw file
  1. #!/usr/bin/env python
  2. import sys
  3. import getopt
  4. import gettext
  5. import json
  6. def main(argv):
  7. inputfile = ''
  8. outputfile = ''
  9. try:
  10. opts, args = getopt.getopt(argv, "hi:o:", ["ifile=", "ofile="])
  11. except getopt.GetoptError:
  12. print 'test.py -i <inputfile> -o <outputfile>'
  13. sys.exit(2)
  14. for opt, arg in opts:
  15. if opt == '-h':
  16. print 'test.py -i <inputfile> -o <outputfile>'
  17. sys.exit()
  18. elif opt in ("-i", "--ifile"):
  19. inputfile = arg
  20. elif opt in ("-o", "--ofile"):
  21. outputfile = arg
  22. if not inputfile:
  23. print 'test.py -i <inputfile> -o <outputfile>'
  24. sys.exit()
  25. with open(inputfile) as ifile:
  26. catalog = gettext.GNUTranslations(ifile)._catalog
  27. del catalog['']
  28. output = json.dumps(catalog)
  29. if outputfile:
  30. with open(outputfile, 'w') as ofile:
  31. ofile.write(output)
  32. else:
  33. print output
  34. if __name__ == "__main__":
  35. main(sys.argv[1:])