
    ce                       U d Z ddlmZ g dZddlZddlZddlZddlZddlZddl	Z	 e
            Zded<    G d dej        ej        	          Z G d
 deej        	          Z G d de          Z G d de          ZdS )z5Utilities to simplify configuration files management.    )annotations)UpdaterJsonUpdaterYamlUpdaterN
typing.AnyKEEPc                      e Zd ZdZdZddZedd	            Zej        dd            Zedd            Z	e
j        dd            Ze
j        dd            ZddZefddZddZd dZdS )!r   au  
    Abstract helper that allows updating configuration files of general formats.

    It is used to hide general actions of reading and writing files with corresponding serialization.

    This class might also be used as a context manager. On successful exit - content of the configuration will be saved
    automatically.

    .. code-block:: python

        with JsonUpdater(path='/some/file') as configuration:
            configuration.content['some_property'] = 'new_value'

        # configuration file is saved automatically, unless an exception raised in main body

    :param path: Path to the configuration file.
    )	__content__pathpathstrreturnNonec                F    || _         |                                 | _        dS )z*Initialize new :class:`~Updater` instance.N)_Updater__path_read_Updater__content)selfr   s     Glib/python3.11/site-packages/anaconda_navigator/utils/configurations.py__init__zUpdater.__init__)   s    %)ZZ\\    r   c                    | j         S )z"Content of the configuration file.r   r   s    r   contentzUpdater.content.   s     ~r   valuec                    || _         dS )zUpdate `content` value.Nr   )r   r   s     r   r   zUpdater.content3   s     r   c                    | j         S )zPath to the configuration.)r   r   s    r   r   zUpdater.path8   s     {r   c                    dS )a  
        Read content of the configuration file.

        .. note::

            You can use value of the :attr:`~Updater.path` to locate the configuration file.

        .. warning::

            Do not use :attr:`~Updater.content`! This is the value that must be set by current function, and which must
            not rely on the previous configuration content.

        :return: Content of the configuration file.
        N r   s    r   r   zUpdater._read=         r   c                    dS )a  
        Write new configuration to the file.

        .. note::

            You can use value of the :attr:`~Updater.path` to locate the configuration file, and
            :attr:`~Updater.content` for the content of the configuration that should be saved.
        Nr    r   s    r   _writezUpdater._writeN   r!   r   c                8    |                                  | _        dS )z!Load configuration from the file.N)r   r   r   s    r   loadzUpdater.loadY   s    r   c                    | j         }|t          ur|| _         	 |                                  dS # t          $ r	 || _          w xY w)ap  
        Save configuration to the file.

        :param path: New path to save configuration to. This will also update value of the :attr:`~Updater.path` and
                     affect subsequent :meth:`~Updater.load` calls (unless there would be an error writing to file).

                     If not provided - current :attr:`~Updater.path` will be used.
        N)r   r   r#   BaseException)r   r   path_backups      r   savezUpdater.save]   sX      ;tDK	KKMMMMM 	 	 	%DK	s	   / Ac                    | S )z.Enter into the configuration updating context.r    r   s    r   	__enter__zUpdater.__enter__o   s    r   exc_type+typing.Optional[typing.Type[BaseException]]exc_valtyping.Optional[BaseException]exc_tb$typing.Optional[types.TracebackType]c                6    ||                                   dS dS )z
        Exit from the configuration updating context.

        If no exception raised - configuration would be saved.
        N)r)   )r   r,   r.   r0   s       r   __exit__zUpdater.__exit__s   s!     ?IIKKKKK ?r   N)r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   )r,   r-   r.   r/   r0   r1   r   r   )__name__
__module____qualname____doc__	__slots__r   propertyr   setterr   abcabstractmethodr   r#   r%   r   r)   r+   r3   r    r   r   r   r      s=        $ (I2 2 2 2
    X ^   ^    X 	     	   & & & &  $     $        r   r   )	metaclassc                  T     e Zd ZdZdZdd fd
