
    Vh                        d Z ddlmZ ddlmZ ddlZddlm	Z	m
Z
mZ ddlmZmZmZmZmZmZmZmZmZmZ  G d d	e      Z G d
 de      Z G d de      Zy)z7Composite argument value parsers used by other parsers.    )annotationsN   )NativePythonConfigPythonConfigVirtualPythonConfig   )
AbsolutePathParser	AnyParserChoicesParserDocumentationStateIntegerParserMatchConditionsParserParserErrorParserStateParserBoundaryc                  4    e Zd ZdZ	 	 	 	 	 	 ddZddZddZy)	PythonParsera  
    Composite argument parser for Python versions, with support for specifying paths and using virtual environments.

    Allowed formats:

    {version}
    venv/{version}
    venv/system-site-packages/{version}

    The `{version}` has two possible formats:

    X.Y
    X.Y@{path}

    Where `X.Y` is the Python major and minor version number and `{path}` is an absolute path with one of the following formats:

    /path/to/python
    /path/to/python/directory/

    When a trailing slash is present, it is considered a directory, and `python{version}` will be appended to it automatically.

    The default path depends on the context:

    - Known docker/remote environments can declare their own path.
    - The origin host uses `sys.executable` if `{version}` matches the current version in `sys.version_info`.
    - The origin host (as a controller or target) use the `$PATH` environment variable to find `python{version}`.
    - As a fallback/default, the path `/usr/bin/python{version}` is used.

    NOTE: The Python path determines where to find the Python interpreter.
          In the case of an ansible-test managed virtual environment, that Python interpreter will be used to create the virtual environment.
          So the path given will not be the one actually used for the controller or target.

    Known docker/remote environments limit the available Python versions to configured values known to be valid.
    The origin host and unknown environments assume all relevant Python versions are available.
    c                   t        |      }|r|j                  d       t        |      }|r|j                  d       t        |      dgz   }|| _        || _        || _        || _        || _        || _        || _        y )Ndefaultzvenv/zsystem-site-packages/)listappendversionsallow_default
allow_venvversion_choicesfirst_choicesvenv_choices)selfr   r   r   r   r   r   s          `/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/cli/parsers/value_parsers.py__init__zPythonParser.__init__@   s     x.""9-_-  )O,0G/HH *$.*((    c                z   |j                  dd      5 }t        | j                        j                  |      }ddd       dk(  r|j                  dd      5 }t        | j                        j                  |      }ddd       |dk(  rCd}|j                  dd      5 }t        | j
                        j                  |      }ddd       nd}t        ||	      }nt        |
      }j                  dk(  rt               j                  |      |_
        |S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   jxY w);Parse the input from the given state and return the result.z@/FrequiredNvenvzsystem-site-packagesT@)versionsystem_site_packages)r)   )delimitr   r   parser   r   r   r   matchr	   path)r   stateboundaryr)   r*   pythons         r    r,   zPythonParser.parse[   s<    ]]4%]0 	EH#D$6$67==eDG	E
 fte4 H'(9(9:@@GH 00'+$]]3]7 O8+D,@,@AGGNGO O (-$(OcdF'8F>>S ,.44U;FK3	E 	EH HO Os#   %D%D% %D1D"%D.1D:c                    | j                   rdnd}| j                  r|dj                  | j                        z  }n|dz  }|dz  }|S )2Generate and return documentation for this parser.z[venv/[system-site-packages/]] |z{X.Y}z[@{path|dir/}])r   r   joinr   )r   r/   docss      r    documentzPythonParser.documentz   sK     48??/==CHHT1122DGOD  r"   N)r   zc.Sequence[str]r   boolr   r9   r/   r   returnzt.Anyr/   r   r;   zt.Optional[str])__name__
__module____qualname____doc__r!   r,   r8    r"   r    r   r      s3    "H)!) 	)
 )6>r"   r   c                  0     e Zd ZdZd fdZd fdZ xZS )PlatformParserzGComposite argument parser for "{platform}/{version}" formatted choices.c                f    t         |   |t        j                  t        j                  z         y )N)
conditions)superr!   r   CHOICEANY)r   choices	__class__s     r    r!   zPlatformParser.__init__   s&    _-C-CoFYFY-YZr"   c                |    t         |   |      }t        |j                  d            dk7  rt	        d|       |S )r$   /r   zinvalid platform format: )rF   r,   lensplitr   )r   r/   valuerJ   s      r    r,   zPlatformParser.parse   s?    e$u{{3 A% 9%ABBr"   )rI   z	list[str]r;   Noner:   )r=   r>   r?   r@   r!   r,   __classcell__)rJ   s   @r    rC   rC      s    Q[ r"   rC   c                  $    e Zd ZdZdZddZddZy)SshConnectionParserzd
    Composite argument parser for connecting to a host using SSH.
    Format: user@host[:port]
    z{user}@{host}[:{port}]c                   |j                   }|j                  d      5  t        d| j                         j	                  |      }ddd       t        |d       |j                  dd      5 }t        d	| j                         j	                  |      }ddd       t        |d
       j                  r't        d      j	                  |      }t        |d|       |S # 1 sw Y   xY w# 1 sw Y   WxY w)r$   r(   zExpected {user} from: )no_match_messageNuser:Fr%   zExpected {host} from: hosti  port)current_namespacer+   r
   EXPECTED_FORMATr,   setattrr-   r   )r   r/   	namespacerV   colonrX   rY   s          r    r,   zSshConnectionParser.parse   s    ++	]]3 	n0HI]I]H^._`ffglmD	n 		64(]]3]/ 	n50HI]I]H^._`ffglmD	n 		64(;; '--e4DIvt,	n 	n
	n 	ns   )C"/)C."C+.C7c                    | j                   S )r3   )r[   )r   r/   s     r    r8   zSshConnectionParser.document   s    ###r"   Nr:   r<   )r=   r>   r?   r@   r[   r,   r8   rA   r"   r    rS   rS      s    
 /O($r"   rS   )r@   
__future__r   collections.abcabcctypingthost_configsr   r   r   argparsing.parsersr	   r
   r   r   r   r   r   r   r   r   r   rC   rS   rA   r"   r    <module>rh      sQ    = "     k6 k\]  $& $r"   