From 546d657151413d07435f3fb0b2691499ed4b6947 Mon Sep 17 00:00:00 2001 From: Kevin Dangoor Date: Thu, 17 Dec 2009 15:47:29 -0500 Subject: [PATCH] server now provides plugin metadata in JSON rather than as a script --- bespin/config.py | 4 ++++ bespin/controllers.py | 44 ++++-------------------------------- bespin/plugins.py | 34 ++++++++++++++++++++++++++++ bespin/tests/test_plugins.py | 8 +++---- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/bespin/config.py b/bespin/config.py index 662464e..1102c37 100644 --- a/bespin/config.py +++ b/bespin/config.py @@ -74,6 +74,7 @@ c.current_domain_cookie = True c.template_path = [path(__file__).dirname().abspath()] c.base_url = "https://bespin.mozilla.com/" +c.server_base_url = "server/" # Settings for sending email c.email_from = "invalid@ThisIsNotAValidEmailAddressUseSomethingElse.com" @@ -238,6 +239,9 @@ def load_pyconfig(configfile): print(c.fsroot) def activate_profile(): + if c.server_base_url and not c.server_base_url.endswith("/"): + c.server_base_url += "/" + for ep in pkg_resources.iter_entry_points("bespin_extensions"): ep.load() diff --git a/bespin/controllers.py b/bespin/controllers.py index 05efba8..15566cb 100644 --- a/bespin/controllers.py +++ b/bespin/controllers.py @@ -1105,50 +1105,16 @@ def run_deploy(request, response): return response() def _plugin_response(response, path=None, plugin_list=None): - response.content_type = "text/javascript" + response.content_type = "application/json" - parts = [] - metadata = dict() if plugin_list is None: plugin_list = plugins.find_plugins(path) + + metadata = dict((plugin.name, plugin.metadata) + for plugin in plugin_list if not plugin.errors) - for plugin in plugin_list: - if not plugin.errors: - name = plugin.name - - scripts = [] - for scriptname in plugin.scripts: - if plugin.location_name == "user": - url = "/server/getscript/file/at/%s%%3A%s" % ( - plugin.relative_location, scriptname) - else: - url = "/server/plugin/script/%s/%s/%s" % ( - plugin.location_name, name, scriptname) - scripts.append( - {"url": url, - "id": "%s:%s" % (name, scriptname)} - ) - - stylesheets = [] - for stylesheet in plugin.stylesheets: - if plugin.location_name == "user": - url = "/server/file/at/%s%%3A%s" % ( - plugin.relative_location, stylesheet) - else: - url = "/server/plugin/file/%s/%s/%s" % ( - plugin.location_name, name, stylesheet) - stylesheets.append( - {"url": url, - "id": "%s:%s" % (name, stylesheet)} - ) - - item = {"depends": plugin.depends, "scripts": scripts, - "stylesheets": stylesheets} - parts.append("""; tiki.register('%s', %s)""" % (name, simplejson.dumps(item))) - metadata[name] = plugin.metadata - parts.append("""; tiki.require("bespin:plugins").catalog.load(%s);""" % simplejson.dumps(metadata)) - response.body = "\n".join(parts) + response.body = simplejson.dumps(metadata) return response() @expose(r'^/plugin/register/defaults$', 'GET', auth=False) diff --git a/bespin/plugins.py b/bespin/plugins.py index bbc8da1..40dddd1 100644 --- a/bespin/plugins.py +++ b/bespin/plugins.py @@ -105,6 +105,40 @@ class Plugin(object): self._errors = ["Problem with metadata JSON: %s" % (e)] md = {} + server_base_url = config.c.server_base_url + name = self.name + + if self.location_name == "user": + md['scripts'] = [ + dict(url="%sgetscript/file/at/%s%%3A%s" % ( + server_base_url, self.relative_location, + scriptname), + id="%s:%s" % (name, scriptname)) + for scriptname in self.scripts + ] + md['stylesheets'] = [ + dict(url="%sfile/at/%s%%3A%s" % ( + server_base_url, self.relative_location, + stylesheet), + id="%s:%s" % (name, stylesheet)) + for stylesheet in self.stylesheets + ] + else: + md['scripts'] = [ + dict(url="%splugin/script/%s/%s/%s" % ( + server_base_url, self.location_name, + name, scriptname), + id="%s:%s" % (name, scriptname)) + for scriptname in self.scripts + ] + md['stylesheets'] = [ + dict(url="%splugin/file/%s/%s/%s" % ( + server_base_url, self.location_name, name, + stylesheet), + id="%s:%s" % (name, stylesheet)) + for stylesheet in self.stylesheets + ] + self._metadata = md return md diff --git a/bespin/tests/test_plugins.py b/bespin/tests/test_plugins.py index 4c9bdc1..be4bf19 100644 --- a/bespin/tests/test_plugins.py +++ b/bespin/tests/test_plugins.py @@ -176,7 +176,7 @@ def test_lookup_plugin(): def test_default_plugin_registration(): response = app.get("/plugin/register/defaults") - assert response.content_type == "text/javascript" + assert response.content_type == "application/json" assert "plugin1" in response.body assert "plugin/script/testplugins/plugin1/thecode.js" in response.body assert "plugin/file/testplugins/plugin1/resources/foo/foo.css" in response.body @@ -221,7 +221,7 @@ def test_user_installed_plugins(): response = app.put("/file/at/BespinSettings/plugins/BiggerPlugin/somedir/script.js", "exports.foo = 1;\n") response = app.get("/plugin/register/user") - assert response.content_type == "text/javascript" + assert response.content_type == "application/json" assert "MyPlugin" in response.body assert "BiggerPlugin" in response.body assert "file/at/BespinSettings/plugins/MyPlugin.js%3A" in response.body @@ -234,7 +234,7 @@ def test_user_installed_plugins(): "pluginOrdering": ["EditablePlugin"] }""") response = app.get("/plugin/register/user") - assert response.content_type == "text/javascript" + assert response.content_type == "application/json" assert "MyPlugin" in response.body assert "BiggerPlugin" in response.body assert "EditablePlugin" in response.body @@ -253,7 +253,7 @@ def test_plugin_reload(): _init_data() response = app.get("/plugin/reload/plugin2") print response.body - assert '"plugin2": {}' in response.body + assert '"plugin2": {' in response.body # just need the plugin, not its dependents assert '"depends": ["plugin2"]' not in response.body \ No newline at end of file