HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //proc/self/root/lib/python3.6/site-packages/ipalib/__pycache__/cli.cpython-36.opt-1.pyc
3

�d[e=��
@s�dZddlmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZyddlZddlZWnek
r�dZZYnXddlZddlmZddlmZmZmZmZejr�eZ ej!�re"e�ej#d�ddl$m%Z%ddl$m&Z&dd	l$m'Z'dd
l(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1ddl2m3Z3m4Z4ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;dd
l<m=Z=ddl$m>Z>ddl?m@Z@ddlAmBZBddlCZCejDeE�ZFdd�ZGdd�ZHGdd�de&jI�ZJGdd�de%jK�ZLGdd�de%jM�ZNGdd�dejO�ZPGdd�de%jM�ZQGdd �d e%jM�ZReLeQeRfZSGd!d"�d"�ZTGd#d$�d$ejU�ZVGd%d&�d&ejW�ZXGd'd(�d(e&jY�ZZGd)d*�d*ejU�Z[eZeJeQeLeNfZ\d+d,�Z]dS)-z+
Functionality for Command Line Interface.
�)�print_functionN)�input)�check_client_configuration�	get_pager�get_terminal_height�
open_in_pagerzutf-8)�frontend)�backend)�plugable)	�PublicError�CommandError�	HelpError�
InternalError�NoSuchNamespaceError�ValidationError�NotFound�NotConfiguredError�PromptFailed)�CLI_TAB�LDAP_GENERALIZED_TIME_FORMAT)�File�
BinaryFile�Str�Enum�Any�Flag)�_)�api)�DNSName)�ScriptErrorcCs|jdd�S)zl
    Takes a Python identifier and transforms it into form suitable for the
    Command Line Interface.
    r�-)�replace)�name�r#�/usr/lib/python3.6/cli.py�to_cliPsr%cCst|�jdd�S)zh
    Takes a string from the Command Line Interface and transforms it into a
    Python identifier.
    r r)�strr!)�cli_namer#r#r$�from_cliYsr(c@sFeZdZdZdd�ZdHdd�Zdd�Zejr>d	d
�Z	dd�Z
nd
d
�Z	dd�Z
dIdd�Zdd�Zdd�Z
dJdd�ZdKdd�ZdLdd�ZdMdd�ZdNd d!�Zdid"gdfd#d$�ZdOd%d&�ZdPd'd(�ZdQd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�ZdRd7d8�Zd9d:�Zefd;d<�Zd=d>�Z dSd@dA�Z!dTdBdC�Z"dUdDdE�Z#dVdFdG�Z$dS)W�textuiz;
    Backend plugin to nicely format output to stdout.
    cCsTtjj�rPy0tjtjtjtjddddd��}tj	d|�dSt
k
rNYnXdS)z�
        Return the width (in characters) of output tty.

        If stdout is not a tty, this method will return ``None``.
        ZHHHHr�N)�sys�stdout�isatty�fcntlZioctl�termiosZ
TIOCGWINSZ�struct�pack�unpack�IOError)�selfZwinsizer#r#r$�
get_tty_widthfs
ztextui.get_tty_widthNcsbt|�ttfkr"tdtt|f��t|�dkr2dS�dkrLtdd�|D��St�fdd�|D��S)a�
        Return the max width (in characters) of a specified column.

        For example:

        >>> ui = textui(api)
        >>> rows = [
        ...     ('a', 'package'),
        ...     ('an', 'egg'),
        ... ]
        >>> ui.max_col_width(rows, col=0)  # len('an')
        2
        >>> ui.max_col_width(rows, col=1)  # len('package')
        7
        >>> ui.max_col_width(['a', 'cherry', 'py'])  # len('cherry')
        6
        zrows: need %r or %r; got %rrNcss|]}t|�VqdS)N)�len)�.0�rowr#r#r$�	<genexpr>�sz'textui.max_col_width.<locals>.<genexpr>c3s|]}t|��VqdS)N)r6)r7r8)�colr#r$r9�s)�type�list�tuple�	TypeErrorr6�max)r4�rowsr:r#)r:r$�
max_col_widthwsztextui.max_col_widthcCst|dd�dkrdS|jS)N�encodingzUTF-8)�getattrrB)r4�streamr#r#r$Z__get_encoding�sztextui.__get_encodingcsLt|�tkr"�jtj�}|j|�St|�ttfkrHt�fdd�|D��S|S)z1
            Decode text from stdin.
            c3s|]}�j|�VqdS)N)�decode)r7�v)r4r#r$r9�sz textui.decode.<locals>.<genexpr>)r;�bytes�_textui__get_encodingr+�stdinrEr<r=)r4�valuerBr#)r4r$rE�s
z
textui.decodecCs|jtj�}|j|�S)z;
            Encode text for output to stdout.
            )rHr+r,�encode)r4Zunicode_textrBr#r#r$rK�sz
