[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r15717: First commit of tools to convert between wml and po formats. (in translation/trunk: . tools)
Author: ampleyfly
Date: 2008-07-07 06:23:53 -0400 (Mon, 07 Jul 2008)
New Revision: 15717
Added:
translation/trunk/tools/
translation/trunk/tools/po2wml.py
translation/trunk/tools/wml2po.py
Log:
First commit of tools to convert between wml and po formats. Only handles paragraphs for now.
Added: translation/trunk/tools/po2wml.py
===================================================================
--- translation/trunk/tools/po2wml.py (rev 0)
+++ translation/trunk/tools/po2wml.py 2008-07-07 10:23:53 UTC (rev 15717)
@@ -0,0 +1,90 @@
+import sys
+import getopt
+import re
+
+def usage():
+ print 'Usage: po2wml [-h | --help] [-i | --input] [-o | --output] [-t | --template]'
+
+def unescape(string):
+ return string.replace('\\n', '\n')
+
+def replace_string(indata, matchiter, string):
+ stack = []
+
+ while True:
+ try:
+ match = matchiter.next()
+ if match.group(1):
+ print 'paragraph closed'
+ start = stack.pop()
+ end = match.start()
+ return indata[:start] + string + indata[end:], matchiter
+ else:
+ print 'paragraph opened'
+ stack.append(match.end())
+ except StopIteration:
+ print 'couldn not find string to replace with %s' % string
+ break
+
+def parse(infile, outfile, template):
+ templatedata = template.read()
+ regex = re.compile('<(/?)p\s*>')
+ matchiter = regex.finditer(templatedata)
+
+ for line in infile:
+ if line.startswith(('#', 'msgid')):
+ continue
+ elif line.startswith('msgstr'):
+ string = line[7:].lstrip(' "').rstrip(' "\n')
+ string = unescape(string)
+ if string != '':
+ print 'trying to substitute for %s from %s' % (string, line)
+ templatedata, matchiter = replace_string(templatedata, matchiter, string)
+ else:
+ # ugly way of stepping past current paragraph
+ matchiter = replace_string(templatedata, matchiter, string)[1]
+
+ outfile.write(templatedata)
+
+
+def main(argv):
+ try:
+ opts, args = getopt.getopt(argv, 'hi:o:t:', ['help', 'input=', 'output=', 'template='])
+ except getopt.GetoptError:
+ usage()
+ sys.exit(2)
+
+ infile = outfile = template = None
+
+ for opt, arg in opts:
+ if opt in ('-h', '--help'):
+ usage()
+ sys.exit()
+ elif opt in ('-i', '--input'):
+ try:
+ infile = open(arg, 'r')
+ except IOError:
+ print 'The file %s could not be opened for reading, exiting...' % arg
+ sys.exit(-1)
+ elif opt in ('-o', '--output'):
+ try:
+ outfile = open(arg, 'w')
+ except IOError:
+ print 'The file %s could not be opened for writing, exiting...' % arg
+ sys.exit(-1)
+ elif opt in ('-t', '--template'):
+ try:
+ template = open(arg, 'r')
+ except IOError:
+ print 'The file %s could not be opened for reading, exiting...' % arg
+ sys.exit(-1)
+
+ if infile == None: infile = sys.stdin
+ if outfile == None: outfile = sys.stdout
+ if template == None: template = sys.stdin
+
+ parse(infile, outfile, template)
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
+
Added: translation/trunk/tools/wml2po.py
===================================================================
--- translation/trunk/tools/wml2po.py (rev 0)
+++ translation/trunk/tools/wml2po.py 2008-07-07 10:23:53 UTC (rev 15717)
@@ -0,0 +1,75 @@
+import sys
+import getopt
+import re
+
+string_no = 1
+
+def usage():
+ print 'Usage: wml2po [-h | --help] [-i | --input] [-o | --output]'
+
+def escape(string):
+ return string.replace('\n', '\\n')
+
+def add_po_string(filename, outfile, string):
+ global string_no
+ outdata = '#: %s:%d\n' % (filename, string_no)
+ outdata += 'msgid "%s"\n' % escape(string)
+ outdata += 'msgstr ""\n'
+ outdata += '\n'
+ outfile.write(outdata)
+ string_no = string_no + 1
+
+def parse(infile, outfile):
+ indata = infile.read()
+ regex = re.compile('<(/?)p\s*>')
+ matchiter = regex.finditer(indata)
+
+ stack = []
+
+ for match in matchiter:
+ if match.group(1):
+ #print 'paragraph closed'
+ start = stack.pop()
+ end = match.start()
+ contents = indata[start:end]
+ #print 'start %d, end %d, contents: %s' % (start, end, contents)
+
+ add_po_string(infile.name, outfile, contents)
+ else:
+ #print 'paragraph opened'
+ stack.append(match.end())
+
+def main(argv):
+ try:
+ opts, args = getopt.getopt(argv, 'hi:o:', ['help', 'input=', 'output='])
+ except getopt.GetoptError:
+ usage()
+ sys.exit(2)
+
+ infile = outfile = None
+
+ for opt, arg in opts:
+ if opt in ('-h', '--help'):
+ usage()
+ sys.exit()
+ elif opt in ('-i', '--input'):
+ try:
+ infile = open(arg, 'r')
+ except IOError:
+ print 'The file %s could not be opened for reading, exiting...' % arg
+ sys.exit(-1)
+ elif opt in ('-o', '--output'):
+ try:
+ outfile = open(arg, 'w')
+ except IOError:
+ print 'The file %s could not be opened for writing, exiting...' % arg
+ sys.exit(-1)
+
+ if infile == None: infile = sys.stdin
+ if outfile == None: outfile = sys.stdout
+
+ parse(infile, outfile)
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
+