From 77eca33f2695eea690dff7999c0e1bd3df0e1733 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 29 Apr 2012 10:56:33 +1200 Subject: Refactor application definitions and startup. Also, create one of the dodgiest web testing trusses in history. Tornado just seems to have no nice way of doing this. --- libpathod/__init__.py | 20 ---------- libpathod/app.py | 83 ++++++++++++++++++++++++++++++++++++++++++ libpathod/handlers.py | 45 ----------------------- libpathod/templates/frame.html | 4 +- 4 files changed, 85 insertions(+), 67 deletions(-) create mode 100644 libpathod/app.py delete mode 100644 libpathod/handlers.py (limited to 'libpathod') diff --git a/libpathod/__init__.py b/libpathod/__init__.py index ff1b88f8..8b137891 100644 --- a/libpathod/__init__.py +++ b/libpathod/__init__.py @@ -1,21 +1 @@ -from tornado import web, template -import handlers, utils -class PathodApp(web.Application): - def __init__(self, *args, **kwargs): - self.templates = template.Loader(utils.data.path("templates")) - web.Application.__init__(self, *args, **kwargs) - - -def application(**settings): - return PathodApp( - [ - (r"/", handlers.Index), - (r"/log", handlers.Log), - (r"/help", handlers.Help), - (r"/preview", handlers.Preview), - (r"/p/.*", handlers.Pathod, settings), - ], - static_path = utils.data.path("static"), - template_path = utils.data.path("templates"), - ) diff --git a/libpathod/app.py b/libpathod/app.py new file mode 100644 index 00000000..15d99023 --- /dev/null +++ b/libpathod/app.py @@ -0,0 +1,83 @@ +import urllib +import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver +import rparse, utils + +class _Page(tornado.web.RequestHandler): + def render(self, name, **kwargs): + b = self.application.templates.load(name + ".html").generate(**kwargs) + self.write(b) + + +class Index(_Page): + name = "index" + section = "main" + def get(self): + self.render(self.name, section=self.section) + + +class Preview(_Page): + name = "preview" + section = "main" + def get(self): + self.render(self.name, section=self.section) + + +class Help(_Page): + name = "help" + section = "help" + def get(self): + self.render(self.name, section=self.section) + + +class Log(_Page): + name = "log" + section = "log" + def get(self): + self.render(self.name, section=self.section) + + +class Pathod(object): + anchor = "/p/" + def __init__(self, application, request, **settings): + self.application, self.request, self.settings = application, request, settings + spec = urllib.unquote(self.request.uri)[len(self.anchor):] + try: + self.response = rparse.parse(self.settings, spec) + except rparse.ParseException, v: + self.response = rparse.InternalResponse( + 800, + "Error parsing response spec: %s\n"%v.msg + v.marked() + ) + + def _execute(self, transforms, *args, **kwargs): + self.response.render(self.request) + + +class PathodApp(tornado.web.Application): + def __init__(self, **settings): + self.templates = tornado.template.Loader(utils.data.path("templates")) + tornado.web.Application.__init__( + self, + [ + (r"/", Index), + (r"/log", Log), + (r"/help", Help), + (r"/preview", Preview), + (r"/p/.*", Pathod, settings), + ], + static_path = utils.data.path("static"), + template_path = utils.data.path("templates"), + debug=True + ) + + +# begin nocover +def run(application, port, ssl_options): + http_server = tornado.httpserver.HTTPServer( + application, + ssl_options=ssl_options + ) + http_server.listen(port) + tornado.ioloop.IOLoop.instance().start() + + diff --git a/libpathod/handlers.py b/libpathod/handlers.py deleted file mode 100644 index ebf85b03..00000000 --- a/libpathod/handlers.py +++ /dev/null @@ -1,45 +0,0 @@ -import urllib -import tornado.web -import rparse - -class _Page(tornado.web.RequestHandler): - def render(self, name, **kwargs): - b = self.application.templates.load(name).generate(**kwargs) - self.write(b) - - -class Index(_Page): - def get(self): - self.render("index.html", section="main") - - -class Preview(_Page): - def get(self): - self.render("index.html", section="main") - - -class Help(_Page): - def get(self): - self.render("help.html", section="help") - - -class Log(_Page): - def get(self): - self.render("log.html", section="log") - - -class Pathod(object): - anchor = "/p/" - def __init__(self, application, request, **settings): - self.application, self.request, self.settings = application, request, settings - spec = urllib.unquote(self.request.uri)[len(self.anchor):] - try: - self.response = rparse.parse(self.settings, spec) - except rparse.ParseException, v: - self.response = rparse.InternalResponse( - 800, - "Error parsing response spec: %s\n"%v.msg + v.marked() - ) - - def _execute(self, transforms, *args, **kwargs): - self.response.render(self.request) diff --git a/libpathod/templates/frame.html b/libpathod/templates/frame.html index f985aaa1..08dbd1ae 100644 --- a/libpathod/templates/frame.html +++ b/libpathod/templates/frame.html @@ -2,7 +2,7 @@ - Omnid + Pathod