Commits

Alex Grönholm committed 06e759a

Refactored code to be Python 3 compatible

  • Participants
  • Parent commits cb68b04

Comments (0)

Files changed (7)

File fanstatic/codegen.py

+from fanstatic import compat
+
+
 def _visit(resource, result, dead):
     if dead[(resource.library, resource.relpath)]:
         return
 def generate_code(**kw):
     resource_to_name = {}
     resources = []
-    for name, resource in kw.items():
+    for name, resource in compat.iteritems(kw):
         resource_to_name[(resource.library, resource.relpath)] = name
         resources.append(resource)
 
     libraries = {}
     for resource in resources:
         libraries[resource.library.name] = resource.library
-        for mode_name, mode_resource in resource.modes.items():
+        for mode_name, mode_resource in compat.iteritems(resource.modes):
             libraries[mode_resource.library.name] = mode_resource.library
-    libraries = sorted(libraries.values(), key=lambda library: library.name)
+    libraries = sorted(compat.itervalues(libraries), key=lambda library: library.name)
 
     result = []
     # import on top
             s += supersedes_s
         if resource.modes:
             items = []
-            for mode_name, mode in resource.modes.items():
+            for mode_name, mode in compat.iteritems(resource.modes):
                 items.append((mode_name,
                               generate_inline_resource(mode, resource)))
             items = sorted(items)

File fanstatic/compat.py

+"""Python 2/3 compatibility module."""
+
+import sys
+
+
+if sys.version_info[0] < 3:
+    iteritems = lambda x: x.iteritems()
+    iterkeys = lambda x: x.iterkeys()
+    itervalues = lambda x: x.itervalues()
+    dict_items = lambda x: x.items()
+    dict_keys = lambda x: x.keys()
+    dict_values = lambda x: x.values()
+    u = lambda x: unicode(x)
+    maxsize = sys.maxint
+    basestring = basestring
+else:
+    iteritems = lambda x: x.items()
+    iterkeys = lambda x: x.keys()
+    itervalues = lambda x: x.values()
+    dict_items = lambda x: list(x.items())
+    dict_keys = lambda x: list(x.keys())
+    dict_values = lambda x: list(x.values())
+    u = lambda x: x
+    maxsize = sys.maxsize
+    basestring = str

File fanstatic/config.py

 from paste.util.converters import asbool
-from fanstatic import DEBUG, MINIFIED
+
+from fanstatic import DEBUG, MINIFIED, compat
 
 BOOL_CONFIG = set(['versioning', 'recompute_hashes', DEBUG, MINIFIED,
                    'bottom', 'force_bottom', 'bundle', 'rollup',
 
 def convert_config(config):
     result = {}
-    for key, value in config.items():
+    for key, value in compat.iteritems(config):
         if key in BOOL_CONFIG:
             result[key] = asbool(value)
         else:

File fanstatic/core.py

 import re
 import threading
 
+from fanstatic import compat
 import fanstatic.checksum
 
 DEFAULT_SIGNATURE = 'fanstatic'
         # the maximum library number is the maximum number of the
         # depending libraries + 1
         max_library_nr = 0
-        for resource in self.known_resources.values():
+        for resource in compat.itervalues(self.known_resources):
             for dep in resource.depends:
                 # we don't care about resources in the same library
                 if dep.library is self:
             # If we do not know about the filename extension inclusion
             # order, we render the resource after all others.
             self.order, _ = inclusion_renderers.get(
-                self.ext, (sys.maxint, None))
+                self.ext, (compat.maxsize, None))
 
-        assert not isinstance(depends, basestring)
+        assert not isinstance(depends, compat.basestring)
         self.depends = set()
         if depends is not None:
             # Normalize groups into the underlying resources...
         for mode_name, argument in [(DEBUG, debug), (MINIFIED, minified)]:
             if argument is None:
                 continue
-            elif isinstance(argument, basestring):
+            elif isinstance(argument, compat.basestring):
                 mode_resource = Resource(
                     library, argument, bottom=bottom, renderer=renderer,
                     depends=depends, dont_bundle=dont_bundle)
             mode_resource.dependency_nr = self.dependency_nr
             self.modes[mode_name] = mode_resource
 
-        assert not isinstance(supersedes, basestring)
+        assert not isinstance(supersedes, compat.basestring)
         self.supersedes = supersedes or []
 
         self.rollups = []
         # mode
         # XXX what if mode is full-fledged resource which lists
         # supersedes itself?
-        for mode_name, mode in self.modes.items():
+        for mode_name, mode in compat.iteritems(self.modes):
             for resource in self.supersedes:
                 superseded_mode = resource.mode(mode_name)
                 # if there is no such mode, let's skip it
         self.ext = extension
         self.required = required
 
-        assert not isinstance(depends, basestring)
+        assert not isinstance(depends, compat.basestring)
         self.depends = set()
         if depends is not None:
             # Normalize groups into the underlying resources...
         self.dependency_nr = slot.dependency_nr
 
         self.modes = {}
-        for key, resource in resource.modes.items():
+        for key, resource in compat.iteritems(resource.modes):
             self.modes[key] = FilledSlot(slot, resource)
 
         if not resource.depends.issubset(slot.depends):

File fanstatic/test_core.py

                        NEEDED,
                        UnknownResourceExtensionError,
                        UnknownResourceError,
-                       set_resource_file_existence_checking)
+                       set_resource_file_existence_checking, compat)
 
 from fanstatic.core import inclusion_renderers
 from fanstatic.core import thread_local_needed_data
     x1 = Resource(foo, 'a.js', minified='a.min.js')
 
     assert len(foo.known_resources) == 2
