visitors.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # sql/visitors.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Visitor/traversal interface and library functions.
  8. SQLAlchemy schema and expression constructs rely on a Python-centric
  9. version of the classic "visitor" pattern as the primary way in which
  10. they apply functionality. The most common use of this pattern
  11. is statement compilation, where individual expression classes match
  12. up to rendering methods that produce a string result. Beyond this,
  13. the visitor system is also used to inspect expressions for various
  14. information and patterns, as well as for usage in
  15. some kinds of expression transformation. Other kinds of transformation
  16. use a non-visitor traversal system.
  17. For many examples of how the visit system is used, see the
  18. sqlalchemy.sql.util and the sqlalchemy.sql.compiler modules.
  19. For an introduction to clause adaption, see
  20. http://techspot.zzzeek.org/2008/01/23/expression-transformations/
  21. """
  22. from collections import deque
  23. from .. import util
  24. import operator
  25. from .. import exc
  26. __all__ = ['VisitableType', 'Visitable', 'ClauseVisitor',
  27. 'CloningVisitor', 'ReplacingCloningVisitor', 'iterate',
  28. 'iterate_depthfirst', 'traverse_using', 'traverse',
  29. 'traverse_depthfirst',
  30. 'cloned_traverse', 'replacement_traverse']
  31. class VisitableType(type):
  32. """Metaclass which assigns a `_compiler_dispatch` method to classes
  33. having a `__visit_name__` attribute.
  34. The _compiler_dispatch attribute becomes an instance method which
  35. looks approximately like the following::
  36. def _compiler_dispatch (self, visitor, **kw):
  37. '''Look for an attribute named "visit_" + self.__visit_name__
  38. on the visitor, and call it with the same kw params.'''
  39. visit_attr = 'visit_%s' % self.__visit_name__
  40. return getattr(visitor, visit_attr)(self, **kw)
  41. Classes having no __visit_name__ attribute will remain unaffected.
  42. """
  43. def __init__(cls, clsname, bases, clsdict):
  44. if clsname != 'Visitable' and \
  45. hasattr(cls, '__visit_name__'):
  46. _generate_dispatch(cls)
  47. super(VisitableType, cls).__init__(clsname, bases, clsdict)
  48. def _generate_dispatch(cls):
  49. """Return an optimized visit dispatch function for the cls
  50. for use by the compiler.
  51. """
  52. if '__visit_name__' in cls.__dict__:
  53. visit_name = cls.__visit_name__
  54. if isinstance(visit_name, str):
  55. # There is an optimization opportunity here because the
  56. # the string name of the class's __visit_name__ is known at
  57. # this early stage (import time) so it can be pre-constructed.
  58. getter = operator.attrgetter("visit_%s" % visit_name)
  59. def _compiler_dispatch(self, visitor, **kw):
  60. try:
  61. meth = getter(visitor)
  62. except AttributeError:
  63. raise exc.UnsupportedCompilationError(visitor, cls)
  64. else:
  65. return meth(self, **kw)
  66. else:
  67. # The optimization opportunity is lost for this case because the
  68. # __visit_name__ is not yet a string. As a result, the visit
  69. # string has to be recalculated with each compilation.
  70. def _compiler_dispatch(self, visitor, **kw):
  71. visit_attr = 'visit_%s' % self.__visit_name__
  72. try:
  73. meth = getattr(visitor, visit_attr)
  74. except AttributeError:
  75. raise exc.UnsupportedCompilationError(visitor, cls)
  76. else:
  77. return meth(self, **kw)
  78. _compiler_dispatch.__doc__ = \
  79. """Look for an attribute named "visit_" + self.__visit_name__
  80. on the visitor, and call it with the same kw params.
  81. """
  82. cls._compiler_dispatch = _compiler_dispatch
  83. class Visitable(util.with_metaclass(VisitableType, object)):
  84. """Base class for visitable objects, applies the
  85. ``VisitableType`` metaclass.
  86. """
  87. class ClauseVisitor(object):
  88. """Base class for visitor objects which can traverse using
  89. the traverse() function.
  90. """
  91. __traverse_options__ = {}
  92. def traverse_single(self, obj, **kw):
  93. for v in self._visitor_iterator:
  94. meth = getattr(v, "visit_%s" % obj.__visit_name__, None)
  95. if meth:
  96. return meth(obj, **kw)
  97. def iterate(self, obj):
  98. """traverse the given expression structure, returning an iterator
  99. of all elements.
  100. """
  101. return iterate(obj, self.__traverse_options__)
  102. def traverse(self, obj):
  103. """traverse and visit the given expression structure."""
  104. return traverse(obj, self.__traverse_options__, self._visitor_dict)
  105. @util.memoized_property
  106. def _visitor_dict(self):
  107. visitors = {}
  108. for name in dir(self):
  109. if name.startswith('visit_'):
  110. visitors[name[6:]] = getattr(self, name)
  111. return visitors
  112. @property
  113. def _visitor_iterator(self):
  114. """iterate through this visitor and each 'chained' visitor."""
  115. v = self
  116. while v:
  117. yield v
  118. v = getattr(v, '_next', None)
  119. def chain(self, visitor):
  120. """'chain' an additional ClauseVisitor onto this ClauseVisitor.
  121. the chained visitor will receive all visit events after this one.
  122. """
  123. tail = list(self._visitor_iterator)[-1]
  124. tail._next = visitor
  125. return self
  126. class CloningVisitor(ClauseVisitor):
  127. """Base class for visitor objects which can traverse using
  128. the cloned_traverse() function.
  129. """
  130. def copy_and_process(self, list_):
  131. """Apply cloned traversal to the given list of elements, and return
  132. the new list.
  133. """
  134. return [self.traverse(x) for x in list_]
  135. def traverse(self, obj):
  136. """traverse and visit the given expression structure."""
  137. return cloned_traverse(
  138. obj, self.__traverse_options__, self._visitor_dict)
  139. class ReplacingCloningVisitor(CloningVisitor):
  140. """Base class for visitor objects which can traverse using
  141. the replacement_traverse() function.
  142. """
  143. def replace(self, elem):
  144. """receive pre-copied elements during a cloning traversal.
  145. If the method returns a new element, the element is used
  146. instead of creating a simple copy of the element. Traversal
  147. will halt on the newly returned element if it is re-encountered.
  148. """
  149. return None
  150. def traverse(self, obj):
  151. """traverse and visit the given expression structure."""
  152. def replace(elem):
  153. for v in self._visitor_iterator:
  154. e = v.replace(elem)
  155. if e is not None:
  156. return e
  157. return replacement_traverse(obj, self.__traverse_options__, replace)
  158. def iterate(obj, opts):
  159. """traverse the given expression structure, returning an iterator.
  160. traversal is configured to be breadth-first.
  161. """
  162. # fasttrack for atomic elements like columns
  163. children = obj.get_children(**opts)
  164. if not children:
  165. return [obj]
  166. traversal = deque()
  167. stack = deque([obj])
  168. while stack:
  169. t = stack.popleft()
  170. traversal.append(t)
  171. for c in t.get_children(**opts):
  172. stack.append(c)
  173. return iter(traversal)
  174. def iterate_depthfirst(obj, opts):
  175. """traverse the given expression structure, returning an iterator.
  176. traversal is configured to be depth-first.
  177. """
  178. # fasttrack for atomic elements like columns
  179. children = obj.get_children(**opts)
  180. if not children:
  181. return [obj]
  182. stack = deque([obj])
  183. traversal = deque()
  184. while stack:
  185. t = stack.pop()
  186. traversal.appendleft(t)
  187. for c in t.get_children(**opts):
  188. stack.append(c)
  189. return iter(traversal)
  190. def traverse_using(iterator, obj, visitors):
  191. """visit the given expression structure using the given iterator of
  192. objects.
  193. """
  194. for target in iterator:
  195. meth = visitors.get(target.__visit_name__, None)
  196. if meth:
  197. meth(target)
  198. return obj
  199. def traverse(obj, opts, visitors):
  200. """traverse and visit the given expression structure using the default
  201. iterator.
  202. """
  203. return traverse_using(iterate(obj, opts), obj, visitors)
  204. def traverse_depthfirst(obj, opts, visitors):
  205. """traverse and visit the given expression structure using the
  206. depth-first iterator.
  207. """
  208. return traverse_using(iterate_depthfirst(obj, opts), obj, visitors)
  209. def cloned_traverse(obj, opts, visitors):
  210. """clone the given expression structure, allowing
  211. modifications by visitors."""
  212. cloned = {}
  213. stop_on = set(opts.get('stop_on', []))
  214. def clone(elem):
  215. if elem in stop_on:
  216. return elem
  217. else:
  218. if id(elem) not in cloned:
  219. cloned[id(elem)] = newelem = elem._clone()
  220. newelem._copy_internals(clone=clone)
  221. meth = visitors.get(newelem.__visit_name__, None)
  222. if meth:
  223. meth(newelem)
  224. return cloned[id(elem)]
  225. if obj is not None:
  226. obj = clone(obj)
  227. return obj
  228. def replacement_traverse(obj, opts, replace):
  229. """clone the given expression structure, allowing element
  230. replacement by a given replacement function."""
  231. cloned = {}
  232. stop_on = set([id(x) for x in opts.get('stop_on', [])])
  233. def clone(elem, **kw):
  234. if id(elem) in stop_on or \
  235. 'no_replacement_traverse' in elem._annotations:
  236. return elem
  237. else:
  238. newelem = replace(elem)
  239. if newelem is not None:
  240. stop_on.add(id(newelem))
  241. return newelem
  242. else:
  243. if elem not in cloned:
  244. cloned[elem] = newelem = elem._clone()
  245. newelem._copy_internals(clone=clone, **kw)
  246. return cloned[elem]
  247. if obj is not None:
  248. obj = clone(obj, **opts)
  249. return obj