Zedd            Zeefd fdZ	 xZ
S )EncodingAwareUpdaterz
    Modified :class:`~Updater`, which also allows setting encoding to read configuration with.

    :param path: Path to the configuration file.
    :param encoding: Encoding to use for the configuration file.
    )
__encodingNr   r   encodingtyping.Optional[str]r   r   c                Z    || _         t                                          |           dS )z7Initialize new :class:`~EncodingAwareUpdater` instance.r   N)_EncodingAwareUpdater__encodingsuperr   )r   r   rC   	__class__s      r   r   zEncodingAwareUpdater.__init__   s+    08d#####r   c                    | j         S )z+Current encoding of the configuration file.)rG   r   s    r   rC   zEncodingAwareUpdater.encoding   s     r   c                    | j         }|t          ur|| _         	 t                                          |           dS # t          $ r	 || _          w xY w)a`  
        Save configuration to the file.

        :param path: New path to save configuration to. This will also update value of the
                     :attr:`~EncodingAwareUpdater.path` and affect subsequent :meth:`~Updater.load` calls (unless there
                     would be an error writing to file).

                     If not provided - current :attr:`~EncodingAwareUpdater.path` will be used.

        :param encoding: New encoding to save configuration file with. This also updates
                         :attr:`~EncodingAwareUpdater.encoding` in the way similar to `path` argument.
        rF   N)rG   r   rH   r)   r'   )r   r   rC   encoding_backuprI   s       r   r)   zEncodingAwareUpdater.save   sf     154&DO	GGLLdL##### 	 	 	-DO	s	   "> A)N)r   r   rC   rD   r   r   )r   rD   )r6   r7   r8   r9   r:   r   r;   rC   r   r)   __classcell__)rI   s   @r   rA   rA      s           I$ $ $ $ $ $ $
    X  $d           r   rA   c                  &    e Zd ZdZdZd	dZd
dZdS )r   z
    Configuration updater, that supports configuration files with JSON content.

    :param path: Path to the configuration file.
    :param encoding: Encoding to use for the configuration file.
    r    r   r   c                    t          | j        d| j                  5 }t          j        |          cddd           S # 1 swxY w Y   dS zn
        Read content of the configuration file.

        :return: Content of the configuration file.
        rtrC   N)openr   rC   jsonr%   r   streams     r   r   zJsonUpdater._read   s     $)TDM::: 	%f9V$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   >AAr   c                    t          | j        d| j                  5 }t          j        | j        |           ddd           dS # 1 swxY w Y   dS z$Write new configuration to the file.wtrR   N)rS   r   rC   rT   dumpr   rU   s     r   r#   zJsonUpdater._write        $)TDM::: 	,fIdlF+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,   AA	A	Nr4   r5   r6   r7   r8   r9   r:   r   r#   r    r   r   r   r      sM          I% % % %, , , , , ,r   r   c                  &    e Zd ZdZdZd	dZd
dZdS )r   z
    Configuration updater, that supports configuration files with YAML content.

    :param path: Path to the configuration file.
    :param encoding: Encoding to use for the configuration file.
    r    r   r   c                    t          | j        d| j                  5 }t          j        |t          j                  cddd           S # 1 swxY w Y   dS rP   )rS   r   rC   yamlr%   
FullLoaderrU   s     r   r   zYamlUpdater._read   s     $)TDM::: 	6f9VT_55	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   A		AAr   c                    t          | j        d| j                  5 }t          j        | j        |           ddd           dS # 1 swxY w Y   dS rX   )rS   r   rC   r`   rZ   r   rU   s     r   r#   zYamlUpdater._write   r[   r\   Nr4   r5   r]   r    r   r   r   r      sM          I6 6 6 6, , , , , ,r   r   )r9   
__future__r   __all__r=   
contextlibtypestypingrT   r`   objectr   __annotations__AbstractContextManagerABCMetar   rA   r   r   r    r   r   <module>rl      sS   < ; ; " " " " " "
3
3
3 



         688    k k k k kj/3; k k k k\( ( ( ( (7ck ( ( ( (V, , , , ,& , , ,6, , , , ,& , , , , ,r   