Alex Grönholm avatar Alex Grönholm committed 56cc158

Fixed compatibility with Python 3.2 and 3.3; added more test environments to tox.ini

Comments (0)

Files changed (11)

fanstatic/checksum.py

 import hashlib
 from datetime import datetime
 
+from fanstatic import compat
+
 VCS_NAMES = ['.svn', '.git', '.bzr', '.hg']
 IGNORED_EXTENSIONS = ['.swp', '.tmp', '.pyc', '.pyo']
 
 def md5(path):
     chcksm = hashlib.md5()
     for path in sorted(list(list_directory(path, include_directories=False))):
-        chcksm.update(path)
+        chcksm.update(compat.as_bytestring(path))
         try:
             f = open(path, 'rb')
             while True:

fanstatic/compat.py

     dict_keys = lambda x: x.keys()
     dict_values = lambda x: x.values()
     u = lambda x: unicode(x)
+    as_bytestring = lambda x: x
     maxsize = sys.maxint
     basestring = basestring
 else:
     dict_items = lambda x: list(x.items())
     dict_keys = lambda x: list(x.keys())
     dict_values = lambda x: list(x.values())
+    as_bytestring = lambda x: x.encode('utf-8')
     u = lambda x: x
     maxsize = sys.maxsize
     basestring = str
-
-is_pypy = '__pypy__' in sys.builtin_module_names

fanstatic/config.py

 
 # From paste.util.converters.
 def asbool(obj):
-    if isinstance(obj, (str, unicode)):
+    if isinstance(obj, compat.basestring):
         obj = obj.strip().lower()
         if obj in ['true', 'yes', 'on', 'y', 't', '1']:
             return True

fanstatic/injector.py

 
         # The wrapped application may have `needed` resources.
         if needed.has_resources():
-            result = needed.render_topbottom_into_html(response.body)
-            # Reset the body...
-            response.body = ''
-            # And write the result. The `write` method handles unicode results.
-            response.write(result)
+            response.text = needed.render_topbottom_into_html(response.text)
 
         # Clean up after our behinds.
         fanstatic.del_needed()

fanstatic/publisher.py

             contents.append(fh.read())
             fh.close()
         return webob.Response(
-            body='\n'.join(contents),
+            body=b'\n'.join(contents),
             last_modified=mtime,
         ).conditional_response_app
 

fanstatic/test_codegen.py

                                debug='giantj-debug.js')
     non_inlinable = Resource(foo, 'j3.js', debug=Resource(bar,
                                                           'j4.js'))
-    assert generate_code(j1=j1, j2=j2, giantj=giantj,
-                         non_inlinable=non_inlinable) == '''\
+    generated = generate_code(j1=j1, j2=j2, giantj=giantj,
+                              non_inlinable=non_inlinable)
+    assert generated.startswith('''\
 from fanstatic import Library, Resource
 
 # This code is auto-generated and not PEP8 compliant
 
-bar = Library('bar', '')
-foo = Library('foo', '')
-
-j1 = Resource(foo, 'j1.js', debug='j1-debug.js')
-j2 = Resource(foo, 'j2.js', debug='j2-debug.js')
-giantj = Resource(foo, 'giantj.js', supersedes=[j1, j2], debug='giantj-debug.js')
-non_inlinable = Resource(foo, 'j3.js', debug=Resource(bar, 'j4.js'))'''
+''')
+    assert "bar = Library('bar', '')" in generated
+    assert "foo = Library('foo', '')" in generated
+    assert "j1 = Resource(foo, 'j1.js', debug='j1-debug.js')" in generated
+    assert "j2 = Resource(foo, 'j2.js', debug='j2-debug.js')" in generated
+    assert "giantj = Resource(foo, 'giantj.js', supersedes=[j1, j2], debug='giantj-debug.js')" in generated
+    assert "non_inlinable = Resource(foo, 'j3.js', debug=Resource(bar, 'j4.js'))" in generated
 
 
 def test_generate_source_control_name():

fanstatic/test_injector.py

 import webob
 
 from fanstatic import (Library, Resource, Injector, get_needed,
-                       NEEDED, compat)
+                       NEEDED)
 
 
 def test_incorrect_configuration_options():
 
     with pytest.raises(TypeError) as e:
         Injector(app, mode='qux', incorrect='configoption')
