
    Vh;:                         d dl mZmZmZ eZdZdZd dlmZ d dl	Z	d dl
mZ d dlmZmZ  G d d	e      Z G d
 de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: osx_defaults
author:
# DO NOT RE-ADD GITHUB HANDLE!
  - Franck Nijhof (!UNKNOWN)
short_description: Manage macOS user defaults
description:
  - This module allows users to read, write, and delete macOS user defaults from Ansible scripts.
  - MacOS applications and other programs use the defaults system to record user preferences and other information that must
    be maintained when the applications are not running (such as default font for new documents, or the position of an Info
    panel).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  domain:
    description:
      - The domain is a domain name of the form C(com.companyname.appname).
    type: str
    default: NSGlobalDomain
  host:
    description:
      - The host on which the preference should apply.
      - The special value V(currentHost) corresponds to the C(-currentHost) switch of the defaults commandline tool.
    type: str
  key:
    description:
      - The key of the user preference.
    type: str
  type:
    description:
      - The type of value to write.
    type: str
    choices: [array, bool, boolean, date, float, int, integer, string]
    default: string
  check_type:
    description:
      - Checks if the type of the provided O(value) matches the type of an existing default.
      - If the types do not match, raises an error.
    type: bool
    default: true
    version_added: 8.6.0
  array_add:
    description:
      - Add new elements to the array for a key which has an array as its value.
    type: bool
    default: false
  value:
    description:
      - The value to write.
      - Only required when O(state=present).
    type: raw
  state:
    description:
      - The state of the user defaults.
      - If set to V(list) will query the given parameter specified by O(key). Returns V(null) is nothing found or mis-spelled.
    type: str
    choices: [absent, list, present]
    default: present
  path:
    description:
      - The path in which to search for C(defaults).
    type: str
    default: /usr/bin:/usr/local/bin
notes:
  - Apple Mac caches defaults. You may need to logout and login to apply the changes.
a  
- name: Set boolean valued key for application domain
  community.general.osx_defaults:
    domain: com.apple.Safari
    key: IncludeInternalDebugMenu
    type: bool
    value: true
    state: present

- name: Set string valued key for global domain
  community.general.osx_defaults:
    domain: NSGlobalDomain
    key: AppleMeasurementUnits
    type: string
    value: Centimeters
    state: present

- name: Set int valued key for arbitrary plist
  community.general.osx_defaults:
    domain: /Library/Preferences/com.apple.SoftwareUpdate
    key: AutomaticCheckEnabled
    type: int
    value: 1
  become: true

- name: Set int valued key only for the current host
  community.general.osx_defaults:
    domain: com.apple.screensaver
    host: currentHost
    key: showClock
    type: int
    value: 1

- name: Defaults to global domain and setting value
  community.general.osx_defaults:
    key: AppleMeasurementUnits
    type: string
    value: Centimeters

- name: Setting an array valued key
  community.general.osx_defaults:
    key: AppleLanguages
    type: array
    value:
      - en
      - nl

- name: Removing a key
  community.general.osx_defaults:
    domain: com.geekchimp.macable
    key: ExampleKeyToRemove
    state: absent
)datetimeN)AnsibleModule)binary_type	text_typec                       e Zd Zd Zy)OSXDefaultsExceptionc                     || _         y )N)message)selfmsgs     r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/osx_defaults.py__init__zOSXDefaultsException.__init__   s	        N)__name__
__module____qualname__r    r   r   r   r      s    r   r   c                   l    e Zd ZdZd Zed        Zed        Zd Zd Z	ed        Z
d Zd	 Zd
 Z	 d Zy)OSXDefaultsz& Class to manage Mac OS user defaults c                    d| _         || _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _	        |j                  d	   | _
        |j                  d
   | _        | j                  j                  dd| j                  j                  d            | _        | j                  st        d      | j                  dk7  r,| j!                  | j                  | j                        | _	        yy)zN Initialize this module. Finds 'defaults' executable and preps the parameters Ndomainhostkey
