Permalink
Browse files

add comments, change register app behavior

  • Loading branch information...
1 parent 183ca5f commit 32d133c2c202fbe64af2c70d0f1b5da58fb5656d lepture committed Apr 18, 2012
Showing with 81 additions and 4 deletions.
  1. +1 −1 example/app_template_example/app.py
  2. +73 −2 july/app.py
  3. +3 −0 july/template.py
  4. +4 −1 july/web.py
@@ -14,6 +14,6 @@ def get(self):
]
app = JulyApplication(handlers=handlers, debug=True)
-app.register_app(myapp, url_prefix='app')
+app.register_app(myapp, url_prefix='/app')
run_server(app)
View
@@ -7,6 +7,52 @@
class JulyApp(object):
+ """July App
+
+ July App is like BluePrint in Flask, it splits a tornado project into
+ several apps.
+
+ The basic usage, consider your app as::
+
+ myapp/
+ __init__.py
+ handlers.py
+
+ And in your ``~myapp.handlers`` define::
+
+ class MyHandler(RequestHandler):
+ def get(self):
+ self.write('hello july app')
+
+ handlers = [
+ ('/', MyHandler),
+ ]
+
+ app = JulyApp('a readable name', __name__, handlers=handlers)
+
+ # you may also add a handler by ``app.add_handler``
+ # app.add_handler(('/', MyHandler))
+
+ .. admonition::
+
+ You should always define an app in the same module level of your
+ handlers. And it is suggested that you use ``__name__`` all the time.
+
+ :param template_folder:
+
+ the folder name for templates in your app, for example, in your app::
+
+ myapp/
+ __init__.py
+ handlers.py
+ templates/
+
+ so that template_folder is ``templates``. And you can define your app
+ with app template supports::
+
+ app = JulyApp('name', __name__, template_folder='templates')
+
+ """
_first_register = True
def __init__(self, name, import_name, template_folder=None,
@@ -22,6 +68,9 @@ def __init__(self, name, import_name, template_folder=None,
else:
self.template_path = None
+ def add_handler(self, handler):
+ self.handlers.append(handler)
+
def first_register(self):
if not self._first_register:
return False
@@ -32,6 +81,28 @@ def first_register(self):
class JulyApplication(object):
"""July Application
+
+ This is a wrapper for ``tornado.web.Application``.
+ You define a JulyApplication::
+
+ application = JulyApplication()
+
+ and when this application is called, it creates a
+ ``tornado.web.Application``, which means, application() is somewhat a
+ ``tornado.web.Application()``.
+
+ so, you can do more with application before it is called.
+
+ The most useful task for JulyApplication is registering a July App.
+ Define you July App, and register it to your application::
+
+ simple_app = JulyApp('name', __name__, handlers=handlers)
+ application.register_app(simple_app)
+
+ And start your application::
+
+ application().listen(8888)
+ ioloop.IOLoop.instance().start()
"""
def __init__(self, handlers=None, default_host="", transforms=None,
@@ -66,10 +137,10 @@ def register_app_handlers(self, app, url_prefix):
else:
kwargs = {}
- pattern = '/%s%s' % (url_prefix, pattern)
+ pattern = '%s%s' % (url_prefix, pattern)
spec = URLSpec(pattern, handler, kwargs)
elif isinstance(spec, URLSpec):
- pattern = '/%s%s' % (url_prefix, spec.regex.pattern)
+ pattern = '%s%s' % (url_prefix, spec.regex.pattern)
spec = URLSpec(pattern, spec.handler_class,
spec.kwargs, spec.name)
View
@@ -3,6 +3,9 @@
class JulyTemplateLoader(Loader):
+ """July Template Loader
+ """
+
def __init__(self, root_directory, app, **kwargs):
super(JulyTemplateLoader, self).__init__(root_directory, **kwargs)
self.app = app
View
@@ -6,6 +6,9 @@
class JulyHandler(web.RequestHandler):
"""July Handler
+
+ Subclass JulyHandler to make an app, it provides a way to organize a July
+ App, and will support more features in the future.
"""
app_template = True
@@ -85,5 +88,5 @@ def run_server(app):
tornado.locale.load_translations(options.locale_path)
tornado.locale.set_default_locale(options.default_locale)
- logging.info('Start server at 0.0.0.0:%s' % options.port)
+ logging.info('Start server at %s:%s' % (options.address, options.port))
ioloop.IOLoop.instance().start()

0 comments on commit 32d133c

Please sign in to comment.