-    if compat.is_pypy:
-        assert (
-            "__init__() got 2 unexpected "
-            "keyword arguments") in str(e)
-    else:
-        assert (
-            "__init__() got an unexpected "
-            "keyword argument 'incorrect'") in str(e)
+    assert 'keyword argument' in str(e)
 
     with pytest.raises(TypeError) as e:
         Injector(
             app, mode='qux', incorrect='configoption', recompute_hashes=True)
-    if compat.is_pypy:
-        assert (
-            "__init__() got 2 unexpected "
-            "keyword arguments") in str(e)
-    else:
-        assert (
-            "__init__() got an unexpected "
-            "keyword argument 'incorrect'") in str(e)
+    assert 'keyword argument' in str(e)
 
 
 def test_inject():
         needed = get_needed()
         needed.need(y1)
         needed.set_base_url('http://testapp')
-        return ['<html><head></head><body</body></html>']
+        return [b'<html><head></head><body</body></html>']
 
     wrapped_app = Injector(app)
 
     request = webob.Request.blank('/')
     response = request.get_response(wrapped_app)
-    assert response.body == '''\
+    assert response.body == b'''\
 <html><head>
     <link rel="stylesheet" type="text/css" href="http://testapp/fanstatic/foo/b.css" />
 <script type="text/javascript" src="http://testapp/fanstatic/foo/a.js"></script>
     def html_app(environ, start_response):
         start_response('200 OK', [('Content-Type', 'text/html')])
         assert NEEDED in environ
-        return ['<html><head></head><body</body></html>']
+        return [b'<html><head></head><body</body></html>']
 
     wrapped_app = Injector(html_app)
     request = webob.Request.blank('/')
     def textplain_app(environ, start_response):
         start_response('200 OK', [('Content-Type', 'text/plain')])
         assert NEEDED in environ
-        return ['<html><head></head><body</body></html>']
+        return [b'<html><head></head><body</body></html>']
 
     wrapped_app = Injector(textplain_app)
     request = webob.Request.blank('/')
         start_response('200 OK', [('Content-Type', 'text/plain')])
         needed = get_needed()
         needed.need(y1)
-        return ['<html><head></head><body</body></html>']
+        return [b'<html><head></head><body</body></html>']
 
     wrapped_app = Injector(app)
 
     request = webob.Request.blank('/')
     response = request.get_response(wrapped_app)
-    assert response.body == '<html><head></head><body</body></html>'
+    assert response.body == b'<html><head></head><body</body></html>'
 
 
 def test_no_needed_into_non_get_post():
     def app(environ, start_response):
         assert NEEDED not in environ
         start_response('200 OK', [])
-        return ['foo']
+        return [b'foo']
     wrapped_app = Injector(app)
     request = webob.Request.blank('/', method='PUT')
     request.get_response(wrapped_app)
         # header and won't trigger the fault from
         # https://bitbucket.org/fanstatic/fanstatic/issue/49/exception-in-injector-when-app-returns-304
         start_response('304 Not Modified', [('fake', '123')])
-        return ['']
+        return [b'']
     wrapped_app = Injector(app)
     request = webob.Request.blank('/', method='GET')
     request.get_response(wrapped_app)

fanstatic/test_publisher.py

 
     request = webob.Request.blank('/foo/test.js')
     response = request.get_response(app)
-    assert response.body == '/* a test */'
+    assert response.body == b'/* a test */'
 
 
 def test_just_publisher():
 
     request = webob.Request.blank('/foo/:version:something/test.js')
     response = request.get_response(app)
-    assert response.body == '/* a test */'
+    assert response.body == b'/* a test */'
 
 
 def test_resource_no_version_no_cache(tmpdir):
 
     request = webob.Request.blank('/foo/test.js')
     response = request.get_response(app)
-    assert response.body == '/* a test */'
+    assert response.body == b'/* a test */'
     assert response.cache_control.max_age is None
     assert response.expires is None
 
 
     request = webob.Request.blank('/foo/:version:something/test.js')
     response = request.get_response(app)
-    assert response.body == '/* a test */'
+    assert response.body == b'/* a test */'
     assert response.cache_control.max_age == FOREVER
     # the test has just run and will take less than a full day to
     # run. we therefore expect the expires to be greater than
 
     request = webob.Request.blank('/fanstatic/foo/test.js')
     response = request.get_response(delegator)
