
    Vhg(                         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mZ d dlmZ d dlmZmZ d	Zd
Ze	j(                  j+                  ed      ZdZddddddddddd
Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: locale_gen
short_description: Creates or removes locales
description:
  - Manages locales in Debian and Ubuntu systems.
author:
  - Augustus Kling (@AugustusKling)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    type: list
    elements: str
    description:
      - Name and encoding of the locales, such as V(en_GB.UTF-8).
      - Before community.general 9.3.0, this was a string. Using a string still works.
    required: true
  state:
    type: str
    description:
      - Whether the locales shall be present.
    choices: [absent, present]
    default: present
notes:
  - If C(/etc/locale.gen) exists, the module will assume to be using the B(glibc) mechanism, else if C(/var/lib/locales/supported.d/)
    exists it will assume to be using the B(ubuntu_legacy) mechanism, else it will raise an error.
  - When using glibc mechanism, it will manage locales by editing C(/etc/locale.gen) and running C(locale-gen).
  - When using ubuntu_legacy mechanism, it will manage locales by editing C(/var/lib/locales/supported.d/local) and then running
    C(locale-gen).
  - Please note that the code path that uses ubuntu_legacy mechanism has not been tested for a while, because Ubuntu is already
    using the glibc mechanism. There is no support for that, given our inability to test it. Therefore, that mechanism is
    B(deprecated) and will be removed in community.general 13.0.0.
  - Currently the module is B(only supported for Debian and Ubuntu) systems.
  - This module requires the package C(locales) installed in Debian and Ubuntu systems.
z
- name: Ensure a locale exists
  community.general.locale_gen:
    name: de_CH.UTF-8
    state: present

- name: Ensure multiple locales exist
  community.general.locale_gen:
    name:
      - en_GB.UTF-8
      - nl_NL.UTF-8
    state: present
z
mechanism:
  description: Mechanism used to deploy the locales.
  type: str
  choices:
    - glibc
    - ubuntu_legacy
  returned: success
  sample: glibc
  version_added: 10.2.0
N)StateModuleHelper)check_mode_skip)locale_runnerlocale_gen_runnerz/etc/locale.genz/var/lib/locales/supported.dlocalz/usr/share/i18n/SUPPORTEDz.UTF-8z.EUC-JPz.ISO-8859-15z.CP1251z.KOI8-Rz
.ARMSCII-8z.EUC-KRz.GBKz.GB18030z.EUC-TW)
z.utf8z.eucjpz
.iso885915z.cp1251z.koi8rz	.armscii8z.euckrz.gbkz.gb18030z.euctwc                       e Zd ZdgZ e e eddd       eddddg      	      d
      ZdZd Zd Ze	d        Z
d Zd Zd Zd ZddZd Zd Zed        Zy)	LocaleGennameliststrT)typeelementsrequiredpresentabsent)r   defaultchoices)r   state)argument_specsupports_check_modeFc                 b   t        t        t        | j                        t        t        | j                              | _        t
        j                  j                  t              r#d| j                  _
        d| j                  _        nt
        j                  j                  t              rAd| j                  _
        d| j                  _        | j                  j                  ddd	
       n)| j                  dj!                  t        t                     t#        | j                        | _        | j'                          | j                  j)                  d| j+                         d       | j                  j)                  d| j-                  | j                  j*                        dd       y )N)	availableapply_change)ubuntu_legacyglibcFr   Tr   zOn this machine mechanism=ubuntu_legacy is used. This mechanism is deprecated and will be removed from in community.general 13.0.0. If you see this message on a modern Debian or Ubuntu version, please create an issue in the community.general repositoryz13.0.0zcommunity.general)versioncollection_namez<{0} and {1} are missing. Is the package "locales" installed?
is_present)outputstate_tracking)r"   change)dictSUPPORTED_LOCALESapply_change_ubuntu_legacyapply_change_glibc
MECHANISMSospathexistsETC_LOCALE_GENvarsubuntu_mode	mechanismVAR_LIB_LOCALESmodule	deprecatedo_raiseformatr   runnerassert_availablesetr!   _state_nameselfs    p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/locale_gen.py__init_module__zLocaleGen.__init_module__t   s;   +!<< +!44	
 77>>.)$)DII!")DIIWW^^O,$(DII!"1DIIKK!!N !2E	 "  MMX__  $DKK0		lDOO$5eD		&(8(89M9M(NW\eij    c                 `    | j                  | j                               | j                  _        y N)r9   r!   r.   r#   r:   s    r<   __quit_module__zLocaleGen.__quit_module__   s     #'#3#3DOO4E#F		 r>   c                     | rdS dS )Nr   r    )r   s    r<   r9   zLocaleGen._state_name   s    #y11r>   c                 p  	 d}| j                   | j                  j                     d   }t        j                  |      }t        |d      5 }|j                         }ddd       D cg c]  }|j                  |       }}| j                  j                  d|d       g }| j                  j                  D ](  	t        	fd|D              r|j                  	       * | j                  |      }|r0| j                  d	j                  d
j                  |                   yy# 1 sw Y   xY wc c}w )zCheck if the given locales are available on the system. This is done by
        checking either :
        * if the locale is present in /etc/locales.gen
        * or if the locale is present in /usr/share/i18n/SUPPORTEDz5^\s*#?\s*(?P<locale>\S+[\._\S]+) (?P<charset>\S+)\s*$r   rNavailable_lines   )	verbosityc              3   N   K   | ]  }|xr |j                  d       k(    yw)localeN)group).0matchrJ   s     r<   	<genexpr>z-LocaleGen.assert_available.<locals>.<genexpr>   s'     RUu@X!6&!@@Rs   "%zLThe following locales you have entered are not available on your system: {0}z, )r)   r.   r0   recompileopen	readlinesrM   r8   r   anyappendlocale_get_not_presentr4   r5   join)
