
    G@d1                         d Z ddlZddlmZmZmZ ddlZddlmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ  ej        e          Zeeeeeeef         Z G d	 d
          Z G d de          ZdS )z
Spyder API helper mixins.
    N)AnyUnionOptional)QActionQWidget)Shortcut)CONF)ConfigurationKey)	NoDefaultc                   H   e Zd ZdZdZedfdedeeef         de	e
         fdZdde	e
         fdZ	 	 dded	ede	e
         d
efdZ	 ddede	e
         fdZ	 ddede	e
         fdZ	 	 dde
de	e
         de	e
         de
fdZ	 ddede
dede	e
         def
dZed             ZdS )SpyderConfigurationAccessorzQ
    Mixin used to access options stored in the Spyder configuration system.
    Noptiondefaultsectionc                 f    || j         n|}|t          d          t          j        |||          S )a  
        Get an option from the Spyder configuration system.

        Parameters
        ----------
        option: ConfigurationKey
            Name/Tuple path of the option to get its value from.
        default: Union[NoDefault, BasicTypes]
            Fallback value to return if the option is not found on the
            configuration system.
        section: str
            Section in the configuration system, e.g. `shortcuts`. If None,
            then the value of `CONF_SECTION` is used.

        Returns
        -------
        value: BasicTypes
            Value of the option in the configuration section.

        Raises
        ------
        configparser.NoOptionError
            If the section does not exist in the configuration.
        NKA SpyderConfigurationAccessor must define a `CONF_SECTION` class attribute!)CONF_SECTIONAttributeErrorr	   get)selfr   r   r   s       8lib/python3.11/site-packages/spyder/api/config/mixins.pyget_confz$SpyderConfigurationAccessor.get_conf)   sG    8 (/$##G? #  
 x111    c                 b    || j         n|}|t          d          t          j        |          S )a  
        Get all options from the given section.

        Parameters
        ----------
        section: Optional[str]
            Section in the configuration system, e.g. `shortcuts`. If None,
            then the value of `CONF_SECTION` is used.

        Returns
        -------
        values: BasicTypes
            Values of the option in the configuration section.

        Raises
        ------
        configparser.NoOptionError
            If the section does not exist in the configuration.
        Nr   )r   r   r	   options)r   r   s     r   get_conf_optionsz,SpyderConfigurationAccessor.get_conf_optionsN   sC    ( (/$##G? #   |G$$$r   Tvaluerecursive_notificationc                 n    || j         n|}|t          d          t          j        ||||           dS )a  
        Set an option in the Spyder configuration system.

        Parameters
        ----------
        option: ConfigurationKey
            Name/Tuple path of the option to set its value.
        value: BasicTypes
            Value to set on the configuration system.
        section: Optional[str]
            Section in the configuration system, e.g. `shortcuts`. If None,
            then the value of `CONF_SECTION` is used.
        recursive_notification: bool
            If True, all objects that observe all changes on the
            configuration section and objects that observe partial tuple paths
            are notified. For example if the option `opt` of section `sec`
            changes, then the observers for section `sec` are notified.
            Likewise, if the option `(a, b, c)` changes, then observers for
            `(a, b, c)`, `(a, b)` and a are notified as well.
        Nr   )r   )r   r   r	   set)r   r   r   r   r   s        r   set_confz$SpyderConfigurationAccessor.set_confj   sb    2 (/$##G? #   	#9		
 	
 	
 	
 	
 	
r   c                 h    || j         n|}|t          d          t          j        ||           dS )an  
        Remove an option in the Spyder configuration system.

        Parameters
        ----------
        option: ConfigurationKey
            Name/Tuple path of the option to remove its value.
        section: Optional[str]
            Section in the configuration system, e.g. `shortcuts`. If None,
            then the value of `CONF_SECTION` is used.
        Nr   )r   r   r	   remove_optionr   r   r   s      r   remove_confz'SpyderConfigurationAccessor.remove_conf   sL     (/$##G? #   	7F+++++r   c                 d    || j         n|}|t          d          t          j        ||          S )ay  
        Get an option default value in the Spyder configuration system.

        Parameters
        ----------
        option: ConfigurationKey
            Name/Tuple path of the option to remove its value.
        section: Optional[str]
            Section in the configuration system, e.g. `shortcuts`. If None,
            then the value of `CONF_SECTION` is used.
        Nr   )r   r   r	   get_defaultr$   s      r   get_conf_defaultz,SpyderConfigurationAccessor.get_conf_default   sF     (/$##G? #   000r   namecontextplugin_namereturnc                 D    || j         n|}t          j        |||          S )aR  
        Get a shortcut sequence stored under the given name and context.

        Parameters
        ----------
        name: str
            Key identifier under which the shortcut is stored.
        context: Optional[str]
            Name of the shortcut context.
        plugin: Optional[str]
            Name of the plugin where the shortcut is defined.

        Returns
        -------
        shortcut: str
            Key sequence of the shortcut.

        Raises
        ------
        configparser.NoOptionError
            If the section does not exist in the configuration.
        )r   r	   get_shortcut)r   r)   r*   r+   s       r   r.   z(SpyderConfigurationAccessor.get_shortcut   s*    2 (/$##G $<<<r   actionparentc                 F    || j         n|}t          j        ||||          S )a  
        Create a Shortcut namedtuple for a widget.

        The data contained in this tuple will be registered in our shortcuts
        preferences page.

        Parameters
        ----------
        action: QAction
            Action that will use the shortcut.
        name: str
            Key identifier under which the shortcut is stored.
        parent: QWidget
            Parent widget for the shortcut.
        context: Optional[str]
            Name of the context (plugin) where the shortcut was defined.

        Returns
        -------
        shortcut: Shortcut
            Namedtuple with the information of the shortcut as used for the
            shortcuts preferences page.
        )r   r	   config_shortcut)r   r/   r)   r0   r*   shortcut_contexts         r   r2   z+SpyderConfigurationAccessor.config_shortcut   s7    4 184,,W#	
 
 	
