[Python-modules-commits] [python-odf] 96/118: Work on loading subobjects

Wolfgang Borgert debacle at moszumanska.debian.org
Fri Oct 3 21:27:28 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to reference refs/remotes/upstream/master
in repository python-odf.

commit 87606d3e7253ce5b6c1ba7ed896e178ab13c58a5
Author: Søren Roug <soren.roug at eea.europa.eu>
Date:   Sun Aug 15 16:16:10 2010 +0000

    Work on loading subobjects
---
 odf/odf2xhtml.py    | 15 +++++------
 odf/opendocument.py | 71 +++++++++++++++++++++++++++++++----------------------
 tests/testload.py   | 26 ++++++++++++++++++++
 3 files changed, 75 insertions(+), 37 deletions(-)

diff --git a/odf/odf2xhtml.py b/odf/odf2xhtml.py
index e4c6d9e..7de139d 100644
--- a/odf/odf2xhtml.py
+++ b/odf/odf2xhtml.py
@@ -730,13 +730,14 @@ class ODF2XHTML(handler.ContentHandler):
         """
         objhref = attrs[(XLINKNS,"href")]
         # Remove leading "./": from "./Object 1" to "Object 1"
-        objhref = objhref [2:]
+#       objhref = objhref [2:]
        
         # Not using os.path.join since it fails to find the file on Windows.
-        objcontentpath = '/'.join([objhref, 'content.xml'])
+#       objcontentpath = '/'.join([objhref, 'content.xml'])
 
-        #FIXME: There is no parsexml method any longer.
-        self.parsexml(contenthandler=ODF2XHTMLembedded(self.lines), xmlpathlist=[objcontentpath])
+        for c in self.document.childnodes:
+            if c.folder == objhref:
+                self._walknode(c.topnode)
 
     def s_draw_object_ole(self, tag, attrs):
         """ A <draw:object-ole> is embedded OLE object in the document (e.g. MS Graph).
@@ -1398,10 +1399,10 @@ ol, ul { padding-left: 2em; }
         self.lines = []
         self._wfunc = self._wlines
         if isinstance(odffile, basestring):
-            doc = load(odffile)
+            self.document = load(odffile)
         else:
-            doc = odffile
-        self._walknode(doc.topnode)
+            self.document = odffile
+        self._walknode(self.document.topnode)
 
     def _walknode(self, node):
         if node.nodeType == Node.ELEMENT_NODE:
diff --git a/odf/opendocument.py b/odf/opendocument.py
index 3622ec2..e219d8c 100644
--- a/odf/opendocument.py
+++ b/odf/opendocument.py
@@ -128,12 +128,12 @@ class OpenDocument:
             self.element_dict[element.qname] = []
         self.element_dict[element.qname].append(element)
         if element.qname == (STYLENS, u'style'):
-            self._register_stylename(element) # Add to style dictionary
+            self.__register_stylename(element) # Add to style dictionary
         styleref = element.getAttrNS(TEXTNS,u'style-name')
         if styleref is not None and self._styles_ooo_fix.has_key(styleref):
             element.setAttrNS(TEXTNS,u'style-name', self._styles_ooo_fix[styleref])
 
