[Python-modules-commits] [python-odf] 72/118: Implemented isInstanceOf()

Wolfgang Borgert debacle at moszumanska.debian.org
Fri Oct 3 21:27:24 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 9d0056b4d60019d4f7ed34d2f8e909a4070ac757
Author: Søren Roug <soren.roug at eea.europa.eu>
Date:   Thu Dec 31 15:20:05 2009 +0000

    Implemented isInstanceOf()
---
 odf/element.py     | 21 +++++++++++++++++++++
 tests/testtypes.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)

diff --git a/odf/element.py b/odf/element.py
index 9754c25..f0938ba 100644
--- a/odf/element.py
+++ b/odf/element.py
@@ -165,6 +165,18 @@ class Node(xml.dom.Node):
         oldChild.parentNode = None
         return oldChild
 
+    def __str__(self):
+        val = []
+        for c in self.childNodes:
+            val.append(str(c))
+        return ''.join(val)
+
+    def __unicode__(self):
+        val = []
+        for c in self.childNodes:
+            val.append(unicode(c))
+        return u''.join(val)
+
 defproperty(Node, "firstChild", doc="First child node, or None.")
 defproperty(Node, "lastChild",  doc="Last child node, or None.")
 
@@ -221,6 +233,9 @@ class Text(Childless, Node):
         self.data = data
 
     def __str__(self):
+        return self.data.encode()
+
+    def __unicode__(self):
         return self.data
 
     def toXml(self,level,f):
@@ -452,3 +467,9 @@ class Element(Node):
         obj = element(check_grammar=False)
         return self._getElementsByObj(obj,[])
 
+    def isInstanceOf(self, element):
+        """ This is a check to see if the object is an instance of a type """
+        obj = element(check_grammar=False)
+        return self.qname == obj.qname
+
+
diff --git a/tests/testtypes.py b/tests/testtypes.py
new file mode 100644
index 0000000..238a61e
--- /dev/null
+++ b/tests/testtypes.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2007 Søren Roug, European Environment Agency
+#
+# This is free software.  You may redistribute it under the terms
+# of the Apache license and the GNU General Public License Version
+# 2 or at your option any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# Contributor(s):
+#
+
+import unittest, os
+import cStringIO
+import zipfile
+from odf import teletype
+from odf.opendocument import OpenDocumentText, load
+from odf import office, text
+
+class TestTypes(unittest.TestCase):
+    
+
+
+    def test_paras(self):
+        """ Grab all paragraphs and check they are paragraphs """
+        poem_odt = os.path.join(
+            os.path.dirname(__file__), "examples", "serious_poem.odt")
+        d = load(poem_odt)
+        allparas = d.getElementsByType(text.P)
+
+        for p in allparas:
+            self.assertTrue(p.isInstanceOf(text.P))
+
+    def test_body(self):
+        """ Check that the document's body is <office:body> """
+        poem_odt = os.path.join(
+            os.path.dirname(__file__), "examples", "serious_poem.odt")
+        d = load(poem_odt)
+        self.assertTrue(d.body.isInstanceOf(office.Body))
+        self.assertFalse(d.body.isInstanceOf(text.P))
+        self.assertTrue(d.body.parentNode.isInstanceOf(office.Document))
+        self.assertTrue(d.topnode.isInstanceOf(office.Document))
+
+
+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