r   c                     t           j        S )z%Get old Spyder configuration version.)r	   old_spyder_versionr   s    r   old_conf_versionz,SpyderConfigurationAccessor.old_conf_version   s     &&r   N)NT)NN)__name__
__module____qualname____doc__r   r   r
   r   
BasicTypesr   strr   r   boolr!   r%   r(   r.   r   r   r   r2   propertyr7    r   r   r   r      s         L :C*.#2 #2)#2	: 56#2 #3-#2 #2 #2 #2J% % % % % %> +/04	$
 $
)$
"$
 #3-$
 *.	$
 $
 $
 $
P .2, ,,,%c], , , ,0 371 1!11"*3-1 1 1 1. 7;)-= ==&.sm=!#=25= = = =< &* 
  
! 
), 
6= 
c] 
.6 
  
  
  
D ' ' X' ' 'r   r   c                   H     e Zd ZdZ fdZd Zd Zd Zdede	de
fd	Z xZS )
SpyderConfigurationObservera  
    Concrete implementation of the protocol
    :class:`spyder.config.types.ConfigurationObserver`.

    This mixin enables a class to receive configuration updates seamlessly,
    by registering methods using the
    :function:`spyder.api.config.decorators.on_conf_change` decorator, which
    receives a configuration section and option to observe.

    When a change occurs on any of the registered configuration options,
    the corresponding registered method is called with the new value.
    c           	         t                                                       | j        t          j        d|  d           i | _        t          i           | _        |                                  | 	                                 | j        D ]W}|| j        n|}| j        |         }|D ]:}t                              |  d| d|            t          j        | ||           ;Xd S )NzRA SpyderConfigurationObserver must define a `CONF_SECTION` class attribute! Hint: z) or its parent should define the section.z is observing z in section )super__init__r   warningswarn_configuration_listenersr    _multi_option_listeners_gather_observers_merge_none_observersloggerdebugr	   observe_configuration)r   r   observed_optionsr   	__class__s       r   rF   z$SpyderConfigurationObserver.__init__  s4   $M*.     )+%'*2ww$   ""$$$ 4 	B 	BG+2?d''G#<WE* B B 5 5F 5 5+25 5 6 6 6*4&AAAAB	B 	Br   c                 .    t          j        |            d S r8   )r	   unobserve_configurationr6   s    r   __del__z#SpyderConfigurationObserver.__del__%  s    $T*****r   c                 |   t          |           D ]}t          | |d          }t          |d          r|j        }t	          |          dk    r| xj        |hz  c_        |D ]Z\  }}| j                            |i           }|                    |g           }|                    |           |||<   || j        |<   [dS )z7Gather all the methods decorated with `on_conf_change`.N_conf_listen   )	dirgetattrhasattrrV   lenrJ   rI   r   append)r   method_namemethodinfor   r   section_listenersoption_listenerss           r   rK   z-SpyderConfigurationObserver._gather_observers)  s    t99 	O 	OKT;55Fv~.. O*t99q==00[MA00'+ O OOGV(,(E(I(I)% )%%'8'<'<VR'H'H$$++K8880@%f-=ND1'::	O 	Or   c                 F   | j                             di           }| j                             | j        i           }|D ]8}|                    |g           }|                    |g           }||z   }|||<   9|| j         | j        <   | j                             dd           dS )z@Replace observers that declared section as None by CONF_SECTION.N)rI   r   r   pop)r   default_selectorssection_selectorsr   default_option_receiverssection_option_receiversmerged_receiverss          r   rL   z1SpyderConfigurationObserver._merge_none_observers:  s     9==dBGG 9==r# # ( 	9 	9F'8'<'<VR'H'H$'8'<'<VR'H'H$(+CC (8f%%;L%d&78%))$55555r   r   r   r   c                     | j                             |i           }|                    |g           }|D ]3}t          | |          }|| j        v r |||           ( ||           4dS )a  
        Handle configuration updates for the option `option` on the section
        `section`, whose new value corresponds to `value`.

        Parameters
        ----------
        option: ConfigurationKey
            Configuration option that did change.
        section: str
            Name of the section where `option` is contained.
        value: Any
            New value of the configuration option that produced the event.
        N)rI   r   rY   rJ   )r   r   r   r   section_receiversoption_receiversreceiverr^   s           r   on_configuration_changez3SpyderConfigurationObserver.on_configuration_changeJ  s     !9==grJJ,00<<( 	 	HT8,,F4777vu%%%%u	 	r   )r9   r:   r;   r<   rF   rT   rK   rL   r
   r>   r   rm   __classcell__)rQ   s   @r   rC   rC      s         B B B B B.+ + +O O O"6 6 6 .>  '*       r   rC   )r<   loggingtypingr   r   r   rG   qtpy.QtWidgetsr   r   spyder.config.guir   spyder.config.managerr	   spyder.config.typesr
   spyder.config.userr   	getLoggerr9   rM   r?   intr>   tuplelistdictr=   r   rC   rA   r   r   <module>r{      s@   
  ' ' ' ' ' ' ' ' ' '  , + + + + + + + ' & & & & & & & & & & & 0 0 0 0 0 0 ( ( ( ( ( ( 
	8	$	$4c5$45
^' ^' ^' ^' ^' ^' ^' ^'B` ` ` ` `"= ` ` ` ` `r   