o
    L                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
dZdd Zdd ZG dd dejZdS )z0Calliope parsed resource parameter info objects.    )absolute_import)division)unicode_literalsN)parser_errors)
properties)resource_cache)resource_propertyc                 C   s<   |  ddd}|r|d | }ttdd|dS )zGReturns a conventional dest name given param name with optional prefix.-_zs?I[Dd]$ )replacestripr   ConvertToSnakeCaseresub)paramprefixname r   O/tmp/google-cloud-sdk/lib/googlecloudsdk/command_lib/util/parameter_info_lib.pyGetDestFromParam   s   r   c                 C   s   d|  dd S )z3Returns a conventional flag name given a dest name.z--r
   r	   )r   )destr   r   r   GetFlagFromDest'   s   r   c                 C   s   |  dddS )z3Returns a conventional dest name given a flag name.r	   r
   )r   r   )flagr   r   r   GetDestFromFlag,   s   r   c                       sv   e Zd ZdZd fdd	Zedd Zedd Zd	d
 Zdd Z	dddZ
		dddZdddZdddZ  ZS )ParameterInfoByConventiona(  An object for accessing parameter values in the program state.

  "program state" is defined by this class.  It could include parsed command
  line arguments and properties.  The class can also map between resource and
  program parameter names.

  This ParameterInfo object provides default methods based on resource
  argument naming conventions. It should be used as a fallback only. The
  ResourceArgument object should derive a ParameterInfo that provides the
  exact parameter/argument information. It can do this in the ParameterInfo
  method of the completer object.

  The naming conventions are:

    - A parsed resource parameter name, with trailing s?I[Dd] deleted, and
      converted to snake_case is also the command line flag or positional parsed
      args Namespace dest name.
    - Argument specific flag names use the dest as a prefix, for example,
      if dest is 'foo', then the zone flag is either --foo-zone or --zone.
    - Property values are in the property section named by the collection API
      (the first dotted component of the collection name) and/or the core
      section, checked in that order.  For example, for the 'compute.instances'
      collection the API is 'compute' and the zone property is 'compute/zone',
      and the 'project' property is 'compute/project' or 'core/project'.

  Attributes:
    _api: The collection API name.
    _argument: The argument object that the completer for this parameter info
      is attached to.
    _parsed_args: The parsed command line args Namespace.
    _prefix: The related flag prefix.
  Nc                    sT   t t| jdi | || _|| _|r|jnd | _|r%|dd | _d S d | _d S )N.r   r   )	superr   __init___parsed_args	_argumentr   _prefixsplit_api)selfparsed_argsargument
collectionkwargs	__class__r   r   r   S   s
   "z"ParameterInfoByConvention.__init__c                 C      | j S N)r    r$   r   r   r   r&   Z      z"ParameterInfoByConvention.argumentc                 C   r+   r,   )r   r-   r   r   r   r%   ^   r.   z%ParameterInfoByConvention.parsed_argsc              	   C   sb   g }| j r|| j|| j d || |D ]}z| j||fW   S  tjy.   Y qw dS )zReturns the argument parser (flag_name, flag_dest) for dest.

    Args:
      dest: The resource argument dest name.

    Returns:
      Returns the argument parser (flag_name, flag_dest) for dest.
    r   )NN)r!   appendGetDestr   GetFlagr   UnknownDestinationException)r$   r   dests	flag_destr   r   r   _GetFlagAndDestb   s   	
z)ParameterInfoByConvention._GetFlagAndDestc              	   C   s^   g }| j r|| j d |  || |D ]}z
t| W   S  tjy,   Y qw dS )zReturns the property value for dest.

    Args:
      dest: The resource argument dest.

    Returns:
      The property value for dest.
    /N)r#   r0   r   
FromStringGetNoSuchPropertyError)r$   r   propspropr   r   r   _GetPropertyValuev   s   	
z+ParameterInfoByConvention._GetPropertyValuec                 C   s   t ||dS )a  Returns the argument parser dest name for parameter_name with prefix.

    Args:
      parameter_name: The resource parameter name.
      prefix: The prefix name for parameter_name if not None.

    Returns:
      The argument parser dest name for parameter_name.
    r/   )r   )r$   parameter_namer   r   r   r   r1      s   
z!ParameterInfoByConvention.GetDestTFc           	      C   s   ~|  |}| |\}}|s|d7 }| |\}}|sdS | j|}|du r/|r/| |}||krH|du r9|}|rH|du rA|S dj||dS dS )a  Returns the command line flag for parameter[=parameter_value].

    Args:
      parameter_name: The parameter name.
      parameter_value: The parameter value if not None. Otherwise
        GetValue() is used to get the value.
      check_properties: Check property values if parsed_args don't help.
      for_update: Return flag for a cache update command.

    Returns:
      The command line flag the for parameter.
    sNTz{name}={value})r   value)r1   r6   r   GetValuer=   format)	r$   r>   parameter_valuecheck_properties
for_updater   r   r5   program_valuer   r   r   r2      s&   

z!ParameterInfoByConvention.GetFlagc              	   C   sh   d}|  |}| j || jd|fD ]}z	| j|}W  n
 tjy&   Y qw |du r2|r2| |}|S )zReturns the program state value for parameter_name.

    Args:
      parameter_name: The parameter name.
      check_properties: Check property values if parsed_args don't help.

    Returns:
      The program state value for parameter_name.
    Nr/   )r1   r!   r   rA   r   r3   r=   )r$   r>   rD   r@   r   r   r   r   r   rA      s   


z"ParameterInfoByConvention.GetValuec                 C   s   d}| j j||dS )zExecutes command in the current CLI.

    Args:
      command: The command arg list to execute.
      call_arg_complete: Enable arg completion if True.

    Returns:
      Returns the list of resources from the command.
    F)call_arg_complete)r   _Execute)r$   commandrG   r   r   r   Execute   s   
z!ParameterInfoByConvention.Executer,   )NTF)T)F)__name__
__module____qualname____doc__r   propertyr&   r%   r6   r=   r1   r2   rA   rJ   __classcell__r   r   r)   r   r   1   s    !




#r   r,   )rN   
__future__r   r   r   r   googlecloudsdk.callioper   googlecloudsdk.corer   googlecloudsdk.core.cacher   googlecloudsdk.core.resourcer   r   r   r   ParameterInfor   r   r   r   r   <module>   s   
	