plugin_api

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

View File

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

View File

@ -69,9 +69,10 @@ def register_admin_page(id, title, url):
@logcall
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:
del admin_pages[i]
return
#detail:
@logcall
@ -80,9 +81,16 @@ def register_detail_tab(id, title, page):
@logcall
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:
del detail_tabs[i]
return
#def register_page
def register_page(url, method):
pass #TODO :(
#register vars in template.
template.Template.globals["admin_pages"] = admin_pages

View File

@ -1,9 +1,8 @@
#!/usr/bin/env python
# -*- 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
# 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.
from render import render, error_page
import page_decorators as deco
import config_tabs_webui #auto registers
import config_tabs_deluge #auto registers
from config import config_page
from torrent_options import torrent_options
from torrent_move import torrent_move
#plugin like api's
import menu_manager
from menu_manager import TB
#import forms
#
#debugerror
from debugerror import deluge_debugerror
web.webapi.internalerror = deluge_debugerror
#
import lib.webpy022 as web
from lib.webpy022.http import seeother, url
@ -63,8 +53,14 @@ import os
#from json_api import json_api #secuity leak, todo:fix
#special/complex pages:
from torrent_add import torrent_add
#self registering pages:
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
@ -100,7 +96,7 @@ menu_manager.register_toolbar_item("recheck",_("Recheck"), "view-refresh.png" ,T
"POST","'/torrent/recheck/", False)
#routing:
urls = (
urls = [
"/login", "login",
"/index", "index",
"/torrent/info/(.*)", "torrent_info",
@ -137,9 +133,11 @@ urls = (
"/", "home",
"", "home",
"/robots.txt","robots"
)
]
#/routing
#pages:
class login:
@deco.deluge_page_noauth
@ -418,8 +416,6 @@ class daemon_control:
time.sleep(1) #pause a while to let it start?
proxy.set_core_uri( uri )
#other stuff:
class remote_torrent_add:
"""
@ -467,3 +463,28 @@ class robots:
#/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 -*-
#
# 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
# 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
# statement from all source files in the program, then also delete it here.
#relative:
from webserver_common import ws,REVNO,VERSION
from utils import *
@ -48,23 +46,40 @@ class subclassed_render(object):
self.apply_cfg()
def apply_cfg(self):
if not hasattr(ws,"config"):
print "render.py: WARNING,no config"
return
cache = ws.config.get('cache_templates')
self.base_template = template.render(
os.path.join(ws.webui_path, 'templates/deluge/'),
cache=cache)
self.cache = ws.config.get('cache_templates')
self.renderers = []
self.plugin_renderers = []
self.template_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')),
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):
if hasattr(self.sub_template, attr):
return getattr(self.sub_template, attr)
else:
return getattr(self.base_template, attr)
if self.cache and attr in self.template_cache:
return self.template_cache[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):
"for plugins/templates"
@ -79,12 +94,10 @@ def error_page(error):
#template-defs:
def category_tabs(torrent_list):
filter_tabs, category_tabs = get_category_choosers(torrent_list)
return render.part_categories(filter_tabs, category_tabs)
def template_crop(text, end):
try:
if len(text) > end:

View File

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