diff options
author | JensDiemer <git@jensdiemer.de> | 2014-09-14 20:35:19 +0200 |
---|---|---|
committer | JensDiemer <git@jensdiemer.de> | 2014-09-14 20:35:19 +0200 |
commit | 23e519e6cf884403088b1b3809755039dbd985f2 (patch) | |
tree | 02bce5aeea39a57b1b4d4e666e77dda0ad310521 /creole | |
parent | 57715724df7b9f09dc77a098ebed154265843ad7 (diff) | |
download | creole-23e519e6cf884403088b1b3809755039dbd985f2.tar.gz |
Use origin PyPi code to check generated reStructuredText in setup.py
Diffstat (limited to 'creole')
-rw-r--r-- | creole/__init__.py | 2 | ||||
-rw-r--r-- | creole/rest2html/pypi_rest2html.py | 88 | ||||
-rw-r--r-- | creole/setup_utils.py | 32 | ||||
-rw-r--r-- | creole/tests/test_setup_utils.py | 21 |
4 files changed, 125 insertions, 18 deletions
diff --git a/creole/__init__.py b/creole/__init__.py index fcf9330..5581dac 100644 --- a/creole/__init__.py +++ b/creole/__init__.py @@ -20,7 +20,7 @@ from __future__ import division, absolute_import, print_function, unicode_literals -__version__ = (1, 2, 0) +__version__ = (1, 2, 1) __api__ = (1, 0) # Creole 1.0 spec - http://wikicreole.org/ diff --git a/creole/rest2html/pypi_rest2html.py b/creole/rest2html/pypi_rest2html.py new file mode 100644 index 0000000..62fca41 --- /dev/null +++ b/creole/rest2html/pypi_rest2html.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# coding: utf-8 + +""" + Try to be so strict as PyPi. + + Code based on: + https://bitbucket.org/pypa/pypi/src/tip/description_utils.py + + see also: + https://bitbucket.org/pypa/pypi/issue/161/rest-formatting-fails-and-there-is-no-way +""" + +from __future__ import division, absolute_import, print_function + +import urlparse + +from creole.exceptions import DocutilsImportError + +try: + import docutils + from docutils import io, readers + from docutils.core import publish_doctree, Publisher + from docutils.writers import get_writer_class + from docutils.transforms import TransformError, Transform +except ImportError as err: + msg = ( + "%s - You can't use rest2html!" + " Please install: http://pypi.python.org/pypi/docutils" + ) % err + raise DocutilsImportError(msg) + + +ALLOWED_SCHEMES = '''file ftp gopher hdl http https imap mailto mms news nntp +prospero rsync rtsp rtspu sftp shttp sip sips snews svn svn+ssh telnet +wais irc'''.split() + + +def pypi_rest2html(source, output_encoding='unicode'): + """ + >>> pypi_rest2html("test!") + u'<p>test!</p>\n' + """ + settings_overrides = { + 'raw_enabled': 0, # no raw HTML code + 'file_insertion_enabled': 0, # no file/URL access + 'halt_level': 2, # at warnings or errors, raise an exception + 'report_level': 5, # never report problems with the reST code + } + + # Convert reStructuredText to HTML using Docutils. + document = publish_doctree(source=source, + settings_overrides=settings_overrides) + + for node in document.traverse(): + if node.tagname == '#text': + continue + if node.hasattr('refuri'): + uri = node['refuri'] + elif node.hasattr('uri'): + uri = node['uri'] + else: + continue + o = urlparse.urlparse(uri) + if o.scheme not in ALLOWED_SCHEMES: + raise TransformError('link scheme not allowed') + + # now turn the transformed document into HTML + reader = readers.doctree.Reader(parser_name='null') + pub = Publisher(reader, source=io.DocTreeInput(document), + destination_class=io.StringOutput) + pub.set_writer('html') + pub.process_programmatic_settings(None, settings_overrides, None) + pub.set_destination(None, None) + pub.publish() + parts = pub.writer.parts + + output = parts['body'] + + if output_encoding != 'unicode': + output = output.encode(output_encoding) + + return output + + +if __name__ == '__main__': + import doctest + print(doctest.testmod()) diff --git a/creole/setup_utils.py b/creole/setup_utils.py index d86f36a..97ae94d 100644 --- a/creole/setup_utils.py +++ b/creole/setup_utils.py @@ -3,23 +3,23 @@ """ utils for distutils setup ~~~~~~~~~~~~~~~~~~~~~~~~~ - + Get README.creole as ReStructuredText on-the-fly for setup.long_description - + More information: https://code.google.com/p/python-creole/wiki/UseInSetup - + usage in setup.py e.g.: --------------------------------------------------------------------------- #!/usr/bin/env python # coding: utf-8 - + import os import sys from setuptools import setup, find_packages - + PACKAGE_ROOT = os.path.dirname(os.path.abspath(__file__)) - + try: from creole.setup_utils import get_long_description except ImportError: @@ -30,15 +30,15 @@ long_description = None else: long_description = get_long_description(PACKAGE_ROOT) - + setup( ... long_description = long_description, ... ) --------------------------------------------------------------------------- - - :copyleft: 2011-2012 by the python-creole team, see AUTHORS for more details. + + :copyleft: 2011-2014 by the python-creole team, see AUTHORS for more details. :license: GNU GPL v3 or above, see LICENSE for more details. """ @@ -80,6 +80,7 @@ def get_long_description(package_root, filename="README.creole", raise_errors=No raise_errors = should_raise_errors() if raise_errors: + sys.stderr.write("Test creole2rest and raise an error, if rendering failed...\n") # raise a error if a unknown node found unknown_emit = raise_unknown_node else: @@ -116,13 +117,16 @@ def get_long_description(package_root, filename="README.creole", raise_errors=No ) else: if raise_errors: - # Test created ReSt code - from creole.rest2html.clean_writer import rest2html + # Test created ReSt code like PyPi does it. + from creole.rest2html.pypi_rest2html import pypi_rest2html try: - rest2html(long_description_rest_unicode, traceback=True, enable_exit_status=1, exit_status_level=2) + pypi_rest2html(long_description_rest_unicode) except SystemExit as e: - print("Error creole2rest self test failed: rest2html() exist with status code: %s" % e.args[0]) - raise + msg = "Error creole2rest self test failed: rest2html() exist with status code: %s\n" % e.args[0] + sys.stderr.write(msg) + sys.exit(msg) + except Exception as e: + sys.exit("ReSt2html error: %s" % e) else: if "check" in sys.argv: print("Generating creole to ReSt to html, ok.") diff --git a/creole/tests/test_setup_utils.py b/creole/tests/test_setup_utils.py index 6b5735d..52d897d 100644 --- a/creole/tests/test_setup_utils.py +++ b/creole/tests/test_setup_utils.py @@ -4,7 +4,7 @@ """ unittest for setup_utils ~~~~~~~~~~~~~~~~~~~~~~~~ - + https://code.google.com/p/python-creole/wiki/UseInSetup :copyleft: 2011-2014 by python-creole team, see AUTHORS for more details. @@ -83,12 +83,12 @@ class SetupUtilsTests(BaseCreoleTest): self.assertEqual(long_description, "-------\nnoerror\n-------") finally: fd.close() - + def test_get_long_description_error_handling(self): """ Test if get_long_description will raised a error, if description produce a ReSt error. - + We test with this error: <string>:102: (ERROR/3) Document or section may not begin with a transition. """ @@ -98,6 +98,21 @@ class SetupUtilsTests(BaseCreoleTest): finally: fd.close() + def test_get_long_description_error_handling2(self): + """ + Test if get_long_description will raised a error, if description + produce a ReSt error. + + We test with this error: + SystemExit: ReSt2html error: link scheme not allowed + """ + path, filename, fd = self._tempfile(b"[[foo://bar]]") +# print(get_long_description(path, filename, raise_errors=True)) + try: + self.assertRaises(SystemExit, get_long_description, path, filename, raise_errors=True) + finally: + fd.close() + def test_wrong_path_without_raise_errors(self): self.assertEqual( get_long_description("wrong/path", raise_errors=False).replace("u'", "'"), |