plugin_api

This commit is contained in:
Martijn Voncken 2008-03-01 10:16:33 +00:00
commit 62c23829be
6 changed files with 134 additions and 39 deletions

View file

@ -57,7 +57,7 @@ def WebServer(debug = False):
middleware = [] middleware = []
import pages import pages
return create_webserver(pages.urls, pages, middleware) return create_webserver(pages.urls, pages.page_classes, middleware)
def run(debug = False): def run(debug = False):
server = WebServer(debug) server = WebServer(debug)

View file

@ -69,9 +69,10 @@ def register_admin_page(id, title, url):
@logcall @logcall
def unregister_admin_page(page_id): def unregister_admin_page(page_id):
for (i, (id, title, url)) in enumerate(admin_pages): for (i, (id, title, url)) in list(enumerate(admin_pages)):
if id == page_id: if id == page_id:
del admin_pages[i] del admin_pages[i]
return
#detail: #detail:
@logcall @logcall
@ -80,9 +81,16 @@ def register_detail_tab(id, title, page):
@logcall @logcall
def unregister_detail_tab(tab_id): def unregister_detail_tab(tab_id):
for (i, (id, title, tab)) in enumerate(detail_tabs): for (i, (id, title, tab)) in list(enumerate(detail_tabs)):
if id == tab_id: if id == tab_id:
del detail_tabs[i] del detail_tabs[i]
return
#def register_page
def register_page(url, method):
pass #TODO :(
#register vars in template. #register vars in template.
template.Template.globals["admin_pages"] = admin_pages template.Template.globals["admin_pages"] = admin_pages

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# deluge_webserver.py
# #
# Copyright (C) Martijn Voncken 2007 <mvoncken@gmail.com> # Copyright (C) Martijn Voncken 2008 <mvoncken@gmail.com>
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -36,22 +35,13 @@ from utils import *
import utils #todo remove the line above. import utils #todo remove the line above.
from render import render, error_page from render import render, error_page
import page_decorators as deco import page_decorators as deco
import config_tabs_webui #auto registers
import config_tabs_deluge #auto registers
from config import config_page from config import config_page
from torrent_options import torrent_options from torrent_options import torrent_options
from torrent_move import torrent_move from torrent_move import torrent_move
#debugerror
#plugin like api's
import menu_manager
from menu_manager import TB
#import forms
#
from debugerror import deluge_debugerror from debugerror import deluge_debugerror
web.webapi.internalerror = deluge_debugerror web.webapi.internalerror = deluge_debugerror
#
import lib.webpy022 as web import lib.webpy022 as web
from lib.webpy022.http import seeother, url from lib.webpy022.http import seeother, url
@ -63,8 +53,14 @@ import os
#from json_api import json_api #secuity leak, todo:fix #from json_api import json_api #secuity leak, todo:fix
#special/complex pages: #self registering pages:
from torrent_add import torrent_add import config_tabs_webui #auto registers
import config_tabs_deluge #auto registers
from torrent_add import torrent_add #todo: self-register.
#plugin like api's
import menu_manager
from menu_manager import TB
#plugin-like api's : register #plugin-like api's : register
@ -100,7 +96,7 @@ menu_manager.register_toolbar_item("recheck",_("Recheck"), "view-refresh.png" ,T
"POST","'/torrent/recheck/", False) "POST","'/torrent/recheck/", False)
#routing: #routing:
urls = ( urls = [
"/login", "login", "/login", "login",
"/index", "index", "/index", "index",
"/torrent/info/(.*)", "torrent_info", "/torrent/info/(.*)", "torrent_info",
@ -137,9 +133,11 @@ urls = (
"/", "home", "/", "home",
"", "home", "", "home",
"/robots.txt","robots" "/robots.txt","robots"
) ]
#/routing #/routing
#pages: #pages:
class login: class login:
@deco.deluge_page_noauth @deco.deluge_page_noauth
@ -418,8 +416,6 @@ class daemon_control:
time.sleep(1) #pause a while to let it start? time.sleep(1) #pause a while to let it start?
proxy.set_core_uri( uri ) proxy.set_core_uri( uri )
#other stuff: #other stuff:
class remote_torrent_add: class remote_torrent_add:
""" """
@ -467,3 +463,28 @@ class robots:
#/pages #/pages
#for plugins..
page_classes = dict(globals()) #test-1
def register_page(url, klass):
urls.append(url)
urls.append(klass.__name__)
page_classes[klass.__name__] = klass
def unregister_page(url, klass):
page_classes[klass.__name__] = None
"""
class test:
@deco.deluge_page
def GET(self, name):
return "HI"
register_page('/test(.*)', test)
print urls
print page_classes['index']
print page_classes['test']
"""

View file

@ -0,0 +1,52 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
# Copyright (C) Martijn Voncken 2008 <mvoncken@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
#
import lib.webpy022 as web
import lib.newforms_plus as forms
from render import render
from utils import logcall
from config import register_block as register_config_block
from config import unregister_block as unregister_config_block
from pages import register_page, unregister_page
from pages import register_page, unregister_page
from menu_manager import TB
from menu_manager import register_admin_page, unregister_admin_page
from menu_manager import register_toolbar_item, unregister_toolbar_item
register_template_path = render.register_template_path
unregister_template_path = render.unregister_template_path

View file

@ -1,8 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# webserver_framework.py
# #
# Copyright (C) Martijn Voncken 2007 <mvoncken@gmail.com> # Copyright (C) Martijn Voncken 2008 <mvoncken@gmail.com>
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -30,7 +29,6 @@
# this exception statement from your version. If you delete this exception # this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
#relative: #relative:
from webserver_common import ws,REVNO,VERSION from webserver_common import ws,REVNO,VERSION
from utils import * from utils import *
@ -48,23 +46,40 @@ class subclassed_render(object):
self.apply_cfg() self.apply_cfg()
def apply_cfg(self): def apply_cfg(self):
if not hasattr(ws,"config"): self.cache = ws.config.get('cache_templates')
print "render.py: WARNING,no config" self.renderers = []
return self.plugin_renderers = []
cache = ws.config.get('cache_templates') self.template_cache = {}
self.base_template = template.render(
os.path.join(ws.webui_path, 'templates/deluge/'),
cache=cache)
self.sub_template = template.render( #future: better/more subclassing.
self.renderers.append(template.render(
os.path.join(ws.webui_path, 'templates/%s/' % ws.config.get('template')), os.path.join(ws.webui_path, 'templates/%s/' % ws.config.get('template')),
cache=cache) cache=False))
self.renderers.append(template.render(
os.path.join(ws.webui_path, 'templates/deluge/'),cache=False))
@logcall
def register_template_path(self, path):
self.plugin_renderers.append(template.render(path))
@logcall
def unregister_template_path(self, path):
for i, renderer in list(ennumerate(self.plugin_renderers)):
if renderer.loc == path:
del self.plugin_renderers[i]
return
def __getattr__(self, attr): def __getattr__(self, attr):
if hasattr(self.sub_template, attr): if self.cache and attr in self.template_cache:
return getattr(self.sub_template, attr) return self.template_cache[attr]
else:
return getattr(self.base_template, attr) for renderer in self.renderers + self.plugin_renderers:
if hasattr(renderer, attr):
self.template_cache[attr] = getattr(renderer, attr)
return getattr(renderer, attr)
raise AttributeError, 'no template named "%s" ' % attr
def __getitem__(self, item): def __getitem__(self, item):
"for plugins/templates" "for plugins/templates"
@ -79,12 +94,10 @@ def error_page(error):
#template-defs: #template-defs:
def category_tabs(torrent_list): def category_tabs(torrent_list):
filter_tabs, category_tabs = get_category_choosers(torrent_list) filter_tabs, category_tabs = get_category_choosers(torrent_list)
return render.part_categories(filter_tabs, category_tabs) return render.part_categories(filter_tabs, category_tabs)
def template_crop(text, end): def template_crop(text, end):
try: try:
if len(text) > end: if len(text) > end:

View file

@ -130,6 +130,7 @@ class Ws:
def __init__(self): def __init__(self):
self.webui_path = os.path.dirname(__file__) self.webui_path = os.path.dirname(__file__)
self.env = 'UNKNOWN' self.env = 'UNKNOWN'
self.config = {}
try: try:
self.config_dir = deluge.common.CONFIG_DIR self.config_dir = deluge.common.CONFIG_DIR