textui.encodecCs|S)Nr#)r4rJr#r#r$rE�scCs|S)Nr#)r4rJr#r#r$rK�scCs |dks|dkr||S||S)Nr*r#)r4�n�singular�pluralr#r#r$�
choose_number�sztextui.choose_numbercCsNt|�tkrtj|�jd�St|�tjkr4|jt�St|t	�rFt
|�S|SdS)z�
        Convert a binary value to base64. We know a value is binary
        if it is a python bytes type, otherwise it is a plain string.
        This function also converts datetime and DNSName values to string.
        �asciiN)r;rG�base64Z	b64encoderE�datetimeZstrftimer�
isinstancer�unicode)r4rJr#r#r$�
encode_binary�s

ztextui.encode_binarycCstt|��dS)z?
        Print exactly like ``print`` statement would.
        N)�printrT)r4�stringr#r#r$�print_plain�sztextui.print_plaincCsH|dkr|j�}|dk	r8|t|�kr8|d|d�d}tt|��dS)a�
        Force printing on a single line, using ellipsis if needed.

        For example:

        >>> ui = textui(api)
        >>> ui.print_line('This line can fit!', width=18)
        This line can fit!
        >>> ui.print_line('This line wont quite fit!', width=18)
        This line wont ...

        The above example aside, you normally should not specify the
        ``width``.  When you don't, it is automatically determined by calling
        `textui.get_tty_width()`.
        N�z...)r5r6rVrT)r4�text�widthr#r#r$�
print_line�s
ztextui.print_linecCs6|dkr|j�}x tj|j�|�D]}t|�q"WdS)a+
        Print a paragraph, automatically word-wrapping to tty width.

        For example:

        >>> text = '''
        ... Python is a dynamic object-oriented programming language that can
        ... be used for many kinds of software development.
        ... '''
        >>> ui = textui(api)
        >>> ui.print_paragraph(text, width=45)
        Python is a dynamic object-oriented
        programming language that can be used for
        many kinds of software development.

        The above example aside, you normally should not specify the
        ``width``.  When you don't, it is automatically determined by calling
        `textui.get_tty_width()`.

        The word-wrapping is done using the Python ``textwrap`` module.  See:

            http://docs.python.org/library/textwrap.html
        N)r5�textwrap�wrap�striprV)r4rZr[�liner#r#r$�print_paragraph�sztextui.print_paragraphr*cCstt||�dS)a~
        Print at specified indentation level.

        For example:

        >>> ui = textui(api)
        >>> ui.print_indented('One indentation level.')
          One indentation level.
        >>> ui.print_indented('Two indentation levels.', indent=2)
            Two indentation levels.
        >>> ui.print_indented('No indentation.', indent=0)
        No indentation.
        N)rVr)r4rZ�indentr#r#r$�print_indentedsztextui.print_indentedcCs0x*|D]"\}}|jd||j|�f|�qWdS)a�
        Print (key = value) pairs, one pair per line.

        For example:

        >>> items = [
        ...     ('in_server', True),
        ...     ('mode', u'production'),
        ... ]
        >>> ui = textui(api)
        >>> ui.print_keyval(items)
          in_server = True
          mode = u'production'
        >>> ui.print_keyval(items, indent=0)
        in_server = True
        mode = u'production'

        Also see `textui.print_indented`.
        z%s = %rN)rcrU)r4r@rb�keyrJr#r#r$�print_keyvalsztextui.print_keyval�%s: %sTcs�t|ttf�s,�j||�j|�f|��nj|r\x&|D]}�j||�j|�f|�q6W�n:�fdd�|D�}t|�dkr�t|d�ttfkr�x0|D](}dj|�}�j||�j|�f|�q�WdSt|�dkr�djdd�|D��}ndS�j�}	|	o�|�rJd	t	|d
t|�df}
|	t|
�8}	t
j||	dd
�}t|�dk�rTdg}n
d}
|g}�j|||df|�x(|dd�D]}�jd	|
|f��qzWdS)a�
        Print an ldap attribute.

        For example:

        >>> attr = 'dn'
        >>> ui = textui(api)
        >>> ui.print_attribute(attr, u'dc=example,dc=com')
          dn: dc=example,dc=com
        >>> attr = 'objectClass'
        >>> ui.print_attribute(attr, [u'top', u'someClass'], one_value_per_line=False)
          objectClass: top, someClass
        >>> ui.print_attribute(attr, [u'top', u'someClass'])
          objectClass: top
          objectClass: someClass
        csg|]}�j|��qSr#)rU)r7rF)r4r#r$�
<listcomp>Bsz*textui.print_attribute.<locals>.<listcomp>rz: Nz, css|]}t|�VqdS)N)r&)r7rFr#r#r$r9Ksz)textui.print_attribute.<locals>.<genexpr>z%s%s� �F)Zbreak_long_words�r*)
rSr<r=rcrUr6r;�joinr5rr]r^rX)r4�attrrJ�formatrb�one_value_per_linerF�lrZZline_lenZs_indentr`r#)r4r$�print_attribute's8
" 


ztextui.print_attributeZdncsX������fdd�}x"|D]���kr|����=qWxt��D]�|��qDWdS)z+
        Print an ldap entry dict.
        cs>��kr$�j������d�n�j�����d�dS)N)rbrn)rp)�a)rl�attr_map�entryrbrnr4r#r$�
print_attris
z'textui.print_entry1.<locals>.print_attrN)�sorted)r4rsrbrrZ
attr_orderrnrtr#)rlrrrsrbrnr4r$�print_entry1`s	


