translate.storage.placeables.terminology

Package translate :: Package storage :: Package placeables :: Module terminology |
[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.placeables.terminology

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2009 Zuza Software Foundation
      5  #
      6  # This file is part of the Translate Toolkit.
      7  #
      8  # This program is free software; you can redistribute it and/or modify
      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
     11  # (at your option) any later version.
     12  #
     13  # This program is distributed in the hope that it will be useful,
     14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  # GNU General Public License for more details.
     17  #
     18  # You should have received a copy of the GNU General Public License
     19  # along with this program; if not, see <http://www.gnu.org/licenses/>.
     20
     21  """
     22  Contains the placeable that represents a terminology term.
     23  """
     24
     25  from translate.storage.placeables import base, StringElem
     26
     27  __all__ = ['TerminologyPlaceable', 'parsers']
     28
     29
     30 -class TerminologyPlaceable(base.Ph):
     31      """Terminology distinguished from the rest of a string by being a placeable."""
     32
     33      matchers = []
     34      """A list of matcher objects to use to identify terminology."""
     35      translations = []
     36      """The available translations for this placeable."""
     37
     38 -    def __init__(self, *args, **kwargs):
     39          self.translations = []
     40          super(TerminologyPlaceable, self).__init__(*args, **kwargs)
     41
     42      @classmethod
     43 -    def parse(cls, pstr):
     44          parts = []
     45          matches = []
     46          match_info = {}
     47
     48          for matcher in cls.matchers:
     49              matches.extend(matcher.matches(pstr))
     50              match_info.update(matcher.match_info)
     51
     52          lastend = 0
     53
     54          def sort_matches(x, y):
     55              # This function will sort a list of matches according to the match's starting
     56              # position, putting the one with the longer source text first, if two are the same.
     57              c = cmp(match_info[x.source]['pos'], match_info[y.source]['pos'])
     58              return c and c or cmp(len(y.source), len(x.source))
     59          matches.sort(sort_matches)
     60
     61          for match in matches:
     62              info = match_info[match.source]
     63              if info['pos'] < lastend:
     64                  continue
     65              end = info['pos'] + len(match.source)
     66              if 'newtermlen' in info:
     67                  end = info['pos'] + info['newtermlen']
     68
     69              if lastend < info['pos']:
     70                  parts.append(StringElem(pstr[lastend:info['pos']]))
     71
     72              term_string = pstr[info['pos']:end]
     73              term_placeable = cls([term_string])
     74              parts.append(term_placeable)
     75
     76              # Get translations for the placeable
     77              for m in matches:
     78                  m_info = match_info[m.source]
     79                  m_end = m_info['pos']
     80                  if 'newtermlen' in m_info:
     81                      m_end += m_info['newtermlen']
     82                  else:
     83                      m_end += len(m.source)
     84                  if info['pos'] == m_info['pos'] and end == m_end:
     85                      term_placeable.translations.append(m.target)
     86
     87              # remove duplicates:
     88              term_placeable.translations = list(set(term_placeable.translations))
     89
     90              lastend = end
     91          if lastend != len(pstr) and parts:
     92              parts.append(StringElem(pstr[lastend:]))
     93
     94          return parts or None
     95
     96 -    def translate(self):
     97          return self.translations and self.translations[0] or super(TerminologyPlaceable, self).translate()
     98
     99
    100  parsers = [TerminologyPlaceable.parse]
    101