| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
| 3 # | 3 # |
| 4 # Copyright 2002-2007 Zuza Software Foundation | 4 # Copyright 2002-2007 Zuza Software Foundation |
| 5 # | 5 # |
| 6 # This file is part of translate. | 6 # This file is part of translate. |
| 7 # | 7 # |
| 8 # translate is free software; you can redistribute it and/or modify | 8 # translate is free software; you can redistribute it and/or modify |
| 9 # it under the terms of the GNU General Public License as published by | 9 # it under the terms of the GNU General Public License as published by |
| 10 # the Free Software Foundation; either version 2 of the License, or | 10 # the Free Software Foundation; either version 2 of the License, or |
| 11 # (at your option) any later version. | 11 # (at your option) any later version. |
| 12 # | 12 # |
| 13 # translate is distributed in the hope that it will be useful, | 13 # translate is distributed in the hope that it will be useful, |
| 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 # GNU General Public License for more details. | 16 # GNU General Public License for more details. |
| 17 # | 17 # |
| 18 # You should have received a copy of the GNU General Public License | 18 # You should have received a copy of the GNU General Public License |
| 19 # along with translate; if not, write to the Free Software | 19 # along with translate; if not, write to the Free Software |
| 20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | 21 |
| 22 """Classes that hold units of .po files (pounit) or entire files (pofile). | 22 """Classes that hold units of .po files (pounit) or entire files (pofile). |
| 23 | 23 |
| 24 Gettext-style .po (or .pot) files are used in translations for KDE, GNOME and | 24 Gettext-style .po (or .pot) files are used in translations for KDE, GNOME and |
| 25 many other projects. | 25 many other projects. |
| 26 | 26 |
| 27 This uses libgettextpo from the gettext package. Any version before 0.17 will | 27 This uses libgettextpo from the gettext package. Any version before 0.17 will |
| 28 at least cause some subtle bugs or may not work at all. Developers might want | 28 at least cause some subtle bugs or may not work at all. Developers might want |
| 29 to have a look at gettext-tools/libgettextpo/gettext-po.h from the gettext | 29 to have a look at gettext-tools/libgettextpo/gettext-po.h from the gettext |
| 30 package for the public API of the library. | 30 package for the public API of the library. |
| 31 """ | 31 """ |
| 32 | 32 |
| 33 from translate.misc.multistring import multistring | 33 from translate.misc.multistring import multistring |
| 34 from translate.storage import pocommon | 34 from translate.storage import pocommon |
| 35 from translate.misc import quote | 35 from translate.misc import quote |
| 36 from translate.lang import data | 36 from translate.lang import data |
| 37 from ctypes import * | 37 from ctypes import * |
| 38 import ctypes.util | 38 import ctypes.util |
| 39 try: | 39 try: |
| 40 import cStringIO as StringIO | 40 import cStringIO as StringIO |
| 41 except ImportError: | 41 except ImportError: |
| 42 import StringIO | 42 import StringIO |
| 43 import os | 43 import os |
| 44 import pypo | 44 import pypo |
| 45 import re | 45 import re |
| 46 import sys | 46 import sys |
| 47 import tempfile | 47 import tempfile |
| 48 | 48 |
| 49 lsep = " " | 49 lsep = " " |
| 50 """Seperator for #: entries""" | 50 """Seperator for #: entries""" |
| 51 | 51 |
| 52 STRING = c_char_p | 52 STRING = c_char_p |
| 53 | 53 |
| 54 # Structures | 54 # Structures |
| 55 class po_message(Structure): | 55 class po_message(Structure): |
| 56 _fields_ = [] | 56 _fields_ = [] |
| 57 | 57 |
| 58 # Function prototypes | 58 # Function prototypes |
| 59 xerror_prototype = CFUNCTYPE(None, c_int, POINTER(po_message), STRING, c_uint, c
_uint, c_int, STRING) | 59 xerror_prototype = CFUNCTYPE(None, c_int, POINTER(po_message), STRING, c_uint, c
_uint, c_int, STRING) |
| 60 xerror2_prototype = CFUNCTYPE(None, c_int, POINTER(po_message), STRING, c_uint,
c_uint, c_int, STRING, POINTER(po_message), STRING, c_uint, c_uint, c_int, STRIN
G) | 60 xerror2_prototype = CFUNCTYPE(None, c_int, POINTER(po_message), STRING, c_uint,
c_uint, c_int, STRING, POINTER(po_message), STRING, c_uint, c_uint, c_int, STRIN
G) |
| 61 | 61 |
| 62 | 62 |
| 63 # Structures (error handler) | 63 # Structures (error handler) |
| 64 class po_xerror_handler(Structure): | 64 class po_xerror_handler(Structure): |
| 65 _fields_ = [('xerror', xerror_prototype), | 65 _fields_ = [('xerror', xerror_prototype), |
| 66 ('xerror2', xerror2_prototype)] | 66 ('xerror2', xerror2_prototype)] |
| 67 | 67 |
| 68 class po_error_handler(Structure): | 68 class po_error_handler(Structure): |
| 69 _fields_ = [ | 69 _fields_ = [ |
| 70 ('error', CFUNCTYPE(None, c_int, c_int, STRING)), | 70 ('error', CFUNCTYPE(None, c_int, c_int, STRING)), |
| 71 ('error_at_line', CFUNCTYPE(None, c_int, c_int, STRING, c_uint, STRING)), | 71 ('error_at_line', CFUNCTYPE(None, c_int, c_int, STRING, c_uint, STRING)), |
| 72 ('multiline_warning', CFUNCTYPE(None, STRING, STRING)), | 72 ('multiline_warning', CFUNCTYPE(None, STRING, STRING)), |
| 73 ('multiline_error', CFUNCTYPE(None, STRING, STRING)), | 73 ('multiline_error', CFUNCTYPE(None, STRING, STRING)), |
| 74 ] | 74 ] |
| 75 | 75 |
| 76 # Callback functions for po_xerror_handler | 76 # Callback functions for po_xerror_handler |
| 77 def xerror_cb(severity, message, filename, lineno, column, multilint_p, message_
text): | 77 def xerror_cb(severity, message, filename, lineno, column, multilint_p, message_
text): |
| 78 print >> sys.stderr, "xerror_cb", severity, message, filename, lineno, colum
n, multilint_p, message_text | 78 print >> sys.stderr, "xerror_cb", severity, message, filename, lineno, colum
n, multilint_p, message_text |
| 79 if severity >= 1: | 79 if severity >= 1: |
| 80 raise ValueError(message_text) | 80 raise ValueError(message_text) |
| 81 | 81 |
| 82 def xerror2_cb(severity, message1, filename1, lineno1, column1, multiline_p1, me
ssage_text1, message2, filename2, lineno2, column2, multiline_p2, message_text2)
: | 82 def xerror2_cb(severity, message1, filename1, lineno1, column1, multiline_p1, me
ssage_text1, message2, filename2, lineno2, column2, multiline_p2, message_text2)
: |
| 83 print >> sys.stderr, "xerror2_cb", severity, message1, filename1, lineno1, c
olumn1, multiline_p1, message_text1, message2, filename2, lineno2, column2, mult
iline_p2, message_text2 | 83 print >> sys.stderr, "xerror2_cb", severity, message1, filename1, lineno1, c
olumn1, multiline_p1, message_text1, message2, filename2, lineno2, column2, mult
iline_p2, message_text2 |
| 84 if severity >= 1: | 84 if severity >= 1: |
| 85 raise ValueError(message_text1) | 85 raise ValueError(message_text1) |
| 86 | 86 |
| 87 | 87 |
| 88 | 88 |
| 89 # Load libgettextpo | 89 # Load libgettextpo |
| 90 gpo = None | 90 gpo = None |
| 91 # 'gettextpo' is recognised on Unix, while only 'libgettextpo' is recognised on | 91 # 'gettextpo' is recognised on Unix, while only 'libgettextpo' is recognised on |
| 92 # windows. Therefore we test both. | 92 # windows. Therefore we test both. |
| 93 names = ['gettextpo', 'libgettextpo'] | 93 names = ['gettextpo', 'libgettextpo'] |
| 94 for name in names: | 94 for name in names: |
| 95 lib_location = ctypes.util.find_library(name) | 95 lib_location = ctypes.util.find_library(name) |
| 96 if lib_location: | 96 if lib_location: |
| 97 gpo = cdll.LoadLibrary(lib_location) | 97 gpo = cdll.LoadLibrary(lib_location) |
| 98 if gpo: | 98 if gpo: |
| 99 break | 99 break |
| 100 else: | 100 else: |
| 101 # Now we are getting desperate, so let's guess a unix type DLL that might | 101 # Now we are getting desperate, so let's guess a unix type DLL that might |
| 102 # be in LD_LIBRARY_PATH or loaded with LD_PRELOAD | 102 # be in LD_LIBRARY_PATH or loaded with LD_PRELOAD |
| 103 try: | 103 try: |
| 104 gpo = cdll.LoadLibrary('libgettextpo.so') | 104 gpo = cdll.LoadLibrary('libgettextpo.so') |
| 105 except OSError, e: | 105 except OSError, e: |
| 106 raise ImportError("gettext PO library not found") | 106 raise ImportError("gettext PO library not found") |
| 107 | 107 |
| 108 # Setup return and paramater types | 108 # Setup return and paramater types |
| 109 # File access | 109 # File access |
| 110 gpo.po_file_read_v3.argtypes = [STRING, POINTER(po_xerror_handler)] | 110 gpo.po_file_read_v3.argtypes = [STRING, POINTER(po_xerror_handler)] |
| 111 gpo.po_file_write_v2.argtypes = [c_int, STRING, POINTER(po_xerror_handler)] | 111 gpo.po_file_write_v2.argtypes = [c_int, STRING, POINTER(po_xerror_handler)] |
| 112 gpo.po_file_write_v2.retype = c_int | 112 gpo.po_file_write_v2.retype = c_int |
| 113 | 113 |
| 114 # Header | 114 # Header |
| 115 gpo.po_file_domain_header.restype = STRING | 115 gpo.po_file_domain_header.restype = STRING |
| 116 gpo.po_header_field.restype = STRING | 116 gpo.po_header_field.restype = STRING |
| 117 gpo.po_header_field.argtypes = [STRING, STRING] | 117 gpo.po_header_field.argtypes = [STRING, STRING] |
| 118 | 118 |
| 119 # Locations (filepos) | 119 # Locations (filepos) |
| 120 gpo.po_filepos_file.restype = STRING | 120 gpo.po_filepos_file.restype = STRING |
| 121 gpo.po_message_filepos.restype = c_int | 121 gpo.po_message_filepos.restype = c_int |
| 122 gpo.po_message_filepos.argtypes = [c_int, c_int] | 122 gpo.po_message_filepos.argtypes = [c_int, c_int] |
| 123 gpo.po_message_add_filepos.argtypes = [c_int, STRING, c_int] | 123 gpo.po_message_add_filepos.argtypes = [c_int, STRING, c_size_t] |
| 124 | 124 |
| 125 # Message (get methods) | 125 # Message (get methods) |
| 126 gpo.po_message_comments.restype = STRING | 126 gpo.po_message_comments.restype = STRING |
| 127 gpo.po_message_extracted_comments.restype = STRING | 127 gpo.po_message_extracted_comments.restype = STRING |
| 128 gpo.po_message_prev_msgctxt.restype = STRING | 128 gpo.po_message_prev_msgctxt.restype = STRING |
| 129 gpo.po_message_prev_msgid.restype = STRING | 129 gpo.po_message_prev_msgid.restype = STRING |
| 130 gpo.po_message_prev_msgid_plural.restype = STRING | 130 gpo.po_message_prev_msgid_plural.restype = STRING |
| 131 gpo.po_message_is_format.restype = c_int | 131 gpo.po_message_is_format.restype = c_int |
| 132 gpo.po_message_msgctxt.restype = STRING | 132 gpo.po_message_msgctxt.restype = STRING |
| 133 gpo.po_message_msgid.restype = STRING | 133 gpo.po_message_msgid.restype = STRING |
| 134 gpo.po_message_msgid_plural.restype = STRING | 134 gpo.po_message_msgid_plural.restype = STRING |
| 135 gpo.po_message_msgstr.restype = STRING | 135 gpo.po_message_msgstr.restype = STRING |
| 136 gpo.po_message_msgstr_plural.restype = STRING | 136 gpo.po_message_msgstr_plural.restype = STRING |
| 137 | 137 |
| 138 # Message (set methods) | 138 # Message (set methods) |
| 139 gpo.po_message_set_comments.argtypes = [c_int, STRING] | 139 gpo.po_message_set_comments.argtypes = [c_int, STRING] |
| 140 gpo.po_message_set_extracted_comments.argtypes = [c_int, STRING] | 140 gpo.po_message_set_extracted_comments.argtypes = [c_int, STRING] |
| 141 gpo.po_message_set_fuzzy.argtypes = [c_int, c_int] | 141 gpo.po_message_set_fuzzy.argtypes = [c_int, c_int] |
| 142 gpo.po_message_set_msgctxt.argtypes = [c_int, STRING] | 142 gpo.po_message_set_msgctxt.argtypes = [c_int, STRING] |
| 143 | 143 |
| 144 # Setup the po_xerror_handler | 144 # Setup the po_xerror_handler |
| 145 xerror_handler = po_xerror_handler() | 145 xerror_handler = po_xerror_handler() |
| 146 xerror_handler.xerror = xerror_prototype(xerror_cb) | 146 xerror_handler.xerror = xerror_prototype(xerror_cb) |
| 147 xerror_handler.xerror2 = xerror2_prototype(xerror2_cb) | 147 xerror_handler.xerror2 = xerror2_prototype(xerror2_cb) |
| 148 | 148 |
| 149 def escapeforpo(text): | 149 def escapeforpo(text): |
| 150 return pypo.escapeforpo(text) | 150 return pypo.escapeforpo(text) |
| 151 | 151 |
| 152 def quoteforpo(text): | 152 def quoteforpo(text): |
| 153 return pypo.quoteforpo(text) | 153 return pypo.quoteforpo(text) |
| 154 | 154 |
| 155 def unquotefrompo(postr, joinwithlinebreak=False): | 155 def unquotefrompo(postr, joinwithlinebreak=False): |
| 156 return pypo.unquotefrompo(postr, joinwithlinebreak) | 156 return pypo.unquotefrompo(postr, joinwithlinebreak) |
| 157 | 157 |
| 158 def encodingToUse(encoding): | 158 def encodingToUse(encoding): |
| 159 return pypo.encodingToUse(encoding) | 159 return pypo.encodingToUse(encoding) |
| 160 | 160 |
| 161 def get_libgettextpo_version(): | 161 def get_libgettextpo_version(): |
| 162 """Returns the libgettextpo version | 162 """Returns the libgettextpo version |
| 163 | 163 |
| 164 @return: a three-value tuple containing the libgettextpo version in the | 164 @return: a three-value tuple containing the libgettextpo version in the |
| 165 following format: | 165 following format: |
| 166 (major version, minor version, subminor version) | 166 (major version, minor version, subminor version) |
| 167 """ | 167 """ |
| 168 libversion = c_long.in_dll(gpo, 'libgettextpo_version') | 168 libversion = c_long.in_dll(gpo, 'libgettextpo_version') |
| 169 major = libversion.value >> 16 | 169 major = libversion.value >> 16 |
| 170 minor = libversion.value >> 8 | 170 minor = libversion.value >> 8 |
| 171 subminor = libversion.value - (major << 16) - (minor << 8) | 171 subminor = libversion.value - (major << 16) - (minor << 8) |
| 172 return major, minor, subminor | 172 return major, minor, subminor |
| 173 | 173 |
| (...skipping 281 matching lines...) Show 10 above Show 10 below |
| 455 def resurrect(self): | 455 def resurrect(self): |
| 456 gpo.po_message_set_obsolete(self._gpo_message, False) | 456 gpo.po_message_set_obsolete(self._gpo_message, False) |
| 457 | 457 |
| 458 def hasplural(self): | 458 def hasplural(self): |
| 459 return gpo.po_message_msgid_plural(self._gpo_message) is not None | 459 return gpo.po_message_msgid_plural(self._gpo_message) is not None |
| 460 | 460 |
| 461 def _extract_msgidcomments(self, text=None): | 461 def _extract_msgidcomments(self, text=None): |
| 462 """Extract KDE style msgid comments from the unit. | 462 """Extract KDE style msgid comments from the unit. |
| 463 | 463 |
| 464 @rtype: String | 464 @rtype: String |
| 465 @return: Returns the extracted msgidcomments found in this unit's msgid. | 465 @return: Returns the extracted msgidcomments found in this unit's msgid. |
| 466 | 466 |
| 467 """ | 467 """ |
| 468 | 468 |
| 469 if not text: | 469 if not text: |
| 470 text = gpo.po_message_msgid(self._gpo_message) | 470 text = gpo.po_message_msgid(self._gpo_message) |
| 471 if text: | 471 if text: |
| 472 msgidcomment = re.search("_: (.*)\n", text) | 472 msgidcomment = re.search("_: (.*)\n", text) |
| 473 if msgidcomment: | 473 if msgidcomment: |
| 474 return msgidcomment.group(1).decode(self._encoding) | 474 return msgidcomment.group(1).decode(self._encoding) |
| 475 return u"" | 475 return u"" |
| 476 | 476 |
| 477 def __str__(self): | 477 def __str__(self): |
| 478 pf = pofile() | 478 pf = pofile() |
| 479 pf.addunit(self) | 479 pf.addunit(self) |
| 480 return str(pf) | 480 return str(pf) |
| 481 | 481 |
| 482 def getlocations(self): | 482 def getlocations(self): |
| 483 locations = [] | 483 locations = [] |
| 484 i = 0 | 484 i = 0 |
| 485 location = gpo.po_message_filepos(self._gpo_message, i) | 485 location = gpo.po_message_filepos(self._gpo_message, i) |
| 486 while location: | 486 while location: |
| 487 locname = gpo.po_filepos_file(location) | 487 locname = gpo.po_filepos_file(location) |
| 488 locline = gpo.po_filepos_start_line(location) | 488 locline = gpo.po_filepos_start_line(location) |
| 489 if locline == -1: | 489 if locline == -1: |
| 490 locstring = locname | 490 locstring = locname |
| 491 else: | 491 else: |
| 492 locstring = locname + ":" + str(locline) | 492 locstring = locname + ":" + str(locline) |
| 493 locations.append(locstring) | 493 locations.append(locstring) |
| 494 i += 1 | 494 i += 1 |
| 495 location = gpo.po_message_filepos(self._gpo_message, i) | 495 location = gpo.po_message_filepos(self._gpo_message, i) |
| 496 return locations | 496 return locations |
| 497 | 497 |
| 498 def addlocation(self, location): | 498 def addlocation(self, location): |
| 499 for loc in location.split(): | 499 for loc in location.split(): |
| 500 parts = loc.split(":") | 500 parts = loc.split(":") |
| 501 file = parts[0] | 501 file = parts[0] |
| 502 if len(parts) == 2: | 502 if len(parts) == 2: |
| 503 line = int(parts[1]) | 503 line = int(parts[1]) |
| 504 else: | 504 else: |
| 505 line = -1 | 505 # libgettextpo requires a (size_t)(-1) if no file |
| 506 # location is available |
| 507 line = c_size_t(-1) |
| 506 gpo.po_message_add_filepos(self._gpo_message, file, line) | 508 gpo.po_message_add_filepos(self._gpo_message, file, line) |
| 507 | 509 |
| 508 def getcontext(self): | 510 def getcontext(self): |
| 509 msgctxt = gpo.po_message_msgctxt(self._gpo_message) | 511 msgctxt = gpo.po_message_msgctxt(self._gpo_message) |
| 510 msgidcomment = self._extract_msgidcomments() | 512 msgidcomment = self._extract_msgidcomments() |
| 511 if msgctxt: | 513 if msgctxt: |
| 512 return msgctxt + msgidcomment | 514 return msgctxt + msgidcomment |
| 513 else: | 515 else: |
| 514 return msgidcomment | 516 return msgidcomment |
| 515 | 517 |
| 516 class pofile(pocommon.pofile): | 518 class pofile(pocommon.pofile): |
| 517 UnitClass = pounit | 519 UnitClass = pounit |
| 518 def __init__(self, inputfile=None, encoding=None, unitclass=pounit): | 520 def __init__(self, inputfile=None, encoding=None, unitclass=pounit): |
| 519 self.UnitClass = unitclass | 521 self.UnitClass = unitclass |
| 520 pocommon.pofile.__init__(self, unitclass=unitclass) | 522 pocommon.pofile.__init__(self, unitclass=unitclass) |
| 521 self._gpo_memory_file = None | 523 self._gpo_memory_file = None |
| 522 self._gpo_message_iterator = None | 524 self._gpo_message_iterator = None |
| 523 self._encoding = encodingToUse(encoding) | 525 self._encoding = encodingToUse(encoding) |
| 524 if inputfile is not None: | 526 if inputfile is not None: |
| 525 self.parse(inputfile) | 527 self.parse(inputfile) |
| 526 else: | 528 else: |
| 527 self._gpo_memory_file = gpo.po_file_create() | 529 self._gpo_memory_file = gpo.po_file_create() |
| 528 self._gpo_message_iterator = gpo.po_message_iterator(self._gpo_memor
y_file, None) | 530 self._gpo_message_iterator = gpo.po_message_iterator(self._gpo_memor
y_file, None) |
| 529 | 531 |
| 530 def addunit(self, unit): | 532 def addunit(self, unit): |
| 531 gpo.po_message_insert(self._gpo_message_iterator, unit._gpo_message) | 533 gpo.po_message_insert(self._gpo_message_iterator, unit._gpo_message) |
| 532 self.units.append(unit) | 534 self.units.append(unit) |
| 533 | 535 |
| 534 def removeduplicates(self, duplicatestyle="merge"): | 536 def removeduplicates(self, duplicatestyle="merge"): |
| 535 """make sure each msgid is unique ; merge comments etc from duplicates i
nto original""" | 537 """make sure each msgid is unique ; merge comments etc from duplicates i
nto original""" |
| 536 msgiddict = {} | 538 msgiddict = {} |
| 537 uniqueunits = [] | 539 uniqueunits = [] |
| 538 # we sometimes need to keep track of what has been marked | 540 # we sometimes need to keep track of what has been marked |
| 539 # TODO: this is using a list as the pos aren't hashable, but this is slo
w... | 541 # TODO: this is using a list as the pos aren't hashable, but this is slo
w... |
| 540 markedpos = [] | 542 markedpos = [] |
| 541 def addcomment(thepo): | 543 def addcomment(thepo): |
| 542 thepo.msgidcomment = " ".join(thepo.getlocations()) | 544 thepo.msgidcomment = " ".join(thepo.getlocations()) |
| 543 markedpos.append(thepo) | 545 markedpos.append(thepo) |
| 544 for thepo in self.units: | 546 for thepo in self.units: |
| 545 if thepo.isheader(): | 547 if thepo.isheader(): |
| 546 uniqueunits.append(thepo) | 548 uniqueunits.append(thepo) |
| 547 continue | 549 continue |
| 548 if duplicatestyle.startswith("msgid_comment"): | 550 if duplicatestyle.startswith("msgid_comment"): |
| 549 msgid = thepo._extract_msgidcomments() + thepo.source | 551 msgid = thepo._extract_msgidcomments() + thepo.source |
| 550 else: | 552 else: |
| 551 msgid = thepo.source | 553 msgid = thepo.source |
| 552 if duplicatestyle == "msgid_comment_all": | 554 if duplicatestyle == "msgid_comment_all": |
| 553 addcomment(thepo) | 555 addcomment(thepo) |
| 554 uniqueunits.append(thepo) | 556 uniqueunits.append(thepo) |
| 555 elif msgid in msgiddict: | 557 elif msgid in msgiddict: |
| (...skipping 79 matching lines...) Show 10 above Show 10 below |
| 635 posrc = input.read() | 637 posrc = input.read() |
| 636 input.close() | 638 input.close() |
| 637 input = posrc | 639 input = posrc |
| 638 | 640 |
| 639 needtmpfile = not os.path.isfile(input) | 641 needtmpfile = not os.path.isfile(input) |
| 640 if needtmpfile: | 642 if needtmpfile: |
| 641 # This is not a file - we write the string to a temporary file | 643 # This is not a file - we write the string to a temporary file |
| 642 fd, fname = tempfile.mkstemp(prefix='translate', suffix='.po') | 644 fd, fname = tempfile.mkstemp(prefix='translate', suffix='.po') |
| 643 os.write(fd, input) | 645 os.write(fd, input) |
| 644 input = fname | 646 input = fname |
| 645 os.close(fd) | 647 os.close(fd) |
| 646 | 648 |
| 647 self._gpo_memory_file = gpo.po_file_read_v3(input, xerror_handler) | 649 self._gpo_memory_file = gpo.po_file_read_v3(input, xerror_handler) |
| 648 if self._gpo_memory_file is None: | 650 if self._gpo_memory_file is None: |
| 649 print >> sys.stderr, "Error:" | 651 print >> sys.stderr, "Error:" |
| 650 | 652 |
| 651 if needtmpfile: | 653 if needtmpfile: |
| 652 os.remove(input) | 654 os.remove(input) |
| 653 | 655 |
| 654 # Handle xerrors here | 656 # Handle xerrors here |
| 655 self._header = gpo.po_file_domain_header(self._gpo_memory_file, None) | 657 self._header = gpo.po_file_domain_header(self._gpo_memory_file, None) |
| 656 if self._header: | 658 if self._header: |
| 657 charset = gpo.po_header_field(self._header, "Content-Type") | 659 charset = gpo.po_header_field(self._header, "Content-Type") |
| 658 if charset: | 660 if charset: |
| 659 charset = re.search("charset=([^\\s]+)", charset).group(1) | 661 charset = re.search("charset=([^\\s]+)", charset).group(1) |
| 660 self._encoding = encodingToUse(charset) | 662 self._encoding = encodingToUse(charset) |
| 661 self._gpo_message_iterator = gpo.po_message_iterator(self._gpo_memory_fi
le, None) | 663 self._gpo_message_iterator = gpo.po_message_iterator(self._gpo_memory_fi
le, None) |
| 662 newmessage = gpo.po_next_message(self._gpo_message_iterator) | 664 newmessage = gpo.po_next_message(self._gpo_message_iterator) |
| 663 while newmessage: | 665 while newmessage: |
| 664 newunit = pounit(gpo_message=newmessage) | 666 newunit = pounit(gpo_message=newmessage) |
| 665 self.units.append(newunit) | 667 self.units.append(newunit) |
| 666 newmessage = gpo.po_next_message(self._gpo_message_iterator) | 668 newmessage = gpo.po_next_message(self._gpo_message_iterator) |
| 667 self._free_iterator() | 669 self._free_iterator() |
| 668 | 670 |
| 669 def __del__(self): | 671 def __del__(self): |
| 670 # We currently disable this while we still get segmentation faults. | 672 # We currently disable this while we still get segmentation faults. |
| 671 # Note that this is definitely leaking memory because of this. | 673 # Note that this is definitely leaking memory because of this. |
| 672 return | 674 return |
| 673 self._free_iterator() | 675 self._free_iterator() |
| 674 if self._gpo_memory_file is not None: | 676 if self._gpo_memory_file is not None: |
| 675 gpo.po_file_free(self._gpo_memory_file) | 677 gpo.po_file_free(self._gpo_memory_file) |
| 676 self._gpo_memory_file = None | 678 self._gpo_memory_file = None |
| 677 | 679 |
| 678 def _free_iterator(self): | 680 def _free_iterator(self): |
| 679 # We currently disable this while we still get segmentation faults. | 681 # We currently disable this while we still get segmentation faults. |
| 680 # Note that this is definitely leaking memory because of this. | 682 # Note that this is definitely leaking memory because of this. |
| 681 return | 683 return |
| 682 if self._gpo_message_iterator is not None: | 684 if self._gpo_message_iterator is not None: |
| 683 gpo.po_message_iterator_free(self._gpo_message_iterator) | 685 gpo.po_message_iterator_free(self._gpo_message_iterator) |
| 684 self._gpo_message_iterator = None | 686 self._gpo_message_iterator = None |
| OLD | NEW |