ztextui.print_entry1c
	Cs<d}x2|D]*}	|std�d}|j|	||||||�q
WdS)NTrjF)rV�print_entry)
r4�entries�order�labels�flags�	print_allrmrb�firstrsr#r#r$�
print_entrieszs
ztextui.print_entriesc

Cs�t|ttf�rt|�}|dkr*t�}d}nd}|dk	�r@�x|D]�}	|	|krNq@|j|	|	�}
|j|	g�}||	}d|kr�|ddfgdgkr�q@t|t�r�tj|�dkr�q@|j||
df|�|j|||||||dd�n`t|ttf�o�t	dd	�|D���r$|j
|
d||�|j|||||||d�n|j
|
||||�||	=q@W|�r|x4t|�D](}	|j|	|	�}
|j
|	||	|||��qPWdS)
NTFZsuppress_emptyrjrr*)rbcss|]}t|t�VqdS)N)rS�dict)r7�valr#r#r$r9�sz%textui.print_entry.<locals>.<genexpr>)
rSr<r=r�getrZentry_countrcrw�allrpr~ru)
r4rsryrzr{r|rmrbrnrd�label�flagrJr#r#r$rw�sH


ztextui.print_entryrr cCs<|t|�}|r|j||�|j||�|r8|j||�dS)a�
        Print a string with a dashed line above and/or below.

        For example:

        >>> ui = textui(api)
        >>> ui.print_dashed('Dashed above and below.')
        -----------------------
        Dashed above and below.
        -----------------------
        >>> ui.print_dashed('Only dashed below.', above=False)
        Only dashed below.
        ------------------
        >>> ui.print_dashed('Only dashed above.', below=False)
        ------------------
        Only dashed above.
        N)r6rc)r4rWZaboveZbelowrb�dashZdashesr#r#r$�print_dashed�sztextui.print_dashedcCs|j|ddd�dS)z�
        Print a primary header at indentation level 0.

        For example:

        >>> ui = textui(api)
        >>> ui.print_h1('A primary header')
        ================
        A primary header
        ================
        r�=)rbr�N)r�)r4rZr#r#r$�print_h1�sztextui.print_h1cCs|j|ddd�dS)z�
        Print a secondary header at indentation level 1.

        For example:

        >>> ui = textui(api)
        >>> ui.print_h2('A secondary header')
          ------------------
          A secondary header
          ------------------
        r*r )rbr�N)r�)r4rZr#r#r$�print_h2�sztextui.print_h2cCs|jdt|��dS)ae
        Print a command name.

        The typical use for this is to mark the start of output from a
        command.  For example, a hypothetical ``show_status`` command would
        output something like this:

        >>> ui = textui(api)
        >>> ui.print_name('show_status')
        ------------
        show-status:
        ------------
        z%s:N)r�r%)r4r"r#r#r$�
print_name�sztextui.print_namecCs|j||�dS)N)r�)r4�msg�outputr#r#r$�print_header�sztextui.print_headercCs|j|�dS)z�
        Print a summary at the end of a comand's output.

        For example:

        >>> ui = textui(api)
        >>> ui.print_summary('Added user "jdoe"')
        -----------------
        Added user "jdoe"
        -----------------
        N)r�)r4r�r#r#r$�
print_summary�sztextui.print_summarycCs,t|�tk	rt|�}|j|j|||��dS)a�
        Print a summary count.

        The typical use for this is to print the number of items returned
        by a command, especially when this return count can vary.  This
        preferably should be used as a summary and should be the final text
        a command outputs.

        For example:

        >>> ui = textui(api)
        >>> ui.print_count(1, '%d goose', '%d geese')
        -------
        1 goose
        -------
        >>> ui.print_count(['Don', 'Sue'], 'Found %d user', 'Found %d users')
        -------------
        Found 2 users
        -------------

        If ``count`` is not an integer, it must be a list or tuple, and then
        ``len(count)`` is used as the count.
        N)r;�intr6r�rO)r4�countrMrNr#r#r$�print_count	sztextui.print_countcCstdt|��dS)Nz
  ** %s **)rVrT)r4rZr#r#r$�print_error(sztextui.print_errorcCsBy|j||j|���Sttfk
r<t�t|d��YnXdS)z�Prompt user for input

        Handles encoding the prompt and decoding the input.
        On end of stream or ctrl+c, raise PromptFailed.
        )r"N)rErK�KeyboardInterrupt�EOFErrorrVr)r4�promptr��prompt_funcr#r#r$�
prompt_helper+s
ztextui.prompt_helpercCs|jd||f�dS)Nz
>>> %s: %s)rX)r4Z	attribute�errorr#r#r$�print_prompt_attribute_error7sz#textui.print_prompt_attribute_errorFcCs@|rd|}nd|}|dkr(d|}nd||f}|j||�S)z(
        Prompt user for input.
        z[%s]z%sNz%s: z	%s [%s]: )r�)r4r��defaultZ
get_values�optionalr�r#r#r$r�:s

z
textui.promptcCszd}|dk	r|rd}nd}|r,d||f}nd|}x@|j||�j�}|d
krRdS|dkr^dS|dk	r6|dkr6|Sq6W|S)a�
        Prompt user for yes/no input. This method returns True/False according
        to user response.

        Parameter "default" should be True, False or None

        If Default parameter is not None, user can enter an empty input instead
        of Yes/No answer. Value passed to Default is returned in that case.

        If Default parameter is None, user is asked for Yes/No answer until
        a correct answer is provided. Answer is then returned.
        NZYesZNoz%s Yes/No (default %s): z%s Yes/No: �yes�yTrL�noFrj)r�r�)rLr�)r��lower)r4r�r�Zdefault_promptr��datar#r#r$�prompt_yesnoIs"ztextui.prompt_yesnocCs�tjj�rzdt|�}ttd�t|d��}x`|j||tjd�}|sH|S|j||tjd�}||krf|S|jtd��q.Wn|j	tjj
