-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (116 loc) · 4.6 KB
/
Copy pathmain.py
File metadata and controls
139 lines (116 loc) · 4.6 KB
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
import sys
import config, util, constants
from util import emit
from declarative import ModelFactory
def emit_table(indent, db, options, table):
"""Emit table representation."""
emit('%s%s%s%s = %r' % (indent, options.table_prefix, table.name, options.table_suffix, table))
emit_index(indent, db, options, table)
def emit_z3c_objects(indent, db, options, table):
emit_table(indent, db, options, table)
emit(indent + ('class %(tn)sObject(MappedClassBase): pass\n'
'%(tab)smapper(%(tn)sObject, %(tn)s)') % {'tn':table.name, 'tab':indent})
def emit_index(indent, db, options, table):
"""docstring for emit_index"""
if not options.noindex:
indexes = []
if not table.indexes:
# for certain dialects we need to include index support
if hasattr(db.dialect, 'indexloader'):
indexes = db.dialect.indexloader(db).indexes(table)
else:
print >>config.err, 'It seems that this dialect does not support indexes!'
else:
indexes = list(table.indexes)
emit(*[indent + repr(index) for index in indexes])
def main():
config.configure()
options = config.options
if options.declarative:
config.interactive = None
if options.interactive:
config.interactive = True
config.schema = None
if options.schema:
config.schema = options.schema
config.example=False
if options.example:
config.example=True
factory = ModelFactory(config)
emit(repr(factory))
config.out.close()
config.out = sys.stdout
print >>config.err, "Output written to %s" % options.output
return
import formatter
formatter.monkey_patch_sa()
import sqlalchemy
from sqlalchemy.engine.reflection import Inspector
db, options = config.engine, config.options
metadata = sqlalchemy.MetaData(db)
print >>config.err, 'Starting...'
conn = db.connect()
inspector = Inspector.from_engine(conn)
if options.schema != None:
reflection_schema=options.schema
else:
try:
reflection_schema = inspector.default_schema_name
except NotImplementedError:
reflection_schema = None
tablenames = inspector.get_table_names(reflection_schema)
# fixme: don't set up output until we're sure there's work to do!
if options.tables:
subset, missing, unglobbed = util.glob_intersection(tablenames,
options.tables)
for identifier in missing:
print >>config.err, 'Table "%s" not found.' % identifier
for glob in unglobbed:
print >>config.err, '"%s" matched no tables.' % glob
if not subset:
print >>config.err, "No tables matched!"
sys.exit(1)
tablenames = subset
# some header with imports
if options.generictypes:
dialect = ''
else:
d1 = 'from sqlalchemy.databases.%s import *\n' % db.name
d2 = 'from sqlalchemy.dialects.%s import *\n' % db.name
#Determine with one is correct...
dialect = util.select_imports([d1, d2])
header = options.z3c and constants.HEADER_Z3C or constants.HEADER
emit(header % {'dialect': dialect, 'encoding': options.encoding})
for tname in tablenames:
print >>config.err, "Generating python model for table %s" % (
util.as_sys_str(tname))
table = sqlalchemy.Table(tname, metadata, schema=reflection_schema, autoload=True)
if options.schema is None:
# we're going to remove the schema from the table so that it
# isn't rendered in the output. If we don't put back the
# correct value, it may cause errors when other tables reference
# this one.
original_schema = table.schema
table.schema = None
else:
original_schema = options.schema
indent = ''
INC = '\n\n'
emit(INC)
if options.z3c:
emit_z3c_objects(constants.TAB, db, options, table)
else:
emit_table('', db, options, table)
table.schema = original_schema
if options.z3c:
emit(constants.FOOTER_Z3C)
# print some example
if options.example:
emit('\n' + constants.FOOTER_EXAMPLE % {
'url': unicode(db.url), 'tablename': tablenames[0]})
if options.output:
emit('\n')
config.out.close()
config.out = sys.stdout
print >>config.err, "Output written to %s" % options.output
# vim:ts=4:sw=4:expandtab