[Python-modules-commits] [python-odf] 105/118: Stricter handling of boolean values

Wolfgang Borgert debacle at moszumanska.debian.org
Fri Oct 3 21:27:29 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 3c5e7f6da385d40a34ec0ab5bad95c0066a72260
Author: Søren Roug <soren.roug at eea.europa.eu>
Date:   Sun Apr 15 12:17:03 2012 +0000

    Stricter handling of boolean values
---
 examples/manualpagebreak.py | 38 ++++++++++++++++++++++++++++++++++++++
 odf/attrconverters.py       | 18 ++++++++++++------
 odf/namespaces.py           |  2 +-
 setup.py                    |  4 ++--
 tests/testconverters.py     | 14 ++++++++++++++
 tests/testlengths.py        | 16 ++++++++--------
 6 files changed, 75 insertions(+), 17 deletions(-)

diff --git a/examples/manualpagebreak.py b/examples/manualpagebreak.py
new file mode 100644
index 0000000..f4f01cc
--- /dev/null
+++ b/examples/manualpagebreak.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2012 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):
+#
+
+# This example shows how to create a manual page break.
+
+from odf.opendocument import OpenDocumentText
+from odf.style import Style, TextProperties, ParagraphProperties
+from odf.text import P
+
+
+textdoc = OpenDocumentText()
+# Create a style for the paragraph with page-break
+withbreak = Style(name="WithBreak", parentstylename="Standard", family="paragraph")
+withbreak.addElement(ParagraphProperties(breakbefore="page"))
+textdoc.automaticstyles.addElement(withbreak)
+
+p = P(text=u'First paragraph')
+textdoc.text.addElement(p)
+p = P(stylename=withbreak,text=u'Second paragraph')
+textdoc.text.addElement(p)
+textdoc.save("pagebreak_odfpy.odt")
diff --git a/odf/attrconverters.py b/odf/attrconverters.py
index b75f80a..00b08be 100644
--- a/odf/attrconverters.py
+++ b/odf/attrconverters.py
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (C) 2006-2010 Søren Roug, European Environment Agency
+# Copyright (C) 2006-2012 Søren Roug, European Environment Agency
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -32,11 +32,15 @@ def cnv_anyURI(attribute, arg, element):
     return unicode(arg)
 
 def cnv_boolean(attribute, arg, element):
-    if arg.lower() in ("false","no"):
+    """ XML Schema Part 2: Datatypes Second Edition
+        An instance of a datatype that is defined as boolean can have the
+        following legal literals {true, false, 1, 0}
+    """
+    if str(arg).lower() in ("0","false","no"):
         return "false"
-    if arg:
+    if str(arg).lower() in ("1","true","yes"):
         return "true"
-    return "false"
+    raise ValueError, "'%s' not allowed as Boolean value for %s" % (str(arg), attribute)
 
 # Potentially accept color values
 def cnv_color(attribute, arg, element):