�j��SdS)zx
        Prompt user for a password or read it in via stdin depending
        on whether there is a tty or not.
        z%s: z!Enter %(label)s again to verify: )r�)r�zPasswords do not match!N)r+rIr-rTrrr��getpassr�rE�readliner_)r4r��confirmr�Z
repeat_promptZpw1Zpw2r#r#r$�prompt_passwordos
ztextui.prompt_passwordcCsL|jjstjj�rdSt|�}|dkr8ttd�d��d}xL|D]D}i}x|D]}	|j|	d�||	<qPW|j	d|||f�|d}qBW|r�|j
|dd�x�y|jd	|�}
Wntk
r�dSX|
j
�dkr�dS|
j
�dkr�dSy.t|
�d}||k�odkn�rPWntk
�r*YnX|j	d
|�q�W|j	d�|S)a�
        Display a list of lines in with formatting defined in ``format``.
        ``attrs`` is a list of attributes in the format.

        Prompt user for a selection and return the value (index of
        ``entries`` -1).

        If only one entry is provided then always return 0.

        Return: 0..n for the index of the selected entry
                -1 if all entries should be displayed
                -2 to quit, no entries to be displayed
        r*rzNo matching entries found)�reasonrjz%d: %szFound %d matchzFound %d matchesz*Choose one: (1 - %s), a for all, q to quitri�qrqz&Please enter a number between 1 and %s������r�r�)�env�interactiver+r,r-r6rrr�r\r�r�r�r�r��	Exception)r4rxrmZattrsZ
display_countZcounter�i�e�drqZrespZ	selectionr#r#r$�select_entry�s@


ztextui.select_entry)N)N)N)N)r*)r*)rfr*T)NNNTrfr*)NNNTrfr*)TTrr )N)NNF)N)T)T)%�__name__�
__module__�__qualname__�__doc__r5rArH�six�PY2rErKrOrUrXr\rarcrerprvr~rwr�r�r�r�r�r�r�r�rr�r�r�r�r�r�r#r#r#r$r)asF







9
	
.



&
r)cs�eZdZdZGdd�d�Zedded�ed�d�fZed	d
gd�fZ	e
�ZdZd
d�Z
dd�Z�fdd�Zddd�Zdd�Zdd�Z�ZS)�helpz.
    Display help for a command or topic.
    c@s6eZdZdZdd�Zedd��Zddd�Zd	d
�ZdS)
zhelp.Writerz$
        Writer abstraction
        cCs||_g|_dS)N)�outfile�buffer)r4r�r#r#r$�__init__�szhelp.Writer.__init__cCs*d}x |jD]}|t|jd��7}qW|S)Nr�
)r�r6�split)r4Zlengthr`r#r#r$�
buffer_length�szhelp.Writer.buffer_lengthrjcCs|jjt|��dS)N)r��appendrT)r4rWr#r#r$r��szhelp.Writer.appendcCspt�}|r4|jt�kr4dj|j�jd�}t||�n8y"x|jD]}t||jd�q>WWnt	k
rjYnXdS)Nr�zutf-8)�file)
rr�rrkr�rKrrVr�r3)r4Zpagerr�r`r#r#r$�write�szhelp.Writer.writeN)rj)	r�r�r�r�r��propertyr�r�r�r#r#r#r$�Writer�s

