diff options
-rw-r--r-- | libpathod/__init__.py | 20 | ||||
-rw-r--r-- | libpathod/app.py | 83 | ||||
-rw-r--r-- | libpathod/handlers.py | 45 | ||||
-rw-r--r-- | libpathod/templates/frame.html | 4 | ||||
-rwxr-xr-x | pathod | 9 | ||||
-rw-r--r-- | test/test_app.py | 35 | ||||
-rw-r--r-- | todo | 5 |
7 files changed, 128 insertions, 73 deletions
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 @@ <html lang="en"> <head> <meta charset="utf-8"> - <title>Omnid</title> + <title>Pathod</title> <link href="/static/bootstrap.min.css" rel="stylesheet"> <style type="text/css"> body { @@ -16,7 +16,7 @@ <div class="navbar navbar-fixed"> <div class="navbar-inner"> <div class="container"> - <a class="brand" href="#">Omnid</a> + <a class="brand" href="#">Pathod</a> <ul class="nav"> <li {% if section== "main" %} class="active" {% end %}><a href="/">Main</a></li> <li {% if section== "log" %} class="active" {% end %}><a href="/log">Log</a></li> @@ -1,6 +1,6 @@ #!/usr/bin/env python import argparse -import libpathod +from libpathod import app import tornado.ioloop if __name__ == "__main__": @@ -12,10 +12,11 @@ if __name__ == "__main__": ) args = parser.parse_args() - libpathod.application(staticdir=args.staticdir).listen(args.port) + application = app.PathodApp( + staticdir=args.staticdir + ) print "pathod listening on port %s"%args.port try: - tornado.ioloop.IOLoop.instance().start() + app.run(application, args.port, None) except KeyboardInterrupt: pass - diff --git a/test/test_app.py b/test/test_app.py new file mode 100644 index 00000000..6355a88a --- /dev/null +++ b/test/test_app.py @@ -0,0 +1,35 @@ +import libpry +from libpathod import app +from tornado import httpserver + + +class uApplication(libpry.AutoTree): + def dummy_page(self, path): + # A hideous, hideous kludge, but Tornado seems to have no more sensible + # way to do this. + a = app.PathodApp(staticdir=None) + for h in a.handlers[0][1]: + if h.regex.match(path): + klass = h.handler_class + r = httpserver.HTTPRequest("GET", path) + del r.connection + return klass(a, r) + + def test_create(self): + assert app.PathodApp(staticdir=None) + + def test_index(self): + page = self.dummy_page("/") + page.get() + assert "".join(page._write_buffer) + + def test_help(self): + page = self.dummy_page("/help") + page.get() + assert "".join(page._write_buffer) + + + +tests = [ + uApplication() +] @@ -1,6 +1,7 @@ -- Shortcuts - HTTPS - Anchors -- Sequences +- Logs, log reset, log retrieval +- Add anchors programmatically? +- Sequences |