ó
ø¢TQc           @   s  d  Z  d d l Z d d l Z d d l m Z m Z 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 e f d	 „  ƒ  YZ d
 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s„   
Base classes for writing management commands (named commands which can
be executed through ``django-admin.py`` or ``manage.py``).

iÿÿÿÿN(   t   make_optiont   OptionParser(   t   ImproperlyConfigured(   t   color_style(   t	   force_str(   t   StringIOt   CommandErrorc           B   s   e  Z d  Z RS(   sÛ  
    Exception class indicating a problem while executing a management
    command.

    If this exception is raised during the execution of a management
    command, it will be caught and turned into a nicely-printed error
    message to the appropriate output stream (i.e., stderr); as a
    result, raising this exception (with a sensible description of the
    error) is the preferred way to indicate that something has gone
    wrong in the execution of a command.

    (   t   __name__t
   __module__t   __doc__(    (    (    s4   ../Django//lib/python/django/core/management/base.pyR      s   c         C   sB   |  j  r |  j  t j d <n  |  j r> t j j d |  j ƒ n  d S(   s¡   
    Include any default options that all commands should accept here
    so that ManagementUtility can handle them before searching for
    user commands.

    t   DJANGO_SETTINGS_MODULEi    N(   t   settingst   ost   environt
   pythonpatht   syst   patht   insert(   t   options(    (    s4   ../Django//lib/python/django/core/management/base.pyt   handle_default_options#   s    		t   OutputWrapperc           B   s5   e  Z d  Z d d d „ Z d „  Z d d d „ Z RS(   s&   
    Wrapper around stdout/stderr
    s   
c         C   sF   | |  _  d  |  _ t | d ƒ r9 | j ƒ  r9 | |  _ n  | |  _ d  S(   Nt   isatty(   t   _outt   Nonet
   style_funct   hasattrR   t   ending(   t   selft   outR   R   (    (    s4   ../Django//lib/python/django/core/management/base.pyt   __init__4   s
    		c         C   s   t  |  j | ƒ S(   N(   t   getattrR   (   R   t   name(    (    s4   ../Django//lib/python/django/core/management/base.pyt   __getattr__;   s    c         C   s–   | d  k r |  j p | } | r> | j | ƒ r> | | 7} n  g  | |  j d „  f D] } | d  k	 rT | ^ qT d } |  j j t | | ƒ ƒ ƒ d  S(   Nc         S   s   |  S(   N(    (   t   x(    (    s4   ../Django//lib/python/django/core/management/base.pyt   <lambda>B   s    i    (   R   R   t   endswithR   R   t   writeR   (   R   t   msgR   R   t   f(    (    s4   ../Django//lib/python/django/core/management/base.pyR$   >   s    N(   R   R   R	   R   R   R    R$   (    (    (    s4   ../Django//lib/python/django/core/management/base.pyR   0   s   	t   BaseCommandc           B   sø   e  Z d  Z e d d d d d d d d d	 d
 d d d d d g d d ƒe d d d ƒe d d d ƒe d d d d d ƒf Z d Z d Z e Z e Z	 e
 Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d" e
 d  „ Z d! „  Z RS(#   s‹  
    The base class from which all management commands ultimately
    derive.

    Use this class if you want access to all of the mechanisms which
    parse the command-line arguments and work out what code to call in
    response; if you don't need to change any of that behavior,
    consider using one of the subclasses defined in this file.

    If you are interested in overriding/customizing various aspects of
    the command-parsing and -execution behavior, the normal flow works
    as follows:

    1. ``django-admin.py`` or ``manage.py`` loads the command class
       and calls its ``run_from_argv()`` method.

    2. The ``run_from_argv()`` method calls ``create_parser()`` to get
       an ``OptionParser`` for the arguments, parses them, performs
       any environment changes requested by options like
       ``pythonpath``, and then calls the ``execute()`` method,
       passing the parsed arguments.

    3. The ``execute()`` method attempts to carry out the command by
       calling the ``handle()`` method with the parsed arguments; any
       output produced by ``handle()`` will be printed to standard
       output and, if the command is intended to produce a block of
       SQL statements, will be wrapped in ``BEGIN`` and ``COMMIT``.

    4. If ``handle()`` or ``execute()`` raised any exception (e.g.
       ``CommandError``), ``run_from_argv()`` will  instead print an error
       message to ``stderr``.

    Thus, the ``handle()`` method is typically the starting point for
    subclasses; many built-in commands and command types either place
    all of their logic in ``handle()``, or perform some additional
    parsing work in ``handle()`` and then delegate from it to more
    specialized methods as needed.

    Several attributes affect behavior at various steps along the way:

    ``args``
        A string listing the arguments accepted by the command,
        suitable for use in help messages; e.g., a command which takes
        a list of application names might set this to '<appname
        appname ...>'.

    ``can_import_settings``
        A boolean indicating whether the command needs to be able to
        import Django settings; if ``True``, ``execute()`` will verify
        that this is possible before proceeding. Default value is
        ``True``.

    ``help``
        A short description of the command, which will be printed in
        help messages.

    ``option_list``
        This is the list of ``optparse`` options which will be fed
        into the command's ``OptionParser`` for parsing arguments.

    ``output_transaction``
        A boolean indicating whether the command outputs SQL
        statements; if ``True``, the output will automatically be
        wrapped with ``BEGIN;`` and ``COMMIT;``. Default value is
        ``False``.

    ``requires_model_validation``
        A boolean; if ``True``, validation of installed models will be
        performed prior to executing the command. Default value is
        ``True``. To validate an individual application's models
        rather than all applications' models, call
        ``self.validate(app)`` from ``handle()``, where ``app`` is the
        application's Python module.

    s   -vs   --verbosityt   actiont   storet   destt	   verbosityt   defaultt   1t   typet   choicet   choicest   0t   2t   3t   helps[   Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose outputs
   --settingss›   The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.s   --pythonpathsM   A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".s   --tracebackt
   store_trues   Print traceback on exceptiont    c         C   s   t  ƒ  |  _ d  S(   N(   R   t   style(   R   (    (    s4   ../Django//lib/python/django/core/management/base.pyR   §   s    c         C   s
   t  j ƒ  S(   s«   
        Return the Django version, which should be correct for all
        built-in Django commands. User-supplied commands should
        override this method.

        (   t   djangot   get_version(   R   (    (    s4   ../Django//lib/python/django/core/management/base.pyR9   ª   s    c         C   s5   d | |  j  f } |  j r- d | |  j f S| Sd S(   s~   
        Return a brief description of how to use this command, by
        default from the attribute ``self.help``.

        s   %%prog %s [options] %ss   %s

%sN(   t   argsR4   (   R   t
   subcommandt   usage(    (    s4   ../Django//lib/python/django/core/management/base.pyR<   ³   s    	c      	   C   s1   t  d | d |  j | ƒ d |  j ƒ  d |  j ƒ S(   s|   
        Create and return the ``OptionParser`` which will be used to
        parse the arguments to this command.

        t   progR<   t   versiont   option_list(   R   R<   R9   R?   (   R   t	   prog_nameR;   (    (    s4   ../Django//lib/python/django/core/management/base.pyt   create_parser¿   s    c         C   s    |  j  | | ƒ } | j ƒ  d S(   sb   
        Print the help message for this command, derived from
        ``self.usage()``.

        N(   RA   t
   print_help(   R   R@   R;   t   parser(    (    s4   ../Django//lib/python/django/core/management/base.pyRB   Ê   s    c         C   sÛ   |  j  | d | d ƒ } | j | d ƒ \ } } t | ƒ y |  j | | j Ž  Wn€ t k
 rÖ } t |  d t t j	 |  j
 j ƒ ƒ } | j r© | j t j ƒ  ƒ n | j d | j j | f ƒ t j d ƒ n Xd S(   sê   
        Set up any environment changes requested (e.g., Python path
        and Django settings), then run this command. If the
        command raises a ``CommandError``, intercept it and print it sensibly
        to stderr.
        i    i   i   t   stderrs   %s: %sN(   RA   t
   parse_argsR   t   executet   __dict__t	   ExceptionR   R   R   RD   R7   t   ERRORt	   tracebackR$   t
   format_exct	   __class__R   t   exit(   R   t   argvRC   R   R:   t   eRD   (    (    s4   ../Django//lib/python/django/core/management/base.pyt   run_from_argvÓ   s    
$	c   	      O   s”  d } t | j d t j ƒ ƒ |  _ t | j d t j ƒ |  j j ƒ |  _ |  j r€ d d l	 m
 } | j ƒ  } | j d ƒ n  zð |  j r© | j d ƒ r© |  j ƒ  n  |  j | | Ž  } | ro|  j r0d d l m } m } | | j d | ƒ } | j j ƒ  r0|  j j |  j j | j j ƒ  ƒ ƒ q0n  |  j j | ƒ |  j ro|  j j d	 |  j j d
 ƒ ƒ qon  Wd | d k	 r| j | ƒ n  Xd S(   sÂ   
        Try to execute this command, performing model validation if
        needed (as controlled by the attribute
        ``self.requires_model_validation``, except if force-skipped).
        t   stdoutRD   iÿÿÿÿ(   t   translations   en-ust   skip_validation(   t   connectionst   DEFAULT_DB_ALIASt   databases   
s   COMMIT;N(   R   R   t   getR   RQ   RD   R7   RI   t   can_import_settingst   django.utilsRR   t   get_languaget   activatet   requires_model_validationt   validatet   handlet   output_transactiont	   django.dbRT   RU   t   opst   start_transaction_sqlR$   t   SQL_KEYWORD(	   R   R:   R   t
   saved_langRR   t   outputRT   RU   t
   connection(    (    s4   ../Django//lib/python/django/core/management/base.pyRF   è   s,    '		+	*c         C   s“   d d l  m } t ƒ  } | | | ƒ } | rZ | j d ƒ | j ƒ  } t d | ƒ ‚ n  | r |  j j d | | d k r d p„ d f ƒ n  d	 S(
   s•   
        Validates the given app, raising CommandError for any errors.

        If app is None, then this will validate all installed apps.

        iÿÿÿÿ(   t   get_validation_errorsi    s'   One or more models did not validate:
%ss   %s error%s foundi   t   sR6   N(   t!   django.core.management.validationRg   R   t   seekt   readR   RQ   R$   (   R   t   appt   display_num_errorsRg   Rh   t
   num_errorst
   error_text(    (    s4   ../Django//lib/python/django/core/management/base.pyR]     s    	c         O   s   t  ƒ  ‚ d S(   sb   
        The actual logic of the command. Subclasses must implement
        this method.

        N(   t   NotImplementedError(   R   R:   R   (    (    s4   ../Django//lib/python/django/core/management/base.pyR^      s    N(   R   R   R	   R    R?   R4   R:   t   TrueRX   R\   t   FalseR_   R   R9   R<   RA   RB   RP   RF   R   R]   R^   (    (    (    s4   ../Django//lib/python/django/core/management/base.pyR'   G   s0   K											't
   AppCommandc           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   s  
    A management command which takes one or more installed application
    names as arguments, and does something with each of them.

    Rather than implementing ``handle()``, subclasses must implement
    ``handle_app()``, which will be called once for each application.

    s   <appname appname ...>c   
      O   sÃ   d d l  m } | s% t d ƒ ‚ n  y& g  | D] } | j | ƒ ^ q/ } Wn) t t f k
 rv } t d | ƒ ‚ n Xg  } x6 | D]. } |  j | |  }	 |	 r„ | j |	 ƒ q„ q„ Wd j | ƒ S(   Niÿÿÿÿ(   t   modelss   Enter at least one appname.s8   %s. Are you sure your INSTALLED_APPS setting is correct?s   
(	   R`   Rt   R   t   get_appR   t   ImportErrort
   handle_appt   appendt   join(
   R   t
   app_labelsR   Rt   t	   app_labelt   app_listRO   Re   Rl   t
   app_output(    (    s4   ../Django//lib/python/django/core/management/base.pyR^   4  s    &c         K   s   t  ƒ  ‚ d S(   s­   
        Perform the command's actions for ``app``, which will be the
        Python module corresponding to an application name given on
        the command line.

        N(   Rp   (   R   Rl   R   (    (    s4   ../Django//lib/python/django/core/management/base.pyRw   C  s    (   R   R   R	   R:   R^   Rw   (    (    (    s4   ../Django//lib/python/django/core/management/base.pyRs   )  s   	t   LabelCommandc           B   s,   e  Z d  Z d Z d Z d „  Z d „  Z RS(   s€  
    A management command which takes one or more arbitrary arguments
    (labels) on the command line, and does something with each of
    them.

    Rather than implementing ``handle()``, subclasses must implement
    ``handle_label()``, which will be called once for each label.

    If the arguments should be names of installed applications, use
    ``AppCommand`` instead.

    s   <label label ...>t   labelc         O   sh   | s t  d |  j ƒ ‚ n  g  } x6 | D]. } |  j | |  } | r) | j | ƒ q) q) Wd j | ƒ S(   Ns   Enter at least one %s.s   
(   R   R   t   handle_labelRx   Ry   (   R   t   labelsR   Re   R   t   label_output(    (    s4   ../Django//lib/python/django/core/management/base.pyR^   ]  s    c         K   s   t  ƒ  ‚ d S(   s~   
        Perform the command's actions for ``label``, which will be the
        string as given on the command line.

        N(   Rp   (   R   R   R   (    (    s4   ../Django//lib/python/django/core/management/base.pyR€   h  s    (   R   R   R	   R:   R   R^   R€   (    (    (    s4   ../Django//lib/python/django/core/management/base.pyR~   M  s
   	t   NoArgsCommandc           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   s8  
    A command which takes no arguments on the command line.

    Rather than implementing ``handle()``, subclasses must implement
    ``handle_noargs()``; ``handle()`` itself is overridden to ensure
    no arguments are passed to the command.

    Attempting to pass arguments will raise ``CommandError``.

    R6   c         O   s"   | r t  d ƒ ‚ n  |  j |   S(   Ns$   Command doesn't accept any arguments(   R   t   handle_noargs(   R   R:   R   (    (    s4   ../Django//lib/python/django/core/management/base.pyR^   ~  s    c         K   s   t  ƒ  ‚ d S(   s2   
        Perform this command's actions.

        N(   Rp   (   R   R   (    (    s4   ../Django//lib/python/django/core/management/base.pyR„   ƒ  s    (   R   R   R	   R:   R^   R„   (    (    (    s4   ../Django//lib/python/django/core/management/base.pyRƒ   q  s   
	(   R	   R   R   t   optparseR    R   RJ   R8   t   django.core.exceptionsR   t   django.core.management.colorR   t   django.utils.encodingR   t   django.utils.sixR   RH   R   R   t   objectR   R'   Rs   R~   Rƒ   (    (    (    s4   ../Django//lib/python/django/core/management/base.pyt   <module>   s    	â$$