/
__init__.py
343 lines (287 loc) · 12.2 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
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
from django.conf import settings, UserSettingsHolder
from django.core import signals
from django.core.exceptions import ImproperlyConfigured
from django.dispatch import dispatcher
try:
# Only exists in Python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
__all__ = ('backend', 'connection', 'DatabaseError')
# singleton to represent the default connection in connections
class dummy(object):
def __str__(self):
return '<default>'
_default = dummy()
del dummy
# storage for local default connection
_local = local()
if not settings.DATABASE_ENGINE:
settings.DATABASE_ENGINE = 'dummy'
def connect(settings):
"""Connect to the database specified in settings. Returns a
ConnectionInfo on success, raises ImproperlyConfigured if the
settings don't specify a valid database connection.
"""
info = ConnectionInfo(settings)
# Register an event that closes the database connection
# when a Django request is finished.
dispatcher.connect(info.close, signal=signals.request_finished)
# Register an event that resets connection.queries
# when a Django request is started.
dispatcher.connect(info.reset_queries, signal=signals.request_started)
return info
class ConnectionInfo(object):
"""Encapsulates all information about a connection and the backend
to which it belongs. Provides methods for loading backend
creation, introspection, and shell modules, closing the
connection, and resetting the connection's query log.
"""
def __init__(self, settings=None):
if settings is None:
from django.conf import settings
self.settings = settings
self.backend = self.load_backend()
self.connection = self.backend.DatabaseWrapper(settings)
self.DatabaseError = self.backend.DatabaseError
def __repr__(self):
return "Connection: %r (ENGINE=%s NAME=%s)" \
% (self.connection,
self.settings.DATABASE_ENGINE,
self.settings.DATABASE_NAME)
def close(self):
"""Close connection"""
self.connection.close()
def get_introspection_module(self):
return __import__('django.db.backends.%s.introspection' %
self.settings.DATABASE_ENGINE, '', '', [''])
def get_creation_module(self):
return __import__('django.db.backends.%s.creation' %
self.settings.DATABASE_ENGINE, '', '', [''])
def load_backend(self):
try:
backend = __import__('django.db.backends.%s.base' %
self.settings.DATABASE_ENGINE, '', '', [''])
except ImportError, e:
# The database backend wasn't found. Display a helpful error
# message listing all possible database backends.
import os
backend_dir = os.path.join(__path__[0], 'backends')
available_backends = [f for f in os.listdir(backend_dir)
if not f.startswith('_') \
and not f.startswith('.') \
and not f.endswith('.py') \
and not f.endswith('.pyc')]
available_backends.sort()
if self.settings.DATABASE_ENGINE not in available_backends:
raise ImproperlyConfigured, \
"%r isn't an available database backend. "\
"Available options are: %s" % \
(self.settings.DATABASE_ENGINE,
", ".join(map(repr, available_backends)))
else:
# If there's some other error, this must be an error
# in Django itself.
raise
return backend
def runshell(self):
__import__('django.db.backends.%s.client' %
self.settings.DATABASE_ENGINE, '', '', ['']
).runshell(self.settings)
def reset_queries(self):
"""Reset log of queries executed by connection"""
self.connection.queries = []
class LazyConnectionManager(object):
"""Manages named connections lazily, instantiating them as
they are requested.
"""
def __init__(self):
self.local = local()
self.local.connections = {}
def __iter__(self):
return self.local.connections.keys()
def __getattr__(self, attr):
return getattr(self.local.connections, attr)
def __getitem__(self, k):
try:
return self.local.connections[k]
except (AttributeError, KeyError):
return self.connect(k)
def __setitem__(self, k, v):
try:
self.local.connections[k] = v
except AttributeError:
# First access in thread
self.local.connections = {k: v}
def connect(self, name):
"""Return the connection with this name in
settings.OTHER_DATABASES. Creates the connection if it doesn't yet
exist. Reconnects if it does. If the name requested is the default
connection (a singleton defined in django.db), then the default
connection is returned.
"""
try:
cnx = self.local.connections
except AttributeError:
cnx = self.local.connections = {}
if name in cnx:
cnx[name].close()
if name is _default:
cnx[name] = connect(settings)
return cnx[name]
try:
info = settings.OTHER_DATABASES[name]
except KeyError:
raise ImproperlyConfigured, \
"No database connection '%s' has been configured" % name
except AttributeError:
raise ImproperlyConfigured, \
"No OTHER_DATABASES in settings."
# In settings it's a dict, but connect() needs an object:
# pass global settings so that the default connection settings
# can be defaults for the named connections.
database = UserSettingsHolder(settings)
for k, v in info.items():
setattr(database, k, v)
cnx[name] = connect(database)
return cnx[name]
def reset(self):
self.local.connections = {}
def model_connection_name(klass):
"""Get the connection name that a model is configured to use, with the
current settings.
"""
app = klass._meta.app_label
model = klass.__name__
app_model = "%s.%s" % (app, model)
# Quick exit if no OTHER_DATABASES defined
if (not hasattr(settings, 'OTHER_DATABASES')
or not settings.OTHER_DATABASES):
return _default
# Look in MODELS for the best match: app_label.Model. If that isn't
# found, take just app_label. If nothing is found, use the default
maybe = None
for name, db_def in settings.OTHER_DATABASES.items():
if not 'MODELS' in db_def:
continue
mods = db_def['MODELS']
# Can't get a better match than this
if app_model in mods:
return name
elif app in mods:
if maybe is not None:
raise ImproperlyConfigured, \
"App %s appears in more than one OTHER_DATABASES " \
"setting (%s and %s)" % (maybe, name)
maybe = name
if maybe:
return maybe
# No named connection for this model; use the default
return _default
class ConnectionInfoDescriptor(object):
"""Descriptor used to access database connection information from a
manager or other connection holder. Keeps a thread-local cache of
connections per instance, and always returns the same connection for an
instance in particular thread during a particular request.
Any object that includes a ``model`` attribute that holds a model class
can use this descriptor to manage connections.
"""
def __init__(self):
self.local = local()
self.local.cnx = {}
dispatcher.connect(self.reset, signal=signals.request_finished)
def __get__(self, instance, type=None):
if instance is None:
raise AttributeError, \
"ConnectionInfo is accessible only through an instance"
try:
instance_connection = self.local.cnx.get(instance, None)
except AttributeError:
# First access in this thread
self.local.cnx = {}
instance_connection = None
if instance_connection is None:
instance_connection = self.get_connection(instance)
self.local.cnx[instance] = instance_connection
return instance_connection
def __set__(self, instance, value):
try:
self.local.cnx[instance] = instance_connection
except AttributeError:
# First access in thread
self.local.cnx = {instance: instance_connection}
def __delete__(self, instance):
try:
del self.local.cnx[instance]
except (AttributeError, KeyError):
# Not stored, no need to reset
pass
def get_connection(self, instance):
return connections[model_connection_name(instance.model)]
def reset(self):
self.local.cnx = {}
class LocalizingProxy:
"""A lazy-initializing proxy. The proxied object is not
initialized until the first attempt to access it. This is used to
attach module-level properties to local storage.
"""
def __init__(self, name, storage, func, *arg, **kw):
self.__name = name
self.__storage = storage
self.__func = func
self.__arg = arg
self.__kw = kw
def __getattr__(self, attr):
# Private (__*) attributes are munged
if attr.startswith('_LocalizingProxy'):
return self.__dict__[attr]
try:
return getattr(getattr(self.__storage, self.__name), attr)
except AttributeError:
setattr(self.__storage, self.__name, self.__func(*self.__arg,
**self.__kw))
return getattr(getattr(self.__storage, self.__name), attr)
def __setattr__(self, attr, val):
# Private (__*) attributes are munged
if attr.startswith('_LocalizingProxy'):
self.__dict__[attr] = val
return
try:
print self.__storage, self.__name
stor = getattr(self.__storage, self.__name)
except AttributeError:
stor = self.__func(*self.__arg)
setattr(self.__storage, self.__name, stor)
setattr(stor, attr, val)
# Create a manager for named connections
connections = LazyConnectionManager()
# Backwards compatibility: establish the default connection and set the
# default connection properties at module level, using the lazy proxy so that
# each thread may have a different default connection, if so configured
connection_info = LocalizingProxy('connection_info', _local,
lambda: connections[_default])
connection = LocalizingProxy('connection', _local,
lambda: connections[_default].connection)
backend = LocalizingProxy('backend', _local,
lambda: connections[_default].backend)
DatabaseError = LocalizingProxy('DatabaseError', _local,
lambda: connections[_default].DatabaseError)
get_introspection_module = LocalizingProxy(
'get_introspection_module', _local,
lambda: connections[_default].get_introspection_module)
get_creation_module = LocalizingProxy(
'get_creation_module', _local,
lambda: connections[_default].get_creation_module)
runshell = LocalizingProxy('runshell', _local,
lambda: connections[_default].runshell)
# Reset connections on request finish, to make sure each request can
# load the correct connections for its settings
dispatcher.connect(connections.reset, signal=signals.request_finished)
# Register an event that rolls back all connections
# when a Django request has an exception.
def _rollback_on_exception():
from django.db import transaction
transaction.rollback_unless_managed()
dispatcher.connect(_rollback_on_exception,
signal=signals.got_request_exception)