Commits

Eric Knibbe committed ddbc1f7 Draft

Python 3 support
(changes since rev 91)

  • Participants
  • Parent commits 242e54a

Comments (1)

Files changed (7)

File python/restview/Makefile

 check test:
 	$(PYTHON) test.py
 
+.PHONY: coverage
+coverage:
+	tox -e coverage
+
 .PHONY: dist
 dist:
 	$(PYTHON) setup.py sdist

File python/restview/README.txt

 
 - New option: restview --long-description.
 
+- Add Python 3 support (LP#1093098).  Patch by myint (no public email provided).
+
 1.2.2 (2010-09-14)
 ------------------
 

File python/restview/restview

-#!/usr/bin/python
+#!/usr/bin/env python
 import sys, os
 sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
 

File python/restview/setup.py

 import os
 from setuptools import setup
 
+def test_suite():
+    import doctest
+    return doctest.DocTestSuite('restview.restviewhttp')
+
 def read(filename):
     return open(os.path.join(os.path.dirname(__file__), filename)).read()
 
     for line in read(filename).splitlines():
         if line.startswith('__version__'):
             d = {}
-            exec line in d
+            exec(line, d)
             return d['__version__']
     raise AssertionError("couldn't find __version__ in %s" % filename)
 
         'Intended Audience :: End Users/Desktop',
         'License :: OSI Approved :: GNU General Public License (GPL)',
         'Programming Language :: Python',
+        'Programming Language :: Python :: 2',
+        'Programming Language :: Python :: 3',
         'Operating System :: OS Independent',
         'Topic :: Documentation',
         'Topic :: Internet :: WWW/HTTP :: HTTP Servers',
     include_package_data=True,
     install_requires=['docutils'],
     extras_require={'syntax': ['pygments']},
+    test_suite='__main__.test_suite',
     zip_safe=False,
     entry_points="""
     [console_scripts]

File python/restview/src/restview/restviewhttp.py

 Needs docutils and a web browser.  Will syntax-highlight code or doctest blocks
 if you have pygments installed.
 """
+from __future__ import print_function
 
 import os
 import re
 import optparse
 import threading
 import webbrowser
-import BaseHTTPServer
-import SocketServer
+
+try:
+    import BaseHTTPServer
+except ImportError:
+    import http.server as BaseHTTPServer
+
+try:
+    import SocketServer
+except ImportError:
+    import socketserver as SocketServer
+
 import cgi
-import urllib
-import time
-import urlparse
+
+try:
+    from urllib import unquote
+except ImportError:
+    from urllib.parse import unquote
+
+try:
+    from urlparse import parse_qs
+except ImportError:
+    from urllib.parse import parse_qs
 
 from docutils import core
 from docutils.writers import html4css1
+import time
 
 try:
     import pygments
 except ImportError:
     pygments = None
 
+
+try:
+    unicode
+except NameError:
+    unicode = str
+
+
 __version__ = "1.3.0dev"
 
 
         content = self.do_GET_or_HEAD()
 
     def do_GET_or_HEAD(self):
-        self.path = urllib.unquote(self.path)
+        self.path = unquote(self.path)
         root = self.server.renderer.root
         command = self.server.renderer.command
         if self.path == '/':
                 return self.handle_list(root)
         elif self.path.startswith('/polling?'):
             saved_atime = last_atime
-            self.path = urlparse.parse_qs(self.path.split('?', 1)[-1])['pathname'][0]
+            self.path = parse_qs(self.path.split('?', 1)[-1])['pathname'][0]
             if self.path == '/':
                 self.path = os.path.basename(root)
             self.server.renderer.root_mtime = os.stat(self.translate_path()).st_mtime
                 return self.handle_rest_data(f.read())
             finally:
                 f.close()
-        except IOError, e:
+        except IOError as e:
             self.log_error('%s', e)
             self.send_error(404, 'File not found: %s' % self.path)
 
                 return self.handle_rest_data(f.read())
             finally:
                 f.close()
-        except OSError, e:
+        except OSError as e:
             self.log_error('%s' % e)
             self.send_error(500, 'Command execution failed')
 
         try:
             core.publish_string(rest_input, writer=writer,
                                 settings_overrides=settings_overrides)
-        except Exception, e:
+        except Exception as e:
             return self.render_exception(e.__class__.__name__, str(e),
                                          rest_input)
         return self.return_markup(writer.output)
         ('', 1234)
 
         >>> try: parse_address('notanumber')
-        ... except ValueError, e: print e
+        ... except ValueError as e: print(e)
         Invalid address: notanumber
 
         >>> try: parse_address('la:la:la')
-        ... except ValueError, e: print e
+        ... except ValueError as e: print(e)
         Invalid address: la:la:la
 
     """
     if opts.listen:
         try:
             server.local_address = parse_address(opts.listen)
-        except ValueError, e:
+        except ValueError as e:
             sys.exit(str(e))
     host = get_host_name(server.local_address[0])
     port = server.listen()
     url = 'http://%s:%d/' % (host, port)
-    print "Listening on %s" % url
+    print("Listening on %s" % url)
     if opts.browser:
         # launch the web browser in the background as it may block
         t = threading.Thread(target=webbrowser.open, args=(url, ))

File python/restview/test.py

-#!/usr/bin/python
+#!/usr/bin/env python
 import unittest
 import doctest
 

File python/restview/tox.ini

+[tox]
+envlist =
+    py26,py27,py32,py33,pypy
+
+[testenv]
+deps =
+    docutils
+    pygments
+commands =
+    python setup.py test -q
+
+[testenv:coverage]
+basepython = python2.7
+deps =
+    {[testenv]deps}
+    coverage
+commands =
+    coverage run setup.py test -q
+    coverage report --include='src/restview/*'