o
    voiK                     @  s   d dl mZ d dlmZ d dlZd dlZd dlmZ d dlm	Z	 d dlm
Z
 d dlmZ dd	lmZ dd
lmZ ddlmZ erFddlmZ i ZeeZG dd dZdddZdddZe  dS )    )annotations)metadataN)
ModuleType)Callable)Pattern)TYPE_CHECKING   )util)DispatchPriority)PriorityDispatcher)PriorityDispatchResultc                   @  sX   e Zd ZdZd!ddZd"dd	Z	
d#dejdd$ddZe	d%ddZ
e	d&dd Zd
S )'Plugina  Describe a series of functions that are pulled in as a plugin.

    This is initially to provide for portable lists of autogenerate
    comparison functions, however the setup for a plugin can run any
    other kinds of global registration as well.

    .. versionadded:: 1.18.0

    namestrc                 C  s>   || _ td| |tv rtd| d| t|< t | _d S )Nzsetup plugin %szA plugin named z is already registered)r   loginfo_all_plugins
ValueErrorr   autogenerate_comparators)selfr    r   K/home/ubuntu/.local/lib/python3.10/site-packages/alembic/runtime/plugins.py__init__#   s   zPlugin.__init__returnNonec                 C  s   t | j= dS )zremove this pluginN)r   r   )r   r   r   r   remove+   s   zPlugin.removeNdefault)	qualifierpriorityfn%Callable[..., PriorityDispatchResult]compare_targetcompare_element
str | Noner   r   r
   c                C  s   | j j||||d| dS )aU  Register an autogenerate comparison function.

        See the section :ref:`plugins_registering_autogenerate` for detailed
        examples on how to use this method.

        :param fn: The comparison function to register. The function receives
         arguments specific to the type of comparison being performed and
         should return a :class:`.PriorityDispatchResult` value.

        :param compare_target: The type of comparison being performed
         (e.g., ``"table"``, ``"column"``, ``"type"``).

        :param compare_element: Optional sub-element being compared within
         the target type.

        :param qualifier: Database dialect qualifier. Use ``"default"`` for
         all dialects, or specify a dialect name like ``"postgresql"`` to
         register a dialect-specific handler. Defaults to ``"default"``.

        :param priority: Execution priority for this comparison function.
         Functions are executed in priority order from
         :attr:`.DispatchPriority.FIRST` to :attr:`.DispatchPriority.LAST`.
         Defaults to :attr:`.DispatchPriority.MEDIUM`.

        )subgroupr   r   N)r   dispatch_for)r   r   r!   r"   r   r   r   r   r   add_autogenerate_comparator0   s   "z"Plugin.add_autogenerate_comparatorcomparatorsr   include_plugins	list[str]c                   s   t  }i  t  }|D ]}|dr|t|dd  q
t| |< q
t D ]-tfdd|D r6q( fdd D }|sCq(|| t	dj
 |j q(t  |}|rjtd	d
| dS )z_Populate all current autogenerate comparison functions into
        a given PriorityDispatcher.~   Nc                 3  s    | ]	}|  jV  qd S Nmatchr   ).0excl)pluginr   r   	<genexpr>l   s    zAPlugin.populate_autogenerate_priority_dispatch.<locals>.<genexpr>c                   s    g | ]} |  jr|qS r   r-   )r/   inclincluder1   r   r   
<listcomp>o   s
    zBPlugin.populate_autogenerate_priority_dispatch.<locals>.<listcomp>z!setting up autogenerate plugin %szDid not locate plugins: z, )set
startswithadd_make_rer   valuesanyupdater   r   r   populate_withr   
differencer	   CommandErrorjoin)clsr'   r(   excludematched_expressionsr   include_matchesnever_matchedr   r4   r   'populate_autogenerate_priority_dispatchY   s0   

z.Plugin.populate_autogenerate_priority_dispatchmoduler   c                 C  s   | t| dS )a<  Call the ``setup()`` function of a plugin module, identified by
        passing the module object itself.

        E.g.::

            from alembic.runtime.plugins import Plugin
            import myproject.alembic_plugin

            # Register the plugin manually
            Plugin.setup_plugin_from_module(
                myproject.alembic_plugin,
                "myproject.custom_operations"
            )

        This will generate a new :class:`.Plugin` object with the given
        name, which will register itself in the global list of plugins.
        Then the module's ``setup()`` function is invoked, passing that
        :class:`.Plugin` object.

        This exact process is invoked automatically at import time for any
        plugin module that is published via the ``alembic.plugins`` entrypoint.

        N)setupr   )rB   rH   r   r   r   r   setup_plugin_from_module   s   zPlugin.setup_plugin_from_module)r   r   r   r   r,   )r   r    r!   r   r"   r#   r   r   r   r
   r   r   )r'   r   r(   r)   r   r   )rH   r   r   r   r   r   )__name__
__module____qualname____doc__r   r   r
   MEDIUMr&   classmethodrG   rJ   r   r   r   r   r      s    


	)&r   r   r   r   Pattern[str]c                 C  sf   |  d}d}|D ]}|dkr|d7 }q	| r|d| 7 }q	td| td|dd   d	S )
N. *z\..+?z\.zInvalid plugin expression ^r   $)splitisidentifierr   recompile)r   tokensregtokenr   r   r   r:      s   

r:   r   c                  C  s2   t jddD ]} |  D ]	}t|| j qqd S )Nzalembic.plugins)group)r   entry_pointsloadr   rJ   r   )
entrypointmodr   r   r   _setup   s
   rd   )r   r   r   rR   rK   )
__future__r   	importlibr   loggingrZ   typesr   typingr   r   r   rT   r	   r
   r   r   r   	getLoggerrL   r   r   r:   rd   r   r   r   r   <module>   s(    
 


