From 5007a246fbe42b086a8e28887f0464a7a0d394c8 Mon Sep 17 00:00:00 2001 From: Brian Duggan Date: Fri, 25 May 2018 11:21:49 -0400 Subject: [PATCH] Update app directory structure, add home and routes --- app.py | 43 ++++++--------------------------------- atst/__init__.py | 0 atst/app.py | 13 ++++++++++++ atst/handler.py | 24 ++++++++++++++++++++++ atst/handlers/__init__.py | 0 atst/handlers/main.py | 5 +++++ atst/home.py | 3 +++ requirements.txt | 1 + tests/test_basic.py | 2 +- 9 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 atst/__init__.py create mode 100644 atst/app.py create mode 100644 atst/handler.py create mode 100644 atst/handlers/__init__.py create mode 100644 atst/handlers/main.py create mode 100644 atst/home.py diff --git a/app.py b/app.py index 8c1f0c08..b4e36c1e 100755 --- a/app.py +++ b/app.py @@ -1,42 +1,11 @@ #!/usr/bin/env python +from atst.app import make_app import tornado.ioloop -import tornado.web import os -from webassets import Environment, Bundle -# Set up assets. -static_path = os.path.join(os.path.dirname(__file__), "static") -scss_path = os.path.join(os.path.dirname(__file__), "scss") -assets = Environment(directory=scss_path, url='/static') -css = Bundle('atat.scss', filters='scss', output='../static/assets/out.css') -assets.register('css', css) -helpers = { - 'assets': assets -} - -class MainHandler(tornado.web.RequestHandler): - - def get_template_namespace(self): - ns = super(MainHandler, self).get_template_namespace() - ns.update(helpers) - return ns - - def get(self): - self.render("hello.html.to") - -def make_app(): - app = tornado.web.Application([ - (r"/", MainHandler), - ], - debug=os.getenv('DEBUG',False), - template_path=os.path.join(os.path.dirname(__file__), "templates"), - static_path=static_path - ) - return app - -if __name__ == "__main__": - app = make_app() - app.listen(8888) - print("Listening on http://localhost:8888") - tornado.ioloop.IOLoop.current().start() +app = make_app(debug=os.getenv('DEBUG',False)) +port = 8888 +app.listen(port) +print("Listening on http://localhost:%i" % port) +tornado.ioloop.IOLoop.current().start() diff --git a/atst/__init__.py b/atst/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/atst/app.py b/atst/app.py new file mode 100644 index 00000000..76cfa737 --- /dev/null +++ b/atst/app.py @@ -0,0 +1,13 @@ +import tornado.web +from atst.handlers.main import MainHandler +from atst.home import home + +def make_app(**kwargs): + app = tornado.web.Application([ + (r"/", MainHandler), + ], + template_path = home.child('templates'), + static_path = home.child('static'), + **kwargs + ) + return app diff --git a/atst/handler.py b/atst/handler.py new file mode 100644 index 00000000..9f47aedf --- /dev/null +++ b/atst/handler.py @@ -0,0 +1,24 @@ +import os +from webassets import Environment, Bundle +import tornado.web +from atst.home import home + +class BaseHandler(tornado.web.RequestHandler): + + def get_template_namespace(self): + assets = Environment( + directory = home.child('scss'), + url = '/static') + css = Bundle( + 'atat.scss', + filters = 'scss', + output = '../static/assets/out.css') + + assets.register( 'css', css ) + helpers = { + 'assets': assets + } + + ns = super(BaseHandler, self).get_template_namespace() + ns.update(helpers) + return ns diff --git a/atst/handlers/__init__.py b/atst/handlers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/atst/handlers/main.py b/atst/handlers/main.py new file mode 100644 index 00000000..fdde8fc3 --- /dev/null +++ b/atst/handlers/main.py @@ -0,0 +1,5 @@ +from atst.handler import BaseHandler + +class MainHandler(BaseHandler): + def get(self): + self.render("hello.html.to") diff --git a/atst/home.py b/atst/home.py new file mode 100644 index 00000000..8d72d7c0 --- /dev/null +++ b/atst/home.py @@ -0,0 +1,3 @@ +from unipath import Path + +home = Path( __file__ ).parent.parent diff --git a/requirements.txt b/requirements.txt index fbddde00..470daaa4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ tornado==5.0.2 webassets==0.12.1 pytest==3.6.0 pytest-tornado==0.5.0 +Unipath==1.1 diff --git a/tests/test_basic.py b/tests/test_basic.py index b4fc343b..76dd5c92 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1,6 +1,6 @@ import pytest import tornado.web -from app import make_app +from atst.app import make_app @pytest.fixture def app():