/
launch.py
executable file
·100 lines (82 loc) · 3.51 KB
/
launch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
import cwd
assert cwd
import os
from tornado.ioloop import IOLoop
from tornado.options import options, parse_command_line, parse_config_file
from tornado.web import Application
from breeze import uimodules
from breeze.apps.core.handlers.errors import NotFoundHandler
from breeze.apps.core.handlers.static import AppStaticFileHandler
from breeze.apps.core.urls import setup_urls
from breeze.registry import AdminRegistry, AppRegistry, FormRegistry
from breeze.template import MapTemplateLoader
class Breeze(object):
def __init__(self, project_dir, exclude_apps=()):
self.project_dir = project_dir
self.breeze_dir = os.path.abspath(os.path.dirname(__file__))
self.parse_options()
self.apps = AppRegistry((self.breeze_dir, self.project_dir), exclude=exclude_apps)
self.admins = AdminRegistry(self.apps)
self.forms = FormRegistry(self.apps)
self.settings = {
'cookie_secret': options.cookie_secret,
'debug': options.debug,
'login_url': '/sign-in/',
'static_path': os.path.join(self.breeze_dir, 'static'),
'static_handler_class': AppStaticFileHandler.create(self),
'template_loader': self.get_template_loader(),
'ui_modules': list(self.get_ui_modules()),
}
def get_template_loader(self):
def get_template_paths():
yield 'breeze', os.path.join(self.breeze_dir, 'templates')
for app in self.apps.keys():
for root in (self.breeze_dir, self.project_dir):
path = os.path.join(root, 'apps', app, 'templates')
if os.path.exists(path):
yield app, path
main_templates = (
os.path.join(self.project_dir, 'templates'),
os.path.join(self.breeze_dir, 'templates'),
)
return MapTemplateLoader(dict(get_template_paths()), *main_templates)
def get_ui_modules(self):
yield uimodules
for app in self.apps.values():
if hasattr(app, 'uimodules'):
yield app.uimodules
def get_urls(self):
if options.setup:
urls = list(setup_urls)
else:
urls = []
for app_name, app in self.apps.iteritems():
if hasattr(app, 'urls'):
try:
urls += app.urls.urls
except AttributeError, error:
raise AttributeError('%s: %s' % (app_name, error))
urls.append((r'/.*', NotFoundHandler))
return urls
def parse_options(self):
parse_config_file(os.path.join(self.breeze_dir, 'options.conf'))
parse_command_line()
if options.settings:
# Import the settings from the provided settings module.
current_settings = __import__(name=options.settings, fromlist=['*'])
for name in dir(current_settings):
if name in options:
value = getattr(current_settings, name)
option = options[name]
option.set(value)
# Parse the command line again as it has top priority.
parse_command_line()
def launch(self):
"""Create and run the application server."""
application = Application(self.get_urls(), **self.settings)
application.breeze = self
application.listen(options.listen_port, address=options.listen_address)
IOLoop.instance().start()
if __name__ == '__main__':
Breeze(cwd.root_dir).launch()