egg_render for templates files in plugin/templates (#497)

This commit is contained in:
Martijn Voncken 2008-09-23 17:34:57 +00:00
parent a870101dbe
commit bb7c5cb4af
4 changed files with 59 additions and 25 deletions

View File

@ -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)

View File

@ -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

View File

@ -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')

View File

@ -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