File: //opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/rewriter.pyo
�
�M!Vc           @   s:   d  d l  m Z d  d l m Z d e f d �  �  YZ d S(   i����(   t   util(   t   opst   Rewriterc           B   s�   e  Z d  Z e j �  Z d Z d �  Z d �  Z	 d �  Z
 d �  Z d �  Z e j
 e j � d �  � Z e j
 e j � d �  � Z e j
 e j � d �  � Z d	 �  Z d
 �  Z d �  Z RS(
   sP  A helper object that allows easy 'rewriting' of ops streams.
    The :class:`.Rewriter` object is intended to be passed along
    to the
    :paramref:`.EnvironmentContext.configure.process_revision_directives`
    parameter in an ``env.py`` script.    Once constructed, any number
    of "rewrites" functions can be associated with it, which will be given
    the opportunity to modify the structure without having to have explicit
    knowledge of the overall structure.
    The function is passed the :class:`.MigrationContext` object and
    ``revision`` tuple that are passed to the  :paramref:`.Environment
    Context.configure.process_revision_directives` function normally,
    and the third argument is an individual directive of the type
    noted in the decorator.  The function has the choice of  returning
    a single op directive, which normally can be the directive that
    was actually passed, or a new directive to replace it, or a list
    of zero or more directives to replace it.
    .. seealso::
        :ref:`autogen_rewriter` - usage example
    .. versionadded:: 0.8
    c         C   s   t  j �  |  _ d  S(   N(   R    t
   Dispatchert   dispatch(   t   self(    (    sN   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/rewriter.pyt   __init__%   s    c         C   s5   |  j  j |  j  � } | j j |  j � | | _ | S(   s�  Produce a "chain" of this :class:`.Rewriter` to another.
        This allows two rewriters to operate serially on a stream,
        e.g.::
            writer1 = autogenerate.Rewriter()
            writer2 = autogenerate.Rewriter()
            @writer1.rewrites(ops.AddColumnOp)
            def add_column_nullable(context, revision, op):
                op.column.nullable = True
                return op
            @writer2.rewrites(ops.AddColumnOp)
            def add_column_idx(context, revision, op):
                idx_op = ops.CreateIndexOp(
                    'ixc', op.table_name, [op.column.name])
                return [
                    op,
                    idx_op
                ]
            writer = writer1.chain(writer2)
        :param other: a :class:`.Rewriter` instance
        :return: a new :class:`.Rewriter` that will run the operations
         of this writer, then the "other" writer, in succession.
        (   t	   __class__t   __new__t   __dict__t   updatet   _chained(   R   t   othert   wr(    (    sN   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/rewriter.pyt   chain(   s    	c         C   s   |  j  j | � S(   s�  Register a function as rewriter for a given type.
        The function should receive three arguments, which are
        the :class:`.MigrationContext`, a ``revision`` tuple, and
        an op directive of the type indicated.  E.g.::
            @writer1.rewrites(ops.AddColumnOp)
            def add_column_nullable(context, revision, op):
                op.column.nullable = True
                return op
        (   R   t   dispatch_for(   R   t   operator(    (    sN   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/rewriter.pyt   rewritesK   s    
c         c   sd   y |  j  j  | � } Wn t k
 r4 d  } | Vn, Xx( t j | | | | � � D] } | VqQ Wd  S(   N(   R   t
   ValueErrort   NoneR    t   to_list(   R   t   contextt   revisiont	   directivet	   _rewritert   r_directive(    (    sN   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/rewriter.pyt   _rewriteZ   s    
		c         C   s6   |  j  | | | � |  j r2 |  j | | | � n  d  S(   N(   t   process_revision_directivesR   (   R   R   R   t
   directives(    (    sN   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/rewriter.pyt   __call__e   s    	c   	      C   s�   g  } x[ | j  D]P } |  j | | | j � } t | � d k rO t d � � n  | j | d � q W| | _ g  } x[ | j D]P } |  j | | | j � } t | � d k r� t d � � n  | j | d � q} W| | _ d  S(   Ni   s5   Can only return single object for UpgradeOps traversei    s7   Can only return single object for DowngradeOps traverse(   t   upgrade_ops_listt
   _traverse_fort   upgrade_opst   lenR   t   appendt   downgrade_ops_listt
   downgrade_ops(	   R   R   R   R   R   R    t   retR#   R$   (    (    sN   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/rewriter.pyt   _traverse_scriptj   s"