From 1da4a22d9d3fed8eabfbb1ccedc64a47c19b8fef Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: May 22 2015 14:07:10 +0000 Subject: Get hubs working on python3 as well as python2. --- diff --git a/hubs/models.py b/hubs/models.py index 352854e..6d328de 100644 --- a/hubs/models.py +++ b/hubs/models.py @@ -36,7 +36,7 @@ from sqlalchemy.orm import backref import fedmsg import fedmsg.utils -import widgets +import hubs.widgets class HubsBase(object): @@ -117,8 +117,8 @@ class Hub(BASE): def _config_default(context): plugin_name = context.current_parameters['plugin'] - plugin = widgets.registry[plugin_name] - arguments = getattr(plugin, 'widget_arguments', []) + plugin = hubs.widgets.registry[plugin_name] + arguments = getattr(plugin.data, 'widget_arguments', []) return json.dumps(dict([(arg.name, arg.default) for arg in arguments])) @@ -147,8 +147,9 @@ class Widget(BASE): } def render(self, request, session): - module = widgets.registry[self.plugin] - return widgets.render(module, request, session, self, **self.config) + module = hubs.widgets.registry[self.plugin] + render = hubs.widgets.render + return render(module, request, session, self, **self.config) class User(BASE): diff --git a/hubs/widgets/__init__.py b/hubs/widgets/__init__.py index 8a74f2a..420c0a3 100644 --- a/hubs/widgets/__init__.py +++ b/hubs/widgets/__init__.py @@ -1,9 +1,9 @@ -import dummy -import stats -import rules -import sticky +from hubs.widgets import dummy +from hubs.widgets import stats +from hubs.widgets import rules +from hubs.widgets import sticky -from base import AGPLv3 +from hubs.widgets.base import AGPLv3 registry = { 'dummy': dummy, @@ -15,16 +15,31 @@ registry = { def validate_registry(registry): """ Ensure that the widgets in the registry have the bits they need. + - Check that a template is available and has a render callable. - Look for a data function, etc.. - - Ensure it has the right number of arguments, do things to help debugging. """ - raise NotImplementedError() for name, module in registry.items(): - pass + if not hasattr(module, 'template'): + raise AttributeError('%r has no "template"' % module) + if not hasattr(module.template, 'render'): + raise AttributeError('%r\'s template has no "render"' % module) + if not callable(module.template.render): + raise TypeError('%r\'s template.render not callable' % module) + + if not hasattr(module, 'data'): + raise AttributeError('%r has not "data" function' % module) + if not callable(module.data): + raise TypeError('%r\'s "data" is not callable' % module) + + if hasattr(module, 'chrome'): + if not callable(module.chrome): + raise TypeError('%r\'s "chrome" is not callable' % module) + def prepare_registry(registry): """ Do things ahead of time that we can to the registry. + - Wrap a cache layer around the data functions. - Wrap any chrome around the render functions. """ @@ -41,8 +56,7 @@ def prepare_registry(registry): # do this only if the module provides a cache invalidator # and warn in the logs if there's not one present? -# TODO -- actually call this -#validate_registry(registry) +validate_registry(registry) prepare_registry(registry) def render(module, request, session, widget, *args, **kwargs): @@ -50,8 +64,6 @@ def render(module, request, session, widget, *args, **kwargs): Call this to render a widget into HTML """ - - # The API returns exactly this data. Shared cache data = module.data(request, session, widget, *args, **kwargs) diff --git a/hubs/widgets/base.py b/hubs/widgets/base.py index de78e5e..410bab5 100644 --- a/hubs/widgets/base.py +++ b/hubs/widgets/base.py @@ -1,9 +1,9 @@ import collections import functools import hashlib -import urllib import flask +from six.moves import urllib_parse Argument = collections.namedtuple( 'Argument', ('name', 'default', 'validator', 'help')) @@ -43,7 +43,7 @@ def wraps(original): def avatar(username, size=32): openid = 'http://%s.id.fedoraproject.org/' % username - query = urllib.urlencode({'s': size, 'd': 'retro'}) - hash = hashlib.sha256(openid).hexdigest() + query = urllib_parse.urlencode({'s': size, 'd': 'retro'}) + hash = hashlib.sha256(openid.encode('utf-8')).hexdigest() template = "https://seccdn.libravatar.org/avatar/%s?%s" return template % (hash, query) diff --git a/hubs/widgets/chrome.py b/hubs/widgets/chrome.py index 07c4745..4ea42c6 100644 --- a/hubs/widgets/chrome.py +++ b/hubs/widgets/chrome.py @@ -1,4 +1,4 @@ -from base import wraps +from hubs.widgets.base import wraps import jinja2 diff --git a/hubs/widgets/dummy.py b/hubs/widgets/dummy.py index 68119b5..fb44797 100644 --- a/hubs/widgets/dummy.py +++ b/hubs/widgets/dummy.py @@ -1,5 +1,5 @@ -from chrome import panel -from base import argument +from hubs.widgets.chrome import panel +from hubs.widgets.base import argument import jinja2 diff --git a/hubs/widgets/stats.py b/hubs/widgets/stats.py index c5dbea0..a181453 100644 --- a/hubs/widgets/stats.py +++ b/hubs/widgets/stats.py @@ -1,4 +1,4 @@ -from chrome import panel +from hubs.widgets.chrome import panel import jinja2 diff --git a/hubs/widgets/sticky.py b/hubs/widgets/sticky.py index 416fd0e..60da776 100644 --- a/hubs/widgets/sticky.py +++ b/hubs/widgets/sticky.py @@ -1,5 +1,5 @@ -from chrome import panel -from base import argument +from hubs.widgets.chrome import panel +from hubs.widgets.base import argument import jinja2