Revision | 29422 (tree) |
---|---|
Time | 2022-06-19 04:29:31 |
Author | ![]() |
Migrate xml2po.py scripts to Python3. (*checkpointing* ... so far only tested on Linux)
@@ -33,6 +33,9 @@ | ||
33 | 33 | # it is also not necessary until we start supporting extracting strings from more |
34 | 34 | # than one document type at the same time |
35 | 35 | # |
36 | + | |
37 | +from __future__ import print_function | |
38 | + | |
36 | 39 | import re |
37 | 40 | import libxml2 |
38 | 41 | import os |
@@ -133,7 +136,7 @@ | ||
133 | 136 | hash = self._md5_for_file(fullpath) |
134 | 137 | else: |
135 | 138 | hash = "THIS FILE DOESN'T EXIST" |
136 | - print >>sys.stderr, "Warning: image file '%s' not found." % fullpath | |
139 | + print("Warning: image file '%s' not found." % fullpath, file=sys.stderr) | |
137 | 140 | |
138 | 141 | msg.outputMessage("@@image: '%s'; md5=%s" % (attr, hash), node.lineNo(), |
139 | 142 | "When image changes, this message will be marked fuzzy or untranslated for you.\n"+ |
@@ -201,13 +204,13 @@ | ||
201 | 204 | # Perform some tests when ran standalone |
202 | 205 | if __name__ == '__main__': |
203 | 206 | test = docbookXmlMode() |
204 | - print "Ignored tags : " + repr(test.getIgnoredTags()) | |
205 | - print "Final tags : " + repr(test.getFinalTags()) | |
206 | - print "Space-preserve tags: " + repr(test.getSpacePreserveTags()) | |
207 | + print("Ignored tags : " + repr(test.getIgnoredTags())) | |
208 | + print("Final tags : " + repr(test.getFinalTags())) | |
209 | + print("Space-preserve tags: " + repr(test.getSpacePreserveTags())) | |
207 | 210 | |
208 | - print "Credits from string: '%s'" % test.getStringForTranslators() | |
209 | - print "Explanation for credits:\n\t'%s'" % test.getCommentForTranslators() | |
211 | + print("Credits from string: '%s'" % test.getStringForTranslators()) | |
212 | + print("Explanation for credits:\n\t'%s'" % test.getCommentForTranslators()) | |
210 | 213 | |
211 | - print "String for translation: '%s'" % test.getStringForTranslation() | |
212 | - print "Explanation for translation:\n\t'%s'" % test.getCommentForTranslation() | |
214 | + print("String for translation: '%s'" % test.getStringForTranslation()) | |
215 | + print("Explanation for translation:\n\t'%s'" % test.getCommentForTranslation()) | |
213 | 216 |
@@ -32,6 +32,9 @@ | ||
32 | 32 | # 2.2 second stable release (release 2), and 2.2.* bugfix releases |
33 | 33 | # ... |
34 | 34 | # |
35 | + | |
36 | +from __future__ import print_function | |
37 | + | |
35 | 38 | import sys |
36 | 39 | import libxml2 |
37 | 40 | import gettext |
@@ -98,7 +101,7 @@ | ||
98 | 101 | |
99 | 102 | """ % (tstamp) |
100 | 103 | |
101 | - out.write(tmp.encode('utf-8')) | |
104 | + out.write(tmp) | |
102 | 105 | |
103 | 106 | def outputAll(self, out): |
104 | 107 | self.outputHeader(out) |
@@ -159,7 +162,7 @@ | ||
159 | 162 | tree = ctxt.doc() |
160 | 163 | newnode = tree.getRootElement() |
161 | 164 | except: |
162 | - print >> sys.stderr, """Error while normalizing string as XML:\n"%s"\n""" % (text) | |
165 | + print("""Error while normalizing string as XML:\n"%s"\n""" % (text), file=sys.stderr) | |
163 | 166 | return text |
164 | 167 | |
165 | 168 | normalizeNode(newnode) |
@@ -340,7 +343,7 @@ | ||
340 | 343 | ctxt.parseDocument() |
341 | 344 | newnode = ctxt.doc() |
342 | 345 | except: |
343 | - print >> sys.stderr, """Error while parsing translation as XML:\n"%s"\n""" % (text.encode('utf-8')) | |
346 | + print("""Error while parsing translation as XML:\n"%s"\n""" % (text.encode('utf-8')), file=sys.stderr) | |
344 | 347 | return |
345 | 348 | |
346 | 349 | newelem = newnode.getRootElement() |
@@ -448,7 +451,7 @@ | ||
448 | 451 | if (node and node.type=='entity_ref'): |
449 | 452 | try: |
450 | 453 | # it would be nice if debugDumpNode could use StringIO, but it apparently cannot |
451 | - tmp = file(".xml2po-entitychecking","w+") | |
454 | + tmp = open(".xml2po-entitychecking","w+") | |
452 | 455 | node.debugDumpNode(tmp,0) |
453 | 456 | tmp.seek(0) |
454 | 457 | tmpstr = tmp.read() |
@@ -536,7 +539,7 @@ | ||
536 | 539 | sys.stderr.write("Error: Option '-o' is not yet supported when updating translations directly.\n") |
537 | 540 | sys.exit(8) |
538 | 541 | elif opt in ('-v', '--version'): |
539 | - print VERSION | |
542 | + print(VERSION) | |
540 | 543 | sys.exit(0) |
541 | 544 | elif opt in ('-h', '--help'): |
542 | 545 | sys.stderr.write("Error: If you want help, please use `%s --help' without '-u' option.\n" % (allargs[0])) |
@@ -590,7 +593,7 @@ | ||
590 | 593 | global t1 |
591 | 594 | t2 = datetime.now() |
592 | 595 | tdelta = t2 - t1 |
593 | - print >> sys.stderr, messg," (",tdelta.seconds, ",", tdelta.microseconds,")" | |
596 | + print(messg," (",tdelta.seconds, ",", tdelta.microseconds,")", file=sys.stderr) | |
594 | 597 | |
595 | 598 | # timetick( "xml2po started") |
596 | 599 |
@@ -618,9 +621,9 @@ | ||
618 | 621 | import getopt, fileinput |
619 | 622 | |
620 | 623 | def usage (with_help = False): |
621 | - print >> sys.stderr, "Usage: %s [OPTIONS] [XMLFILE]..." % (sys.argv[0]) | |
624 | + print("Usage: %s [OPTIONS] [XMLFILE]..." % (sys.argv[0]), file=sys.stderr) | |
622 | 625 | if (with_help): |
623 | - print >> sys.stderr, """ | |
626 | + print(""" | |
624 | 627 | OPTIONS may be some of: |
625 | 628 | -a --automatic-tags Automatically decides if tags are to be considered |
626 | 629 | "final" or not |
@@ -646,7 +649,7 @@ | ||
646 | 649 | using -p option for each XML file: |
647 | 650 | %s -p de.po chapter1.xml > chapter1.de.xml |
648 | 651 | %s -p de.po chapter2.xml > chapter2.de.xml |
649 | -""" % (sys.argv[0], sys.argv[0], sys.argv[0]) | |
652 | +""" % (sys.argv[0], sys.argv[0], sys.argv[0]), file=sys.stderr) | |
650 | 653 | sys.exit(0) |
651 | 654 | |
652 | 655 | if len(sys.argv) < 2: usage() |
@@ -683,7 +686,7 @@ | ||
683 | 686 | elif opt in ('-o', '--output'): |
684 | 687 | output = arg |
685 | 688 | elif opt in ('-v', '--version'): |
686 | - print VERSION | |
689 | + print(VERSION) | |
687 | 690 | sys.exit(0) |
688 | 691 | elif opt in ('-h', '--help'): |
689 | 692 | usage(True) |
@@ -693,7 +696,7 @@ | ||
693 | 696 | filenames.append(args.pop()) |
694 | 697 | |
695 | 698 | if len(filenames) > 1 and mode=='merge': |
696 | - print >> sys.stderr, "Error: You can merge translations with only one XML file at a time." | |
699 | + print("Error: You can merge translations with only one XML file at a time.", file=sys.stderr) | |
697 | 700 | sys.exit(2) |
698 | 701 | |
699 | 702 | try: |
@@ -700,11 +703,11 @@ | ||
700 | 703 | CurrentXmlMode = load_mode(default_mode)() |
701 | 704 | except: |
702 | 705 | CurrentXmlMode = None |
703 | - print >> sys.stderr, "Warning: cannot load module '%s', using automatic detection (-a)." % (default_mode) | |
706 | + print("Warning: cannot load module '%s', using automatic detection (-a)." % (default_mode), file=sys.stderr) | |
704 | 707 | automatic = 1 |
705 | 708 | |
706 | 709 | if mode=='merge' and mofile=='': |
707 | - print >> sys.stderr, "Error: You must specify MO file when merging translations." | |
710 | + print("Error: You must specify MO file when merging translations.", file=sys.stderr) | |
708 | 711 | sys.exit(3) |
709 | 712 | |
710 | 713 | ultimate_tags = read_finaltags(ultimate) |
@@ -736,7 +739,7 @@ | ||
736 | 739 | # timetick( "document parsed") |
737 | 740 | doc = ctxt.doc() |
738 | 741 | except: |
739 | - print >> sys.stderr, "Error: cannot open file '%s'." % (filename) | |
742 | + print("Error: cannot open file '%s'." % (filename), file=sys.stderr) | |
740 | 743 | sys.exit(1) |
741 | 744 | |
742 | 745 | msg.setFilename(filename) |
@@ -752,9 +755,9 @@ | ||
752 | 755 | out = sys.stdout |
753 | 756 | else: |
754 | 757 | try: |
755 | - out = file(output, 'w') | |
758 | + out = open(output, 'w') | |
756 | 759 | except: |
757 | - print >> sys.stderr, "Error: cannot open file %s for writing." % (output) | |
760 | + print("Error: cannot open file %s for writing." % (output), file=sys.stderr) | |
758 | 761 | sys.exit(5) |
759 | 762 | |
760 | 763 | if mode != 'merge': |