a
    a&                     @   s   d dl 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l	Z	d dl
Z
d dlZG dd deZG dd	 d	e jZG d
d deZG dd deZG dd deZdS )    N)colour)SambaOption)get_samba_logger)LdbErrorc                   @   s   e Zd ZejZdS )OptionN)__name__
__module____qualname__optparseZSUPPRESS_HELP r   r   7/usr/lib/python3/dist-packages/samba/netcmd/__init__.pyr      s   r   c                   @   s   e Zd ZdddZdd ZdS )PlainHelpFormatter c                    sF   | j | j  d| j |d} fdd|D }d|d }|S )N 
c                    s   g | ]}t j| d qS ))Zinitial_indentZsubsequent_indent)textwrapZfill).0pZ
desc_widthindentr   r   
<listcomp>*   s   z9PlainHelpFormatter.format_description.<locals>.<listcomp>)widthZcurrent_indentsplitjoin)selfdescriptionZ
paragraphsZwrapped_paragraphsresultr   r   r   format_description&   s    

z%PlainHelpFormatter.format_descriptionc                 C   s   |rd| d S dS d S )Nr   r   r   )r   epilogr   r   r   format_epilog3   s    z PlainHelpFormatter.format_epilogN)r   )r   r   r	   r   r   r   r   r   r   r   %   s   
r   c                   @   s   e Zd ZdZdd ZeeZdd ZeeZdd Z	ee	Z
dZg Zg Zi Zd	ZdZdZdZejejfd
dZdddZdd ZdddZdd Zdd Zdd ZdddZdd ZdS ) CommandzA samba-tool command.c                 C   s   | j  d dS )Nr   r   )__doc__
splitlinesrstripr   r   r   r   _get_short_description=   s    zCommand._get_short_descriptionc                 C   s0   | j d}|d d td|dd   S )Nr   r      )r!   r   r   dedentr   )r   linesr   r   r   _get_full_descriptionB   s    zCommand._get_full_descriptionc                 C   s"   | j j}|dr|dd  S |S )NZcmd_   )	__class__r   
startswith)r   namer   r   r   	_get_nameH   s    
zCommand._get_nameNFc                 C   s   || _ || _d S N)outferrf)r   r0   r1   r   r   r   __init__]   s    zCommand.__init__c                 C   s   |  |\}}|  d S r/   )_create_parserprint_usage)r   progparser_r   r   r   usagea   s    zCommand.usagec           
      C   s2  t |tr(|j\}}}|j}|j}d}nt \}}}|}d}d}t |trl|j\}}	| j	
d||	f  nt |tr| j	
d|  d}n~t |tr| j	
d||f  n^t|tu r| j	
d||f  d}n8|du r| j	
d	|  n| j	
d
t|||f  d}|st dkr.tj|| j	d dS )zdisplay a command errorFzuncaught exceptionTzERROR(ldb): %s - %s
zERROR(assert): %s
zERROR(runtime): %s - %s
zERROR(exception): %s - %s
Nz
ERROR: %s
zERROR(%s): %s - %s
   )file)
isinstanceCommandErrorexception_infoinner_exceptionmessagesysexc_infor   argsr1   writeAssertionErrorRuntimeErrortype	ExceptionstrsambaZget_debug_level	tracebackprint_tb)
r   eetypeZevalueZ
etracebackr>   r?   Zforce_tracebackZ	ldb_ecodeZldb_emsgr   r   r   show_command_errore   s4    




zCommand.show_command_errorc                 C   sj   t j| j| jt ||d}|| j i }t| j	 D ](}| j| }||||< |
||  q8||fS )N)r8   r   Z	formatterr5   r   )r
   ZOptionParsersynopsisfull_descriptionr   Zadd_optionstakes_optionssortedtakes_optiongroupskeysZadd_option_group)r   r5   r   r6   optiongroupsr-   Zoptiongroupr   r   r   r3      s    
zCommand._create_parserc                 C   s   | j |d  d S )Nr   )r0   rC   )r   textr   r   r   r?      s    zCommand.messagec              
   G   s\  |  |d \}}|t|\}}|dd  }t|j}|jD ]"}|jD ]}|jd urJ||j= qJq@|| d}	d}
