U
    ]                     @   sx   d Z ddlZddlZddlm  mZ ddlm	Z	 ddlm
Z
 ddlmZ eeZdd Zdd	 Zd
d Zdd ZdS )z*Contains UI methods for Apache operations.    N)errors)
interfaces)osc                 C   sj   | s
t  S dd | D }|r0|d dd |d< tjtjjd|dd\}}|tjkrft	|| }|S g S )zSelect multiple Vhosts to install the certificate for

    :param vhosts: Available Apache VirtualHosts
    :type vhosts: :class:`list` of type `~obj.Vhost`

    :returns: List of VirtualHosts
    :rtype: :class:`list`of type `~obj.Vhost`
    c                 S   s   g | ]}|  d  qS )
)display_repr).0vhost r	   </usr/lib/python3/dist-packages/certbot_apache/display_ops.py
<listcomp>   s     z)select_vhost_multiple.<locals>.<listcomp>NzJWhich VirtualHosts would you like to install the wildcard certificate for?T)Ztagsforce_interactive)
listzope	component
getUtilityr   IDisplayZ	checklistdisplay_utilOK_reversemap_vhosts)vhostsZ	tags_listcodenamesreturn_vhostsr	   r	   r
   select_vhost_multiple   s    	 


r   c                 C   s<   t  }| D ],}|D ]"}|  | kr|| qq
|S )zmHelper function for select_vhost_multiple for mapping string
    representations back to actual vhost objects)r   r   stripappend)r   r   r   Z	selectionr   r	   r	   r
   r   %   s    r   c                 C   s,   |sdS t | |\}}|tjkr(|| S dS )zSelect an appropriate Apache Vhost.

    :param vhosts: Available Apache VirtualHosts
    :type vhosts: :class:`list` of type `~obj.Vhost`

    :returns: VirtualHost or `None`
    :rtype: `~obj.Vhost` or `None`

    N)_vhost_menur   r   )domainr   r   tagr	   r	   r
   select_vhost0   s    

r    c                 C   sB  t jtd td d }|dk r6td d}d}nt|d }|| }g }|D ]}t| dkrxtt| }n| sd}nd	}|	d
j
tj|jd| |d| |jrdnd|jrdnd||d qRz,tjtjjd
| tj|dd\}}	W n6 tjk
r8   d
| }
t|
 t|
Y nX ||	fS )zSelect an appropriate Apache Vhost.

    :param vhosts: Available Apache Virtual Hosts
    :type vhosts: :class:`list` of type `~obj.Vhost`

    :returns: Display tuple - ('code', tag')
    :rtype: `tuple`

    ZHTTPSZEnabled	      zFDisplay size is too small for certbot_apache.display_ops._vhost_menu()   g333333? zMultiple Namesz@{fn:{fn_size}s} | {name:{name_size}s} | {https:5s} | {active:7s}N)fnnameZhttpsZactiveZfn_size	name_sizezrWe were unable to find a vhost with a ServerName or Address of {0}.{1}Which virtual host would you like to choose?T)r   zEncountered vhost ambiguity when trying to find a vhost for {0} but was unable to ask for user guidance in non-interactive mode. Certbot may need vhosts to be explicitly labelled with ServerName or ServerAlias directives.)r   ZWIDTHlenloggerdebugintZ	get_namesnextiterr   formatr   pathbasenameZfilepZsslenabledr   r   r   r   r   Zmenulinesepr   ZMissingCommandlineFlagZwarning)r   r   Z
free_charsZfilename_sizeZdisp_name_sizechoicesr   Z	disp_namer   r   msgr	   r	   r
   r   A   sR    

  
r   )__doc__ZloggingZzope.componentr   Zcertbot.display.utilZdisplayutilr   Zcertbotr   r   Zcertbot.compatr   Z	getLogger__name__r)   r   r   r    r   r	   r	   r	   r
   <module>   s   
