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  __all__ = ['TerminologyPlaceable', 'parsers']
     26
     27  from translate.storage.placeables import base, StringElem
     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          def sort_matches(x, y):
     54              # This function will sort a list of matches according to the match's starting
     55              # position, putting the one with the longer source text first, if two are the same.
     56              c = cmp(match_info[x.source]['pos'], match_info[y.source]['pos'])
     57              return c and c or cmp(len(y.source), len(x.source))
     58          matches.sort(sort_matches)
     59
     60          for match in matches:
     61              info = match_info[match.source]
     62              if info['pos'] < lastend:
     63                  continue
     64              end = info['pos'] + len(match.source)
     65              if 'newtermlen' in info:
     66                  end = info['pos'] + info['newtermlen']
     67
     68              if lastend < info['pos']:
     69                  parts.append(StringElem(pstr[lastend:info['pos']]))
     70
     71              term_string = pstr[info['pos']:end]
     72              term_placeable = cls([term_string])
     73              parts.append(term_placeable)
     74
     75              # Get translations for the placeable
     76              for m in matches:
     77                  m_info = match_info[m.source]
     78                  m_end = m_info['pos']
     79                  if 'newtermlen' in m_info:
     80                      m_end += m_info['newtermlen']
     81                  else:
     82                      m_end += len(m.source)
     83                  if info['pos'] == m_info['pos'] and end == m_end:
     84                      term_placeable.translations.append(m.target)
     85
     86              # remove duplicates:
     87              term_placeable.translations = list(set(term_placeable.translations))
     88
     89              lastend = end
     90          if lastend != len(pstr):
     91              parts.append(StringElem(pstr[lastend:]))
     92
     93          return parts or None
     94
     95 -    def translate(self):
     96          return self.translations and self.translations[0] or super(TerminologyPlaceable, self).translate()
     97
     98
     99  parsers = [TerminologyPlaceable.parse]
    100