1
2 r"""
3 ====================================
4 Type inspection and representation
5 ====================================
6
7 Type inspection and representation.
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 inspect as _inspect
36
37 import sqlalchemy as _sa
38
39
40 -class Type(object):
41 """
42 Type container
43
44 :IVariables:
45 `_ctype` : SA type
46 Column type
47
48 `_dialect` : ``str``
49 Dialect name
50 """
51
52 - def __init__(self, ctype, dialect_name, symbols):
53 """
54 Initialization
55
56 :Parameters:
57 `ctype` : SA type
58 Column type
59
60 `dialect_name` : ``str``
61 Dialect name
62 """
63 self._ctype = ctype
64 self._dialect = dialect_name
65 self._symbols = symbols
66
67 @classmethod
69 """
70 Construct by SA column
71
72 :Parameters:
73 `column` : SA column
74 SA column
75
76 :Return: New Type instance
77 :Rtype: `Type`
78 """
79 return cls(
80 column.type,
81 column.table.metadata.bind.dialect.name,
82 symbols,
83 )
84
86 """
87 Make string representation
88
89 :Return: The string representation
90 :Rtype: ``str``
91 """
92 mod = self._symbols.types.resolve(self._ctype, self._dialect)
93 params = []
94 try:
95 spec = _inspect.getargspec(self._ctype.__init__)
96 except TypeError:
97 pass
98 else:
99 defaults = dict(zip(spec[0][::-1], (spec[3] or ())[::-1]))
100 kwds = False
101 for arg in spec[0][1:]:
102 value = getattr(self._ctype, arg)
103 if arg in defaults and defaults[arg] == value:
104 kwds = True
105 continue
106 if isinstance(value, _sa.types.TypeEngine):
107 rvalue = repr(
108 self.__class__(value, self._dialect, self._symbols)
109 )
110 else:
111 rvalue = repr(value)
112 if kwds:
113 params.append('%s=%s' % (arg, rvalue))
114 else:
115 params.append(rvalue)
116 if not kwds and spec[1] is not None:
117 if _find_class(self._ctype, '__init__') is not \
118 _sa.types.TypeEngine:
119 params.extend(list(
120 map(repr, getattr(self._ctype, spec[1]))
121 ))
122
123 params = ', '.join(params)
124 if params:
125 params = "(%s)" % (params,)
126 return "%s.%s%s" % (mod, self._ctype.__class__.__name__, params)
127
130 """
131 Find class where a method is defined
132
133 :Parameters:
134 `first_cls` : type
135 Class to start with
136
137 `name` : ``str``
138 Method name
139
140 :Return: class or ``None``
141 :Rtype: ``type``
142 """
143 for cls in _inspect.getmro(first_cls):
144 if name in cls.__dict__:
145 return cls
146 return None
147