-
Notifications
You must be signed in to change notification settings - Fork 2k
/
common.py
133 lines (104 loc) · 3.63 KB
/
common.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
# encoding: utf-8
# This file contains commonly used parts of external libraries. The idea is
# to help in removing helpers from being used as a dependency by many files
# but at the same time making it easy to change for example the json lib
# used.
#
# NOTE: This file is specificaly created for
# from ckan.common import x, y, z to be allowed
import flask
import pylons
from flask_babel import gettext as flask_gettext
from pylons.i18n import _ as pylons_gettext, ungettext
from pylons import g, response
import simplejson as json
try:
from collections import OrderedDict # from python 2.7
except ImportError:
from sqlalchemy.util import OrderedDict
def is_flask():
'''
A centralised way to determine whether to return flask versions of common
functions, or Pylon versions.
Currently using the presence of `flask.request`, though we may want to
change that for something more robust.
'''
try:
pylons.request.environ
pylons_request_available = True
except TypeError:
pylons_request_available = False
if (flask.request and
(flask.request.environ.get('ckan.app') == 'flask_app' or
flask.request.environ.get('ckan.wsgiparty.setup') or
not pylons_request_available)):
return True
else:
return False
class Request(object):
'''
Wraps the request object, returning attributes from either the Flask or
Pylons request object, depending of whether flask.request is available.
'''
@property
def params(self):
''' Special case as request.params is used all over the place.
'''
if is_flask():
return flask.request.args
else:
return pylons.request.params
def __getattr__(self, name):
if is_flask():
return getattr(flask.request, name, None)
else:
return getattr(pylons.request, name, None)
request = Request()
def _(text):
# TODO: As is this will only work in the context of a web request
# Do we need something for non-web processes like paster commands?
# Pylons have the translator object which we need to fake but maybe
# that's not necessary at all
if is_flask():
# TODO: For some reasone the Flask gettext changes 'String %s' to
# 'String {}' (maybe it's the babel version?)
if '%s' in text:
return flask_gettext(text).replace('{}', '%s')
else:
return flask_gettext(text)
else:
return pylons_gettext(text)
class PylonsStyleContext(object):
def __getattr__(self, name):
if is_flask():
return getattr(flask.g, name, None)
else:
return getattr(pylons.c, name, None)
def __setattr__(self, name, value):
if is_flask():
return setattr(flask.g, name, value)
else:
return setattr(pylons.c, name, value)
def __delattr__(self, name):
if is_flask():
return delattr(flask.g, name, None)
else:
return delattr(pylons.c, name, None)
c = PylonsStyleContext()
class Session():
def __getattr__(self, name):
if is_flask():
return getattr(flask.session, name, None)
else:
return getattr(pylons.session, name, None)
def __setattr__(self, name, value):
if is_flask():
return setattr(flask.session, name, value)
else:
return setattr(pylons.session, name, value)
def __delattr__(self, name):
if is_flask():
return delattr(flask.session, name, None)
else:
return delattr(pylons.session, name, None)
session = Session()