CSS is now registered with Tiki

This commit is contained in:
Kevin Dangoor 2009-12-09 14:28:54 -05:00
Родитель 613f55e4af
Коммит 6b29fbaac6
5 изменённых файлов: 76 добавлений и 14 удалений

Просмотреть файл

@ -37,6 +37,7 @@ from paste.auth import auth_tkt
from paste.proxy import Proxy
import simplejson
import tempfile
import static
from webob import Request, Response
from bespin.config import c
@ -1128,8 +1129,19 @@ def _plugin_response(response, path=None, plugin_list=None):
{"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)
item = {"depends": plugin.depends, "scripts": scripts}
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))
@ -1200,6 +1212,34 @@ def load_script(request, response):
script_text = plugin.get_script_text(script_path)
response.body = _wrap_script(plugin_name, script_path, script_text)
return response()
@expose(r'^/plugin/file/(?P<plugin_location>[^/]+)/(?P<plugin_name>[^/]+)/(?P<path>.*)', 'GET', auth=False)
def load_file(request, response):
plugin_name = request.kwargs['plugin_name']
plugin_location = request.kwargs['plugin_location']
script_path = request.kwargs['path']
if ".." in plugin_name or ".." in script_path or ".." in plugin_location:
raise BadRequest("'..' not allowed in plugin or script names")
path = None
for path_entry in c.plugin_path:
if path_entry['name'] == plugin_location:
path = path_entry
if path is None:
raise FileNotFound("Plugin location %s unknown" % (plugin_location))
plugin = plugins.lookup_plugin(plugin_name, [path])
if not plugin:
response.status = "404 Not Found"
response.content_type = "text/plain"
response.body = "Plugin " + plugin_name + " does not exist"
return response()
# use the static package to actually serve the file
newapp = static.Cling(plugin.location)
request.path_info = "/" + "/".join(request.path_info.split("/")[5:])
return newapp(request.environ, response.start_response)
@expose(r'^/plugin/reload/(?P<plugin_name>.+)', 'GET')
def reload_plugin(request, response):
@ -1303,7 +1343,6 @@ class URLRelayCompatibleProxy(Proxy):
def make_app():
from webob import Response
import static
static_app = static.Cling(c.static_dir)
if c.static_override:
from paste.cascade import Cascade

Просмотреть файл

@ -60,13 +60,13 @@ class BespinRequest(Request):
return None
class BespinResponse(Response):
def __init__(self, environ, start_request, **kw):
def __init__(self, environ, start_response, **kw):
super(BespinResponse, self).__init__(**kw)
self.environ = environ
self.start_request = start_request
self.start_response = start_response
def __call__(self):
return super(BespinResponse, self).__call__(self.environ, self.start_request)
return super(BespinResponse, self).__call__(self.environ, self.start_response)
def error(self, status, e):
self.status = status

Просмотреть файл

@ -48,7 +48,7 @@ class Plugin(object):
if md:
return md.get('depends', [])
return []
@property
def metadata(self):
try:
@ -90,19 +90,30 @@ class Plugin(object):
self._metadata = md
return md
@property
def scripts(self):
def _putFilesInAttribute(self, attribute, glob):
"""Finds all of the plugin files matching the given glob
and puts them in the attribute given. If the
attribute is already set, it is returned directly."""
try:
return self._scripts
return getattr(self, attribute)
except AttributeError:
loc = self.location
if loc.isdir():
scripts = [loc.relpathto(f) for f in self.location.walkfiles("*.js")]
l = [loc.relpathto(f) for f in self.location.walkfiles(glob)]
else:
scripts = [""]
self._scripts = scripts
return scripts
l = [""]
setattr(self, attribute, l)
return l
@property
def stylesheets(self):
return self._putFilesInAttribute("_stylesheets", "*.css")
@property
def scripts(self):
return self._putFilesInAttribute("_scripts", "*.js")
def get_script_text(self, scriptname):
"""Look up the script at scriptname within this plugin."""

Просмотреть файл

@ -0,0 +1 @@
body {}

Просмотреть файл

@ -105,6 +105,10 @@ def test_plugin_metadata():
errors = p.errors
assert errors
def test_plugin_stylesheets():
plugin = plugins.lookup_plugin("plugin1")
assert plugin.stylesheets == ["resources/foo/foo.css"]
def test_lookup_plugin():
plugin = plugins.lookup_plugin("DOES NOT EXIST")
assert plugin is None
@ -121,6 +125,7 @@ def test_default_plugin_registration():
assert response.content_type == "text/javascript"
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
assert "NOT THERE" not in response.body
assert "plugin3" not in response.body
@ -141,6 +146,12 @@ def test_get_single_file_script():
assert "exports.someFunction" in response.body
assert "SingleFilePlugin1:index" in response.body
def test_get_stylesheet():
response = app.get("/plugin/file/testplugins/plugin1/resources/foo/foo.css")
content_type = response.content_type
assert content_type == "text/css"
assert "body {}" in response.body
def test_bad_script_request():
response = app.get("/plugin/script/testplugins/NOPLUGIN/somefile.js", status=404)