flake8 and pep257 po2js.py

This commit is contained in:
Blake Grotewold
2014-11-11 08:52:09 -05:00
parent 01f15f41f1
commit 93ffadb189
+29 -31
View File
@@ -1,58 +1,56 @@
#!/usr/bin/python #!/usr/bin/python
# """convert .po to .js file."""
# convert .po to .js
#
import json import json
import optparse import optparse
import os import os
import polib import polib
import re import re
import string
import sys import sys
parser = optparse.OptionParser(usage="usage: %prog [options] pofile...") parser = optparse.OptionParser(usage="usage: %prog [options] pofile...")
parser.add_option("--callback", default="_.setTranslation", dest="callback", help="callback function to call with data") parser.add_option("--callback", default="_.setTranslation", dest="callback",
parser.add_option("--quiet", action="store_false", default=True, dest="verbose", help="don't print status messages to stdout") help="callback function to call with data")
parser.add_option("--quiet", action="store_false", default=True,
dest="verbose", help="don't print status messages to stdout")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if args == None or len(args) == 0: if args is None or len(args) == 0:
print("ERROR: you must specify at least one po file to translate"); print("ERROR: you must specify at least one po file to translate")
sys.exit(1) sys.exit(1)
paramFix = re.compile("(\\(([0-9])\\))") paramFix = re.compile("(\\(([0-9])\\))")
for srcfile in args: for srcfile in args:
destfile = os.path.splitext(srcfile)[0] + ".js" destfile = os.path.splitext(srcfile)[0] + ".js"
if options.verbose: if options.verbose:
print("INFO: converting %s to %s" % (srcfile, destfile)) print("INFO: converting %s to %s" % (srcfile, destfile))
xlate_map = {} xlate_map = {}
po = polib.pofile(srcfile, autodetect_encoding=False, encoding="utf-8", wrapwidth=-1) po = polib.pofile(srcfile, autodetect_encoding=False,
for entry in po: encoding="utf-8", wrapwidth=-1)
if entry.obsolete or entry.msgstr == '' or entry.msgstr == entry.msgid: for entry in po:
continue if entry.obsolete or entry.msgstr == '' or entry.msgstr == entry.msgid:
continue
xlate_map[entry.msgid] = entry.msgstr; xlate_map[entry.msgid] = entry.msgstr
dest = open(destfile, "w") dest = open(destfile, "w")
dest.write(options.callback); dest.write(options.callback)
dest.write("("); dest.write("(")
encoder = json.JSONEncoder() encoder = json.JSONEncoder()
for part in encoder.iterencode(xlate_map): for part in encoder.iterencode(xlate_map):
if part.startswith('"function('): if part.startswith('"function('):
dest.write(part[1:-1]); dest.write(part[1:-1])
else: else:
dest.write(part); dest.write(part)
dest.write(");\n")
dest.close()
dest.write(");\n")
dest.close()