
    Vh                          d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlZd dlmZ d dlmZmZ  G d d	e      Zd
 Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionas  
module: kdeconfig
short_description: Manage KDE configuration files
version_added: "6.5.0"
description:
  - Add or change individual settings in KDE configuration files.
  - It uses B(kwriteconfig) under the hood.
options:
  path:
    description:
      - Path to the config file. If the file does not exist it will be created.
    type: path
    required: true
  kwriteconfig_path:
    description:
      - Path to the kwriteconfig executable. If not specified, Ansible will try to discover it.
    type: path
  values:
    description:
      - List of values to set.
    type: list
    elements: dict
    suboptions:
      group:
        description:
          - The option's group. One between this and O(values[].groups) is required.
        type: str
      groups:
        description:
          - List of the option's groups. One between this and O(values[].group) is required.
        type: list
        elements: str
      key:
        description:
          - The option's name.
        type: str
        required: true
      value:
        description:
          - The option's value. One between this and O(values[].bool_value) is required.
        type: str
      bool_value:
        description:
          - Boolean value.
          - One between this and O(values[].value) is required.
        type: bool
    required: true
  backup:
    description:
      - Create a backup file.
    type: bool
    default: false
extends_documentation_fragment:
  - files
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
requirements:
  - kwriteconfig
author:
  - Salvatore Mesoraca (@smeso)
a2  
- name: Ensure "Homepage=https://www.ansible.com/" in group "Branding"
  community.general.kdeconfig:
    path: /etc/xdg/kickoffrc
    values:
      - group: Branding
        key: Homepage
        value: https://www.ansible.com/
    mode: '0644'

- name: Ensure "KEY=true" in groups "Group" and "Subgroup", and "KEY=VALUE" in Group2
  community.general.kdeconfig:
    path: /etc/xdg/someconfigrc
    values:
      - groups: [Group, Subgroup]
        key: KEY
        bool_value: true
      - group: Group2
        key: KEY
        value: VALUE
    backup: true
z # N)AnsibleModule)to_bytesto_textc                   0    e Zd ZdZddZd Zd Zd Zd Zy)	TemporaryDirectoryz-Basic backport of tempfile.TemporaryDirectoryNc                 J    d | _         t        j                  |||      | _         y N)nametempfilemkdtemp)selfsuffixprefixdirs       o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/kdeconfig.py__init__zTemporaryDirectory.__init__q   s    	$$VVS9	    c                     | j                   S r   )r   r   s    r   	__enter__zTemporaryDirectory.__enter__u   s    yyr   c                 n    | j                   r)t        j                  | j                   d       d | _         y y )NT)ignore_errors)r   shutilrmtreer   s    r   rmzTemporaryDirectory.rmx   s'    99MM$))48DI r   c                 $    | j                          y r   r   )r   excvaluetbs       r   __exit__zTemporaryDirectory.__exit__}       	r   c                 $    | j                          y r   r    r   s    r   __del__zTemporaryDirectory.__del__   r%   r   ) tmpN)	__name__
__module____qualname____doc__r   r   r   r$   r'    r   r   r
   r
   n   s    7:
r   r
   c                 $   |d|d|g}|D ]  }|j                  d|g        t        |t              r9|j                  ddg       |r|j                  d       n%|j                  d       n|j                  d|g       | j	                  |d	
       y)z"Invoke kwriteconfig with argumentsz--filez--keyz--groupz--typebooltruefalsez--T)check_rcN)extend
isinstancer0   appendrun_command)modulecmdpathgroupskeyr"   argsgroups           r   run_kwriteconfigr?      s    4#.D (Y&'(%Xv&'KKKK T5M"
td+r   c           	         t        dd| j                  d         }t        | j                  d         }t        j                  j                  |d      }t        |      }t        dd|d   |d         }	 t        |d      5 }	 t        |d	      5 }	|	j                         }
d d d        |j                  
       	 t        |
      |d
