From 77c55d5c950de7eafaba68a07067e942d76cf724 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Fri, 19 Jul 2013 12:22:38 -0400 Subject: [PATCH 1/5] refs #54: Added a test showing expected behaviour --- pydocx/tests/test_docx.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pydocx/tests/test_docx.py b/pydocx/tests/test_docx.py index f5b39995..6d6e15a0 100644 --- a/pydocx/tests/test_docx.py +++ b/pydocx/tests/test_docx.py @@ -1,12 +1,13 @@ import base64 - from os import path +from tempfile import NamedTemporaryFile from nose.plugins.skip import SkipTest from pydocx.tests import assert_html_equal, BASE_HTML from pydocx.parsers.Docx2Html import Docx2Html from pydocx.DocxParser import ZipFile +from pydocx.exceptions import MalformedDocxException def convert(path, *args, **kwargs): @@ -749,6 +750,15 @@ def test_missing_numbering(): ''') +def test_malformed_docx_exception(): + with NamedTemporaryFile(suffix='.docx') as f: + try: + convert(f.name) + raise AssertionError('MalformedDocxException was not raised') + except MalformedDocxException: + pass + + def _converter(*args, **kwargs): # Having a converter that does nothing is the same as if abiword fails to # convert. From ce615a0a9c48e9146ced1824164d5bf05ed935fa Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Fri, 19 Jul 2013 12:23:05 -0400 Subject: [PATCH 2/5] refs #54: if BadZipFile is raised then raise a pydocx exception instead --- pydocx/DocxParser.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pydocx/DocxParser.py b/pydocx/DocxParser.py index 5608c504..f78963ad 100644 --- a/pydocx/DocxParser.py +++ b/pydocx/DocxParser.py @@ -14,6 +14,7 @@ find_ancestor_with_tag, has_descendant_with_tag, ) +from pydocx.exceptions import MalformedDocxException logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("NewParser") @@ -37,7 +38,10 @@ @contextmanager def ZipFile(path): # This is not needed in python 3.2+ - f = zipfile.ZipFile(path) + try: + f = zipfile.ZipFile(path) + except zipfile.BadZipfile: + raise MalformedDocxException('Passed in document is not a docx') yield f f.close() From 49af8646001f7be9be306faa6cc94295161c9f2a Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Fri, 19 Jul 2013 12:24:41 -0400 Subject: [PATCH 3/5] refs #54: update note --- CHANGELOG | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 17808c30..b9fa0edd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ Changelog ========= +* 0.3.8 + * If zipfile fails to open the passed in file, we are now raising a + `MalformedDocxException` instead of a `BadZipFIle`. * 0.3.7 * Some inline tags (most notably the underline tag) could have a `val` of `none` and that would signify that the style is disabled. A `val` of From c67e61272a15913d2da27cfa8a0b6185d9dd176f Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Fri, 19 Jul 2013 15:47:18 -0400 Subject: [PATCH 4/5] refs #54: used `raises` instead --- pydocx/tests/test_docx.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pydocx/tests/test_docx.py b/pydocx/tests/test_docx.py index 6d6e15a0..3ec4b1f7 100644 --- a/pydocx/tests/test_docx.py +++ b/pydocx/tests/test_docx.py @@ -3,6 +3,7 @@ from tempfile import NamedTemporaryFile from nose.plugins.skip import SkipTest +from nose.tools import raises from pydocx.tests import assert_html_equal, BASE_HTML from pydocx.parsers.Docx2Html import Docx2Html @@ -750,13 +751,10 @@ def test_missing_numbering(): ''') +@raises(MalformedDocxException) def test_malformed_docx_exception(): with NamedTemporaryFile(suffix='.docx') as f: - try: - convert(f.name) - raise AssertionError('MalformedDocxException was not raised') - except MalformedDocxException: - pass + convert(f.name) def _converter(*args, **kwargs): From bd4484c865880fe5a7ae67444cb7331c22b685ec Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Fri, 19 Jul 2013 15:49:18 -0400 Subject: [PATCH 5/5] refs #54: updated the readme --- README.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.rst b/README.rst index 2f750299..531a3ec2 100644 --- a/README.rst +++ b/README.rst @@ -222,6 +222,11 @@ The base parser `Docx2Html` relies on certain css class being set for certain be * class `pydocx-strike` -> Strike a line through. * class `pydocx-hidden` -> Hide the text. +Exceptions +########## + +Right now there is only one custom exception (`MalformedDocxException`). It is raised if either the `xml` or `zipfile` libraries raise an exception. + Optional Arguments ##################