check_typetype	array_addvaluestatepathdefaultsF:)requiredopt_dirsz%Unable to locate defaults executable.absent)current_valuemoduleparamsr   r   r   r   r   r   r    r!   r"   get_bin_pathsplit
executabler   _convert_type)r   r)   s     r   r   zOSXDefaults.__init__   s    "mmH-MM&)	==' --5MM&)	{3]]7+
]]7+
MM&)	 ++22YY__S) 3 
 &'NOO ::!++DIItzzBDJ "r   c                     t        |       }|j                  d      r|dd  j                         S |j                         S )N-   )str
startswithisdigit)r    as_strs     r   is_intzOSXDefaults.is_int   s;    Uc"!":%%''>>##r   c                 4   | dk(  rt        |      S | dv rSt        |t        t        f      r|j	                         }|dv ry|dv ryt        dj                  t        |                  | dk(  r7	 t        j                  |j                  d	      d
   j                         d      S | dv rCt        j                  |      s#t        dj                  t        |                  t        |      S | dk(  r	 t!        |      }|S | dk(  rt        |t"              st        d      |S t        dj                  |             # t        $ r$ t        dj                  t        |                  w xY w# t        $ r$ t        dj                  t        |                  w xY w)z Converts value to given type string)boolboolean)Tr1   true1yesT)Fr   false0noFzInvalid boolean value: {0}date+r   %Y-%m-%d %H:%M:%Sz<Invalid date value: {0}. Required format yyy-mm-dd hh:mm:ss.)intintegerzInvalid integer value: {0}floatzInvalid float value: {0}arrayz,Invalid value. Expected value to be an arrayzType is not supported: {0})r2   
