[Python-modules-commits] [python-odf] 36/118: Keep the extra files when loading and saving

Wolfgang Borgert debacle at moszumanska.debian.org
Fri Oct 3 21:27:19 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 07387ff8e9529891c3a664e18fd3615c1f71e3de
Author: Søren Roug <soren.roug at eea.europa.eu>
Date:   Tue Jan 13 21:24:28 2009 +0000

    Keep the extra files when loading and saving
---
 grammar/OpenDocument-schema-v1.1.rng |  3 +++
 grammar/gen_allowed_attrs.py         |  8 +++----
 grammar/test_grammar.py              |  2 +-
 odf/grammar.py                       | 45 +++++++++++++++++++++---------------
 odf/opendocument.py                  | 33 +++++++++++++++++++++-----
 tests/teststyles.py                  |  4 ++--
 6 files changed, 64 insertions(+), 31 deletions(-)

diff --git a/grammar/OpenDocument-schema-v1.1.rng b/grammar/OpenDocument-schema-v1.1.rng
index 60f0d0c..32ee051 100644
--- a/grammar/OpenDocument-schema-v1.1.rng
+++ b/grammar/OpenDocument-schema-v1.1.rng
@@ -556,6 +556,9 @@
                 <optional>
                     <ref name="draw-layer-set"/>
                 </optional>
+                <optional>
+                    <ref name="table-table-template"/>
+                </optional>
             </interleave>
         </element>
     </optional>
diff --git a/grammar/gen_allowed_attrs.py b/grammar/gen_allowed_attrs.py
index 21662ea..bbf0471 100755
--- a/grammar/gen_allowed_attrs.py
+++ b/grammar/gen_allowed_attrs.py
@@ -72,10 +72,10 @@ class S22RelaxParser(handler.ContentHandler):
             self.elements[(currelement.ns, currelement.name)] = currelement
             currelement = None
         elif tag == (RELAXNS, 'attribute'):
-            currelement.attrs[(currnode.ns, currnode.name)] = currnode
             currnode = None
         elif tag == (RELAXNS, 'name'):
             currnode.name = self.text()
+            currelement.attrs[(currnode.ns, currnode.name)] = currnode
         elif tag == (RELAXNS, 'anyName'):
             currnode.name = "__ANYNAME__"
         self.data = []
@@ -102,11 +102,11 @@ if __name__ == "__main__":
         if e.name == u'__ANYNAME__':
             continue
         print "# allowed_attributes"
-        if len(e.attrs.values()) == 1 and e.attrs.values()[0].name == u'__ANYNAME__':
+        if len(e.attrs.keys()) == 1 and e.attrs.values()[0].name == u'__ANYNAME__':
             print "\t(%sNS,u'%s'): None," % (nsdict.get(e.ns,'unknown').upper(), e.name)
         else:
             print "\t(%sNS,u'%s'):(" % (nsdict.get(e.ns,'unknown').upper(), e.name)
-            for a in e.attrs.values():
-                print "\t\t(%sNS,u'%s')," % (nsdict.get(a.ns,'unknown').upper(), a.name)
+            for a in e.attrs.keys():
+                print "\t\t(%sNS,u'%s')," % (nsdict.get(a[0],'unknown').upper(), a[1])
             print "\t),"
     print "}"
diff --git a/grammar/test_grammar.py b/grammar/test_grammar.py
index 29e6ca8..6e150f9 100644
--- a/grammar/test_grammar.py
+++ b/grammar/test_grammar.py
@@ -67,7 +67,7 @@ class TestRequiredAtts(unittest.TestCase):
             assert element in grammarnew.required_attributes, "%s:%s not in new grammar" % element
             newatts = grammarnew.required_attributes[element]
             for attr in atts:
-                assert attr in newatts, "Attribute %s:%s not in new grammar" % attr
+                assert attr in newatts, "Attribute %s:%s not in new grammar for %s:%s" % (attr + element)
 
 class TestAllowedChildren(unittest.TestCase):
     def testAllowedChildrenNew(self):
