1
2 r"""
3 ==========================
4 Schema module generation
5 ==========================
6
7 Schema module generation code.
8
9 :Copyright:
10
11 Copyright 2010 - 2016
12 Andr\xe9 Malo or his licensors, as applicable
13
14 :License:
15
16 Licensed under the Apache License, Version 2.0 (the "License");
17 you may not use this file except in compliance with the License.
18 You may obtain a copy of the License at
19
20 http://www.apache.org/licenses/LICENSE-2.0
21
22 Unless required by applicable law or agreed to in writing, software
23 distributed under the License is distributed on an "AS IS" BASIS,
24 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 See the License for the specific language governing permissions and
26 limitations under the License.
27
28 """
29 if __doc__:
30
31 __doc__ = __doc__.encode('ascii').decode('unicode_escape')
32 __author__ = r"Andr\xe9 Malo".encode('ascii').decode('unicode_escape')
33 __docformat__ = "restructuredtext en"
34
35 import sqlalchemy as _sa
36
37 from . import _table
38 from . import _template
39
40
42 """
43 Schema container
44
45 :CVariables:
46 `_MODULE_TPL` : ``Template``
47 Template for the module
48
49 :IVariables:
50 `_dialect` : ``str``
51 Dialect name
52
53 `_tables` : `TableCollection`
54 Table collection
55
56 `_schemas` : ``dict``
57 Schema -> module mapping
58
59 `_symbols` : `Symbols`
60 Symbol table
61
62 `_dbname` : ``str`` or ``None``
63 DB identifier
64 """
65
66 _MODULE_TPL = _template.Template('''
67 # -*- coding: ascii -*- pylint: skip-file
68 """
69 ==============================
70 SQLAlchemy schema definition
71 ==============================
72
73 SQLAlchemy schema definition%(dbspec)s.
74
75 :Warning: DO NOT EDIT, this file is generated
76 """
77 __docformat__ = "restructuredtext en"
78
79 import sqlalchemy as %(sa)s
80 from sqlalchemy.dialects import %(dialect)s as %(type)s
81 %(imports)s
82 %(meta)s = %(sa)s.MetaData()
83 %(table)s = %(sa)s.Table
84 %(column)s = %(sa)s.Column
85 %(default)s = %(sa)s.DefaultClause
86 %(lines)s
87 del %(sa)s, %(table)s, %(column)s, %(default)s, %(meta)s
88
89 # vim: nowrap tw=0
90 ''')
91
92 - def __init__(self, conn, tables, schemas, symbols, dbname=None):
93 """
94 Initialization
95
96 :Parameters:
97 `conn` : ``Connection`` or ``Engine``
98 SQLAlchemy connection or engine
99
100 `tables` : ``list``
101 List of tables to reflect, (local name, table name) pairs
102
103 `schemas` : ``dict``
104 schema -> module mapping
105
106 `symbols` : `Symbols`
107 Symbol table
108
109 `dbname` : ``str``
110 Optional db identifier. Used for informational purposes. If
111 omitted or ``None``, the information just won't be emitted.
112 """
113 metadata = _sa.MetaData(conn)
114 self._dialect = metadata.bind.dialect.name
115 self._tables = _table.TableCollection.by_names(
116 metadata, tables, schemas, symbols
117 )
118 self._schemas = schemas
119 self._symbols = symbols
120 self._dbname = dbname
121
122 - def dump(self, fp):
123 """
124 Dump schema module to fp
125
126 :Parameters:
127 `fp` : ``file``
128 File to write to
129 """
130 imports = [item % self._symbols for item in self._symbols.imports]
131 if imports:
132 imports.append('')
133 lines = []
134 for table in self._tables:
135 if table.is_reference:
136 continue
137 if not lines:
138 lines.append('')
139 lines.append('# Table "%s"' % (
140 table.sa_table.name.encode('ascii', 'backslashescape')
141 ))
142 lines.append('%s = %r' % (table.varname, table))
143 lines.append('')
144 lines.append('')
145
146 param = dict(((str(key), value) for key, value in self._symbols),
147 dbspec=" for %s" % self._dbname if self._dbname else "",
148 dialect=self._dialect,
149 imports='\n'.join(imports),
150 lines='\n'.join(lines))
151 fp.write(self._MODULE_TPL.expand(**param))
152 fp.write('\n')
153