
    b&                         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e          Z G d d	e j                  Z G d
 de          Z G d de          Z G d de          ZdS )    N)colour)SambaOption)get_samba_logger)LdbErrorc                       e Zd Zej        ZdS )OptionN)__name__
__module____qualname__optparseSUPPRESS_HELP     7/usr/lib/python3/dist-packages/samba/netcmd/__init__.pyr   r      s        *MMMr   r   c                       e Zd ZddZd ZdS )PlainHelpFormatter c                     | j         | j        z
  d| j        z  |                    d          }fd|D             }d                    |          dz   }|S )N 
c                 @    g | ]}t          j        |           S ))initial_indentsubsequent_indent)textwrapfill).0p
desc_widthindents     r   
<listcomp>z9PlainHelpFormatter.format_description.<locals>.<listcomp>)   sG     ! ! !
 	 M!$)/,24 4 4! ! !r   )widthcurrent_indentsplitjoin)selfdescription
paragraphswrapped_paragraphsresultr   r   s        @@r   format_descriptionz%PlainHelpFormatter.format_description%   s    Z$"55
t** &&t,,
! ! ! ! !
  ! ! ! -..5r   c                     |rd|z   dz   S dS )Nr   r   r   )r%   epilogs     r   format_epilogz PlainHelpFormatter.format_epilog2   s     	&=4''2r   N)r   )r	   r
   r   r*   r-   r   r   r   r   r   $   s7               r   r   c                       e Zd ZdZd Z ee          Zd Z ee          Z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ZddZd	 Zdd
Zd Zd Zd ZddZd ZdS )CommandzA samba-tool command.c                 f    | j                                         d                             d          S )Nr   r   )__doc__
splitlinesrstripr%   s    r   _get_short_descriptionzCommand._get_short_description<   s)    |&&((+224888r   c                     | j                             d          }|d         dz   t          j        d                    |dd                              z   S )Nr   r      )r1   r#   r   dedentr$   )r%   liness     r   _get_full_descriptionzCommand._get_full_descriptionA   sH    ""4((Qx$591E1E!F!FFFr   c                 \    | j         j        }|                    d          r
|dd          S |S )Ncmd_   )	__class__r	   
startswith)r%   names     r   	_get_namezCommand._get_nameG   s2    ~&??6"" 	8Or   NFc                 "    || _         || _        d S N)outferrf)r%   rD   rE   s      r   __init__zCommand.__init__\   s    				r   c                 ^    |                      |          \  }}|                                 d S rC   )_create_parserprint_usage)r%   progparser_s       r   usagezCommand.usage`   s1    ''--	r   c           	         t          |t                    r|j        \  }}}|j        }|j        }d}nt          j                    \  }}}|}d}d}t          |t                    r,|j        \  }}	| j	        
                    d|d|	d           nt          |t                    r | j	        
                    d|z             d}nt          |t                    r"| j	        
                    d|d|d           nt          |          t          u r$| j	        
                    d	|d|d           d}nS|| j	        
                    d|z             n3| j	        
                    dt          |          d|d|d           d}|st!          j                    dk    rt%          j        || j	                   d
S d
S )zdisplay a command errorFzuncaught exceptionTzERROR(ldb): z - r   zERROR(assert): %s
zERROR(runtime): zERROR(exception): Nz
ERROR: %s
zERROR(z):    )file)
isinstanceCommandErrorexception_infoinner_exceptionmessagesysexc_infor   argsrE   writeAssertionErrorRuntimeErrortype	Exceptionstrsambaget_debug_level	tracebackprint_tb)
r%   eetypeevalue
etracebackrT   rU   force_traceback	ldb_ecodeldb_emsgs
             r   show_command_errorzCommand.show_command_errord   s   a&& 		#*+*:'UFJ/OiG#OO*-,..'UFJO*G"Oox00 	#$3$8!YIOOOwwwIJJJJ88 	#IOO1G;<<<"OO66 		#IOOO777FFFKLLLL/""i//IOOOWWWfffMNNN"OO$IOOMW56666IOOOc%jjjj'''666RSSS"O 	;e355::z	:::::: ;:r   c                 ^   t          j        | j        | j        t	                      ||          }|                    | j                   i }t          | j        	                                          D ]8}| j        |         } ||          ||<   |
                    ||                    9||fS )N)rM   r&   	formatterrJ   r,   )r   OptionParsersynopsisfull_descriptionr   add_optionstakes_optionssortedtakes_optiongroupskeysadd_option_group)r%   rJ   r,   rK   optiongroupsr@   optiongroups          r   rH   zCommand._create_parser   s    &--(**f	& & &
 	4-...427799:: 	8 	8D1$7K!,V!4!4L##L$67777|##r   c                 @    | j                             |dz              d S )Nr   )rD   rY   )r%   texts     r   rU   zCommand.message   s     	t$$$$$r   c                 $   |                      |d                   \  }}|                    t          |                    \  }}|dd          }t          |j                  }|j        D ]}|j        D ]}|j        ||j        = |                    |           d}	d}
d}t          | j
                  D ]B\  }}|d         dk    r|d         dk    r|	dz  }	|d         dk    s|d         dk    rd}=|