diff --git a/odf/grammar.py b/odf/grammar.py
index 03a8948..c4a70ad 100644
--- a/odf/grammar.py
+++ b/odf/grammar.py
@@ -3418,7 +3418,6 @@ required_attributes = {
 	(DRAWNS,u'glue-point'): (
 		(SVGNS,u'y'),
 		(SVGNS,u'x'),
-		(DRAWNS,u'align'),
 		(DRAWNS,u'id'),
 	),
 	(DRAWNS,u'gradient'): (
@@ -4312,37 +4311,47 @@ allowed_attributes = {
 		(ANIMNS,u'value'),
 	),
 	(ANIMNS,u'seq'):(
-		(PRESENTATIONNS,u'node-type'),
-		(SMILNS,u'decelerate'),
-		(SMILNS,u'begin'),
-		(SMILNS,u'end'),
+		(ANIMNS,u'id'),
 		(PRESENTATIONNS,u'group-id'),
-		(SMILNS,u'accelerate'),
-		(SMILNS,u'repeatDur'),
-		(SMILNS,u'endsync'),
-		(SMILNS,u'restartDefault'),
+		(PRESENTATIONNS,u'master-element'),
+		(PRESENTATIONNS,u'node-type'),
 		(PRESENTATIONNS,u'preset-class'),
-		(SMILNS,u'fillDefault'),
 		(PRESENTATIONNS,u'preset-id'),
-		(SMILNS,u'autoReverse'),
 		(PRESENTATIONNS,u'preset-sub-type'),
-		(SMILNS,u'repeatCount'),
+		(SMILNS,u'accelerate'),
+		(SMILNS,u'autoReverse'),
+		(SMILNS,u'begin'),
+		(SMILNS,u'decelerate'),
 		(SMILNS,u'dur'),
+		(SMILNS,u'end'),
+		(SMILNS,u'endsync'),
 		(SMILNS,u'fill'),
-		(ANIMNS,u'id'),
+		(SMILNS,u'fillDefault'),
+		(SMILNS,u'repeatCount'),
+		(SMILNS,u'repeatDur'),
 		(SMILNS,u'restart'),
-		(PRESENTATIONNS,u'master-element'),
+		(SMILNS,u'restartDefault'),
 	),
 	(ANIMNS,u'set'):(
+		(ANIMNS,u'sub-item'),
 		(SMILNS,u'accelerate'),
+		(SMILNS,u'accumulate'),
+		(SMILNS,u'autoReverse'),
 		(SMILNS,u'additive'),
 		(SMILNS,u'attributeName'),
+		(SMILNS,u'begin'),
 		(SMILNS,u'decelerate'),
-		(SMILNS,u'to'),
-		(ANIMNS,u'sub-item'),
-		(SMILNS,u'targetElement'),
-		(SMILNS,u'accumulate'),
+		(SMILNS,u'dur'),
+		(SMILNS,u'end'),
 		(SMILNS,u'fill'),
+		(SMILNS,u'fillDefault'),
+		(SMILNS,u'repeatCount'),
+		(SMILNS,u'repeatDur'),
+		(SMILNS,u'restart'),
+		(SMILNS,u'restartDefault'),
+		(SMILNS,u'targetElement'),
+		(SMILNS,u'to'),
+
 	),
 	(ANIMNS,u'transitionFilter'):(
 		(SMILNS,u'direction'),
diff --git a/odf/opendocument.py b/odf/opendocument.py
index 784ff70..6b2e16b 100644
--- a/odf/opendocument.py
+++ b/odf/opendocument.py
@@ -64,6 +64,12 @@ odmimetypes = {
  'application/vnd.oasis.opendocument.text-web':              '.oth',
 }
 
+class OpaqueObject:
+    def __init__(self, filename, mediatype, content=None):
+       self.mediatype = mediatype
+       self.filename = filename
+       self.content = content
+
 class OpenDocument:
     """ A class to hold the content of an OpenDocument document
         Use the xml method to write the XML
@@ -76,6 +82,7 @@ class OpenDocument:
     def __init__(self, mimetype, add_generator=True):
         self.mimetype = mimetype
         self.childobjects = []
+        self._extra = []
         self.folder = "" # Always empty for toplevel documents
         self.topnode = Document(mimetype=self.mimetype)
         self.topnode.ownerDocument = self
@@ -303,12 +310,15 @@ class OpenDocument:
         else:
             self.thumbnail = filecontent
 
-    def addObject(self, document):
+    def addObject(self, document, objectname=None):
         """ Add an object. The object must be an OpenDocument class
             The return value will be the folder in the zipfile the object is stored in
         """
         self.childobjects.append(document)
-        document.folder = "%s/Object %d" % (self.folder, len(self.childobjects))
+        if objectname is None:
+            document.folder = "%s/Object %d" % (self.folder, len(self.childobjects))
+        else:
+            document.folder = objectname
         return ".%s" % document.folder
 
     def _savePictures(self, object, folder):
@@ -348,7 +358,7 @@ class OpenDocument:
         else:
             if addsuffix:
                 outputfile = outputfile + odmimetypes.get(self.mimetype,'.xxx')
-            outputfp = zipfile.ZipFile(outputfile,"w")
+            outputfp = zipfile.ZipFile(outputfile, "w")
         self._zipwrite(outputfp)
         outputfp.close()
 
@@ -382,6 +392,14 @@ class OpenDocument:
             zi.external_attr = UNIXPERMS
             self._z.writestr(zi, self.thumbnail)
 
+        # Write any extra files
+        for op in self._extra:
+            self.manifest.addElement(manifest.FileEntry(fullpath=op.filename, mediatype=op.mediatype))
+            zi = zipfile.ZipInfo(op.filename.encode('utf-8'), self._now)
+            zi.compress_type = zipfile.ZIP_DEFLATED
+            zi.external_attr = UNIXPERMS
+            if op.content is not None:
+                self._z.writestr(zi, op.content)
         # Write manifest
         zi = zipfile.ZipInfo("META-INF/manifest.xml", self._now)
         zi.compress_type = zipfile.ZIP_DEFLATED
@@ -528,15 +546,18 @@ def load(odffile):
             parser.parse(inpsrc)
             del doc._parsing
         except KeyError, v: pass
-    # Add the thumbnail here
-    # Add the images here
+    # FIXME: Add subobjects correctly here
     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))
         elif mentry == "Thumbnails/thumbnail.png":
             doc.addThumbnail(z.read(mentry))
         else:
-            pass  # Add the SUN junk here to the struct somewhere
+            if mvalue['full-path'][-1] == '/':
+                doc._extra.append(OpaqueObject(mvalue['full-path'], mvalue['media-type'], None))
+            else:
+                doc._extra.append(OpaqueObject(mvalue['full-path'], mvalue['media-type'], z.read(mentry)))
+            # Add the SUN junk here to the struct somewhere
             # It is cached data, so it can be out-of-date
     z.close()
     b = doc.getElementsByType(Body)
diff --git a/tests/teststyles.py b/tests/teststyles.py
index 588c2a0..57ef7b1 100644
--- a/tests/teststyles.py
+++ b/tests/teststyles.py
@@ -83,8 +83,8 @@ class TestQattributes(unittest.TestCase):
         textdoc.styles.addElement(standard)
         s = unicode(textdoc.stylesxml(),'UTF-8')
         s.index(u"""<?xml version='1.0' encoding='UTF-8'?>\n""")
-        s.index(u'xmlns:ns28="http://foreignuri.com"')
-        s.index(u'<style:paragraph-properties ns28:enable-numbering="true"/>')
+        s.index(u'xmlns:ns30="http://foreignuri.com"')
+        s.index(u'<style:paragraph-properties ns30:enable-numbering="true"/>')
         s.index(u'<office:styles><style:style style:name="Standard" style:display-name="Standard" style:family="paragraph">')
 
 

-- 
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