r�zcommand?�topiczTopic or CommandzThe topic or command name.)r'r��doczoutfile?�	no_option)r{NcCs�d}d}x�|jjD]�}dj|j|�}ytj|}Wn:tk
rlytj|�}Wnt	k
rfwYnXYnX|j
dk	r�t|j
p�d�j�}y
|j
}Wqtk
r�YqXqW||fS)Nrjz{0}.{1})rZpackagesrmr�r+�modules�KeyError�	importlib�
import_module�ImportErrorr�rTr_r��AttributeError)r4r�r�Zparent_topic�packageZmodule_name�moduler#r#r$�
_get_topic�s$


zhelp._get_topiccCs,t|j|dt|�f�}||j|d<dS)Nr*)r?�_topicsr6)r4�
topic_name�mod_name�mclr#r#r$�_count_topic_mclszhelp._count_topic_mclcsi|_g|_�x�|jjD�]�}||jjj|j�k	r4q|jr<q|jdk	�r�|j|j�\}}|j	dd�d}|dkr�|j}||jkr�|j|dj
|�n|d|gg|j|<t|j|dt|j�f�}||j|d<n�|j|�}|j}||jk�r�||j|dk�r"|j|d|dj
|�n&|d|gg|j|d|<|j
||�t|j|d|dt|j�f�}||j|d|d<n8|dj	dd�dd||d|ggig|j|<|j
||�q|jj
|�qWt|j�dd�|jD�}tdd�|D��|_tt|�j�dS)	Nr�r*rricSsg|]
}|j�qSr#)r")r7�cr#r#r$rg:sz%help._on_finalize.<locals>.<listcomp>css|]}t|�VqdS)N)r6)r7�sr#r#r$r9;sz$help._on_finalize.<locals>.<genexpr>)r�Z	_builtinsr�Command�
get_pluginr"�NO_CLIr�r�r�r�r?r6r�r<�_mtl�superr��_on_finalize)r4r�r�r�r�r�r��topics)�	__class__r#r$r�sH

zhelp._on_finalizecsP|dkrtj}|j|�}t|��|dkr:|jjj|�dS�dkrP|j|�dS�|jkrh|j	�|�n܈|j
kr�|j
�}|jr�t�d��|j
jj|�j|�n�t�fdd�|jj�D��r�|j	�|�nx�dk�r:d}xh|j
D]R}||j
j|j�k	r�q�|j�rq�t|t|j��}|jdjt|j�j|�|j��q�Wn
t�d��|j�dS)Nr�)r�c3s*|]"}t|d�tkr�|dkVqdS)riN)r;r)r7�t)r"r#r$r9Sszhelp.run.<locals>.<genexpr>�commandsrz{0}  {1})r+r,r�r(r�parserZ
print_help�print_topicsr��print_commandsr�r�r
�Backend�cli�build_parser�any�valuesr�r"r?r6r�rmr%�ljust�summaryr�)r4rdr��options�writer�cmdr�Z
cmd_pluginr#)r"r$�run?s>







zhelp.runcCsT|j|�}x<t|jj��D]*\}}|jdjt|�j|j�|d��qW|j	�dS)Nz{0}  {1}r)
r�rur��itemsr�rmr%r�r�r�)r4r�r�r�r�r#r#r$r�ds

zhelp.print_topicscCs�|j|�}||jkr�t|j|d�tkr�xV|j|dD]D}|j|d|d}|j|d}|jdjt|�j|�|��q:W�n4||jkr�|j|d}|j|d}nXg}xR|jj�D]D}t	|dt�s�q�||dkr�q�|d|d}|d|d}Pq�W|j
|�\}}	||jk�r8t|�dk�r8t
|d��|j|�|�r�|j�|jtd��x.|D]&}
|jdjt|
j�j|�|
j���qdW|j�|jtd��|jtd��|j�|j�dS)	Nrirr*z
  {0}  {1})r�zTopic commands:zTo get command help, use:z  ipa <command> --help)r�r�r;rr�rmr%r�r�rSr�r�r6r
rr"r�r�)r4r�r�r�Zsubtopicr�r�r�rFZ_topicr�r#r#r$r�lsF
 



zhelp.print_commands)NN)r�r�r�r�r�rr�
takes_argsrZ
takes_optionsr=�
has_outputr�r�r�r�r�r�r��
__classcell__r#r#)r�r$r��s 8
%r�c@s4eZdZdZeded�d�fZe�ZdZ	dd�Z
dS)�
show_mappingszA
    Show mapping of LDAP attributes to command-line option.
    �command_namezCommand name)r�NcKs�t|�}||jkrt|d��|j|j}d
dg}t|dd�}xB|�D]8}|jr^d|jkr^qH|j|j|jf�t	|t|j��}qHWx.|D]&}t
t|d�j|�d|d	�q�WdS)N)r"�	Parameter�LDAP attribute�	=========�==============rZwebuiz : r*)r�r�)r�r�)
r(r�rr�r6�excluder�r'Z
param_specr?rVr%r�)r4r�r��params�outr��param�itemr#r#r$r��s