-    def _register_stylename(self, element):
+    def __register_stylename(self, element):
         ''' Register a style. But there are three style dictionaries:
             office:styles, office:automatic-styles and office:master-styles
             Chapter 14
@@ -165,7 +165,7 @@ class OpenDocument:
         """ Generates the full document as an XML file
             Always written as a bytestream in UTF-8 encoding
         """
-        self._replaceGenerator()
+        self.__replaceGenerator()
         xml=StringIO()
         xml.write(_XMLPROLOGUE)
         self.topnode.toXml(0, xml)
@@ -197,8 +197,10 @@ class OpenDocument:
         x.write_close_tag(0, xml)
         return xml.getvalue()
 
-    def manifestxml(self):
-        """ Generates the manifest.xml file """
+    def __manifestxml(self):
+        """ Generates the manifest.xml file
+            The self.manifest isn't avaible unless the document is being saved
+        """
         xml=StringIO()
         xml.write(_XMLPROLOGUE)
         self.manifest.toXml(0,xml)
@@ -206,7 +208,7 @@ class OpenDocument:
 
     def metaxml(self):
         """ Generates the meta.xml file """
-        self._replaceGenerator()
+        self.__replaceGenerator()
         x = DocumentMeta()
         x.addElement(self.meta)
         xml=StringIO()
@@ -368,14 +370,14 @@ class OpenDocument:
                 zi.external_attr = UNIXPERMS
                 self._z.writestr(zi, fileobj)
         if hasPictures:
-            self.manifest.addElement(manifest.FileEntry(fullpath="%sPictures/" % folder,mediatype=""))
+            self.manifest.addElement(manifest.FileEntry(fullpath="%sPictures/" % folder, mediatype=""))
         # Look in subobjects
         subobjectnum = 1
         for subobject in object.childobjects:
             self._savePictures(subobject,'%sObject %d/' % (folder, subobjectnum))
             subobjectnum += 1
 
-    def _replaceGenerator(self):
+    def __replaceGenerator(self):
         """ Section 3.1.1: The application MUST NOT export the original identifier
             belonging to the application that created the document.
         """
@@ -394,7 +396,7 @@ class OpenDocument:
             if addsuffix:
                 outputfile = outputfile + odmimetypes.get(self.mimetype,'.xxx')
             outputfp = zipfile.ZipFile(outputfile, "w")
-        self._zipwrite(outputfp)
+        self.__zipwrite(outputfp)
         outputfp.close()
 
     def write(self, outputfp):
@@ -402,9 +404,9 @@ class OpenDocument:
             Writes the ZIP format
         """
         zipoutputfp = zipfile.ZipFile(outputfp,"w")
-        self._zipwrite(zipoutputfp)
+        self.__zipwrite(zipoutputfp)
 
-    def _zipwrite(self, outputfp):
+    def __zipwrite(self, outputfp):
         """ Write the document to an open file pointer
             This is where the real work is done
         """
@@ -445,7 +447,7 @@ class OpenDocument:
         zi = zipfile.ZipInfo("META-INF/manifest.xml", self._now)
         zi.compress_type = zipfile.ZIP_DEFLATED
         zi.external_attr = UNIXPERMS
-        self._z.writestr(zi, self.manifestxml() )
+        self._z.writestr(zi, self.__manifestxml() )
         del self._z
         del self._now
         del self.manifest
@@ -471,8 +473,8 @@ class OpenDocument:
         self._z.writestr(zi, object.contentxml() )
 
         # Write settings
-        if self == object and self.settings.hasChildNodes():
-            self.manifest.addElement(manifest.FileEntry(fullpath="settings.xml",mediatype="text/xml"))
+        if object.settings.hasChildNodes():
+            self.manifest.addElement(manifest.FileEntry(fullpath="%ssettings.xml" % folder, mediatype="text/xml"))
             zi = zipfile.ZipInfo("%ssettings.xml" % folder, self._now)
             zi.compress_type = zipfile.ZIP_DEFLATED
             zi.external_attr = UNIXPERMS
@@ -480,7 +482,7 @@ class OpenDocument:
 
         # Write meta
         if self == object:
-            self.manifest.addElement(manifest.FileEntry(fullpath="meta.xml",mediatype="text/xml"))
+            self.manifest.addElement(manifest.FileEntry(fullpath="meta.xml", mediatype="text/xml"))
             zi = zipfile.ZipInfo("meta.xml", self._now)
             zi.compress_type = zipfile.ZIP_DEFLATED
             zi.external_attr = UNIXPERMS
@@ -575,20 +577,11 @@ def OpenDocumentTextMaster():
     doc.body.addElement(doc.text)
     return doc
 
-def load(odffile):
-    """ Load an ODF file into memory
-        Returns a reference to the structure
-    """
+def __loadxmlparts(z, manifest, doc, objectpath):
     from load import LoadParser
     from xml.sax import make_parser, handler
-    z = zipfile.ZipFile(odffile)
-    mimetype = z.read('mimetype')
-    doc = OpenDocument(mimetype, add_generator=False)
 
-    # Look in the manifest file to see if which of the four files there are
-    manifestpart = z.read('META-INF/manifest.xml')
-    manifest =  manifestlist(manifestpart)
-    for xmlfile in ('settings.xml', 'meta.xml', 'content.xml', 'styles.xml'):
+    for xmlfile in (objectpath+'settings.xml', objectpath+'meta.xml', objectpath+'content.xml', objectpath+'styles.xml'):
         if not manifest.has_key(xmlfile):
             continue
         try:
@@ -605,7 +598,19 @@ def load(odffile):
             parser.parse(inpsrc)
             del doc._parsing
         except KeyError, v: pass
-    # FIXME: Add subobjects correctly here
+
+def load(odffile):
+    """ Load an ODF file into memory
+        Returns a reference to the structure
+    """
+    z = zipfile.ZipFile(odffile)
+    mimetype = z.read('mimetype')
+    doc = OpenDocument(mimetype, add_generator=False)
+
+    # Look in the manifest file to see if which of the four files there are
+    manifestpart = z.read('META-INF/manifest.xml')
+    manifest =  manifestlist(manifestpart)
+    __loadxmlparts(z, manifest, doc, '')
     for mentry,mvalue in manifest.items():
         if mentry[:9] == "Pictures/" and len(mentry) > 9:
             doc.addPicture(mvalue['full-path'], mvalue['media-type'], z.read(mentry))
@@ -613,9 +618,14 @@ def load(odffile):
             doc.addThumbnail(z.read(mentry))
         elif mentry in ('settings.xml', 'meta.xml', 'content.xml', 'styles.xml'):
             pass
-#FIXME: Load subobjects into structure
-#       elif mentry[:7] == "Object " and len(mentry) > 8:
-#           self.childobjects ...
+        # Load subobjects into structure
+        elif mentry[:7] == "Object " and len(mentry) < 11 and mentry[-1] == "/":
+            mimetype = mvalue['media-type']
+            subdoc = OpenDocument(mimetype, add_generator=False)
+            doc.addObject(subdoc, "/" + mentry[:-1])
+            __loadxmlparts(z, manifest, subdoc, mentry)
+        elif mentry[:7] == "Object ":
+            pass # Don't load subobjects as opaque objects
         else:
             if mvalue['full-path'][-1] == '/':
                 doc._extra.append(OpaqueObject(mvalue['full-path'], mvalue['media-type'], None))
@@ -640,4 +650,5 @@ def load(odffile):
     elif mimetype[:42] == 'application/vnd.oasis.opendocument.formula':
         doc.formula = b[0].firstChild
     return doc
+
 # vim: set expandtab sw=4 :
diff --git a/tests/testload.py b/tests/testload.py
index ca81cdb..d084c48 100644
--- a/tests/testload.py
+++ b/tests/testload.py
@@ -142,5 +142,31 @@ class TestExampleDocs(unittest.TestCase):
         self.assertNotEqual(-1, result.find(u'''table:formula="=SQRT([.A1]*[.A1]+[.A2]*[.A2])"'''))
         self.assertNotEqual(-1, result.find(u'''table:formula="=SUM([.A1]:[.A2])"'''))
 
+class TestExampleDocs(unittest.TestCase):
+
+    def test_metagenerator(self):
+        """ Check that meta:generator is the original one """
+        parastyles_odt = os.path.join(
+            os.path.dirname(__file__), "examples", "emb_spreadsheet.odp")
+        d = load(parastyles_odt)
+        meta = unicode(d.metaxml(),'utf-8')
+        self.assertNotEqual(-1, meta.find(u"""<meta:generator>ODFPY"""), "Must not use the original generator string")
+
+
+    def test_spreadsheet(self):
+        """ Load a document containing subobjects """
+        spreadsheet_odt = os.path.join(
+            os.path.dirname(__file__), "examples", "emb_spreadsheet.odp")
+        d = load(spreadsheet_odt)
+        self.assertEqual(1, len(d.childobjects))
+        for s in d.childobjects:
+            print s.folder
+#        mani = unicode(d.manifestxml(),'utf-8')
+#        self.assertNotEqual(-1, mani.find(u''' manifest:full-path="Object 1/"'''), "Must contain the subobject")
+#        self.assertNotEqual(-1, mani.find(u''' manifest:full-path="Object 1/settings.xml"'''), "Must contain the subobject settings.xml")
+
+#        d.save("subobject.odp")
+
+
 if __name__ == '__main__':
     unittest.main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-odf.git



More information about the Python-modules-commits mailing list