-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
390 lines (348 loc) · 15.3 KB
/
application.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import os
import os.path
import sys
from werkzeug.routing import Rule, Map, _rule_re
import medium.config as cfg
from medium.powlib import merge_two_dicts
from medium.database.sqldblib import Base, Session, engine
from medium.config import myapp
from medium.config import routes
from tornado.log import access_log
import logging
import datetime
from logging.handlers import RotatingFileHandler
#
# global logger settings
#
formatter = myapp["logformat"]
log_handler = logging.FileHandler(os.path.abspath(os.path.normpath(myapp["logfile"])))
#log_handler.setLevel(db_handler_log_level)
log_handler.setFormatter(formatter)
#
# analytics_log_handler
#
LOG_FILENAME = './pow_analytics.log'
# Set up a specific logger with our desired output level
analytics_logger = logging.getLogger('PowAnalyticsLogger')
analytics_logger.setLevel(logging.INFO)
# Add the log message handler to the logger (10 MB)
handler = logging.handlers.RotatingFileHandler(
LOG_FILENAME, maxBytes=10*1024*1024, backupCount=5)
analytics_logger.addHandler(handler)
class Application(tornado.web.Application):
#
# handlers class variable is filled by the @add_route decorator.
# merged with the instance variable in __init__
# so classic routes and @add_routes are merged.
#
handlers=[]
#routing list to handle absolute route positioning
handlers_tmp = []
def __init__(self):
self.handlers = routes
# importing !"activates" the add_route decorator
self.import_all_handlers()
h=getattr(self.__class__, "handlers", None)
#self.handlers+=h
# use the absolute positioning routing table
#print(list(reversed(sorted(htmp, key=get_key))))
# just for route ordering. (sotrted)
def get_key(item):
return item[1]
## working version:
#self.show_positioned_routes( list(reversed(sorted(h, key=get_key))) )
#hordered=[x[0] for x in reversed(sorted(h, key=get_key))]
## end!
hordered = self.order_routes(h)
#print(str(hordered))
self.handlers+=hordered
# merge two dictionaries: z = { **a, **b }
# http://stackoverflow.com/questions/38987/how-to-merge-two-python-dictionaries-in-a-single-expression
settings = merge_two_dicts( dict(
template_path=os.path.join(os.path.dirname(__file__), cfg.server_settings["template_path"]),
static_path=os.path.join(os.path.dirname(__file__), cfg.server_settings["static_path"])
) , cfg.server_settings)
super(Application, self).__init__(self.handlers, **settings)
self.Session = Session
self.engine = engine
self.Base = Base
def order_routes(self, routes):
""" order the routes.
1) if no pos is given, routes are appended in the order they "arrived"
2) positioned routes are inserted in this list afterwords.
3) the list is reversed (so pos=0 will be the last routes, pos=1 the second last ...)
You'll most probably add something like:
@app.add_route("/", pos=1) to IndexHandler
and:
@app.add_route(".*", pos=0) to yur ErrorHandler to catch all unhandled routes.
"""
def get_key(item):
return item[1]
hordered = []
tmp=[]
for elem in routes:
if elem[1] == -1:
hordered.append((elem[0], len(hordered)))
else:
tmp.append(elem)
tmp=sorted(tmp, key=get_key)
for elem in tmp:
hordered.insert(elem[1], (elem[0], elem[1]))
#self.show_positioned_routes(hordered)
#hordered=list(sorted(hordered, key=get_key))
hordered=reversed(hordered)
hordered=[x[0] for x in hordered]
return hordered
def log_request(self, handler, message=None):
"""
custom log method
access_log is importef from tornado.log (http://www.tornadoweb.org/en/stable/_modules/tornado/log.html)
access_log = logging.getLogger("tornado.access")
you can define you own log_function in config.py server_settings
"""
#super().log_request(handler)
if "log_function" in self.settings:
self.settings["log_function"](handler)
return
if handler.get_status() < 400:
log_method = access_log.info
elif handler.get_status() < 500:
log_method = access_log.warning
else:
log_method = access_log.error
request_time = 1000.0 * handler.request.request_time()
#log_method("%d %s %.2fms", handler.get_status(),
# handler._request_summary(), request_time)
log_method("%s %d %s %.2fms %s", handler.request.remote_ip, handler.get_status(),
handler._request_summary(), request_time, datetime.datetime.utcnow().strftime(myapp["date_format"])
)
if message:
log_method("%s %d %s %s",
handler.request.remote_ip,
handler.get_status(),
str(message),
datetime.datetime.utcnow().strftime(myapp["date_format"])
)
def log(self, message, status="INFO"):
"""
custom log method
access_log is importef from tornado.log (http://www.tornadoweb.org/en/stable/_modules/tornado/log.html)
access_log = logging.getLogger("tornado.access")
you can define you own log_function in config.py server_settings
status can be: INFO, WARN(INF), ERFR(OR)
"""
if "log_function" in self.settings:
self.settings["log_function"](status, message)
return
if status.lower()== "warn" or status == "warning":
log_method = access_log.warning
status="WARNING"
elif status.lower() == "error" or status == "err":
log_method = access_log.error
status="ERROR"
#log_method("%s %d %s", handler.request.remote_ip, handler.get_status(), str(message))
log_method("%s %s %s",
status,
message,
datetime.datetime.utcnow().strftime(myapp["date_format"]))
else:
log_method = access_log.info
status="INFO"
self.log("You have to give a message when using the application.log() function")
#raise Exception("You have to give a message when using the application.log() function")
def log_analytics(self, request):
"""
logs the request's:
remote IP,
URI
timestamp
for statistical reasons. So you have a basic analytics without installing
the BIg G ;)
"""
log_method=analytics_logger.info
x_real_ip="None"
try:
x_real_ip = request.headers.get("X-Real-IP")
except:
pass
log_method("%s %s %s %s %.2fms %s",
request.remote_ip,
x_real_ip,
request.method,
request.uri,
1000.0 * request.request_time(),
datetime.datetime.utcnow().strftime(myapp["date_format"])
)
def import_all_handlers(self):
"""
imports all handlers to execue the @add_routes decorator.
"""
import os
exclude_list=["base", "powhandler"]
#
# the list of handlers (excluding base. Add more you dont want
# to be loaded or inspected to exclude_list above.)
#
mods=[]
module_path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'handlers'))
#print("importing handlers from: " + module_path)
for mod in os.listdir( module_path ):
mod = mod.split(".")[0]
if not mod.startswith("_") and not mod in exclude_list:
#print(" now processing: " + str(mod))
mods.append(mod)
#print("mods: " + str(mods))
class_list = []
# load all the models from their modules (mods)
#print(str(mods))
import importlib
for m in mods:
#print("importing: " + 'medium.handlers.' + m)
try:
mod = importlib.import_module('medium.handlers.' + m)
except:
pass
#print(dir(mod))
def show_positioned_routes(self, routes):
"""
show all current routes.
"""
print(55*"-")
print(" Positioned Routes:")
print(55*"-")
for elem in routes:
print(str(elem))
def show_routes(self):
"""
show all current routes.
"""
routelist= [(handler.regex.pattern, handler.handler_class) for handler in self.handlers[0][1]]
print(55*"-")
print(" Routing table (order matters) :")
print(55*"-")
for elem in routelist:
print('{0:<20} {1:<30} '.format(elem[0], str(elem[1])))
#
# the RESTful route decorator v2
# with dedicated routes. One per default action
#
def add_rest_routes(self, route, api=None, pos=-1):
"""
cls is the class that will get the RESTful routes
it is automatically the decorated class
self in this decorator is Application
api will insert the given api version into the route (e.g. route=post, api=1.0)
/post/1.0/**all restroutes follow this pattern
1 GET /items #=> index (list)
2 GET /items/1 #=> show
3 GET /items/new #=> new
4 GET /items/1/edit #=> edit
5 GET /items/page/0 #=> page
6 PUT /items/1 #=> update
7 POST /items #=> create
8 DELETE /items/1 #=> destroy
"""
def decorator(cls):
# parent is the parent class of the relation
print("in add_rest_routes")
cls_name = cls.__name__.lower()
#print(cls_name)
action_part = route
if api:
action_part + r"/" + str(api)
# set the base_path fpr rest routes. So you can reference it in templates.
# see: handlers/base.success(...)
setattr(cls, "base_route_rest", action_part)
ID_PATTERN = cfg.myapp["id_pattern"]
routes = [
# tuple (http_method, route, { http_method : method_to_call_in_handler, .. })
( r"/" + action_part + r"/search/?" , { "get" : "search" }),
( r"/" + action_part + r"/list/?", {"get" : "list"}),
( r"/" + action_part + r"/new/?", {"get" : "new"}),
( r"/" + action_part + r"/page/?(?P<page>"+ID_PATTERN+")?/?", { "get" : "page", "params" : ["page"] }),
( r"/" + action_part + r"/show/?(?P<id>"+ID_PATTERN+")?/?", { "get" : "show" , "params" : ["id"]} ),
( r"/" + action_part + r"/(?P<id>"+ID_PATTERN+")/edit/?" , { "get" : "edit", "params" : ["id"] }),
( r"/" + action_part + r"/(?P<id>"+ID_PATTERN+")?/?",
{ "get" : "show" , "put" : "update", "delete" : "destroy", "params" : ["id"]} ),
( r"/" + action_part + r"/?", { "get" : cfg.myapp["default_rest_route"], "post" : "create", "put" : "update", "delete" : "destroy" })
]
routes.reverse()
# BETA: Add the .format regex to the RESTpattern
# this makes it possible to add a .format at an URL. Example /test/12.json (or /test/12/.json)
if cfg.beta_settings["dot_format"]:
routes = [(x[0]+ r"(?:/?\.\w+)?/?", x[1]) for x in routes]
#print("added the following routes: " + r)
handlers=getattr(self.__class__, "handlers", None)
try:
for elem in routes:
handlers.append( ((elem[0],cls, elem[1]), pos) )
except Exception as e:
print("Error in add_rest_routes")
raise e
print("ROUTING: added RESTful routes for: " + cls.__name__ + " as /" + action_part)
#print(dir())
return cls
return decorator
#
# the direct route decorator
#
def add_route(self, route, dispatch={}, pos=-1):
"""
cls is the class that will get the given route / API route
cls is automatically the decorated class
self in this decorator is Application
this will take a 1:1 raw tornado route
for regex with optional parts that are dropped
(Non copturing like: (?: ...) see:
http://stackoverflow.com/questions/9018947/regex-string-with-optional-parts
"""
def decorator(cls):
# parent is the parent class of the relation
cls_name = cls.__name__.lower()
handlers=getattr(self.__class__, "handlers", None)
if _rule_re.match(route):
########################################
# new style Werkzeug route
########################################
r=Rule(route, endpoint=cls_name)
m = Map()
m.add(r)
c=m.bind(cfg.server_settings["host"]+":"+cfg.server_settings["host"], "/")
r.compile()
#print("r1: " + str(r._regex.pattern))
pattern = r._regex.pattern.replace('^\|', "")
#print("r1: " + str(pattern))
fin_route = pattern
# convert the HTTP Methods in dispatch to lowercase
dispatch_lower=dict((k.lower(), v) for k,v in dispatch.items())
route_tuple = (fin_route,cls, dispatch_lower)
handlers.append((route_tuple,pos))
else:
###################################
# old style regex route
###################################
# BETA: this regex is added to every route to make
# 1.the slash at the end optional
# 2.it possible to add a .format paramter.
# Example: route = /test -> also test.json will work
# Example: route = /test/([0-9]+) -> also /test/12.xml will work
if cfg.beta_settings["dot_format"]:
fin_route = route + r"(?:/?\.\w+)?/?"
else:
fin_route = route
# convert the HTTP Methods in dispatch to lowercase
dispatch_lower=dict((k.lower(), v) for k,v in dispatch.items())
route_tuple = (fin_route,cls, dispatch_lower)
#route_tuple = (fin_route,cls, dispatch)
handlers.append((route_tuple,pos))
#print("handlers: " + str(self.handlers))
#print("ROUTING: added route for: " + cls.__name__ + ": " + route + " -> " + fin_route + " dispatch")
print("STD ROUTE (+) : handler: {}, route: {}, fin_route: {}, dispatch(lower): {} ".format(
str(cls.__name__), route, fin_route, str(dispatch_lower)))
return cls
return decorator
app=Application()