zshow_mappings.run)r�r�r�r�rrr�r=r�r�r�r#r#r#r$r��sr�cseZdZ�fdd�Z�ZS)�IPACompletercs$t|tjtjf�r|St�j||�S)N)rSr
�APINameSpaceZAPIr��_callable_postfix)r4r�Zword)r�r#r$r��szIPACompleter._callable_postfix)r�r�r�r�r�r#r#)r�r$r��sr�c@s0eZdZdZdZe�ZdZdd�Zd	dd�Z	dS)
�consolez�Start the IPA interactive Python console, or run a script.

    An IPA API object is initialized and made available
    in the `api` global variable.
    �	filename?Ncsjtjd�tjt|�j�tjjtj	j
d��ytj��Wntk
rNYnX�fdd�}t
j|�dS)Nz
tab: completezconsole.historycs`tjj��}tjj|�s"tj|�tjd�ytj��Wn tk
rZt	j
d��YnXdS)N�2zUnable to store history %s)�os�path�dirname�isdir�makedirsr�Zset_history_length�write_history_file�OSError�logger�	exception)Z	directory)�historyr#r$�save_history�s

z3console._setup_tab_completion.<locals>.save_history)r��parse_and_bindZ
set_completerr�Zcompleterrrkrr�Zdot_ipa�read_history_filer	�atexit�register)r4�localr
r#)rr$�_setup_tab_completion�s

zconsole._setup_tab_completioncKs�t|jtjtd�}|r�y t|��}|j�}WdQRXWn8tk
rn}ztjd|j	|j
f�WYdd}~XnXy$t||dtj
d�}t|t�|�Wq�tk
r�tj�tjd�Yq�Xn&tdk	r�|j|�tjdjd�|d
�dS)N)rZpp�__builtins__z%s: %s�exec)r{r*r��'(Custom IPA interactive Python console)�    api: IPA API object�    pp: pretty printer)r)rrr)rr�pprint�builtins�open�readr3r+�exit�filename�strerror�compilerZ
compiler_flagr�globalsr��	traceback�	print_excr�r�codeZinteractrk)r4rr�r�f�sourcer�Zcompiledr#r#r$r��s8
(

zconsole.run)r)N)
r�r�r�r�r�r=r�r�rr�r#r#r#r$r�src@s4eZdZdZdZdZddd�Zdd�Zd
d	d
�ZdS)�show_apiz%Show attributes on dynamic API object�namespaces*Nc	Cs�|dkrt|j�}n&x |D]}||jkrt|d��qW|}|j|�}tdd�|D��}|jjjd�d}xV|D]N}|ddkr�|r�td�|r�d}td	d
|d|dj	|�|df�qnWt
|�dkr�d
}ndt
|�}|jjj|�dS)N)r"css|]}t|d�VqdS)r*N)r6)r7ror#r#r$r9szshow_api.run.<locals>.<genexpr>r�TrrjFz%s%s %rrhr*riz1 attribute shown.z%d attributes show.)r=rr�_show_api__traverser?r�r)r�rVr�r6r�)	r4Z
namespaces�namesr"�linesZmlr}r`r�r#r#r$r�s.




zshow_api.runcCs2g}x(|D] }|j|}|jd|||�q
W|S)Nz%s)r�_show_api__traverse_namespace)r4r*r+r"�	namespacer#r#r$Z
__traverse*s


zshow_api.__traverserc	Cs�|j|||f�xv|D]n}||}|j|d||f�t|d�sBqx@|D]8}||}t|tj�rHt|�dkrH|j||||d�qHWqWdS)Nr*�__iter__rri)r��hasattrrSr
r�r6r,)	r4r"r-r+Ztab�member_name�memberrLrlr#r#r$Z__traverse_namespace1s


zshow_api.__traverse_namespace)r()N)r)	r�r�r�r�r�r�r�r)r,r#r#r#r$r's
r'c@s$eZdZdd�Zdd�Zdd�ZdS)�	CollectorcCstj|di�dS)N�_Collector__options)�object�__setattr__)r4r#r#r$r�FszCollector.__init__cCsP||jkr4|j|}t|�tkr,||f}n||f}||j|<tj|||�dS)N)r3r;r=r4r5)r4r"rJrFr#r#r$r5Is


