make the new twisted webui runnable via deluge -u web

This commit is contained in:
Damien Churchill 2009-02-11 00:17:31 +00:00
commit 06bb660a10
4 changed files with 91 additions and 34 deletions

View file

@ -55,7 +55,7 @@ class UI:
ui = GtkUI(args) ui = GtkUI(args)
elif selected_ui == "web": elif selected_ui == "web":
log.info("Starting WebUI..") log.info("Starting WebUI..")
from deluge.ui.webui.webui import WebUI from deluge.ui.web.webui import WebUI
ui = WebUI(args) ui = WebUI(args)
elif selected_ui == "console": elif selected_ui == "console":
log.info("Starting ConsoleUI..") log.info("Starting ConsoleUI..")

View file

View file

@ -50,14 +50,43 @@ from deluge.ui.client import client
from deluge.ui.tracker_icons import TrackerIcons from deluge.ui.tracker_icons import TrackerIcons
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# Initialize gettext
try:
locale.setlocale(locale.LC_ALL, "")
if hasattr(locale, "bindtextdomain"):
locale.bindtextdomain("deluge", pkg_resources.resource_filename("deluge", "i18n"))
if hasattr(locale, "textdomain"):
locale.textdomain("deluge")
gettext.bindtextdomain("deluge", pkg_resources.resource_filename("deluge", "i18n"))
gettext.textdomain("deluge")
gettext.install("deluge", pkg_resources.resource_filename("deluge", "i18n"))
except Exception, e:
log.error("Unable to initialize gettext/locale: %s", e)
current_dir = os.path.dirname(__file__)
def rpath(path):
"""
Convert a relative path into an absolute path relative to the location
of this script.
"""
return os.path.join(current_dir, path)
class Template(MakoTemplate): class Template(MakoTemplate):
builtins = {
"_": gettext.gettext,
"version": common.get_version()
}
def render(self, *args, **data): def render(self, *args, **data):
data["_"] = gettext.gettext data.update(self.builtins)
data["version"] = common.get_version()
return MakoTemplate.render(self, *args, **data) return MakoTemplate.render(self, *args, **data)
class JSONException(Exception): pass class JSONException(Exception):
def __init__(self, inner_exception):
self.inner_exception = inner_exception
Exception.__init__(self, str(inner_exception))
class JSON(resource.Resource): class JSON(resource.Resource):
""" """
@ -160,7 +189,7 @@ class JSON(resource.Resource):
elif method in self._remote_methods: elif method in self._remote_methods:
return self._exec_remote(method, params), request_id return self._exec_remote(method, params), request_id
except Exception, e: except Exception, e:
raise JSONException(str(e)) raise JSONException(e)
def _on_rpc_request_finished(self, result, response, request): def _on_rpc_request_finished(self, result, response, request):
""" """
@ -173,7 +202,7 @@ class JSON(resource.Resource):
""" """
Handles any failures that occured while making an rpc call. Handles any failures that occured while making an rpc call.
""" """
print reason print type(reason)
request.setResponseCode(http.INTERNAL_SERVER_ERROR) request.setResponseCode(http.INTERNAL_SERVER_ERROR)
return "" return ""
@ -193,7 +222,7 @@ class JSON(resource.Resource):
""" """
Errback handler to return a HTTP code of 500. Errback handler to return a HTTP code of 500.
""" """
print reason raise reason
request.setResponseCode(http.INTERNAL_SERVER_ERROR) request.setResponseCode(http.INTERNAL_SERVER_ERROR)
return "" return ""
@ -301,7 +330,7 @@ class JSON(resource.Resource):
class GetText(resource.Resource): class GetText(resource.Resource):
def render(self, request): def render(self, request):
request.setHeader("content-type", "text/javascript") request.setHeader("content-type", "text/javascript")
template = Template(filename="gettext.js") template = Template(filename=rpath("gettext.js"))
return template.render() return template.render()
class Upload(resource.Resource): class Upload(resource.Resource):
@ -345,7 +374,7 @@ class Render(resource.Resource):
return "" return ""
filename = os.path.join("render", request.render_file) filename = os.path.join("render", request.render_file)
template = Template(filename=filename) template = Template(filename=rpath(filename))
request.setHeader("content-type", "text/html") request.setHeader("content-type", "text/html")
request.setResponseCode(http.OK) request.setResponseCode(http.OK)
return template.render() return template.render()
@ -378,16 +407,16 @@ class TopLevel(resource.Resource):
def __init__(self): def __init__(self):
resource.Resource.__init__(self) resource.Resource.__init__(self)
self.putChild("css", static.File("css")) self.putChild("css", static.File(rpath("css")))
self.putChild("gettext.js", GetText()) self.putChild("gettext.js", GetText())
self.putChild("icons", static.File("icons")) self.putChild("icons", static.File(rpath("icons")))
self.putChild("images", static.File("images")) self.putChild("images", static.File(rpath("images")))
self.putChild("js", static.File("js")) self.putChild("js", static.File(rpath("js")))
self.putChild("json", JSON()) self.putChild("json", JSON())
self.putChild("upload", Upload()) self.putChild("upload", Upload())
self.putChild("render", Render()) self.putChild("render", Render())
self.putChild("test", static.File("test.html")) self.putChild("test", static.File("test.html"))
self.putChild("themes", static.File("themes")) self.putChild("themes", static.File(rpath("themes")))
self.putChild("tracker", Tracker()) self.putChild("tracker", Tracker())
def getChild(self, path, request): def getChild(self, path, request):
@ -397,27 +426,23 @@ class TopLevel(resource.Resource):
return resource.Resource.getChild(self, path, request) return resource.Resource.getChild(self, path, request)
def render(self, request): def render(self, request):
template = Template(filename="index.html") template = Template(filename=rpath("index.html"))
request.setHeader("content-type", "text/html; charset=utf-8") request.setHeader("content-type", "text/html; charset=utf-8")
return template.render() return template.render()
setupLogger(level="debug") class DelugeWeb:
def __init__(self):
self.site = server.Site(TopLevel())
self.port = 8112
# Initialize gettext if __name__ == "__builtin__":
try: deluge_web = DelugeWeb()
locale.setlocale(locale.LC_ALL, "")
if hasattr(locale, "bindtextdomain"):
locale.bindtextdomain("deluge", pkg_resources.resource_filename("deluge", "i18n"))
if hasattr(locale, "textdomain"):
locale.textdomain("deluge")
gettext.bindtextdomain("deluge", pkg_resources.resource_filename("deluge", "i18n"))
gettext.textdomain("deluge")
gettext.install("deluge", pkg_resources.resource_filename("deluge", "i18n"))
except Exception, e:
log.error("Unable to initialize gettext/locale: %s", e)
site = server.Site(TopLevel())
application = service.Application("DelugeWeb") application = service.Application("DelugeWeb")
sc = service.IServiceCollection(application) sc = service.IServiceCollection(application)
i = internet.TCPServer(8112, site) i = internet.TCPServer(deluge_web.port, deluge_web.site)
i.setServiceParent(sc) i.setServiceParent(sc)
elif __name__ == "__main__":
from twisted.internet import reactor
deluge_web = DelugeWeb()
reactor.listenTCP(deluge_web.port, deluge_web.site)
reactor.run()

32
deluge/ui/web/webui.py Normal file
View file

@ -0,0 +1,32 @@
#
# deluge/ui/web/webui.py
#
# Copyright (C) 2009 Damien Churchill <damoxc@gmail.com>
#
# Deluge is free software.
#
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# deluge 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 deluge. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
#
from twisted.internet import reactor
class WebUI:
def __init__(self, args):
import server
deluge_web = server.DelugeWeb()
reactor.listenTCP(deluge_web.port, deluge_web.site)
reactor.run()