-    assert x1 in foo.known_resources.values()
+    assert x1 in compat.dict_values(foo.known_resources)
 
     # Can not use the same relpath for two Resource declarations.
     with pytest.raises(ConfigurationError):
 
 def test_inclusion_renderers():
     assert sorted(
-        [(order, key) for key, (order, _) in inclusion_renderers.items()]) == [
+        [(order, key) for key, (order, _) in compat.dict_items(inclusion_renderers)]) == [
         (10, '.css'), (20, '.js'), (30, '.ico')]
     _, renderer = inclusion_renderers['.js']
     assert renderer('http://localhost/script.js') == (
     needed.need(app)
     resources = needed.resources()
     for resource in resources:
-        print resource, resource.library.library_nr
+        print((resource, resource.library.library_nr))
     assert resources == [jquery, jqueryui, obviel, obviel_forms,
                          obviel_datepicker, vtab, tabview, bread, app]
 

File fanstatic/test_registry.py

 
 import pytest
 
-from fanstatic import get_library_registry, Library
+from fanstatic import get_library_registry, Library, compat
 
 
 def test_library_registry():
     library_registry = get_library_registry()
     # the 'foo' library has been placed here by the test buildout
     # fixtures/MyPackage by the entry point mechanism
-    assert library_registry.keys() == ['foo']
+    assert compat.dict_keys(library_registry) == ['foo']
 
     # this is a real library, not an entry point
     assert isinstance(library_registry['foo'], Library)
     bar = Library('bar', '')
     library_registry.add(bar)
     assert library_registry['bar'] is bar
-    assert library_registry.keys() == ['foo', 'bar']
+    assert compat.dict_keys(library_registry) == ['foo', 'bar']
 
     baz = Library('baz', '')
     library_registry[baz.name] = baz
     assert library_registry['baz'] is baz
-    assert sorted(library_registry.keys()) == ['bar', 'baz', 'foo']
+    assert sorted(compat.iterkeys(library_registry)) == ['bar', 'baz', 'foo']
 
     # MyPackage has been installed in development mode:
     assert library_registry['foo'].version == None

File fanstatic/test_wsgi.py

 import webob
 
 from fanstatic import (Library, Resource,
-                       get_needed, make_serf)
+                       get_needed, make_serf, compat)
 
 from fanstatic import Fanstatic, ConfigurationError
 
         return ['<html><head></head><body</body></html>']
 
     request = webob.Request.blank('/')
-    wrapped = Fanstatic(app, base_url=u'http://localhost')
+    wrapped = Fanstatic(app, base_url=compat.u('http://localhost'))
     # Fanstatic used to choke on unicode content.
     response = request.get_response(wrapped)