a
    a                     @   s   d Z ddlZddlZddlmZmZmZ ddlZddlZe	e
Zdd ZG dd dZedd	Zed
dZeddZeddZdS )zFSpyder global registries for actions, toolbuttons, toolbars and menus.    N)AnyOptionalDictc                 C   sV   g }t  D ]:}|jr"|jd }nd}| |v r||j d|j  qd|}|S )zr
    Get file and line where the methods that create actions, toolbuttons,
    toolbars and menus are called.
    r    :, )inspectstackcode_contextappendfilenamelinenojoin)funcframesframer
    r   6lib/python3.9/site-packages/spyder/utils/registries.py
get_caller   s    
r   c                   @   s   e Zd ZdZdeedddZdeeee ee ee dd	d
Z	deee ee edddZ
dee ee eeef dddZdd ZedddZdS )SpyderRegistryz>General registry for global references (per plugin) in Spyder.r   )creation_funcobj_typec                 C   s   i | _ || _|| _d S N)registry_mapr   r   )selfr   r   r   r   r   __init__(   s    zSpyderRegistry.__init__NF)objid_plugincontext	overwritec           	      C   s   |dur|nd}|dur|nd}| j |i }||t }||v rzJt| j}|std||  d| d| d| d| d	| d
|  W n tt	fy   Y n0 t
d| d| d| d|  |||< |||< || j |< dS )a  
        Register a reference `obj` for a given plugin name on a given context.

        Parameters
        ----------
        obj: Any
            Object to register as a reference.
        id_: str
            String identifier used to store the object reference.
        plugin: Optional[str]
            Plugin name used to store the reference. Should belong to
            :class:`spyder.api.plugins.Plugins`. If None, then the object will
            be stored under the global `main` key.
        context: Optional[str]
            Additional key used to store and identify the object reference.
            In any Spyder plugin implementation, this context may refer to an
            identifier of a widget. This context enables plugins to define
            multiple actions with the same key that live on different widgets.
            If None, this context will default to the special `__global`
            identifier.
        Nmain__globalz!There already exists a reference z	 with id z under the context z of plugin z. The new reference z. will overwrite the previous reference. Hint: z" should have a different id_. See zRegistering z (z) under context z for plugin )r   getweakrefWeakValueDictionaryr   r   warningswarnRuntimeErrorKeyErrorloggerdebug)	r   r   r   r   r   r    plugin_contextscontext_referencesr   r   r   r   register_reference-   sF    
z!SpyderRegistry.register_reference)r   r   r   returnc                 C   s:   |dur|nd}|dur|nd}| j | }|| }|| S )a  
        Retrieve a stored object reference under a given id of a specific
        context of a given plugin name.

        Parameters
        ----------
        id_: str
            String identifier used to retrieve the object.
        plugin: Optional[str]
            Plugin name used to store the reference. Should belong to
            :class:`spyder.api.plugins.Plugins`. If None, then the object will
            be retrieved from the global `main` key.
        context: Optional[str]
            Additional key that was used to store the object reference.
            In any Spyder plugin implementation, this context may refer to an
            identifier of a widget. This context enables plugins to define
            multiple actions with the same key that live on different widgets.
            If None, this context will default to the special `__global`
            identifier.

        Returns
        -------
        obj: Any
            The object that was stored under the given identifier.

        Raises
        ------
        KeyError
            If neither of `id_`, `plugin` or `context` were found in the
            registry.
        Nr!   r"   r   )r   r   r   r   r,   r-   r   r   r   get_referencec   s
    "
zSpyderRegistry.get_reference)r   r   r/   c                 C   sB   |dur|nd}|dur|nd}| j |i }||t }|S )a  
        Retrieve all stored object references under the context of a
        given plugin name.

        Parameters
        ----------
        plugin: Optional[str]
            Plugin name used to store the reference. Should belong to
            :class:`spyder.api.plugins.Plugins`. If None, then the object will
            be retrieved from the global `main` key.
        context: Optional[str]
            Additional key that was used to store the object reference.
            In any Spyder plugin implementation, this context may refer to an
            identifier of a widget. This context enables plugins to define
            multiple actions with the same key that live on different widgets.
            If None, this context will default to the special `__global`
            identifier.

        Returns
        -------
        objs: Dict[str, Any]
            A dict that contains the actions mapped by their corresponding
            identifiers.
        Nr!   r"   )r   r#   r$   r%   )r   r   r   r,   r-   r   r   r   get_references   s    zSpyderRegistry.get_referencesc                 C   s
   i | _ d S r   r0   r   r   r   r   reset_registry   s    zSpyderRegistry.reset_registry)r/   c                 C   s   d| j  d| j dS )NzSpyderRegistry[r   ])r   r   r3   r   r   r   __str__   s    zSpyderRegistry.__str__)r   )NNF)NN)NN)__name__
__module____qualname____doc__strr   r   r   boolr.   r1   r   r2   r4   r6   r   r   r   r   r   %   s.      7  )  "r   Zcreate_actionZSpyderActionZcreate_toolbuttonZQToolButtonZcreate_toolbarZQToolBarZcreate_menuZ
SpyderMenu)r:   r   Zloggingtypingr   r   r   r&   r$   Z	getLoggerr7   r*   r   r   ZACTION_REGISTRYZTOOLBUTTON_REGISTRYZTOOLBAR_REGISTRYZMENU_REGISTRYr   r   r   r   <module>   s   
 