@@ -454,7 +458,7 @@ attrconverters = {
 	((DRAWNS,u'fill-image-width'), None): cnv_lengthorpercent,
 	((DRAWNS,u'filter-name'), None): cnv_string,
 	((DRAWNS,u'fit-to-contour'), None): cnv_boolean,
-	((DRAWNS,u'fit-to-size'), None): cnv_boolean,
+	((DRAWNS,u'fit-to-size'), None): cnv_string,  # ODF 1.2 says boolean
 	((DRAWNS,u'formula'), None): cnv_string,
 	((DRAWNS,u'frame-display-border'), None): cnv_boolean,
 	((DRAWNS,u'frame-display-scrollbar'), None): cnv_boolean,
@@ -989,7 +993,9 @@ attrconverters = {
 	((STYLENS,u'print-content'), None): cnv_boolean,
 	((STYLENS,u'print-orientation'), None): cnv_string,
 	((STYLENS,u'print-page-order'), None): cnv_string,
-	((STYLENS,u'protect'), None): cnv_boolean,
+	((STYLENS,u'protect'), (STYLENS,u'section-properties')): cnv_boolean,
+	((STYLENS,u'protect'), (STYLENS,u'graphic-properties')): cnv_string,
+#	((STYLENS,u'protect'), None): cnv_boolean,
 	((STYLENS,u'punctuation-wrap'), None): cnv_string,
 	((STYLENS,u'register-true'), None): cnv_boolean,
 	((STYLENS,u'register-truth-ref-style-name'), None): cnv_string,
diff --git a/odf/namespaces.py b/odf/namespaces.py
index cff8a74..74d7b3e 100644
--- a/odf/namespaces.py
+++ b/odf/namespaces.py
@@ -17,7 +17,7 @@
 #
 # Contributor(s):
 #
-TOOLSVERSION = u"ODFPY/0.9.4dev"
+TOOLSVERSION = u"ODFPY/0.9.5dev"
 
 ANIMNS         = u"urn:oasis:names:tc:opendocument:xmlns:animation:1.0"
 CHARTNS        = u"urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
diff --git a/setup.py b/setup.py
index 95cd74f..4d8436a 100644
--- a/setup.py
+++ b/setup.py
@@ -21,7 +21,7 @@
 import platform
 from distutils.core import setup
 
-version = '0.9.4dev'
+version = '0.9.5dev'
 
 if platform.system() in ('Linux','Unix'):
     man1pages = [('share/man/man1', [
@@ -51,7 +51,7 @@ setup(name='odfpy',
         'Intended Audience :: System Administrators',
         'License :: OSI Approved :: Apache Software License',
         'License :: OSI Approved :: GNU General Public License (GPL)',
-        'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)'
+        'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
         'Operating System :: MacOS :: MacOS X',
         'Operating System :: Microsoft :: Windows',
         'Operating System :: POSIX',
diff --git a/tests/testconverters.py b/tests/testconverters.py
index 16b2903..5597c71 100644
--- a/tests/testconverters.py
+++ b/tests/testconverters.py
@@ -53,6 +53,20 @@ class TestConverters(unittest.TestCase):
             else:
                 self.assertEquals(self.allqattrs[(attr, elem)], 1)
 
+    def testBooleanConverter(self):
+        """ Check that the boolean converter understands the values """
+        self.assertEquals(attrconverters.cnv_boolean("usesoftpagebreak", 'false', None), 'false')
+        self.assertEquals(attrconverters.cnv_boolean("usesoftpagebreak", 'true', None), 'true')
+        self.assertEquals(attrconverters.cnv_boolean("usesoftpagebreak", True, None), 'true')
+        self.assertEquals(attrconverters.cnv_boolean("usesoftpagebreak", False, None), 'false')
+        self.assertEquals(attrconverters.cnv_boolean("usesoftpagebreak", 1, None), 'true')
+        self.assertEquals(attrconverters.cnv_boolean("usesoftpagebreak", 0, None), 'false')
+        self.assertRaises(ValueError, attrconverters.cnv_boolean, "usesoftpagebreak", '', None)
+        self.assertRaises(ValueError, attrconverters.cnv_boolean, "usesoftpagebreak", 'on', None)
+        self.assertRaises(ValueError, attrconverters.cnv_boolean, "usesoftpagebreak", None, None)
+#       self.assertEquals(attrconverters.cnv_boolean("usesoftpagebreak", '', None), 'false')
+#       self.assertEquals(attrconverters.cnv_boolean("usesoftpagebreak", 'on', None), 'true')
+
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/tests/testlengths.py b/tests/testlengths.py
index 28a10da..c8c947e 100644
--- a/tests/testlengths.py
+++ b/tests/testlengths.py
@@ -38,10 +38,10 @@ class TestDrawElements(unittest.TestCase):
         draw.AreaRectangle( y="1cm", height="1cm", width="1cm", x="0cm" )
         draw.AreaRectangle( y="1cm", x="1cm", height="1cm", width="0cm" )
         draw.AreaRectangle( y="1cm", x="1cm", width="1cm", height="0cm" )
-        draw.ContourPath( recreateonedit="1cm", viewbox="0 0 1000 1000", d="1cm", height="0cm" )
-        draw.ContourPath( recreateonedit="1cm", viewbox="0 0 1000 1000", d="1cm", width="0cm" )
-        draw.ContourPolygon( points="0,0 1,1", recreateonedit="1cm", viewbox="0 0 1000 1000", height="0cm" )
-        draw.ContourPolygon( points="0,0 1,1", recreateonedit="1cm", viewbox="0 0 1000 1000", width="0cm" )
+        draw.ContourPath( recreateonedit="true", viewbox="0 0 1000 1000", d="1cm", height="0cm" )
+        draw.ContourPath( recreateonedit="true", viewbox="0 0 1000 1000", d="1cm", width="0cm" )
+        draw.ContourPolygon( points="0,0 1,1", recreateonedit="true", viewbox="0 0 1000 1000", height="0cm" )
+        draw.ContourPolygon( points="0,0 1,1", recreateonedit="true", viewbox="0 0 1000 1000", width="0cm" )
         draw.Control( control="1cm", endx="0cm" )
         draw.Control( control="1cm", endy="0cm" )
         draw.Control( control="1cm", height="0cm" )
@@ -119,10 +119,10 @@ class TestDrawElements(unittest.TestCase):
         self.assertRaises(ValueError, draw.AreaRectangle, y="1cm", height="1cm", width="1cm", x="xxx" )
         self.assertRaises(ValueError, draw.AreaRectangle, y="1cm", x="1cm", height="1cm", width="xxx" )
         self.assertRaises(ValueError, draw.AreaRectangle, y="1cm", x="1cm", width="1cm", height="xxx" )
-        self.assertRaises(ValueError, draw.ContourPath, recreateonedit="1cm", viewbox="0 0 1000 1000", d="1cm", height="xxx" )
-        self.assertRaises(ValueError, draw.ContourPath, recreateonedit="1cm", viewbox="0 0 1000 1000", d="1cm", width="xxx" )
-        self.assertRaises(ValueError, draw.ContourPolygon, points="0,0 1,1", recreateonedit="1cm", viewbox="0 0 1000 1000", height="xxx" )
-        self.assertRaises(ValueError, draw.ContourPolygon, points="0,0 1,1", recreateonedit="1cm", viewbox="0 0 1000 1000", width="xxx" )
+        self.assertRaises(ValueError, draw.ContourPath, recreateonedit="true", viewbox="0 0 1000 1000", d="1cm", height="xxx" )
+        self.assertRaises(ValueError, draw.ContourPath, recreateonedit="true", viewbox="0 0 1000 1000", d="1cm", width="xxx" )
+        self.assertRaises(ValueError, draw.ContourPolygon, points="0,0 1,1", recreateonedit="true", viewbox="0 0 1000 1000", height="xxx" )
+        self.assertRaises(ValueError, draw.ContourPolygon, points="0,0 1,1", recreateonedit="true", viewbox="0 0 1000 1000", width="xxx" )
         self.assertRaises(ValueError, draw.Control, control="1cm", endx="xxx" )
         self.assertRaises(ValueError, draw.Control, control="1cm", endy="xxx" )
         self.assertRaises(ValueError, draw.Control, control="1cm", height="xxx" )

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