langhelpers.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import textwrap
  2. import warnings
  3. import inspect
  4. import uuid
  5. import collections
  6. from .compat import callable, exec_, string_types, with_metaclass
  7. from sqlalchemy.util import format_argspec_plus, update_wrapper
  8. from sqlalchemy.util.compat import inspect_getfullargspec
  9. class _ModuleClsMeta(type):
  10. def __setattr__(cls, key, value):
  11. super(_ModuleClsMeta, cls).__setattr__(key, value)
  12. cls._update_module_proxies(key)
  13. class ModuleClsProxy(with_metaclass(_ModuleClsMeta)):
  14. """Create module level proxy functions for the
  15. methods on a given class.
  16. The functions will have a compatible signature
  17. as the methods.
  18. """
  19. _setups = collections.defaultdict(lambda: (set(), []))
  20. @classmethod
  21. def _update_module_proxies(cls, name):
  22. attr_names, modules = cls._setups[cls]
  23. for globals_, locals_ in modules:
  24. cls._add_proxied_attribute(name, globals_, locals_, attr_names)
  25. def _install_proxy(self):
  26. attr_names, modules = self._setups[self.__class__]
  27. for globals_, locals_ in modules:
  28. globals_['_proxy'] = self
  29. for attr_name in attr_names:
  30. globals_[attr_name] = getattr(self, attr_name)
  31. def _remove_proxy(self):
  32. attr_names, modules = self._setups[self.__class__]
  33. for globals_, locals_ in modules:
  34. globals_['_proxy'] = None
  35. for attr_name in attr_names:
  36. del globals_[attr_name]
  37. @classmethod
  38. def create_module_class_proxy(cls, globals_, locals_):
  39. attr_names, modules = cls._setups[cls]
  40. modules.append(
  41. (globals_, locals_)
  42. )
  43. cls._setup_proxy(globals_, locals_, attr_names)
  44. @classmethod
  45. def _setup_proxy(cls, globals_, locals_, attr_names):
  46. for methname in dir(cls):
  47. cls._add_proxied_attribute(methname, globals_, locals_, attr_names)
  48. @classmethod
  49. def _add_proxied_attribute(cls, methname, globals_, locals_, attr_names):
  50. if not methname.startswith('_'):
  51. meth = getattr(cls, methname)
  52. if callable(meth):
  53. locals_[methname] = cls._create_method_proxy(
  54. methname, globals_, locals_)
  55. else:
  56. attr_names.add(methname)
  57. @classmethod
  58. def _create_method_proxy(cls, name, globals_, locals_):
  59. fn = getattr(cls, name)
  60. spec = inspect.getargspec(fn)
  61. if spec[0] and spec[0][0] == 'self':
  62. spec[0].pop(0)
  63. args = inspect.formatargspec(*spec)
  64. num_defaults = 0
  65. if spec[3]:
  66. num_defaults += len(spec[3])
  67. name_args = spec[0]
  68. if num_defaults:
  69. defaulted_vals = name_args[0 - num_defaults:]
  70. else:
  71. defaulted_vals = ()
  72. apply_kw = inspect.formatargspec(
  73. name_args, spec[1], spec[2],
  74. defaulted_vals,
  75. formatvalue=lambda x: '=' + x)
  76. def _name_error(name):
  77. raise NameError(
  78. "Can't invoke function '%s', as the proxy object has "
  79. "not yet been "
  80. "established for the Alembic '%s' class. "
  81. "Try placing this code inside a callable." % (
  82. name, cls.__name__
  83. ))
  84. globals_['_name_error'] = _name_error
  85. translations = getattr(fn, "_legacy_translations", [])
  86. if translations:
  87. outer_args = inner_args = "*args, **kw"
  88. translate_str = "args, kw = _translate(%r, %r, %r, args, kw)" % (
  89. fn.__name__,
  90. tuple(spec),
  91. translations
  92. )
  93. def translate(fn_name, spec, translations, args, kw):
  94. return_kw = {}
  95. return_args = []
  96. for oldname, newname in translations:
  97. if oldname in kw:
  98. warnings.warn(
  99. "Argument %r is now named %r "
  100. "for method %s()." % (
  101. oldname, newname, fn_name
  102. ))
  103. return_kw[newname] = kw.pop(oldname)
  104. return_kw.update(kw)
  105. args = list(args)
  106. if spec[3]:
  107. pos_only = spec[0][:-len(spec[3])]
  108. else:
  109. pos_only = spec[0]
  110. for arg in pos_only:
  111. if arg not in return_kw:
  112. try:
  113. return_args.append(args.pop(0))
  114. except IndexError:
  115. raise TypeError(
  116. "missing required positional argument: %s"
  117. % arg)
  118. return_args.extend(args)
  119. return return_args, return_kw
  120. globals_['_translate'] = translate
  121. else:
  122. outer_args = args[1:-1]
  123. inner_args = apply_kw[1:-1]
  124. translate_str = ""
  125. func_text = textwrap.dedent("""\
  126. def %(name)s(%(args)s):
  127. %(doc)r
  128. %(translate)s
  129. try:
  130. p = _proxy
  131. except NameError:
  132. _name_error('%(name)s')
  133. return _proxy.%(name)s(%(apply_kw)s)
  134. e
  135. """ % {
  136. 'name': name,
  137. 'translate': translate_str,
  138. 'args': outer_args,
  139. 'apply_kw': inner_args,
  140. 'doc': fn.__doc__,
  141. })
  142. lcl = {}
  143. exec_(func_text, globals_, lcl)
  144. return lcl[name]
  145. def _with_legacy_names(translations):
  146. def decorate(fn):
  147. fn._legacy_translations = translations
  148. return fn
  149. return decorate
  150. def asbool(value):
  151. return value is not None and \
  152. value.lower() == 'true'
  153. def rev_id():
  154. return uuid.uuid4().hex[-12:]
  155. def to_list(x, default=None):
  156. if x is None:
  157. return default
  158. elif isinstance(x, string_types):
  159. return [x]
  160. elif isinstance(x, collections.Iterable):
  161. return list(x)
  162. else:
  163. return [x]
  164. def to_tuple(x, default=None):
  165. if x is None:
  166. return default
  167. elif isinstance(x, string_types):
  168. return (x, )
  169. elif isinstance(x, collections.Iterable):
  170. return tuple(x)
  171. else:
  172. return (x, )
  173. def unique_list(seq, hashfunc=None):
  174. seen = set()
  175. seen_add = seen.add
  176. if not hashfunc:
  177. return [x for x in seq
  178. if x not in seen
  179. and not seen_add(x)]
  180. else:
  181. return [x for x in seq
  182. if hashfunc(x) not in seen
  183. and not seen_add(hashfunc(x))]
  184. def dedupe_tuple(tup):
  185. return tuple(unique_list(tup))
  186. class memoized_property(object):
  187. """A read-only @property that is only evaluated once."""
  188. def __init__(self, fget, doc=None):
  189. self.fget = fget
  190. self.__doc__ = doc or fget.__doc__
  191. self.__name__ = fget.__name__
  192. def __get__(self, obj, cls):
  193. if obj is None:
  194. return self
  195. obj.__dict__[self.__name__] = result = self.fget(obj)
  196. return result
  197. class immutabledict(dict):
  198. def _immutable(self, *arg, **kw):
  199. raise TypeError("%s object is immutable" % self.__class__.__name__)
  200. __delitem__ = __setitem__ = __setattr__ = \
  201. clear = pop = popitem = setdefault = \
  202. update = _immutable
  203. def __new__(cls, *args):
  204. new = dict.__new__(cls)
  205. dict.__init__(new, *args)
  206. return new
  207. def __init__(self, *args):
  208. pass
  209. def __reduce__(self):
  210. return immutabledict, (dict(self), )
  211. def union(self, d):
  212. if not self:
  213. return immutabledict(d)
  214. else:
  215. d2 = immutabledict(self)
  216. dict.update(d2, d)
  217. return d2
  218. def __repr__(self):
  219. return "immutabledict(%s)" % dict.__repr__(self)
  220. class Dispatcher(object):
  221. def __init__(self, uselist=False):
  222. self._registry = {}
  223. self.uselist = uselist
  224. def dispatch_for(self, target, qualifier='default'):
  225. def decorate(fn):
  226. if self.uselist:
  227. self._registry.setdefault((target, qualifier), []).append(fn)
  228. else:
  229. assert (target, qualifier) not in self._registry
  230. self._registry[(target, qualifier)] = fn
  231. return fn
  232. return decorate
  233. def dispatch(self, obj, qualifier='default'):
  234. if isinstance(obj, string_types):
  235. targets = [obj]
  236. elif isinstance(obj, type):
  237. targets = obj.__mro__
  238. else:
  239. targets = type(obj).__mro__
  240. for spcls in targets:
  241. if qualifier != 'default' and (spcls, qualifier) in self._registry:
  242. return self._fn_or_list(
  243. self._registry[(spcls, qualifier)])
  244. elif (spcls, 'default') in self._registry:
  245. return self._fn_or_list(
  246. self._registry[(spcls, 'default')])
  247. else:
  248. raise ValueError("no dispatch function for object: %s" % obj)
  249. def _fn_or_list(self, fn_or_list):
  250. if self.uselist:
  251. def go(*arg, **kw):
  252. for fn in fn_or_list:
  253. fn(*arg, **kw)
  254. return go
  255. else:
  256. return fn_or_list
  257. def branch(self):
  258. """Return a copy of this dispatcher that is independently
  259. writable."""
  260. d = Dispatcher()
  261. if self.uselist:
  262. d._registry.update(
  263. (k, [fn for fn in self._registry[k]])
  264. for k in self._registry
  265. )
  266. else:
  267. d._registry.update(self._registry)
  268. return d