forked from daeken/QuestCompanions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metamodel.py
177 lines (145 loc) · 3.94 KB
/
metamodel.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
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.types import AbstractType, Integer
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.orm.attributes import QueryableAttribute
from flask import request
class SessionProxy(object):
def __getattr__(self, name):
return getattr(request._session, name)
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
if type == None:
try:
request._session.commit()
except:
request._session.rollback()
raise
else:
request._session.rollback()
raise
def createLocalSession():
request._session = scoped_session(sessionmaker())
request._session.configure(bind=engine)
def closeLocalSession():
request._session.remove()
transact = SessionProxy()
metadata = sa.MetaData()
__models = []
# Monkey patched into Model-decorated classes
@classmethod
def relation(cls, *args, **kwargs):
return orm.relation(cls, *args, **kwargs)
@classmethod
def create(cls, **kwargs):
obj = cls()
for k, v in kwargs.items():
setattr(obj, k, v)
transact.add(obj)
return obj
def update(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
return self
@classmethod
def all(cls):
return transact.query(cls).all()
def genFilter(cls, kwargs):
if len(kwargs) == 1:
k, v = kwargs.items()[0]
return getattr(cls, k) == v
filters = []
for k, v in kwargs.items():
filters.append(getattr(cls, k) == v)
return sa.and_(*filters)
@classmethod
def some(cls, **kwargs):
filter = genFilter(cls, kwargs)
return transact.query(cls).filter(filter).all()
@classmethod
def one(cls, **kwargs):
filter = genFilter(cls, kwargs)
try:
return transact.query(cls).filter(filter).one()
except:
return None
def Model(cls):
cls.create = create
cls.update = update
cls.all = all
cls.some = some
cls.one = one
cls.relation = relation
__models.append(cls)
return cls
engine = None
def setup(db):
global engine
try:
file('prod', 'r')
engine = sa.create_engine(db, client_encoding='utf8')
except:
engine = sa.create_engine(db)
metadata.bind = engine
initialized = False
for model in __models:
name = model.__name__
params = []
for field in dir(model):
value = getattr(model, field)
if isinstance(value, PrimaryKey):
params = [field] + params
else:
params.append(field)
columns = []
columns.append(sa.Column('id', Integer, primary_key=True))
relations = {}
for field in params:
value = getattr(model, field)
if isinstance(value, Modifier):
columns.append(value.build(field))
delattr(model, field)
elif (
isinstance(value, AbstractType) or
(isinstance(value, type) and issubclass(value, AbstractType))
):
columns.append(sa.Column(field, value))
delattr(model, field)
elif isinstance(value, orm.properties.RelationProperty):
relations[field] = value
delattr(model, field)
table = sa.Table(name, metadata, *columns)
orm.mapper(model, table, properties=relations)
if table.exists():
initialized = True
metadata.create_all()
def sub(func):
if not initialized:
func()
return sub
class Modifier(object):
pass
class PrimaryKey(Modifier):
def __init__(self, type):
self.type = type
def build(self, name):
return sa.Column(name, self.type, primary_key=True)
class ForeignKey(Modifier):
def __init__(self, type, ref, *args, **kwargs):
self.type, self.ref = type, ref
self.args, self.kwargs = args, kwargs
def build(self, name):
return sa.Column(name, self.type, sa.ForeignKey(self.ref), *self.args, **self.kwargs)
class Nullable(Modifier):
def __init__(self, type, *args, **kwargs):
self.type = type
self.args, self.kwargs = args, kwargs
def build(self, name):
return sa.Column(name, self.type, nullable=True, *self.args, **self.kwargs)
class Unique(Modifier):
def __init__(self, type, *args, **kwargs):
self.type = type
self.args, self.kwargs = args, kwargs
def build(self, name):
return sa.Column(name, self.type, unique=True, *self.args, **self.kwargs)