d}t	| j
D ]N\}}|d dkr|d dkr|	d7 }	|d dks|d dkrd}q|
d7 }
qt||	k s|st||
kr|  dS t|| _|| _|| _z| j|i |W S  tyV } z| | W Y d }~dS d }~0 0 d S )	Nr   r&   F?*+T)r3   
parse_argslistdict__dict__Zoption_groupsZoption_listdestupdate	enumerate
takes_argslenr4   raw_argvraw_args
raw_kwargsrunrG   rN   )r   argvr6   rU   optsrB   kwargsZoption_groupZoptionZmin_argsZmax_argsZundetermined_max_argsiargrL   r   r   r   _run   s:    







zCommand._runc                 C   s   t | jdS )z=Run the command. This should be overridden by all subclasses.N)NotImplementedErrorrg   r$   r   r   r   rg      s    zCommand.runr   c                 K   s    t f |p| j| j||d|S )zGet a logger object.)r-   streamverbosequiet)r   r-   r1   )r   r-   rp   rq   rj   r   r   r   
get_logger   s    zCommand.get_loggerc                 C   sj   |  }|dkrt  nL|dkr,t  n:|dkr^t| jdrT| j rTt  qft  ntddS )zHeuristics to work out whether the user wants colour output, from a
        --color=yes|no|auto option. This alters the ANSI 16 bit colour
        "constants" in the colour module to be either real colours or empty
        strings.
        ZnoZyesautoisattyz;Unknown --color option: %s please choose from yes, no, autoN)lowerr   Zswitch_colour_offZswitch_colour_onhasattrr0   rt   r<   )r   Z	requestedr   r   r   apply_colour_choice   s    



zCommand.apply_colour_choice)N)NN)r   FF)r   r   r	   r!   r%   propertyshort_descriptionr)   rP   r.   r-   rO   rb   rQ   rS   hiddenrd   re   rf   r@   stdoutstderrr2   r8   rN   r3   r?   rm   rg   rr   rw   r   r   r   r   r    :   s0   
!
(
r    c                   @   s"   e Zd ZdZdZi ZdddZdS )SuperCommandz&A samba-tool command with subcommands.z%prog <subcommand>Nc                 G   s  || j v r*| j | jd||f g|R  S |dvrFtd||f  g }|dkrt|dkr| j |d }t|tr|jd||d f dg|dd  R  S |d ur|jd||d f dg|dd  R  S d}d}t| j  }|	  t
d	d
 |D }|D ].}| j | }	|	js |d| ||	jf 7 }q |d| 7 }| j||d\}
}t|}|rh|d| |
|\}}|
  dS )Nz%s %s)--helphelpNz%s: no such subcommand: %s
r   r   r&   r~   z
Available subcommands:
c                 S   s   g | ]}t |qS r   )rc   )r   cr   r   r   r         z%SuperCommand._run.<locals>.<listcomp>z  %*s  - %s
zQFor more help on a specific subcommand, please type: %s <subcommand> (-h|--help)
)r   rW   )subcommandsrm   printrc   getr;   r}   r\   rT   sortmaxrz   ry   r3   insertr[   Z
print_help)r   ZmynameZ
subcommandrB   subr   ZsubcmdsZ
max_lengthZcmd_namecmdr6   rU   Z	args_listri   r   r   r   rm      sN    







zSuperCommand._run)N)r   r   r	   r!   rO   r   rm   r   r   r   r   r}      s   r}   c                   @   s"   e Zd ZdZdddZdd ZdS )r<   z1An exception class for samba-tool Command errors.Nc                 C   s   || _ || _t | _d S r/   )r?   r>   r@   rA   r=   )r   r?   r>   r   r   r   r2     s    zCommandError.__init__c                 C   s
   d| j  S )NzCommandError(%s))r?   r$   r   r   r   __repr__  s    zCommandError.__repr__)N)r   r   r	   r!   r2   r   r   r   r   r   r<     s   
r<   )r
   rI   r   Zsamba.getoptr   Zsamba.loggerr   Zldbr   r@   rJ   r   r   ZIndentedHelpFormatterr   objectr    r}   rG   r<   r   r   r   r   <module>   s    )1