-
Notifications
You must be signed in to change notification settings - Fork 9
/
__init__.py
131 lines (98 loc) · 6.12 KB
/
__init__.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Pyramid imports
from pyramid.config import Configurator
from pyramid.request import Request
import pyramid.events
# SQLAlchemy imports
from .model import init_DBSession
# Beaker Sessions
import pyramid_beaker
# Other imports
import re
# Package Imports
from externals.lib.misc import convert_str_with_type, read_json
from externals.lib.pyramid.auto_format import registered_formats
from .templates import helpers as template_helpers
from externals.lib.socket.auth_echo_server import AuthEchoServerManager
# HACK! - Monkeypatch Mako 0.8.1 - HACK!
import mako.filters
html_escape_mako = mako.filters.html_escape
mako.filters.html_escape = lambda s: html_escape_mako(str(s) if not isinstance(s, str) else s)
def main(global_config, **settings):
"""
This function returns a Pyramid WSGI application.
"""
# Setup --------------------------------------------------------------------
# Db
init_DBSession(settings)
# Pyramid Global Settings
config = Configurator(settings=settings) #, autocommit=True
# Register Aditional Includes ---------------------------------------------
config.include('pyramid_mako') # The mako.directories value is updated in the scan for addons. We trigger the import here to include the correct folders.
# Beaker Session Manager
session_factory = pyramid_beaker.session_factory_from_settings(settings)
config.set_session_factory(session_factory)
# Parse/Convert setting keys that have specifyed datatypes
for key in config.registry.settings.keys():
config.registry.settings[key] = convert_str_with_type(config.registry.settings[key])
# Search Config ------------------------------------------------------------
import karakara.views.search
karakara.views.search.search_config = read_json(config.registry.settings['karakara.search.view.config'])
# WebSocket ----------------------------------------------------------------
def authenicator(key):
"""Only admin authenticated keys can connect to the websocket"""
session_data = session_factory(Request({'HTTP_COOKIE':'{0}={1}'.format(config.registry.settings['session.key'],key)}))
return session_data and session_data.get('admin')
socket_manager = AuthEchoServerManager(
authenticator=authenicator,
websocket_port=config.registry.settings['karakara.websocket.port'],
tcp_port =config.registry.settings.get('karakara.tcp.port'),
)
config.registry['socket_manager'] = socket_manager
socket_manager.start()
# Renderers ----------------------------------------------------------------
# AllanC - currently the auto_format decorator does all the formatting work
# it would be far preferable to use the pyramid renderer framework
# issue is, we want to set the renderer to be dynamic based on the url given
# I don't want to define EVERY method with loads of renderer tags
# and I don't want to define 5+ routes for every view callable with differnt formats
# We need a nice way of doing this in pyramid, and right now, after HOURS of trawling
# the doc and experimenting, I cant find one.
#from .renderers.auto_render_factory import AutoRendererFactory, handle_before_render
#config.add_renderer(None , AutoRendererFactory) #'renderers.auto_render_factory.auto_renderer_factory'
#config.add_renderer('.html', 'pyramid.mako_templating.renderer_factory')
#config.add_subscriber(handle_before_render , pyramid.events.BeforeRender) # maybe use this to set renderer?
# closeset ive seen
# http://zhuoqiang.me/a/restful-pyramid
# http://stackoverflow.com/questions/4633320/is-there-a-better-way-to-switch-between-html-and-json-output-in-pyramid
# Routes -------------------------------------------------------------------
# Static Routes
config.add_static_view(name='ext' , path='../externals/static') #cache_max_age=3600 # settings["static.assets"]
config.add_static_view(name='static', path='karakara:{0}'.format(settings["static.assets"])) #cache_max_age=3600 # settings["static.assets"]
config.add_static_view(name='player', path='karakara:{0}'.format(settings["static.player"]))
# AllanC - it's official ... static route setup and generation is a mess in pyramid
#config.add_static_view(name=settings["static.media" ], path="karakara:media" )
config.add_static_view(name='files' , path=settings["static.media"])
# Routes
def append_format_pattern(route):
return re.sub(r'{(.*)}', r'{\1:[^/\.]+}', route) + r'{spacer:[.]?}{format:(%s)?}' % '|'.join(registered_formats())
config.add_route('home' , append_format_pattern('/') )
config.add_route('track' , append_format_pattern('/track/{id}') )
config.add_route('track_list' , append_format_pattern('/track_list') )
config.add_route('queue' , append_format_pattern('/queue') )
config.add_route('fave' , append_format_pattern('/fave') )
config.add_route('admin_toggle' , append_format_pattern('/admin') )
config.add_route('admin_lock' , append_format_pattern('/admin_lock') )
config.add_route('remote' , append_format_pattern('/remote') )
config.add_route('feedback' , append_format_pattern('/feedback') )
config.add_route('settings' , append_format_pattern('/settings') )
config.add_route('random_images' , append_format_pattern('/random_images') )
config.add_route('inject_testdata' , append_format_pattern('/inject_testdata') )
config.add_route('search_tags' , '/search_tags/{tags:.*}')
config.add_route('search_list' , '/search_list/{tags:.*}')
# Events -------------------------------------------------------------------
config.add_subscriber(add_template_helpers_to_event, pyramid.events.BeforeRender)
# Return -------------------------------------------------------------------
config.scan(ignore='.tests')
return config.make_wsgi_app()
def add_template_helpers_to_event(event):
event['h'] = template_helpers