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

View File

@ -33,6 +33,7 @@
# #
from webserver_common import ws, proxy, log from webserver_common import ws, proxy, log
from utils import * from utils import *
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_webui #auto registers
@ -40,6 +41,7 @@ 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
#import forms #import forms
# #
from debugerror import deluge_debugerror from debugerror import deluge_debugerror
@ -54,7 +56,7 @@ from lib.static_handler import static_handler
from operator import attrgetter from operator import attrgetter
import os import os
from json_api import json_api #from json_api import json_api #secuity leak, todo:fix
#special/complex pages: #special/complex pages:
from torrent_add import torrent_add from torrent_add import torrent_add
@ -85,9 +87,10 @@ urls = (
"/about", "about", "/about", "about",
"/logout", "logout", "/logout", "logout",
"/connect","connect", "/connect","connect",
"/daemon/control/(.*)","daemon_control",
#remote-api: #remote-api:
"/remote/torrent/add(.*)", "remote_torrent_add", "/remote/torrent/add(.*)", "remote_torrent_add",
"/json/(.*)","json_api", #"/json/(.*)","json_api",
#static: #static:
"/static/(.*)", "static", "/static/(.*)", "static",
"/template/static/(.*)", "template_static", "/template/static/(.*)", "template_static",
@ -328,12 +331,14 @@ class connect:
@deco.check_session @deco.check_session
@deco.deluge_page_noauth @deco.deluge_page_noauth
def GET(self, name): def GET(self, name):
if proxy.connected(): try:
error = _("Not Connected to a daemon") proxy.ping()
else: connected = proxy.get_core_uri()
error = None except:
connected = None
connect_list = ["http://localhost:58846"] connect_list = ["http://localhost:58846"]
return render.connect(connect_list, error) return render.connect(connect_list, connected)
def POST(self): def POST(self):
vars = web.input(uri = None, other_uri = None) vars = web.input(uri = None, other_uri = None)
@ -348,6 +353,34 @@ class connect:
proxy.set_core_uri(uri) proxy.set_core_uri(uri)
do_redirect() 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: #other stuff:
class remote_torrent_add: class remote_torrent_add:

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
#only for development/debugging.
import deluge_webserver import deluge_webserver
deluge_webserver.ws.init_06(uri = 'http://localhost:58846') 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="/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> </div>

View File

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

View File

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

View File

@ -1,12 +1,34 @@
$def with (connect_list, error) $def with (connect_list, connected)
$:render.header(_("Connect to Daemon")) $:render.header(_("Connect to Daemon"))
$:render.admin_toolbar('connect') $:render.admin_toolbar('connect')
<div class="panel" align="left"> <div class="panel" align="left">
$if error: $if connected:
<div class="error">$error</div> <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"> <form method="POST">
$for i, uri in enumerate(connect_list): $for i, uri in enumerate(connect_list):

View File

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

View File

@ -308,6 +308,30 @@ def get_newforms_data(form_class):
#/utils #/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): class WebUiError(Exception):
"""the message of these exceptions will be rendered in """the message of these exceptions will be rendered in
render.error(e.message) in debugerror.py""" render.error(e.message) in debugerror.py"""