zCollector.__setattr__cCs
t|j�S)N)rr3)r4r#r#r$�
__todict__UszCollector.__todict__N)r�r�r�r�r5r6r#r#r#r$r2Esr2c@seZdZdd�ZdS)�CLIOptionParserFormattercs�g}�j�jd}t|�|kr8d�jd|f}�j}nd�jd||f}d}|j|�|r�tj|�j�}|jd|d|df�|j�fdd�|dd�D��n|d
d	kr�|jd	�dj|�S)Nriz%*s%s
rjz	%*s%-*s  rcsg|]}d�jd|f�qS)z%*s%s
rj)�
help_position)r7r`)r4r#r$rgfsz<CLIOptionParserFormatter.format_argument.<locals>.<listcomp>r*r�r�)	r8Zcurrent_indentr6r�r]r^Z
help_width�extendrk)r4r"�help_string�resultZ	opt_widthZindent_firstZ
help_linesr#)r4r$�format_argumentYs 

z(CLIOptionParserFormatter.format_argumentN)r�r�r�r<r#r#r#r$r7Xsr7c@s*eZdZdZdd�Zd	dd�Zdd�ZdS)
�CLIOptionParserz�
    This OptionParser subclass adds an ability to print positional
    arguments in CLI help. Custom formatter is used to format the argument
    list in the same way as OptionParser formats options.
    cOs0g|_d|krt�|d<tjj|f|�|�dS)N�	formatter)�
_argumentsr7�optparse�OptionParserr�)r4�args�kwargsr#r#r$r�rs
zCLIOptionParser.__init__NcCs�tjj||�}t|t�r�ttd��}|j|�g}|j�x$|j	D]\}}|j
|j||��q@W|j�t
|�dkr~|j
d�ng}dj|�|}|S)zN
        Prepend argument help to standard OptionParser's option help
        zPositional argumentsr*r�rj)r@rA�format_option_helprSr7rTrZformat_headingrbr?r�r<�dedentr6rk)r4r>Zoption_helpZheadingZ	argumentsr"r:r#r#r$rDxs
z"CLIOptionParser.format_option_helpcCs|jj||f�dS)N)r?r�)r4r"r:r#r#r$�add_argument�szCLIOptionParser.add_argument)N)r�r�r�r�r�rDrFr#r#r#r$r=ls
r=c@sbeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	ddd�Z
dd�Zdd�Zdd�Z
dS)r�zC
    Backend plugin for executing from command line interface.
    cCs�t|�dkr@|jjtjd�ttjd�tdtjd�tjd�|d|dd�}}t|�}||jkr�t|�dkr�y|jjt|�tjd�Wnt	k
r�YnX||jks�|j|j
r�t|d��|j|}|S)	z�Given CLI arguments, return the Command to use

        On incorrect invocation, prints out a help message and returns None
        r)r�)r�zError: Command not specifiedrir*N)r")r6r�r�r+�stderrrVrr(rTr
r�r)r4�argvrdr"r�r#r#r$�get_command�s 


zcli.get_commandcCs`|jjrP|j||�y|jd�}Wntk
r6YnXx|D]}|||�q>W|j||�|S)z'Get the keyword arguments for a CommandZinteractive_prompt)r�r��prompt_interactivelyZ
get_callbacksr��
load_files)r4r��kwZ	callbacks�callbackr#r#r$�process_keyword_arguments�s
zcli.process_keyword_argumentsc

Cs�|j|�}|dkrdS|j}|j||dd��}t|tj�sD|j�z�|j||�}|j|f|�}t	|j
�r�x(|j�D]}|jrt|j
|krt||j
=qtW|jf|�\}}|j
|jjj|f|�|�}	|	r�|	SdSWd|j�XdS)Nr*r)rIZ	full_name�parserSr�LocalZcreate_contextrNZexecute�callableZoutput_for_clir��passwordr"Zparams_2_args_optionsrr�r)Zdestroy_context)
r4rHr�r"rLr;r�rBr��rvr#r#r$r��s*


zcli.runcCs@|j|�}|j|t��\}}|j�}|j||�}t|j||��S)N)r��
parse_argsr2r6Zargs_options_2_paramsr�
parse_iter)r4r�rHr�Z	collectorrBr�rLr#r#r$rO�s

z	cli.parseccs.x(|j�D]\}}||jjj|�fVq
WdS)z5
        Decode param values if appropriate.
        N)r�r�r)rE)r4r�rLrdrJr#r#r$rU�szcli.parse_itercs�tdj|j|��t|j�t�d��i���fdd�}�xH|j�D�]:}t|jt|j�d�}d|j	krfq@|j
r~|jjr~d|d<n2t
|t�r�|jd	kr�d
|d<q�d|d<n
|j|d<t|j�}d|g}|jr�|jd
|j�tj||�}|jdkr��j|�n||j�j|�|jr@t|�}td�||d<t
|t��r>d|d<|ttd���}	x,|jD]"}
d|
}|	jtj|f|���qVWq@WxP|j�D]D}|j|dd�}d|j	k�s�|dk�r��q�t|j�}
�j||
��q�W�S)Nrh)Zusage�descriptionr>cs4�j|�}|dkr0tj�|�}�j|�|�|<|S)z0Get or create an option group for the given nameN)r�r@ZOptionGroupZadd_option_group)Z
group_name�option_group)�
option_groupsr�r#r$�_get_option_group�s

z+cli.build_parser.<locals>._get_option_group)�destr�r��
store_true�actionTZstore_false�metavarz--%sz-%szSame as --%sr�ZVALzDeprecated optionsF)�format_name)r=rk�
usage_iterrTr��IPAHelpFormatterr�rr"r{rRr�r�rSrr�Zcli_metavarr%r'Zcli_short_namer�r@Zmake_optionrWZ
add_optionZdeprecated_cli_aliasesrrrB�_cli__get_arg_namerF)r4r�rYZoptionrLr'Zoption_names�optZnew_kw�group�aliasr"�argr�r#)rXr�r$r��sV
	










