Skip to content

Commit

Permalink
Simplify code with the option to sort_components.
Browse files Browse the repository at this point in the history
  • Loading branch information
thefunny42 committed Apr 29, 2012
1 parent f689eec commit ac1d25f
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/grokcore/viewlet/components.py
Expand Up @@ -13,6 +13,8 @@
##############################################################################
"""Grok components"""

from operator import itemgetter

from zope import component, interface
from zope.viewlet.manager import ViewletManagerBase
from zope.viewlet.viewlet import ViewletBase
Expand Down Expand Up @@ -45,24 +47,8 @@ def sort(self, viewlets):
``viewlets`` is a list of tuples of the form (name, viewlet).
"""
# In Grok, the default order of the viewlets is determined by
# util.sort_components. util.sort_components() however expects
# a list of just components, but sort() is supposed to deal
# with a list of (name, viewlet) tuples.
# To handle this situation we first store the name part on the
# viewlet, then use util.sort_components() and then "unpack"
# the name from the viewlet and recreate the list of (name,
# viewlet) tuples, now in the correct order.
s_viewlets = []
for name, viewlet in viewlets:
# Stuff away viewlet name so we can later retrieve it.
# XXX We loose name information in case the same viewlet
# is in the viewlets list twice, but with a different
# name. Most probably this situation doesn't occur.
viewlet.__viewlet_name__ = name
s_viewlets.append(viewlet)
s_viewlets = util.sort_components(s_viewlets)
return [(viewlet.__viewlet_name__, viewlet) for viewlet in s_viewlets]
# Sort viewlets following grok.order rule.
return util.sort_components(viewlets, key=itemgetter(1))

def default_namespace(self):
namespace = {}
Expand Down

0 comments on commit ac1d25f

Please sign in to comment.