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

Source Code for Module translate.storage.qph

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2008 Zuza Software Foundation 
  5  #  
  6  # This file is part of translate. 
  7  # 
  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 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  #  
 13  # translate 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 translate; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 21  # 
 22   
 23  """Module for handling Qt Phrase Book (.qph) files. 
 24   
 25  Extract from the U{Qt Linguist Manual: 
 26  Translators<http://doc.trolltech.com/4.3/linguist-translators.html>}: 
 27  .qph Qt Phrase Book Files are human-readable XML files containing standard 
 28  phrases and their translations. These files are created and updated by Qt 
 29  Linguist and may be used by any number of projects and applications. 
 30   
 31  A DTD to define the format does not seem to exist, but the following U{code 
 32  <http://www.google.com/codesearch?hl=en&q=show:gtsFsbhpVeE:KeGnQG0wDCQ:xOXsNYqccyE&sa=N&ct=rd&cs_p=ftp://ftp.trolltech.com/qt/source/qt-x11-opensource-4.0.0-b1.tar.gz&cs_f=qt-x11-opensource-4.0.0-b1/tools/linguist/linguist/phrase.cpp>}  
 33  provides the reference implementation for the Qt Linguist product. 
 34  """ 
 35   
 36  from translate.storage import lisa 
 37  from lxml import etree 
 38   
39 -class QphUnit(lisa.LISAunit):
40 """A single term in the qph file.""" 41 42 rootNode = "phrase" 43 languageNode = "source" 44 textNode = "" 45 namespace = '' 46
47 - def createlanguageNode(self, lang, text, purpose):
48 """Returns an xml Element setup with given parameters.""" 49 assert purpose 50 langset = etree.Element(self.namespaced(purpose)) 51 langset.text = text 52 return langset
53
54 - def _getsourcenode(self):
55 return self.xmlelement.find(self.namespaced(self.languageNode))
56
57 - def _gettargetnode(self):
58 return self.xmlelement.find(self.namespaced("target"))
59
60 - def getlanguageNodes(self):
61 """We override this to get source and target nodes.""" 62 def not_none(node): 63 return not node is None
64 return filter(not_none, [self._getsourcenode(), self._gettargetnode()])
65
66 - def addnote(self, text, origin=None):
67 """Add a note specifically in a "definition" tag""" 68 assert isinstance(text, unicode) 69 current_notes = self.getnotes(origin) 70 self.removenotes() 71 note = etree.SubElement(self.xmlelement, self.namespaced("definition")) 72 note.text = "\n".join(filter(None, [current_notes, text.strip()]))
73
74 - def getnotes(self, origin=None):
75 #TODO: consider only responding when origin has certain values 76 notenode = self.xmlelement.find(self.namespaced("definition")) 77 comment = '' 78 if not notenode is None: 79 comment = notenode.text 80 return comment
81
82 - def removenotes(self):
83 """Remove all the translator notes.""" 84 note = self.xmlelement.find(self.namespaced("definition")) 85 if not note is None: 86 self.xmlelement.remove(note)
87 88
89 -class QphFile(lisa.LISAfile):
90 """Class representing a QPH file store.""" 91 UnitClass = QphUnit 92 Name = _("Qt Phrase Book") 93 Mimetypes = ["application/x-qph"] 94 Extensions = ["qph"] 95 rootNode = "QPH" 96 bodyNode = "QPH" 97 XMLskeleton = '''<!DOCTYPE QPH> 98 <QPH> 99 </QPH> 100 ''' 101 namespace = '' 102
103 - def initbody(self):
104 """Initialises self.body so it never needs to be retrieved from the XML again.""" 105 self.namespace = self.document.getroot().nsmap.get(None, None) 106 self.body = self.document.getroot() # The root node contains the units
107
108 - def __str__(self):
109 """Converts to a string containing the file's XML. 110 111 We have to override this to ensure mimic the Qt convention: 112 - no XML decleration 113 - plain DOCTYPE that lxml seems to ignore 114 """ 115 # A bug in lxml means we have to output the doctype ourselves. For 116 # more information, see: 117 # http://codespeak.net/pipermail/lxml-dev/2008-October/004112.html 118 # The problem was fixed in lxml 2.1.3 119 output = etree.tostring(self.document, pretty_print=True, 120 xml_declaration=False, encoding='utf-8') 121 if not "<!DOCTYPE QPH>" in output[:30]: 122 output = "<!DOCTYPE QPH>" + output 123 return output
124