isinstancer   r	   lowerr   formatreprr   strptimer,   strip
ValueErrorr   r6   rD   rF   list)	data_typer    s     r   r.   zOSXDefaults._convert_type   s     u:--%+y!9:5588&'C'J'J4PU;'WXX& ((S)9!)<)B)B)DFYZZ
 ,,%%e,*+G+N+NtTY{+[\\u:'![e L'!eT**+YZZL"#?#F#Fy#QRR'  *RYYZ^_dZef   [*+E+L+LTRW[+YZZ[s   .5D: 1E* :-E'*-Fc                 ^    | j                   g S | j                   dk(  rdgS d| j                   gS )zR Returns a normalized list of commandline arguments based on the "host" attribute currentHostz-currentHostz-host)r   r   s    r   
_host_argszOSXDefaults._host_args   s5    99IYY-'"##TYY''r   c                 >    | j                   g| j                         z   S )zS Returns a list containing the "defaults" executable and any common base arguments )r-   rT   rS   s    r   _base_commandzOSXDefaults._base_command   s     4??#444r   c                     | j                         } | j                  d       | j                  d       | D cg c])  }t        j                  dd|j	                  dd            + } }| S c c}w )z0 Converts array output from defaults to an list r   z^ *"?|"?,? *$ z\"")
splitlinespopresubreplace)r    xs     r   _convert_defaults_str_to_listz)OSXDefaults._convert_defaults_str_to_list   sd       " 			!		" NSSQYYuc-BCSS Ts   .A)c                 2   | j                   j                  | j                         d| j                  | j                  gz         \  }}}|dk(  ry|dk7  rt        d|z        |j                         j                  dd      }| j                   j                  | j                         d| j                  | j                  gz         \  }}}|j                         }|dk7  rt        d	|z        |d
k(  r| j                  |      }| j                  ||      | _
        y)z0 Reads value of this domain & key from defaults z	read-typer1   Nr   z:An error occurred while reading key type from defaults: %szType is rY   readz;An error occurred while reading key value from defaults: %srG   )r)   run_commandrV   r   r   r   rM   r_   ra   r.   r(   )r   rcouterrrP   s        r   rc   zOSXDefaults.read  s    {{..t/A/A/C{TXT_T_aeaiaiFj/jkC 7 7&'cfi'ijj IIK''
B7	 {{..t/A/A/Cvt{{\`\d\dFe/efC iik 7&'dgj'jkk 44S9C "//	3?r   c                 *   t        | j                  t              r| j                  rd}nd}nt        | j                  t        t        f      rt        | j                        }n| j                  rA| j                  5t        t        | j                        t        | j                        z
        }nBt        | j                  t              r| j                  j                  d      }n| j                  }| j                  dk(  r| j                  rd| _        t        |t              s|g}| j                  j                  | j                         d| j                   | j"                  d| j                  z   gz   |z   d	
      \  }}}|dk7  rt%        d|z        y)z/ Writes value to this domain & key to defaults TRUEFALSENrC   rG   z	array-addwriter0   F)expand_user_and_varsr   z5An error occurred while writing value to defaults: %s)rH   r    r9   rD   rF   r2   r   r(   rO   setr   strftimer   r)   rd   rV   r   r   r   )r   r    re   rf   rg   s        r   rk   zOSXDefaults.write,  sM    djj$'zz

S%L1

OE^^ 2 2 >TZZ3t/A/A+BBCE

H-JJ''(;<EJJE 99DNN#DI %&GE{{..t/A/A/CwPTP[P[]a]e]egjmqmvmvgvFw/wz/DI / KC 7&'^ad'dee r   c                     | j                   j                  | j                         d| j                  | j                  gz         \  }}}|dk7  rt        d|z        y)z" Deletes defaults key from domain deleter   z6An error occurred while deleting key from defaults: %sN)r)   rd   rV   r   r   r   )r   re   rf   rg   s       r   rp   zOSXDefaults.deleteK  s]    {{..t/A/A/CxQUQ\Q\^b^f^fFg/ghC7&'_be'eff r   c                     | j                          | j                  dk(  r1| j                  j                  | j                  | j
                         | j                  dk(  r5| j
                  y| j                  j                  ry| j                          y| j                  rbt        | j                        }| j
                  At        | j
                  |      s+t        dt        | j
                        j                  z        | j                  dk(  rD| j
                  8| j                  s,t        | j
                        t        | j                        k(  ry| j                  dk(  rY| j
                  M| j                  rAt!        t#        t        | j                        t        | j
                        z
              dk(  ry| j
                  | j                  k(  ry| j                  j                  ry| j%                          y)	NrO   )r   r    r'   FTz#Type mismatch. Type in defaults: %srG   r   )rc   r!   r)   	exit_jsonr   r(   
check_moderp   r   r   r    rH   r   r   r   rm   lenrO   rk   )r   
value_types     r   runzOSXDefaults.runV  s    			::KK!!dhhd6H6H!I ::!!!){{%%KKM ??djj)J!!-jASASU_6`*+PSWX\XjXjSkStSt+tuu 99D$6$6$B4>>D../3tzz?BYY'!d&8&8&D\_`dehimisisetwz{  |N  |N  xO  fO  aP  ]Q  UV  ]V4::-;;!! 	

r   N)r   r   r   __doc__r   staticmethodr6   r.   rT   rV   ra   rc   rk   rp   rv   r   r   r   r   r      sr    0C@ $ $ "S "SH(5  "@Bf>g %r   r   c                     t        t        t        dd      t        d      t        dd      t        dd      t        dd	g d
      t        dd      t        d      t        ddg d      t        dd      	      ddddgff      } 	 t        |       }| j                  |j	                                y # t
        $ r&}| j                  |j                         Y d }~y d }~ww xY w)Nr2   NSGlobalDomain)r   default)r   F)r   no_logr9   Tr8   )rG   r9   r:   rA   rF   rD   rE   r8   )r   r{   choicesrawpresent)r'   rO   r   z/usr/bin:/usr/local/bin)	r   r   r   r   r   r   r    r!   r"   r!   r    )argument_specsupports_check_moderequired_if)r)   )changed)r   )r   dictr   rr   rv   r   	fail_jsonr   )r)   r#   es      r   mainr     s    U,<=5!%.65(  =F  G6E"E9>[\5*CD

 !i'+
F$(f-0 (QYY''(s   ,B; ;	C*C%%C*__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr   r]   ansible.module_utils.basicr   ansible.module_utils.sixr   r	   	Exceptionr   objectr   r   r   r   r   r   <module>r      sf    A @FP4l  	 4 ;9 `& `R(8 zF r   