dz  }
Ct          |          |	k     s|s)t          |          |
k    r|                                 dS t          |          | _        || _        || _        	  | j        |i |S # t"          $ r }|                     |           Y d }~dS d }~ww xY w)	Nr   r7   F?*+T)rH   
parse_argslistdict__dict__option_groupsoption_listdestupdate	enumerate
takes_argslenrI   raw_argvraw_args
raw_kwargsrunr]   rj   )r%   argvrK   rv   optsrX   kwargsoption_groupoptionmin_argsmax_argsundetermined_max_argsiargrc   s                  r   _runzCommand._run   s   #2247;;&&tDzz22
dABBxdm$$"0 	, 	,L&2 , ,;*v{+, 	l###  %00 	 	FAs2w#~~#b'S..A2w#~~RC(,%%AII  *? CIIPXDXDX   2T

 	48T,V,,, 	 	 	##A&&&22222	s   E% %
F/F

Fc                 *    t          | j                  )z=Run the command. This should be overridden by all subclasses.)NotImplementedErrorr   r4   s    r   r   zCommand.run   s    !$(+++r   r   c                 <    t          d|p| j        | j        ||d|S )zGet a logger object.)r@   streamverbosequietr   )r   r@   rE   )r%   r@   r   r   r   s        r   
get_loggerzCommand.get_logger   s;     "495    	r   c                 p   |                                 }|dk    rt          j                     dS |dk    rt          j                     dS |dk    rXt	          | j        d          r.| j                                        rt          j                     dS t          j                     dS t          d          )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.
        noyesautoisattyz;Unknown --color option: %s please choose from yes, no, autoN)lowerr   switch_colour_offswitch_colour_onhasattrrD   r   rR   )r%   	requesteds     r   apply_colour_choicezCommand.apply_colour_choice   s     OO%%	$&&&&&%#%%%%%&  	8,, +1A1A1C1C +')))))(*****   B C C Cr   rC   )NN)r   FF)r	   r
   r   r1   r5   propertyshort_descriptionr:   ro   rA   r@   rn   r   rq   rs   hiddenr   r   r   rV   stdoutstderrrF   rM   rj   rH   rU   r   r   r   r   r   r   r   r/   r/   9   sW       9 9 9 !!788G G G  x 566   8ID HJMFHHJJSZ       ; ; ;B$ $ $ $% % %& & &P, , ,   C C C C Cr   r/   c                   "    e Zd ZdZdZi ZddZdS )SuperCommandz&A samba-tool command with subcommands.z%prog <subcommand>Nc                    || j         v r | j         |         j        |d|g|R  S |dvrt          |d|d           g }|dk    rt          |          dk    r{| j                             |d                   }t          |t                    r" |j        |d|d         dg|dd          R  S |" |j        |d|d         dg|dd          R  S d}d	}t          | j                                                   }|	                                 t          d
 |D                       }|D ]'}| j         |         }	|	j        s|d| ||	j        fz  z  }(|d|z  z  }|                     ||          \  }
}t          |          }|r|                    d|           |
                    |          \  }}|
                                 dS )Nr   )--helphelpNz-hz-Vz	--versionz: no such subcommand: r   r   r   r7   r   z
Available subcommands:
c                 ,    g | ]}t          |          S r   )r   )r   cs     r   r    z%SuperCommand._run.<locals>.<listcomp>   s    222Q#a&&222r   z  %*s  - %s
zQFor more help on a specific subcommand, please type: %s <subcommand> (-h|--help)
)r,   r{   )subcommandsr   printr   getrQ   r   r   rt   sortmaxr   r   rH   insertr   
print_help)r%   myname
subcommandrX   subr,   subcmds
max_lengthcmd_namecmdrK   rv   	args_listr   s                 r   r   zSuperCommand._run   sb   )))44#J/4!66::.7157 7 7 7RRRFFFJJJGHHHD4yy1}}&**4733c<00 /#38vvvtAww$? /%)!""X/ / / /_#38vvvtAww$? /%)!""X/ / / / "J-t',,..//22'22233
 	B 	BH"8,C: B/K3+@-B B Bfiooo#226&2IIJJ	 	,Q
+++&&y11
drr   rC   )r	   r
   r   r1   rn   r   r   r   r   r   r   r      s8        00#HK' ' ' ' ' 'r   r   c                        e Zd ZdZddZd ZdS )rR   z1An exception class for samba-tool Command errors.Nc                 R    || _         || _        t          j                    | _        d S rC   )rU   rT   rV   rW   rS   )r%   rU   rT   s      r   rF   zCommandError.__init__  s%    .!lnnr   c                     d| j         z  S )NzCommandError(%s))rU   r4   s    r   __repr__zCommandError.__repr__  s    !DL00r   rC   )r	   r
   r   r1   rF   r   r   r   r   rR   rR     s=        ;;- - - -
1 1 1 1 1r   rR   )r   r_   r   samba.getoptr   samba.loggerr   ldbr   rV   ra   r   r   IndentedHelpFormatterr   objectr/   r   r]   rR   r   r   r   <module>r      sp  &         $ $ $ $ $ $ ) ) ) ) ) )       



     + + + + +[ + + +    7   *eC eC eC eC eCf eC eC eCP. . . . .7 . . .b	1 	1 	1 	1 	19 	1 	1 	1 	1 	1r   