From b588f3b4ffde969967196c63f78ce5a7855430aa Mon Sep 17 00:00:00 2001 From: Gust Date: Tue, 27 Jan 2015 09:52:53 -0200 Subject: [PATCH] Add hotspots architecture --- colab/proxy/gitlab/gitlab_plugin.py | 7 +++++++ colab/proxy/management/commands/import_proxy_data.py | 2 +- colab/proxy/templatetags/proxy.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ colab/proxy/utils/plugin.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 colab/proxy/gitlab/gitlab_plugin.py create mode 100644 colab/proxy/utils/plugin.py diff --git a/colab/proxy/gitlab/gitlab_plugin.py b/colab/proxy/gitlab/gitlab_plugin.py new file mode 100644 index 0000000..1d2d9c3 --- /dev/null +++ b/colab/proxy/gitlab/gitlab_plugin.py @@ -0,0 +1,7 @@ +from colab.proxy.utils.plugin import ColabPlugin + + +class GitlabPlugin(ColabPlugin): + ''' + Implement the Hotspot functions here + ''' diff --git a/colab/proxy/management/commands/import_proxy_data.py b/colab/proxy/management/commands/import_proxy_data.py index e59d160..1e7a825 100644 --- a/colab/proxy/management/commands/import_proxy_data.py +++ b/colab/proxy/management/commands/import_proxy_data.py @@ -5,7 +5,7 @@ import importlib from django.core.management.base import BaseCommand from django.conf import settings -from colab.proxy.proxybase.proxy_data_api import ProxyDataAPI +from colab.proxy.utils.proxy_data_api import ProxyDataAPI class Command(BaseCommand): diff --git a/colab/proxy/templatetags/proxy.py b/colab/proxy/templatetags/proxy.py index 9b83a34..bca0419 100644 --- a/colab/proxy/templatetags/proxy.py +++ b/colab/proxy/templatetags/proxy.py @@ -1,9 +1,12 @@ +import importlib from django.core.urlresolvers import reverse from django import template from django.core.cache import cache from django.template.loader import render_to_string +from colab.proxy.utils.plugin import ColabPlugin + register = template.Library() @@ -46,3 +49,45 @@ def proxy_menu(context): cache.set(cache_key, menu) return menu + + +class PluginHotspot(template.Node): + def __init__(self, plugin_label, hotspot): + self.hotspot = hotspot + self.plugin_label = template.Variable(plugin_label) + + def render(self, context): + self.plugin_label = self.plugin_label.resolve(context) + module_name = \ + 'colab.proxy.' + self.plugin_label + '.' + self.plugin_label + \ + '_plugin' + + module = importlib.import_module(module_name) + + for module_item_name in dir(module): + + module_class = getattr(module, module_item_name) + + if not isinstance(module_class, type): + continue + + if not issubclass(module_class, ColabPlugin): + continue + + if module_class != ColabPlugin: + api = module_class() + break + + plugin_function = getattr(api, self.hotspot) + + return plugin_function(context) + + +def plugin_hotspot(parser, token): + bits = token.contents.split() + if len(bits) != 3: + raise TypeError("plugin_hotspot tag takes exactly two arguments") + + return PluginHotspot(bits[1], bits[2]) + +plugin_hotspot = register.tag(plugin_hotspot) diff --git a/colab/proxy/utils/plugin.py b/colab/proxy/utils/plugin.py new file mode 100644 index 0000000..b8a3ae3 --- /dev/null +++ b/colab/proxy/utils/plugin.py @@ -0,0 +1,73 @@ +import importlib + + +class ColabPlugin(object): + ''' + Class for declaring hotspots + A hotspot that is called from the html will always pass the context of that + file. + A hotspot executed from the code can have any number of parameters. + ''' + + def html_proxy_hotspot(self, context): + ''' + Example of html proxy, can be called in the html with: + + {% load proxy %} + + {% for proxy_label in proxy %} + {% plugin_hotspot proxy_label menu %} + {% endfor %} + + And at the plugin define a a function at plugiName_plugin.py + + def html_proxy_hotspot(self, context): + return render_to_string('proxy/html_template.html', + {'context_variable': python_variable}) + ''' + pass + + def update_profile(self, user, request): + ''' + Update the profile data, receives the user object and the UpdateView + object. Returns nothing + + Example of code hotspot, might be called somewhere in the code from: + + from colab.proxy.proxy.plugin import plugin_hotspot + from colab.settings import PROXIED_APPS + + + for plugin in PROXIED_APPS: + plugin_hotspot(plugin, 'update_profile', var1, var2) + ''' + pass + + +def plugin_hotspot(plugin_label, hotspot, *args): + module_name = \ + 'colab.proxy.' + plugin_label + '.' + plugin_label + \ + '_plugin' + + module = importlib.import_module(module_name) + + for module_item_name in dir(module): + + module_class = getattr(module, module_item_name) + + if not isinstance(module_class, type): + continue + + if not issubclass(module_class, ColabPlugin): + continue + + if module_class != ColabPlugin: + api = module_class() + break + + plugin_function = getattr(api, hotspot, None) + + if not plugin_function: + return None + + return plugin_function(*args) -- libgit2 0.21.2