fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / storage / odf.py
blob41618bb60a7581356680c9205db14713a6901a83
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2007 Zuza Software Foundation
5 #
6 # This file is part of translate.
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.
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.
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
22 """This class implements the functionality for handling OpenDocument files.
23 """
25 from translate.misc import xmlwrapper
26 from translate.storage import base
27 import zipfile
29 class ODFUnit(base.TranslationUnit):
30 """This class represents an OpenDocument translatable snippet"""
32 class ODFFile(base.TranslationStore, xmlwrapper.XMLWrapper):
33 """This class represents an OpenDocument file"""
34 UnitClass = ODFUnit
35 def __init__(self, inputfile):
36 base.TranslationStore.__init__(self, unitclass=self.UnitClass)
37 self.filename = getattr(inputfile, 'name', '')
38 try:
39 z = zipfile.ZipFile(self.filename, 'r')
40 contents = z.read("content.xml")
41 except (ValueError, zipfile.BadZipfile):
42 contents = open(self.filename, 'r').read()
43 root = xmlwrapper.BuildTree(contents)
44 xmlwrapper.XMLWrapper.__init__(self, root)
45 if self.tag != "document-content": raise ValueError("root %r != 'document-content'" % self.tag)
46 self.body = self.getchild("body")
48 def excludeiterator(self, obj, excludetags):
49 nodes = []
50 for node in obj._children:
51 if xmlwrapper.splitnamespace(node.tag)[1] not in excludetags:
52 nodes.append(node)
53 nodes.extend(self.excludeiterator(node, excludetags))
54 return nodes
56 def getunits(self):
57 nodes = self.excludeiterator(self.body.obj, ["tracked-changes"])
58 paragraphs = []
59 for node in nodes:
60 childns, childtag = xmlwrapper.splitnamespace(node.tag)
61 if childtag == "p" or childtag == "h":
62 paragraphs.append(xmlwrapper.XMLWrapper(node))
63 for child in paragraphs:
64 text = child.gettexts().strip()
65 self.addsourceunit(text)
66 return self.units