-    assert response.body == '/* a test */'
+    assert response.body == b'/* a test */'
 
     # A deeper fanstatic.
     request = webob.Request.blank('/foo/bar/fanstatic/foo/test.js')
     response = request.get_response(delegator)
-    assert response.body == '/* a test */'
+    assert response.body == b'/* a test */'
 
     request = webob.Request.blank('/somethingelse')
     response = request.get_response(delegator)
     publisher = Publisher(LibraryRegistry([foo_library]))
     request = webob.Request.blank('/foo/.svn/entries')
     response = request.get_response(publisher)
-    assert response.body == 'secret'
+    assert response.body == b'secret'
 
     foo_library = Library('foo', foo_library_dir.strpath, ignores=['.svn'])
     publisher = Publisher(LibraryRegistry([foo_library]))
 
     request = webob.Request.blank('/foo/:bundle:test1.js;test2.js')
     response = request.get_response(app)
-    assert response.body == '''/* a test 1 */
+    assert response.body == b'''/* a test 1 */
 /* a test 2 */'''
     assert response.cache_control.max_age is None
 
 
     request = webob.Request.blank('/foo/sub/sub/:bundle:r1.css;r2.css')
     response = request.get_response(app)
-    assert response.body == '''r1
+    assert response.body == b'''r1
 r2'''
 
     r3 = Resource(foo, 'r3.css')
     subdir.join('r4.css').write('r4')
     request = webob.Request.blank('/foo/sub/sub/:bundle:r1.css;r2.css;r4.css')
     response = request.get_response(app)
-    assert response.body == '''r1
+    assert response.body == b'''r1
 r2
 r4'''
 

fanstatic/test_registry.py

     bar = Library('bar', '')
     library_registry.add(bar)
     assert library_registry['bar'] is bar
-    assert compat.dict_keys(library_registry) == ['foo', 'bar']
+    assert sorted(compat.dict_keys(library_registry)) == ['bar', 'foo']
 
     baz = Library('baz', '')
     library_registry[baz.name] = baz

fanstatic/test_wsgi.py

         start_response('200 OK', [])
         needed = get_needed()
         needed.need(y1)
-        return ['<html><head></head><body</body></html>']
+        return [b'<html><head></head><body</body></html>']
 
     wrapped_app = Fanstatic(app, base_url='http://testapp')
 
     request.environ['SCRIPT_NAME'] = '/root' # base_url is defined so SCRIPT_NAME
                                              # shouldn't be taken into account
     response = request.get_response(wrapped_app)
-    assert response.body == '''\
+    assert response.body == b'''\
 <html><head>
     <link rel="stylesheet" type="text/css" href="http://testapp/fanstatic/foo/b.css" />
 <script type="text/javascript" src="http://testapp/fanstatic/foo/a.js"></script>
         start_response('200 OK', [])
         needed = get_needed()
         needed.need(y1)
-        return ['<html><head></head><body</body></html>']
+        return [b'<html><head></head><body</body></html>']
 
     wrapped_app = Fanstatic(app)
 
     request = webob.Request.blank('/path')
     request.environ['SCRIPT_NAME'] = '/root'
     response = request.get_response(wrapped_app)
-    assert response.body == '''\
+    assert response.body == b'''\
 <html><head>
     <link rel="stylesheet" type="text/css" href="/root/fanstatic/foo/b.css" />
 <script type="text/javascript" src="/root/fanstatic/foo/a.js"></script>
     def app(environ, start_response):
         start_response('200 OK', [])
         x1.need()
-        return ['<html><head></head><body</body></html>']
+        return [b'<html><head></head><body</body></html>']
 
     request = webob.Request.blank('/')
     wrapped = Fanstatic(app, base_url=compat.u('http://localhost'))
     serf = Fanstatic(serf, versioning=False)
     request = webob.Request.blank('/')
     response = request.get_response(serf)
-    assert response.body == '''\
+    assert response.body == b'''\
 <html><head>
     <link rel="stylesheet" type="text/css" href="/fanstatic/foo/style.css" />
 </head><body></body></html>'''
 [tox]
-envlist = py27,py33
+envlist = py26,py27,py32,py33,pypy
 indexserver =
     FIXTURES = file://{toxinidir}/fixtures/
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.