zcli.build_parserTcCsD|jr
dSt|j�j�}|s |S|jr.d|}|jr8|Sd|SdS)Nz%s...z[%s])rRr%r'�upperZ
multivalue�required)r4rer^r"r#r#r$Z__get_arg_name"szcli.__get_arg_nameccsFdt|j�Vx*|j�D]}|j|�}|dkr2q|VqWdVdS)Nz!Usage: %%prog [global-options] %sz	[options])r%r"rBra)r4r�rer"r#r#r$r_0s

zcli.usage_itercCsHd}x&|j�D]}|jr|j|krd}PqW�x|j�D�]}|jrN|j|ksb|jrX|sb|jj�r|jr~|j|jf|�||j<|j|kr�||jdk	r�|jr8||j=q8|jr�|j	j
j|j|j
�||j<nD|j|jf|�}|jp�|j}|j||||d�}|dk	�r@|||j<q8|jr8|j|jd�dkr8|j	j
j|j|j
�||j<q8WdS)a]
        Interactively prompt for missing or invalid values.

        By default this method will only prompt for *required* Param that
        have a missing or invalid value.  However, if
        ``self.env.prompt_all`` is ``True``, this method will prompt for any
        params that have a missing values, even if the param is optional.
        TFN)r�r�rL)r�Z	alwaysaskr"rgr�Z
prompt_allZautofillZget_default_ofrRr�r)r�r�r�Zprompt_paramr�)r4r�rLZhonor_alwaysaskr�r�r�rJr#r#r$rJ9s8

zcli.prompt_interactivelycCs��x�|j�D�]|}t|ttf�rd}|j|kr�t||j�ttfkrT||jd}n
||j}y$t||j	��}|j
�}WdQRXWnBtk
r�}z&tt
|j�d||jdfd��WYdd}~XnXnr|j�r:y,tjr�|jtkr�tjjj
�}n
tjj
�}Wn<tk
�r8}ztt
|j�|jdd��WYdd}~XnX|�rn|jtk�rX|||j<n|jjj|�||j<q|jrtt
|j�td�d��qWdS)a�
        Load files from File parameters.

        This has to be done after all required parameters have been read
        (i.e. after prompt_interactively has or would have been called)
        AND before they are passed to the command. This is because:
        1) we need to be sure no more files are going to be added
        2) we load files from the machine where the command was executed
        3) the webUI will use a different way of loading files
        Nrz%s: %s:r*)r"r�zNo file to read)r�rSrrr"r;r=r<rZ	open_moderr3rr%r'rBZstdin_if_missingr��PY3rGr+rIr�r�r)rErgr)r4r�rL�p�rawZfnamer%r�r#r#r$rKgs:

*(zcli.load_filesN)T)r�r�r�r�rIrNr�rOrUr�rar_rJrKr#r#r#r$r��s	@
	.r�c@seZdZdZdd�ZdS)r`a1Formatter suitable for printing IPA command help

    The default help formatter reflows text to fit the terminal, but it
    ignores line/paragraph breaks.
    IPA's descriptions already have correct line breaks. This formatter
    doesn't touch them (save for removing initial/trailing whitespace).
    cCs|r|j�SdSdS)Nrj)r_)r4rVr#r#r$�format_description�sz#IPAHelpFormatter.format_descriptionN)r�r�r�r�rkr#r#r#r$r`�sr`cCsRd}y�|jdd�\}}yt|jd�Wn,tk
rR}ztj|�WYdd}~XnXxtD]}|j|�qZW|j�d|jkr�d|kr�t	��tj|j
jj|��Wn�t
k
r�td�tjd�Yndtk
r�}z
|}WYdd}~Xn@tk
�r*}z"tjd|jjt|��t�}WYdd}~XnX|dk	�rNtj|j�tj|j�dS)	Nr�)�context)r�Z
config_loadedr�rjzoperation abortedz%s: %s)Zbootstrap_with_global_optionsrr�rr+r�cli_pluginsZ
add_plugin�finalizerr�r�r�r�rVr
�inforr�rr�r�r&rr�rZrval)rr�Z_optionsrHr��klassr#r#r$r��s0

r�)^r�Z
__future__rrrr�Zloggingr]r+r�r$r@rrr.r/r0rQr"r��rlcompleterr�r�Z	six.movesrZipalib.utilrrrrrhr&rTr��reloadZsetdefaultencodingZipalibrr	r
Z
ipalib.errorsrrr
rrrrrrZipalib.constantsrrZipalib.parametersrrrrrrZipalib.textrrZipapython.dnsutilrZipapython.admintoolrrRZ	getLoggerr�r
r%r(r�r)rPr�r�r�Z	Completerr�rr'Zcli_application_commandsr2ZIndentedHelpFormatterr7rAr=ZExecutionerr�r`rmr�r#r#r#r$�<module>s�
, 
	`^G8$