r;   regexplocales_availablere_compiledfdlineslinereslocales_not_foundrJ   s
            @r<   r7   zLocaleGen.assert_available   s#   
 J OODII,?,?@Mjj(#S) 	#RLLNE	#3894{  &99		'!<iinn 	1FRcRR!((0	1 !778IJMMhooptpypy  {L  qM  N  O 	# 	#9s   D'(D3'D0c                 N    | j                  | j                  j                         S r@   )rU   r.   r   r:   s    r<   r!   zLocaleGen.is_present   s    ..tyy~~>>>r>   c                 d    t         j                        } |       5 }|j                         \  }}} j                  dk\  r|j                   j
                  _        d d d        g }|D ]7  t         fdj                         D              r'|j                         9 |S # 1 sw Y   IxY w)NrG   c              3   d   K   | ]'  }j                        j                  |      k(   ) y wr@   )fix_case)rL   r\   rJ   r;   s     r<   rN   z3LocaleGen.locale_get_not_present.<locals>.<genexpr>   s(     at}}V,d0CCas   -0)
r   r2   runrH   run_infor.   locale_run_inforS   
splitlinesrT   )	r;   localesr6   ctxrcouterr	not_foundrJ   s	   `       @r<   rU   z LocaleGen.locale_get_not_present   s    t{{+X 	9779LBS~~",/LL		)	9
 	 	)FaPSP^P^P`aa  (	) 	9 	9s   ?B&&B/c                 b    t         j                         D ]  \  }}|j                  ||      } |S )zlocale -a might return the encoding in either lower or upper case.
        Passing through this function makes them uniform for comparisons.)LOCALE_NORMALIZATIONitemsreplace)r;   r   srE   s       r<   rb   zLocaleGen.fix_case   s5     )..0 	&DAq<<1%D	&r>   c                    t        t        d      5 }|j                         }ddd       g }|D ]O  }dt        j                  |      z  }|rd|z  }nd|z  }t        j
                  |      }	|j                  |	|g       Q t        t                    D ]$  }
|D ]  \  }}|j                  |||
         ||
<    & t        t        d      5 }|j                  |       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)z4 Sets the state of the locale. Defaults to enabled. rE   Nz^#?\s*%s (?P<charset>.+)z%s \g<charset>z# %s \g<charset>w)rQ   r-   rR   rO   escaperP   rT   rangelensub
writelines)r;   namesenabledfrr[   locale_regexesr   search_string
new_string	re_searchisearchrp   fws                 r<   set_locale_glibczLocaleGen.set_locale_glibc   s   .#& 	#"LLNE	#  	;D7"))D/IM.$7
0D9


=1I!!9j"9:	; s5z" 	9A%3 9!!::guQx8a9	9
 .#& 	!"MM% 	! 	!'	# 	#&	! 	!s   C&C2&C/2C;c                     | j                  ||dk(         t        | j                        } |       5 }|j                          ddd       y# 1 sw Y   yxY w)zCreate or remove locale.

        Keyword arguments:
        targetState -- Desired state, either present or absent.
        names -- Names list including encoding such as de_CH.UTF-8.
        r   )rz   N)r   r	   r2   rc   )r;   targetStatery   r6   rh   s        r<   r(   zLocaleGen.apply_change_glibc   sO     	ekY.FH"4;;/X 	GGI	 	 	s   AAc                    t        | j                        }|dk(  r! |       5 }|j                          ddd       yt        t        d      5 }|j                         }ddd       t        t        d      5 }D ],  }|j                  d      \  }	}
|	|vs|j                  |       . 	 ddd        |d      5 }|j                          ddd       y# 1 sw Y   yxY w# 1 sw Y   xY w# 1 sw Y   CxY w# 1 sw Y   yxY w)zCreate or remove locale.

        Keyword arguments:
        targetState -- Desired state, either present or absent.
        names -- Name list including encoding such as de_CH.UTF-8.
        r   NrE   rs    purge)r	   r2   rc   rQ   VAR_LIB_LOCALES_LOCALrR   splitwrite)r;   r   ry   r6   rh   r{   contentr   r\   rJ   charsets              r<   r'   z$LocaleGen.apply_change_ubuntu_legacy   s     #4;;/)#  S	  +S1 )R,,.)+S1 'R# 'D&*jjoOFGU*''  C	  ) )' ' s5   CC5C*C*8C6CC'*C36C?c                 
   | j                   j                  | j                   j                  k(  ry  | j                  | j                   j                     d   | j                   j                  | j                   j
                         y )Nr   )r.   r#   r   r)   r0   r   r:   s    r<   __state_fallback__zLocaleGen.__state_fallback__  sS    99##tyy6<		++,^<TYY__diinn]r>   N)T)__name__
__module____qualname__output_paramsr%   r2   use_old_vardictr=   rA   staticmethodr9   r7   r!   rU   rb   r   r(   r'   r   r   rC   r>   r<   r   r   i   s    HM6EDAE9x>ST
 !F O!kFG 2 2O4?!08 ^ ^r>   r   c                  ,    t         j                          y r@   )r   executerC   r>   r<   mainr     s    r>   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr*   rO   Hansible_collections.community.general.plugins.module_utils.module_helperr   Bansible_collections.community.general.plugins.module_utils.mh.decor   Eansible_collections.community.general.plugins.module_utils.locale_genr   r	   r-   r1   r+   rV   r   r&   rn   r   r   r   rC   r>   r<   <module>r      s    A @'R

 
 	 f ^ r #0_g> /   m^! m^` zF r>   