U of@s6ddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z d d l m Z d d l mZe rd d lmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZeddedgdfZGdddZdS)) annotations)Any)Callable)Iterator)List)Tuple)Type) TYPE_CHECKING)Union)util)ops) AddColumnOp) AlterColumnOp) CreateTableOp) DowngradeOps)MigrateOperation)MigrationScript)ModifyTableOps) OpContainer) UpgradeOps)MigrationContext) _GetRevArgrrrNc@seZdZUdZeZdZded<ddddZ d dd d d Z d ddddZ dddddddZ dddddddZ eejdddddddZeejddd ddd!d"Zeejdddddd#d$Zdddd%dd&d'Zddd%ddd(d)Zdddddd*d+Zd,S)-Rewritera5A 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 z7Tuple[Union[ProcessRevisionDirectiveFn, Rewriter], ...]_chainedNone)returncCst|_dSN)r Dispatcherdispatch)selfrrK/opt/hc_python/lib/python3.8/site-packages/alembic/autogenerate/rewriter.py__init__?szRewriter.__init__z+Union[ProcessRevisionDirectiveFn, Rewriter])otherrcCs0|j|j}|j|j|j|f7_|S)aProduce a "chain" of this :class:`.Rewriter` to another. This allows two or more 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. ) __class____new____dict__updater)r!r$wrrrr"chainBs$zRewriter.chainzpUnion[Type[AddColumnOp], Type[MigrateOperation], Type[AlterColumnOp], Type[CreateTableOp], Type[ModifyTableOps]]zCallable[..., Any])operatorrcCs |j|S)aRegister 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 dispatch_for)r!r+rrr"rewriteskszRewriter.rewritesrrrzIterator[MigrateOperation])contextrevision directiverccsvz|j|}Wntk r.d}|VYnDX||jkrB|Vn0t||||gD]}|j|g|_|VqVdSr)r ValueErrorZ _mutationsr Zto_listunion)r!r.r/r0Z _rewriterZ r_directiverrr"_rewrites   zRewriter._rewritezList[MigrationScript])r.r/ directivesrcCs*|||||jD]}||||qdSr)process_revision_directivesr)r!r.r/r4r5rrr"__call__s zRewriter.__call__rc Csg}|jD]4}||||}t|dkr0td||dq ||_g}|jD]4}||||}t|dkrvtd||dqP||_dS)Nz5Can only return single object for UpgradeOps traverserz7Can only return single object for DowngradeOps traverse)upgrade_ops_list _traverse_forlenr1append upgrade_opsdowngrade_ops_list downgrade_ops) r!r.r/r0r8r<retr=r>rrr"_traverse_scripts$    zRewriter._traverse_scriptrcCs||||jdSr)_traverse_listr r!r.r/r0rrr"_traverse_op_containerszRewriter._traverse_op_containercCsdSrrrBrrr"_traverse_any_directivesz Rewriter._traverse_any_directivercCs:t||||}|D]}|j|}|||||q|Sr)listr3 _traverser )r!r.r/r0r4Z traverserrrr"r9s  zRewriter._traverse_forcCs2g}|D]}|||||q||dd<dSr)extendr9)r!r.r/r4destr0rrr"rAszRewriter._traverse_listcCs||||dSr)rA)r!r.r/r4rrr"r5sz$Rewriter.process_revision_directivesN)__name__ __module__ __qualname____doc__r rrFr__annotations__r#r*r-r3r6r,r rr@rrCrrDr9rAr5rrrr"r!s"  )     r) __future__rtypingrrrrrrr r r operationsr Zoperations.opsrrrrrrrrrZruntime.migrationrZscript.revisionrZProcessRevisionDirectiveFnrrrrr"s4