<   d d d        | j                  d   D ]0  }|d   }||d   g}|d   }|d   }||d   }t!        | |||||       2 t        |d	      5 }|j                         }
	 t        |
      |d<   d d d        |d   xs |d   |d
   k7  |d<   | j#                  | j                        }| j$                  r|d   slt'        j(                  ||       | j+                  |      \  }}t        j,                  |||       | j.                  ri }nd }| j1                  ||d   |      |d<   | j.                  r||d<    | j2                  di | |d   ru| j                  d   r6t        j                  j5                  |      r| j7                  |d         |d<   	 | j9                  |t        j                  j;                  |             |d   r| j1                  ||d          n+| j.                  ri }nd }| j1                  ||d   |      |d<   | j.                  r||d<    | j2                  di | y # 1 sw Y   \xY w# t        $ r t        |
      |d
<   Y Yw xY w# t        $ r	 d|d<   Y nw xY w# 1 sw Y   txY w# t        $ r) | j                  dt        j                                Y w xY w# t        $ r t        |
      |d<   Y Xw xY w# 1 sw Y   ^xY w# t        $ r= | j<                  j                  d|d|d   dt        j                                Y \w xY w)NFOKr:   )changedmsgr:   filer(   )beforeafterbefore_headerafter_headerwbrbrE   TrB   zUnable to create temporary file)rC   	tracebackvaluesr;   r>   r<   
bool_valuer"   rF   )diffrN   backupbackup_filezUnable to move temporary file z to z	, IOErrorr.   )dictparamsr   osr:   joinopenreadwriter   UnicodeErrorreprIOError	fail_jsonrK   
format_excr?   load_file_common_arguments
check_moder   copystatuser_and_groupchown_diffset_fs_attributes_if_different	exit_jsonexistsbackup_localatomic_moveabspathansible)r8   tmpdirkwriteconfigresultb_pathtmpfile	b_tmpfilerN   dstsrcb_datarowr;   r<   r"   tmpf	file_argsuidgids                      r   
run_modulerx      s   %Tf0EFFfmmF+,Fggll66*G!IVnF^	Db)T" 	2c
2&$' (3 XXZF(
 		&!2%,V_DN	2 }}X& LX>'l^F%jL!=LEwUKL 
i	 )$	)#FODM) y)LT']d8n-LF911&--@Ii OOFI.,,V4HCHHYS)|| & E EiQWXaQbim E nF9<<!F6N"6"i=="rww~~f'=$*$7$7v$GF=!	Zy"''//&*AB i--i	9JK<<DD"AA)VT]M^eiAjy||vFvE( ( $ 2%)&\DN2  )$(y!)		2 	2  b>)J^J^J`ab"  	) LDM	)	) )<  	ZNN$$^egmntgu)v  CL  CW  CW  CY$  Z	Zs   5M L:L%K: L%(L::LM N0M<!/N' :L	?L%L"L:!L""L:%L73L:6L77L::M?M .M98M9<NNNNN$'AO-,O-c            
         t        t        d      t        dd      t        ddd      t        d      t        d      	      } d
dg}t        t        dd| ||d      t        dd      t        d      t        dd            }t        |dd      }d }|j                  d   !|j                  |j                  d   d      }n0dD ]  }|j                  |      }| n ||j	                  d       |j                  d   D ]  }|d   r	|j	                  d        t        |j                        5 }t        |||       d d d        y # 1 sw Y   y xY w)Nstr)typelist)r{   elementsTF)r{   requiredno_logr0   )r>   r;   r<   r"   rM   )r>   r;   )r"   rM   rQ   )r{   r}   optionsmutually_exclusiverequired_one_ofr~   r:   )r{   r~   )r{   default)rL   r:   kwriteconfig_pathrO   )argument_specadd_file_common_argssupports_check_moder   )r~   )kwriteconfig5rk   kwriteconfig4zkwriteconfig is not installed)rC   rL   r<   z'key' cannot be empty)r   )rQ   r   rR   get_bin_pathr[   r
   rj   rx   )single_value_argrequired_alternativesmodule_argsr8   rk   prognamevrj   s           r   mainr      sy   $E"2#'Ve#D $%$u M"&E"2'+'8	:
 12IJ#,'<$9!# v-F+/
K !! F L}}()5**6==9L+MX\*]J 	H!..x8L'	 !@A]]8$ :x!89: 
	. 1&66<01 1 1s   :EE__main__)
__future__r   r   r   r{   __metaclass__DOCUMENTATIONEXAMPLESRETURNrS   r   r   rK   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   r   objectr
   r?   rx   r   r*   r.   r   r   <module>r      sn    C B@D. 
 	    4 I ,, Pf'1T zF r   