From bb7c5cb4af54622a9e4b204626cfad422f0faf0d Mon Sep 17 00:00:00 2001 From: Martijn Voncken Date: Tue, 23 Sep 2008 17:34:57 +0000 Subject: [PATCH] egg_render for templates files in plugin/templates (#497) --- deluge/plugins/webuipluginbase.py | 40 ++++++++++++++++-------------- deluge/ui/webui/components.py | 2 ++ deluge/ui/webui/lib/egg_handler.py | 8 +----- deluge/ui/webui/lib/egg_render.py | 34 +++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 deluge/ui/webui/lib/egg_render.py diff --git a/deluge/plugins/webuipluginbase.py b/deluge/plugins/webuipluginbase.py index 414ff2267..82ad66085 100644 --- a/deluge/plugins/webuipluginbase.py +++ b/deluge/plugins/webuipluginbase.py @@ -38,35 +38,39 @@ import os api = component.get("WebPluginApi") +""" +def plugin_render_template(*args , **kwargs): + return "plugin-render" + +class plugin_render(): + def __getattr__(self, attr): + return plugin_render_template +""" + + class WebUIPluginBase: """ - /data is automatically served on http as as /plugin-name/data - templates /template are added to api.render + convenience class, you don't have to use this, but it does make things easyer to setup. + * static files: /data is automatically served on http as as /safe-plugin-name/data + * templates: /template are added to api.render.plugin-name. + * pages: urls attribute registers pages : urls = [(url, class), ..] """ + urls= [] def __init__(self, plugin_api, plugin_name): - log.debug("%s plugin initalized.." % plugin_name) + log.debug("%s plugin : start initalize.." % plugin_name) self.plugin = plugin_api self.plugin_name = plugin_name clean_plugin_name = plugin_name.lower().replace(" ","_") + for url , klass in self.urls: + api.page_manager.register_page(url, klass) + class egg_data_static(api.egg_handler): #serves files in /data from egg resource = clean_plugin_name base_path = "data" - template_dir = os.path.join(os.path.dirname(__file__),"template") - api.render.register_template_path(template_dir) + setattr(api.render, clean_plugin_name, api.egg_render(clean_plugin_name, "template")) + api.page_manager.register_page("/%s/data/(.*)" % clean_plugin_name , egg_data_static) - log.debug("%s plugin : end base initalize." % plugin_name) - - - - - - - - - - - - + log.debug("%s plugin : end initalize.." % plugin_name) diff --git a/deluge/ui/webui/components.py b/deluge/ui/webui/components.py index e37095df2..eac8e6076 100644 --- a/deluge/ui/webui/components.py +++ b/deluge/ui/webui/components.py @@ -48,6 +48,7 @@ manager = component.get("ClassName") from deluge import component import lib.newforms_plus as forms from lib.egg_handler import egg_handler +from lib.egg_render import egg_render from deluge.ui.client import aclient from deluge import component, pluginmanagerbase from deluge.configmanager import ConfigManager @@ -188,6 +189,7 @@ class PluginApi(component.Component): import lib.newforms_plus as forms self.egg_handler = egg_handler + self.egg_render = egg_render self.render = render self.web = web self.deco = deco diff --git a/deluge/ui/webui/lib/egg_handler.py b/deluge/ui/webui/lib/egg_handler.py index 83f0246c3..aba9c9b3d 100644 --- a/deluge/ui/webui/lib/egg_handler.py +++ b/deluge/ui/webui/lib/egg_handler.py @@ -4,7 +4,7 @@ # """ static fileserving for web.py -without the need for wsgi wrapper magic. +serves 1 directory from a packed egg, using pkg_resourses """ import web from web import url @@ -20,11 +20,6 @@ from StringIO import StringIO mimetypes.init() # try to read system mime.types import pkg_resources -""" -def get_resource(self, filename): - return pkg_resources.resource_filename("blocklist", os.path.join("data", filename)) - -""" class egg_handler: """ serves files directly from an egg @@ -60,7 +55,6 @@ if __name__ == '__main__': resource = "label" base_path = "data" - base_dir = os.path.expanduser('~') urls = ('/relative/(.*)','static_handler', '/(.*)','usr_static') diff --git a/deluge/ui/webui/lib/egg_render.py b/deluge/ui/webui/lib/egg_render.py new file mode 100644 index 000000000..ecf69a382 --- /dev/null +++ b/deluge/ui/webui/lib/egg_render.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +#(c) Martijn Voncken, mvoncken@gmail.com +#Same Licence as python 2.5 +# +""" +render object for web.py +renders from egg instead of directory. +""" +from web import template +import pkg_resources +import os + + +class egg_render: + """ + templates directly from an egg + """ + def __init__(self, resource, base_path , cache=False): + self.resource = resource + self.base_path = base_path + self.cache = cache + + def __getattr__(self, attr): + filename = attr + ".html" #<--bug, not consistent with the web.py renderer, that renderer ignores extensions. + template_data = pkg_resources.resource_string(self.resource, os.path.join(self.base_path, filename)) + c = template.Template(template_data, filename = filename) + if self.cache: + setattr(self, attr, c) + return c + + +if __name__ == '__main__': + #example: + pass