-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
297 lines (261 loc) · 8.59 KB
/
utils.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
# -*- coding:utf-8 -*-
'''
utils
'''
import sys
import math
import tornado.web
from tornado.options import options
startdir = sys.path[0]
class route(object):
"""
decorates RequestHandlers and builds up a list of routables handlers
Tech Notes (or "What the *@# is really happening here?")
--------------------------------------------------------
Everytime @route('...') is called, we instantiate a new route object which
saves off the passed in URI. Then, since it's a decorator, the function is
passed to the route.__call__ method as an argument. We save a reference to
that handler with our uri in our class level routes list then return that
class to be instantiated as normal.
Later, we can call the classmethod route.get_routes to return that list of
tuples which can be handed directly to the tornado.web.Application
instantiation.
Example
-------
@route('/some/path')
class SomeRequestHandler(RequestHandler):
def get(self):
goto = self.reverse_url('other')
self.redirect(goto)
# so you can do myapp.reverse_url('other')
@route('/some/other/path', name='other')
class SomeOtherRequestHandler(RequestHandler):
def get(self):
goto = self.reverse_url('SomeRequestHandler')
self.redirect(goto)
my_routes = route.get_routes()
"""
_routes = []
def __init__(self, uri, name=None, rank=1):
self._uri = uri
self.name = name
self.rank = rank
def __call__(self, _handler):
"""gets called when we class decorate"""
name = self.name and self.name or _handler.__name__
url = tornado.web.url(self._uri, _handler, name=name)
url.rank = self.rank
self._routes.append(url)
return _handler
@classmethod
def get_routes(cls):
'''get routes'''
import operator
routes = sorted(cls._routes,
key=operator.attrgetter('rank'), reverse=True)
return routes
@classmethod
def add_route(cls, url, rank=1):
'''add route'''
url.rank = rank
cls._routes.append(url)
# Use it as follows to redirect other paths into your decorated handler.
#
# from routes import route, route_redirect
# route_redirect('/smartphone$', '/smartphone/')
# route_redirect('/iphone/$', '/smartphone/iphone/', name='iphone_shortcut')
# @route('/smartphone/$')
# class SmartphoneHandler(RequestHandler):
# def get(self):
# ...
def route_redirect(from_, to, name=None):
'''route_redirect'''
route.add_route(tornado.web.url(from_, tornado.web.RedirectHandler,
dict(url=to), name=name))
def route_add(from_, _handler, name=None):
'''
route_add(r'/(.*)/', RedirectHandler)
'''
route.add_route(tornado.web.url(from_, _handler, name))
@route(r'/(.*)/')
class RedirectHandler(tornado.web.RequestHandler):
'''RedirectHandler'''
def initialize(self, permanent=True):
self._permanent = permanent
def get(self, url):
self.write(url)
if url and not url.startswith('/'):
url = '/' + url
self.redirect(url, permanent=self._permanent)
#连接数据库
from sqlalchemy import event
from sqlalchemy.engine import Engine
import time
#计算数据库查询的时间
@event.listens_for(Engine, "before_cursor_execute")
def before_cursor_execute(conn, cursor, statement,
parameters, context, executemany):
'''before_cursor_execute'''
conn.db_query_start_time = time.time()
@event.listens_for(Engine, "after_cursor_execute")
def after_cursor_execute(conn, cursor, statement,
parameters, context, executemany):
'''after_cursor_execute'''
total = time.time() - conn.db_query_start_time
if not hasattr(conn, 'db_query_times'):
conn.db_query_times = []
conn.db_query_times.append(total)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from tornado.options import options
import config
class Backend(object):
'''Backend'''
def __init__(self):
db_url = 'sqlite:///%s/%s' % (startdir, config.database_name)
engine = create_engine(
db_url,
convert_unicode=True,
encoding='utf-8',
echo=options.debug,
)
self._session = sessionmaker(bind=engine)
@classmethod
def instance(cls):
"""Singleton like accessor to instantiate backend object"""
if not hasattr(cls,"_instance"):
cls._instance = cls()
return cls._instance
def get_session(self):
'''get session'''
return self._session()
def dbhook(read_only=False, auto_commit=True):
'''数据库的hook'''
def _(func):
'''_'''
def wrapper(*a, **kw):
'''wrapper'''
_handler = a[0]
_handler.db = Backend.instance().get_session()
try:
result = func(*a, **kw)
finally:
if read_only:
_handler.db.rollback()
else:
if auto_commit:
_handler.db.commit()
_handler.db.close()
return result
return wrapper
return _
def get_int(val, value=0):
'''try get int, if except return value'''
try:
return int(val)
except ValueError:
return value
def get_pagecount(record_count, page_size):
'''计算页面的总数'''
if record_count == 0:
page_count = 0
else:
page_count = int(math.ceil(record_count / float(page_size)))
if page_count == 0 and record_count > 0:
page_count = 1
return page_count
def urlwrite(site_url, url=''):
'''url write'''
if site_url.endswith('/'):
s_url = site_url.rstrip('/')
else:
s_url = site_url
if url and not url.startswith('/'):
url = '/' + url
url = s_url + url
return url
import xml.etree.cElementTree as ET
def _dict_to_xml_recurse(parent, dictitem):
'''dict to xml recurse'''
assert type(dictitem) is not type([])
if isinstance(dictitem, dict):
for (tag, child) in dictitem.items():
if str(tag).startswith('__'):
parent.set(str(tag).lstrip('__'), str(child))
elif str(tag) == '_text':
parent.text = str(child)
elif type(child) is type([]):
# iterate through the array and convert
for listchild in child:
elem = ET.Element(tag)
parent.append(elem)
_dict_to_xml_recurse(elem, listchild)
else:
elem = ET.Element(tag)
parent.append(elem)
_dict_to_xml_recurse(elem, child)
else:
parent.text = str(dictitem)
def dict_to_et(xmldict):
"""
Converts a dictionary to an XML ElementTree Element
"""
roottag = list(xmldict.keys())[0]
root = ET.Element(roottag)
_dict_to_xml_recurse(root, xmldict[roottag])
return root
from xml.dom import minidom
def dict_to_xml(xmldict):
'''dict to xml'''
rough_string = ET.tostring(dict_to_et(xmldict), 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
class Cache(object):
data = {}
@classmethod
def get(cls, key):
if key in cls.data:
return cls.data.get(key)
return None
@classmethod
def set(cls, key, value, timeout = 0):
if value is not None:
cls.data[key] = value
return value
@classmethod
def delete(cls, key):
if key in cls.data:
cls.data.pop(key)
@classmethod
def clear(self):
self.data.clear()
import datetime
def cached(cache_key=None, timeout_seconds=3000, remove=False):
'''timeout = 60*60*24*100 s= 100 days
'''
def _(cls=None):
'''_'''
if not cache_key:
#key = (cls, tuple(a), tuple(sorted(kw.items())))
assert cls != None
key = (cls)
else:
key = cache_key
key_addtime = (key, '_addtime')
def do_cached(*a, **kw):
'''do cached'''
cached_obj = Cache.get(key)
if not cached_obj:
cached_obj = cls(*a, **kw)
Cache.set(key, cached_obj, timeout_seconds)
return cached_obj
def remove_cached(*a, **kw):
'''remove cached'''
Cache.delete(cache_key)
if cls:
return cls(*a, **kw)
if remove:
return remove_cached
else:
return do_cached
return _