/
context.py
233 lines (192 loc) · 7.32 KB
/
context.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
# -*- coding: utf-8 -*-
"""Convenient, encapsulated SQLALchemy initialization.
Usage::
from bag.sqlalchemy.context import SAContext
sa = SAContext() # you can provide create_engine's args here
# Now define your models with sa.metadata and sa.base
# At runtime:
# Add a working engine:
sa.create_engine('sqlite:///db.sqlite3', echo=False)
# or...
sa.use_memory() # This one immediately creates the tables.
# Now use it:
sa.drop_tables().create_tables()
session = sa.Session()
# Use that session...
session.commit()
# You can also create a copy of sa, bound to another engine:
sa2 = sa.clone('sqlite://')
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from functools import wraps
from sqlalchemy import Table, create_engine
from sqlalchemy.ext.declarative import declarative_base # , declared_attr
from sqlalchemy.orm import sessionmaker, scoped_session
from types import ModuleType
from nine import str
__all__ = ('SAContext',)
class SAContext(object):
"""Provide convenient and encapsulated SQLAlchemy initialization."""
__slots__ = ('base', 'dburi', 'engine', 'Session', '_scoped_session',
'session_extensions')
def __init__(self, base=None, base_class=None, metadata=None,
session_extensions=None, zope_transaction=False, *args, **k):
self.dburi = None
self.engine = None
self.Session = None
self._scoped_session = None
if base:
self.base = base
elif base_class:
self.base = declarative_base(cls=base_class, metadata=metadata)
else:
self.base = declarative_base(metadata=metadata)
self.session_extensions = session_extensions or []
if zope_transaction:
from zope.sqlalchemy import ZopeTransactionExtension
self.session_extensions.append(ZopeTransactionExtension())
if self.metadata.bind:
self._set_engine(self.metadata.bind)
if args or k:
self.create_engine(*args, **k)
def _set_engine(self, engine):
self.engine = engine
self.Session = sessionmaker(bind=engine,
extension=self.session_extensions)
self.dburi = str(engine.url)
def create_engine(self, dburi, **k):
self._set_engine(create_engine(dburi, **k))
return self
def use_memory(self, tables=None, **k):
self.create_engine('sqlite:///:memory:', **k)
self.create_tables(tables=tables)
return self
@property
def ss(self):
"""Return a (memoized) scoped session.
This is created only when first used and then stored.
"""
if not self._scoped_session:
assert self.Session is not None, \
'Tried to use the scoped session before the engine was set.'
self._scoped_session = scoped_session(self.Session)
return self._scoped_session
@property
def metadata(self):
return self.base.metadata
def drop_tables(self, tables=None):
"""Drop tables."""
self.metadata.drop_all(tables=tables, bind=self.engine)
return self
def create_tables(self, tables=None):
"""Create tables."""
self.metadata.create_all(tables=tables, bind=self.engine)
return self
def tables_in(self, context):
"""Return a list containing the tables in the passed *context*.
``context`` may be a dictionary or a module::
tables = sa.tables_in(globals())
"""
tables = []
if isinstance(context, ModuleType): # context is a python module
context = context.__dict__
for val in context.values():
if hasattr(val, '__base__') and val.__base__ is self.base:
tables.append(val.__table__)
elif isinstance(val, Table) and val.metadata is self.metadata:
tables.append(val)
return tables
def clone(self, **k):
"""Copy this object. If keyword args, create another engine."""
from copy import copy
o = copy(self)
if k:
o.create_engine(**k)
return o
def subtransaction(self, fn):
"""Decorator that encloses the decorated function in a subtransaction.
Your system must use our ``ss`` scoped session and it
does not need to call ``commit()`` on the session.
"""
@wraps(fn)
def wrapper(*a, **kw):
self.ss.begin(subtransactions=True)
try:
fn(*a, **kw)
except:
self.ss.rollback()
raise
else:
self.ss.commit()
return wrapper
def transaction(self, fn):
"""Decorator that encloses the decorated function in a transaction.
Your system must use our ``ss`` scoped session and it
does not need to call ``commit()`` on the session.
"""
@wraps(fn)
def wrapper(*a, **kw):
try:
fn(*a, **kw)
except:
self.ss.rollback()
raise
else:
self.ss.commit()
return wrapper
def transient(self, fn):
"""Decorator. Create a subtransaction which is always rewinded.
It is recommended that you apply this
decorator to each of your integrated tests; then you only need to
create the tables once, instead of once per test,
because nothing ever gets persisted. This makes tests run faster.
"""
@wraps(fn)
def wrapper(*a, **kw):
self.ss.begin(subtransactions=True)
self.ss.begin(subtransactions=True)
try:
fn(*a, **kw) # I assume fn consumes the inner subtransaction.
finally:
self.ss.rollback() # Revert the outer subtransaction.
return wrapper
'''
# TYPES FOR SQLITE
# ================
import sqlalchemy.types as types
class AutoDate(types.TypeDecorator):
"""A SQLAlchemy DateTime type that converts strings to datetime
when storing. Prevents TypeError("SQLite Date, Time, and DateTime types
only accept Python datetime objects as input.")
"""
impl = types.DateTime
def process_bind_param(self, value, dialect):
if isinstance(value, basestring):
if value == '':
return None
parts = value.split('.')
dt = datetime.strptime(parts[0], "%Y-%m-%d %H:%M:%S")
if len(parts) > 1:
dt.replace(microsecond=int(parts[1]))
return dt
else:
return value
class Integer(types.TypeDecorator):
impl = types.Integer
def process_bind_param(self, value, dialect):
"""Make sure an int is persisted. Otherwise, SQLite might persist
things such as empty strings...
"""
# return None if not value else int(value)
return None if value is None or value == '' else int(value)
class Numeric(types.TypeDecorator):
impl = types.Numeric
def process_bind_param(self, value, dialect):
"""When you are feeding a CSV file to a SQLite database, you
want empty strings to be automatically converted to None,
for a Decimal column...
"""
return None if value == '' else value
del types
'''