Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/clcommands.cpython-37.pyc

B

�d{T�+�@s�dZddlmZdZddlZddlZddlmZddlm	Z	ddl
mZmZddl
mZGd	d
�d
e�ZGdd�de�ZGd
d�de�ZGdd�de	�ZGdd�de�Ze�ZdZed�dd��Zed�ddd��Zed�ddd��ZdS)z�Helper functions to support command line tools providing more than
one command.

e.g called as "tool command [options] args..." where <options> and <args> are
command'specific
�)�print_functionzrestructuredtext enN)�basename)�
Configuration)�init_log�
get_threshold)�
deprecatedc@seZdZdZdS)�BadCommandUsagez�Raised when an unknown command is used or when a command is not
    correctly used (bad options, too much / missing arguments...).

    Trigger display of command usage.
    N)�__name__�
__module__�__qualname__�__doc__�r
r
�J/opt/alt/python37/lib/python3.7/site-packages/logilab/common/clcommands.pyr&src@seZdZdZdS)�CommandErrorz|Raised when a command can't be processed and we want to display it and
    exit, without traceback nor usage displayed.
    N)r	r
rrr
r
r
rr-src@s`eZdZdZdddddejdfdd�Zddd�Zd	d
�Zddd�Z	dd
d�Z
dd�Zdd�ZdS)�CommandLineaFUsage:

    >>> LDI = cli.CommandLine('ldi', doc='Logilab debian installer',
                              version=version, rcfile=RCFILE)
    >>> LDI.register(MyCommandClass)
    >>> LDI.register(MyOtherCommandClass)
    >>> LDI.run(sys.argv[1:])

    Arguments:

    * `pgm`, the program name, default to `basename(sys.argv[0])`

    * `doc`, a short description of the command line tool

    * `copyright`, additional doc string that will be appended to the generated
      doc

    * `version`, version number of string of the tool. If specified, global
      --version option will be available.

    * `rcfile`, path to a configuration file. If specified, global --C/--rc-file
      option will be available?  self.rcfile = rcfile

    * `logger`, logger to propagate to commands, default to
      `logging.getLogger(self.pgm))`
    NTcCsJ|dkrttjd�}||_||_||_||_||_d|_||_	||_
dS)Nr)r�sys�argv�pgm�doc�	copyright�version�rcfile�logger�logthreshold�check_duplicated_command)�selfrrrrrrrr
r
r�__init__PszCommandLine.__init__FcCs0|jr"|s"|j|ks"td|j��|||j<|S)z,register the given :class:`Command` subclassza command %s is already defined)r�name�AssertionError)r�cls�forcer
r
r�register^s
zCommandLine.registerc
Cs�td|jdd�y|�d�}Wntk
r<|�d�YnX|dkrP|�d�|jdk	rv|dkrvt|j�t�d�|j	}|dk	r�|d	kr�y|�d�}|�d�}Wntk
r�|�d�YnXy|�
|�}Wn2tk
�rtd
|�t�|�d�YnXyt�|�||��Wn�t
k
�rv}z:tddd
�t|��rVtd|dd
�t�t�d�Wdd}~XYnJtk
�r�}z*td|�t�t|���t�d�Wdd}~XYnXdS)z�main command line access point:
        * init logging
        * handle global options (-h/--help, --version, -C/--rc-file)
        * check command
        * run command

        Terminate by :exc:`SystemExit`
        Tz%(levelname)s: %(message)s)�debugrZ	logformatr�)z-hz--helpNz	--version)z-Cz	--rc-filezERROR: no %s commandZInterrupted� )�endz: %s�zERROR:)rr�pop�
IndexError�usage_and_exitr�printr�exitr�get_command�KeyError�main_run�KeyboardInterrupt�strr�help)r�args�argr�command�exc�errr
r
r�runesL	





zCommandLine.runcCs4t�|j�}|g|_|dkr&t|j�}|�|�|S)N)�logging�Loggerr�handlersrr�setLevel)r�handlerrrr
r
r�
create_logger�s

zCommandLine.create_loggercCsD|dkr|j}|dkr8t�|j�}|_|�t|j��|||�S)N)rr8�	getLoggerrr;rr)r�cmdrr
r
rr,�szCommandLine.get_commandcCs�td|jdd�|jr"tddd�td�|jr>td|j�td|j�tdd	�|D��}d|}x:t|���D]*\}}|jsttd||d
|�|�	��qtW|jr�td|j�td|j�|j
r�td
|j�|jr�td|j�d
S)z\display usage for the main program (i.e. when no command supplied)
        and exit
        zusage:r$)r%z [--rc-file=<configuration file>]z)<command> [options] <command argument>...z
%szi
Type "%(pgm)s <command> --help" for more information about a specific
command. Available commands are :
cSsg|]}t|��qSr
)�len)�.0r?r
r
r�
<listcomp>�sz%CommandLine.usage.<locals>.<listcomp>Nz�
Use --rc-file=<configuration file> / -C <configuration file> before the command
to specify a configuration file. Default to %s.
z?%(pgm)s -h/--help
      display this usage information and exitzA%(pgm)s -v/--version
      display version configuration and exit�
)r*rrr�__dict__�max�sorted�items�hidden�short_descriptionrr)rZmax_len�paddingZcmdnamer?r
r
r�usage�s. zCommandLine.usagecCs|��t�|�dS)N)rKrr+)r�statusr
r
rr)�szCommandLine.usage_and_exit)F)N)N)
r	r
rrr8�ERRORrr!r7r=r,rKr)r
r
r
rr5s
0

rc@s^eZdZdZdZdZdZdZdZe	dd��Z
e	dd��Zd	d
�Zdd�Z
dd
d�Zdd�ZdS)�Commanda�Base class for command line commands.

    Class attributes:

    * `name`, the name of the command

    * `min_args`, minimum number of arguments, None if unspecified

    * `max_args`, maximum number of arguments, None if unspecified

    * `arguments`, string describing arguments, used in command usage

    * `hidden`, boolean flag telling if the command should be hidden, e.g. does
      not appear in help's commands list

    * `options`, options list, as allowed by :mod:configuration
    �FNcCs|j�dd�S)Nz    rO)r�replace)rr
r
r�description�szCommand.descriptioncCs|���d�dS)N�.r)rQ�split)rr
r
rrI�szCommand.short_descriptioncCs.d|j|j|��f}tj||d�||_dS)Nz%%prog %s %s

%s)rK)r�	argumentsrQrrr)rrrKr
r
rr�s
zCommand.__init__cCsD|jdk	r t|�|jkr td��|jdk	r@t|�|jkr@td��dS)z&check command's arguments are providedNzmissing argumentztoo many arguments)�min_argsr@r�max_args)rr2r
r
r�
check_args�szCommand.check_argsc
Csd|r|�|�|�|�}y|�|�|�|�Wn.tk
r^}z|j�|�dSd}~XYnXdS)aRun the command and return status 0 if everything went fine.

        If :exc:`CommandError` is raised by the underlying command, simply log
        the error and return status 2.

        Any other exceptions, including :exc:`BadCommandUsage` will be
        propagated.
        �Nr)Zload_file_configurationZload_command_line_configurationrWr7rr�error)rr2rr6r
r
rr.�s	


zCommand.main_runcCs
t��dS)z+run the command with its specific argumentsN)�NotImplementedError)rr2r
r
rr7szCommand.run)N)r	r
rrrTrrHrUrV�classmethodrQrIrrWr.r7r
r
r
rrN�s
rNc@s$eZdZdZdZdZdZdd�ZdS)�ListCommandsCommandz4list available commands, useful for bash completion.Zlistcommandsz	[command]TcCsr|r>|��}t|}xX|jD]\}}td�td|�qWn0tt���}x"|D]}t|}|jsPt|�qPWdS)z+run the command with its specific argumentsz--helpz--N)r'�	_COMMANDS�optionsr*rF�keysrH)rr2r4r?�optname�optdict�commandsr
r
rr7s
zListCommandsCommand.runN)r	r
rrrrTrHr7r
r
r
rr\s
r\z~Copyright (c) 2004-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
http://www.logilab.fr/ -- mailto:contact@logilab.frzuse cls.register(cli)cCsx|D]}t�|�qWdS)zregister existing commandsN)r]r!)rbZ
command_klassr
r
r�register_commands2s
rczuse args.pop(0)cCs |t_|t_|t_t�|�dS)z�command line tool: run command specified by argument list (without the
    program name). Raise SystemExit with status 0 if everything went fine.

    >>> main_run(sys.argv[1:])
    N)r]rrrr7)r2rrrr
r
rr.8sr.�Missing argumentcCsLy|�d�}Wntk
r*t|��YnX|dk	rHt|�|krHtd��|S)z7helper function to get and check command line argumentsrNztoo many arguments)r'r(rr@)Z	args_listZexpected_size_after�msg�valuer
r
r�pop_argDsrg)NNN)Nrd)r�
__future__r�
__docformat__rr8�os.pathrZlogilab.common.configurationrZlogilab.common.logging_extrrZlogilab.common.deprecationr�	Exceptionrr�dictrrNr\r]ZDEFAULT_COPYRIGHTrcr.rgr
r
r
r�<module>s*I