| LEFT | RIGHT |
| 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""" |
| (...skipping 589 matching lines...) Show 10 above Show 10 below |
| 640 posrc = input.read() | 640 posrc = input.read() |
| 641 input.close() | 641 input.close() |
| 642 input = posrc | 642 input = posrc |
| 643 | 643 |
| 644 needtmpfile = not os.path.isfile(input) | 644 needtmpfile = not os.path.isfile(input) |
| 645 if needtmpfile: | 645 if needtmpfile: |
| 646 # This is not a file - we write the string to a temporary file | 646 # This is not a file - we write the string to a temporary file |
| 647 fd, fname = tempfile.mkstemp(prefix='translate', suffix='.po') | 647 fd, fname = tempfile.mkstemp(prefix='translate', suffix='.po') |
| 648 os.write(fd, input) | 648 os.write(fd, input) |
| 649 input = fname | 649 input = fname |
| 650 os.close(fd) | 650 os.close(fd) |
| 651 | 651 |
| 652 self._gpo_memory_file = gpo.po_file_read_v3(input, xerror_handler) | 652 self._gpo_memory_file = gpo.po_file_read_v3(input, xerror_handler) |
| 653 if self._gpo_memory_file is None: | 653 if self._gpo_memory_file is None: |
| 654 print >> sys.stderr, "Error:" | 654 print >> sys.stderr, "Error:" |
| 655 | 655 |
| 656 if needtmpfile: | 656 if needtmpfile: |
| 657 os.remove(input) | 657 os.remove(input) |
| 658 | 658 |
| 659 # Handle xerrors here | 659 # Handle xerrors here |
| 660 self._header = gpo.po_file_domain_header(self._gpo_memory_file, None) | 660 self._header = gpo.po_file_domain_header(self._gpo_memory_file, None) |
| 661 if self._header: | 661 if self._header: |
| 662 charset = gpo.po_header_field(self._header, "Content-Type") | 662 charset = gpo.po_header_field(self._header, "Content-Type") |
| 663 if charset: | 663 if charset: |
| 664 charset = re.search("charset=([^\\s]+)", charset).group(1) | 664 charset = re.search("charset=([^\\s]+)", charset).group(1) |
| 665 self._encoding = encodingToUse(charset) | 665 self._encoding = encodingToUse(charset) |
| 666 self._gpo_message_iterator = gpo.po_message_iterator(self._gpo_memory_fi
le, None) | 666 self._gpo_message_iterator = gpo.po_message_iterator(self._gpo_memory_fi
le, None) |
| 667 newmessage = gpo.po_next_message(self._gpo_message_iterator) | 667 newmessage = gpo.po_next_message(self._gpo_message_iterator) |
| 668 while newmessage: | 668 while newmessage: |
| 669 newunit = pounit(gpo_message=newmessage) | 669 newunit = pounit(gpo_message=newmessage) |
| 670 self.units.append(newunit) | 670 self.units.append(newunit) |
| 671 newmessage = gpo.po_next_message(self._gpo_message_iterator) | 671 newmessage = gpo.po_next_message(self._gpo_message_iterator) |
| 672 self._free_iterator() | 672 self._free_iterator() |
| 673 | 673 |
| 674 def __del__(self): | 674 def __del__(self): |
| 675 # We currently disable this while we still get segmentation faults. | 675 # We currently disable this while we still get segmentation faults. |
| 676 # Note that this is definitely leaking memory because of this. | 676 # Note that this is definitely leaking memory because of this. |
| 677 return | 677 return |
| 678 self._free_iterator() | 678 self._free_iterator() |
| 679 if self._gpo_memory_file is not None: | 679 if self._gpo_memory_file is not None: |
| 680 gpo.po_file_free(self._gpo_memory_file) | 680 gpo.po_file_free(self._gpo_memory_file) |
| 681 self._gpo_memory_file = None | 681 self._gpo_memory_file = None |
| 682 | 682 |
| 683 def _free_iterator(self): | 683 def _free_iterator(self): |
| 684 # We currently disable this while we still get segmentation faults. | 684 # We currently disable this while we still get segmentation faults. |
| 685 # Note that this is definitely leaking memory because of this. | 685 # Note that this is definitely leaking memory because of this. |
| 686 return | 686 return |
| 687 if self._gpo_message_iterator is not None: | 687 if self._gpo_message_iterator is not None: |
| 688 gpo.po_message_iterator_free(self._gpo_message_iterator) | 688 gpo.po_message_iterator_free(self._gpo_message_iterator) |
| 689 self._gpo_message_iterator = None | 689 self._gpo_message_iterator = None |
| LEFT | RIGHT |