
    Je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y)z
Spyder API helper mixins.
    N)AnyUnionOptional)QActionQWidget)Shortcut)CONF)ConfigurationKey)	NoDefaultc                   
   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y)SpyderConfigurationAccessorzQ
    Mixin used to access options stored in the Spyder configuration system.
    Noptiondefaultsectionc                 j    || 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.
        KA SpyderConfigurationAccessor must define a `CONF_SECTION` class attribute!)CONF_SECTIONAttributeErrorr	   get)selfr   r   r   s       :/usr/lib/python3/dist-packages/spyder/api/config/mixins.pyget_confz$SpyderConfigurationAccessor.get_conf)   sA    8 (/$##G? # 
 xx11    c                 f    || 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.
        r   )r   r   r	   options)r   r   s     r   get_conf_optionsz,SpyderConfigurationAccessor.get_conf_optionsN   s=    ( (/$##G? #  ||G$$r   valuerecursive_notificationc                 p    || j                   n|}|t        d      t        j                  ||||       y)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   sF    2 (/$##G? #  	#9		
r   c                 j    || j                   n|}|t        d      t        j                  ||       y)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   s>     (/$##G? #  	7F+r   c                 h    || 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.
        r   )r   r   r	   get_defaultr$   s      r   get_conf_defaultz,SpyderConfigurationAccessor.get_conf_default   sA     (/$##G? #  00r   namecontextplugin_namereturnc                 P    || 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                 R    || 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   s5    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      sG    L :C*.#2)#2	: 56#2 #3-#2J% %> +/04	$
)$
"$
 #3-$
 *.	$
P .2,,,%c],0 371!11"*3-1. 7;)-==&.sm=!#=25=< &* 
! 
), 
6= 
c] 
.6 
D ' '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                          | j                          | j
                  D ]\  }|| j                  n|}| j
                  |   }|D ]6  }t        j                  |  d| d|        t        j                  | ||       8 ^ y )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__  s    $MM**. 0 )+%'*2w$ ""$ 44 	BG+2?d''G#<<WE* Bv^F8 <++2)5 6**4&AB	Br   c                 .    t        j                  |        y r8   )r	   unobserve_configurationr6   s    r   __del__z#SpyderConfigurationObserver.__del__%  s    $$T*r   c                 r   t        |       D ]  }t        | |d      }t        |d      s|j                  }t	        |      dkD  r| xj
                  |hz  c_        |D ]X  \  }}| j                  j                  |i       }|j                  |g       }|j                  |       |||<   || j                  |<   Z  y)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    t9 	OKT;5Fv~.**t9q=00[MA0'+ OOGV(,(E(E(I(I)%%'8'<'<VR'H$$++K80@%f-=ND11':O	Or   c                 \   | j                   j                  di       }| j                   j                  | j                  i       }|D ]0  }|j                  |g       }|j                  |g       }||z   }|||<   2 || j                   | j                  <   | j                   j                  dd       y)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9==dBG 99==r# ( 	9F'8'<'<VR'H$'8'<'<VR'H$(+CC (8f%	9 <M%%d&7&78%%))$5r   r   r   r   c                     | j                   j                  |i       }|j                  |g       }|D ].  }t        | |      }|| j                  v r
 |||       ' ||       0 y)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  sj     !99==gr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.+O"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{      sr   
  ' '  , ' & 0 ( 
		8	$4c5$45
^' ^'B`"= `r   