
                             S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  Sr
SrS	rS
rS rS rS rS rS rS rS rS rS rS rS rg)z$Resource Settings command utilities.    N)encoding)
exceptions)yaml)filesz	settings/organizationfolderprojectc                 \   [         R                  " U 5      n [        R                  " U5      n[        R
                  " U5      n [        R                  " X5      $ ! [        R                   a    Un N/f = f! [         a*  n[        R                  " SR                  X5      5      eSnAff = f)zReturns a message populated from the JSON or YAML file.

Args:
  filepath: str, A local path to an object specification in JSON or YAML
    format.
  message: messages.Message, The message class to populate from the file.
zUnable to parse file [{}]: {}.N)r   ReadFileContentsr   loadjsondumpsYAMLParseErrorr   JsonToMessage	Exceptionr   InvalidInputErrorformat)filepathmessagefile_contentsyaml_objjson_stres         Alib/googlecloudsdk/command_lib/resource_manager/settings/utils.pyGetMessageFromFiler      s     ((2-yy'Hzz(#H!!'44	 
		 H
 
 

&
&'G'N'N(  s)   ,A A7 A43A47
B+%B&&B+c                     U R                   R                  [        5      (       a  U R                   $ [        U R                   -   $ )aP  Returns the setting from the user-specified arguments.

A setting has the following syntax: settings/{setting_name}.

This handles both cases in which the user specifies and does not specify the
constraint prefix.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
)setting_name
startswithSETTINGS_PREFIXargss    r   GetSettingFromArgsr"   6   s8     
!!/22	4,,	,,    c                     U R                   R                  [        5      (       a  U R                   [        [        5      S $ U R                   $ )a  Returns the setting name from the user-specified arguments.

This handles both cases in which the user specifies and does not specify the
setting prefix.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
N)r   r   r   lenr    s    r   GetSettingNameFromArgsr&   H   s@     
!!/22S1233			r#   c                 *    U R                  S5      S   $ )Returns the resource id from the setting path.

A setting path should start with following syntax:
[organizations|folders|projects]/{resource_id}/settings/{setting_name}

Args:
  setting: A String that contains the setting path
/   splitsettings    r   GetSettingNameFromStringr/   X        
s	A	r#   c                     U R                   =(       d    U R                  =(       d    U R                  nU R                   (       a  [        nOU R                  (       a  [        nO[
        nSR                  US-   U5      $ )a  Returns the resource from the user-specified arguments.

A resource has the following syntax:
[organizations|folders|projects]/{resource_id}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
{}/{}s)r   r   r	   ORGANIZATIONFOLDERPROJECTr   )r!   resource_idresource_types      r   GetParentResourceFromArgsr9   d   sV     !!@T[[@DLL+	 M{{MM	+[	99r#   c                 t    U R                  S5      S   nU R                  S5      S   nSR                  X5      $ )zReturns the resource from the user-specified arguments.

A setting path should start with following syntax:
[organizations|folders|projects]/{resource_id}/settings/{setting_name}/value

Args:
  setting: A String that contains the setting path
r)   r      r2   )r,   r   )r.   r8   r7   s      r   GetParentResourceFromStringr<   z   s9     --$Q'-c"1%+		33r#   c                     U R                  S5      (       a  [        nU$ U R                  S5      (       a  [        nU$ U R                  S5      (       a  [        nU$ SnU$ )zReturns the resource type from the setting path.

A setting path should start with following syntax:
[organizations|folders|projects]/{resource_id}/settings/{setting_name}

Args:
  setting: A String that contains the setting path
zorganizations/zfolders/z	projects/invalid)r   r4   r5   r6   )r.   r8   s     r   GetResourceTypeFromStringr?      sp     ()) M 
 *%%M 
 +&&M 
 M	r#   c                 *    U R                  S5      S   $ )r(   r)   r;   r+   r-   s    r   GetResourceIdFromStringrA      r0   r#   c                 P    [        U 5      n[        U 5      nSR                  X5      $ )a*  Returns the settings path from the user-specified arguments.

A settings path has the following syntax:
[organizations|folders|projects]/{resource_id}/settings/{setting_name}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
z{}/settings/{}r9   r&   r   r!   resourcer   s      r   GetSettingsPathFromArgsrF      s)     't,('-,		 	 	88r#   c                 P    [        U 5      n[        U 5      nSR                  X5      $ )a3  Returns the settings path from the user-specified arguments.

A settings path has the following syntax:
[organizations|folders|projects]/{resource_id}/effectiveSettings/{setting_name}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
z{}/effectiveSettings/{}rC   rD   s      r    GetEffectiveSettingsPathFromArgsrH      s)     't,('-,	"	)	)(	AAr#   c                 z    [        U 5      S:X  a  gU R                  S5      n[        U5      S:w  a  gUS   S:w  a  gg)r(   r>   Fr)         settingsT)r?   r,   r%   )r.   setting_lists     r   ValidateSettingPathrN      sD     w'94s#,!A*$	r#   )__doc__r   apitools.base.pyr   4googlecloudsdk.command_lib.resource_manager.settingsr   googlecloudsdk.corer   googlecloudsdk.core.utilr   r   r4   r5   r6   r   r"   r&   r/   r9   r<   r?   rA   rF   rH   rN    r#   r   <module>rU      sg    +  % K $ *	
.-$ 	:,4 ,	9 B r#   