Package translate :: Package storage :: Package placeables :: Module xliff

[hide private]
[frames] | no frames]

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

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2008-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  """Contains XLIFF-specific placeables."""
     22
     23  from translate.storage.placeables import base
     24  from translate.storage.placeables.strelem import StringElem
     25
     26  __all__ = ['Bpt', 'Ept', 'X', 'Bx', 'Ex', 'G', 'It', 'Sub', 'Ph', 'UnknownXML', 'parsers', 'to_xliff_placeables']
     27
     28
     29 -class Bpt(base.Bpt):
     30      pass
     31
     32
     33 -class Ept(base.Ept):
     34      pass
     35
     36
     37 -class Ph(base.Ph):
     38      pass
     39
     40
     41 -class It(base.It):
     42      pass
     43
     44
     45 -class G(base.G):
     46      pass
     47
     48
     49 -class Bx(base.Bx):
     50      pass
     51
     52
     53 -class Ex(base.Ex):
     54      pass
     55
     56
     57 -class X(base.X):
     58      pass
     59
     60
     61 -class Sub(base.Sub):
     62      pass
     63
     64
     65 -class UnknownXML(StringElem):
     66      """Placeable for unrecognized or umimplemented XML nodes. It's main
     67          purpose is to preserve all associated XML data."""
     68      iseditable = True
     69
     70      # INITIALIZERS #
     71 -    def __init__(self, sub=None, id=None, rid=None, xid=None, xml_node=None, **kwargs):
     72          super(UnknownXML, self).__init__(sub=sub, id=id, rid=rid, xid=xid, **kwargs)
     73          if xml_node is None:
     74              raise ValueError('xml_node must be a lxml node')
     75          self.xml_node = xml_node
     76
     77          if sub:
     78              self.has_content = True
     79
     80
     81      # SPECIAL METHODS #
     82 -    def __repr__(self):
     83          """String representation of the sub-tree with the current node as the
     84              root.
     85
     86              Copied from L{StringElem.__repr__}, but includes C{self.xml_node.tag}."""
     87          tag = self.xml_node.tag
     88          if tag.startswith('{'):
     89              tag = tag[tag.index('}')+1:]
     90
     91          elemstr = ', '.join([repr(elem) for elem in self.sub])
     92
     93          return '<%(class)s{%(tag)s}(%(id)s%(rid)s%(xid)s[%(subs)s])>' % {
     94              'class': self.__class__.__name__,
     95              'tag': tag,
     96              'id': self.id  is not None and 'id="%s" ' % (self.id) or '',
     97              'rid': self.rid is not None and 'rid="%s" ' % (self.rid) or '',
     98              'xid': self.xid is not None and 'xid="%s" ' % (self.xid) or '',
     99              'subs': elemstr,
    100          }
    101
    102
    103      # METHODS #
    104 -    def copy(self):
    105          """Returns a copy of the sub-tree.
    106              This should be overridden in sub-classes with more data.
    107
    108              NOTE: C{self.renderer} is B{not} copied."""
    109          from copy import copy
    110          cp = self.__class__(id=self.id, rid=self.rid, xid=self.xid, xml_node=copy(self.xml_node))
    111          for sub in self.sub:
    112              if isinstance(sub, StringElem):
    113                  cp.sub.append(sub.copy())
    114              else:
    115                  cp.sub.append(sub.__class__(sub))
    116          return cp
    117
    118
    119 -def to_xliff_placeables(tree):
    120      if not isinstance(tree, StringElem):
    121          return tree
    122
    123      newtree = None
    124
    125      classmap = {
    126          base.Bpt: Bpt,
    127          base.Ept: Ept,
    128          base.Ph: Ph,
    129          base.It: It,
    130          base.G: G,
    131          base.Bx: Bx,
    132          base.Ex: Ex,
    133          base.X: X,
    134          base.Sub: Sub,
    135      }
    136      for baseclass, xliffclass in classmap.items():
    137          if isinstance(tree, baseclass):
    138              newtree = xliffclass()
    139
    140      if newtree is None:
    141          newtree = tree.__class__()
    142
    143      newtree.id = tree.id
    144      newtree.rid = tree.rid
    145      newtree.xid = tree.xid
    146      newtree.sub = []
    147
    148      for subtree in tree.sub:
    149          newtree.sub.append(to_xliff_placeables(subtree))
    150
    151      return newtree
    152
    153
    154  parsers = []
    155