improved connect-page+web.reloader

This commit is contained in:
Martijn Voncken 2008-02-28 16:55:23 +00:00
parent 51dccc21ae
commit 8a4bf3ab15
9 changed files with 112 additions and 37 deletions

View File

@ -35,10 +35,11 @@ from webserver_common import ws
from lib.webpy022.request import webpyfunc
from lib.webpy022 import webapi
from lib.gtk_cherrypy_wsgiserver import CherryPyWSGIServer
import lib.webpy022 as web
import os
def create_webserver(urls, methods):
func = webapi.wsgifunc(webpyfunc(urls, methods, False))
def create_webserver(urls, methods, middleware):
func = webapi.wsgifunc(webpyfunc(urls, methods, False), *middleware)
server_address=("0.0.0.0", int(ws.config.get('port')))
server = CherryPyWSGIServer(server_address, func, server_name="localhost")
@ -49,14 +50,18 @@ def create_webserver(urls, methods):
print "http://%s:%d/" % server_address
return server
def WebServer():
import pages
return create_webserver(pages.urls, pages)
def WebServer(debug = False):
if debug:
middleware = [web.reloader]
else:
middleware = []
def run():
server = WebServer()
import pages
return create_webserver(pages.urls, pages, middleware)
def run(debug = False):
server = WebServer(debug)
try:
server.start()
except KeyboardInterrupt:
server.stop()

View File

@ -33,6 +33,7 @@
#
from webserver_common import ws, proxy, log
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
@ -40,6 +41,7 @@ import config_tabs_deluge #auto registers
from config import config_page
from torrent_options import torrent_options
from torrent_move import torrent_move
#import forms
#
from debugerror import deluge_debugerror
@ -54,7 +56,7 @@ from lib.static_handler import static_handler
from operator import attrgetter
import os
from json_api import json_api
#from json_api import json_api #secuity leak, todo:fix
#special/complex pages:
from torrent_add import torrent_add
@ -85,9 +87,10 @@ urls = (
"/about", "about",
"/logout", "logout",
"/connect","connect",
"/daemon/control/(.*)","daemon_control",
#remote-api:
"/remote/torrent/add(.*)", "remote_torrent_add",
"/json/(.*)","json_api",
#"/json/(.*)","json_api",
#static:
"/static/(.*)", "static",
"/template/static/(.*)", "template_static",
@ -328,12 +331,14 @@ class connect:
@deco.check_session
@deco.deluge_page_noauth
def GET(self, name):
if proxy.connected():
error = _("Not Connected to a daemon")
else:
error = None
try:
proxy.ping()
connected = proxy.get_core_uri()
except:
connected = None
connect_list = ["http://localhost:58846"]
return render.connect(connect_list, error)
return render.connect(connect_list, connected)
def POST(self):
vars = web.input(uri = None, other_uri = None)
@ -348,6 +353,34 @@ class connect:
proxy.set_core_uri(uri)
do_redirect()
class daemon_control:
@deco.check_session
def POST(self, command):
if command == 'stop':
proxy.shutdown()
elif command == 'start':
self.start()
elif command == 'restart':
proxy.shutdown()
self.start()
else:
raise Exception('Unknown command:"%s"' % command)
seeother('/connect')
def start(self):
import time
uri = web.input(uri = None).uri
if not uri:
uri = 'http://localhost:58846'
port = int(uri.split(':')[2])
utils.daemon_start_localhost(port)
time.sleep(1) #pause a while to let it start?
proxy.set_core_uri( uri )
#other stuff:
class remote_torrent_add:

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python
#only for development/debugging.
import deluge_webserver
deluge_webserver.ws.init_06(uri = 'http://localhost:58846')
deluge_webserver.run()
deluge_webserver.run(debug = True)

View File

@ -13,8 +13,8 @@ $else:
<a href="/refresh/on">$_('Enable')</a><!--WRONG, setting things on a GET-->
]
<a href="/logout">$_('Logout')</a><!--WRONG, setting things on a GET-->
<a href="/config/">$_('Settings')</a>
<a href="/config/">$_('Admin') </a>&nbsp;
</div>

View File

@ -1,4 +1,5 @@
$:render.header(_('About'))
$:render.admin_toolbar('about')
<div class="panel" style="text-align:left">
<h2>Version</h2>
<pre>$version </pre>
@ -20,20 +21,12 @@ $:render.header(_('About'))
<li>Martijn Voncken</li>
</ul>
<h3>Template</h3>
<ul>
<li>Martijn Voncken</li>
<li>somedude</li>
</ul>
<h3>Deluge</h3>
<ul>
<li>Zach Tibbitts</li>
<li>Alon Zakai</li>
<li>Alon Zakai</li>
<li>Marcos Pinto</li>
<li>Andrew Resch</li>
<li>Zach Tibbitts</li>
<li>Alon Zakai</li>
<li>Alex Dedul</li>
</ul>
@ -41,9 +34,7 @@ $:render.header(_('About'))
<ul>
<li>Slurdge</li>
</ul>
</ul>
*and all other authors/helpers/contributors I forgot to mention.
</div>
*and all other authors/helpers/contributors I forgot to mention</li>
$:render.footer()

View File

@ -4,5 +4,7 @@ $def with (active_tab)
<!--<a href='/log'>Log</a>
<a href='/alerts'>Alerts</a>-->
<a href='/connect'>Connect</a>
<a href='/about'>About</a>
<a href='/logout'>Logout</a>
</div>

View File

@ -1,12 +1,34 @@
$def with (connect_list, error)
$def with (connect_list, connected)
$:render.header(_("Connect to Daemon"))
$:render.admin_toolbar('connect')
<div class="panel" align="left">
$if error:
<div class="error">$error</div>
$if connected:
<table><tr>
<td>
$_('Connected to')
$connected
</td><td>
<form method="POST" action="/daemon/control/stop">
<input type="submit" name="stop"
value="$_('Stop')">
</form>
</td><td>
<form method="POST" action="/daemon/control/restart">
<input type="submit" name="restart"
value="$_('Restart')">
</form>
</td></tr></table>
$else:
<div class="error">$_("Not Connected to a daemon")
</div>
<form method="POST" action="/daemon/control/start">
<input type="hidden" name="uri" value="$connect_list[0]">
<input type="submit" name="stop"
value="$_('Start') $connect_list[0]">
</form>
<form method="POST">
$for i, uri in enumerate(connect_list):

View File

@ -16,9 +16,6 @@ $if error > 0:
<input type="submit" name="submit"
id="submit" value="Submit" class="form_input">
</div>
<br />
<a href="/about">$_('About')</a>
</div>
</form>
</div>

View File

@ -308,6 +308,30 @@ def get_newforms_data(form_class):
#/utils
#generic/ non-webui utils todo: move to trunk/core.
def daemon_test_online_status(uri):
"""Tests the status of URI.. Returns True or False depending on status.
"""
online = True
host = None
try:
host = xmlrpclib.ServerProxy(uri)
host.ping()
except socket.error:
online = False
del host
self.online_status[uri] = online
return online
def daemon_start_localhost(port):
"""Starts a localhost daemon"""
port = str(port)
log.info("Starting localhost:%s daemon..", port)
# Spawn a local daemon
os.popen("deluged -p %s" % port)
#exceptions:
class WebUiError(Exception):
"""the message of these exceptions will be rendered in
render.error(e.message) in debugerror.py"""