ó
¸9—^c           @   sÚ   d  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 d l m Z d d l m Z m Z m Z d d	 l m Z d d
 l m Z d e f d „  ƒ  YZ d e
 f d „  ƒ  YZ d S(   s  
SyncData
========

Django command similar to 'loaddata' but also deletes.
After 'syncdata' has run, the database will have the same data as the fixture - anything
missing will of been added, anything different will of been updated,
and anything extra will of been deleted.
iÿÿÿÿN(   t   apps(   t   settings(   t   serializers(   t   BaseCommand(   t   no_style(   t   DEFAULT_DB_ALIASt   connectionst   transaction(   t	   pluralize(   t   signalcommandt   SyncDataErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/syncdata.pyR
      s   t   Commandc           B   sD   e  Z d  Z d Z d Z d „  Z d „  Z e d „  ƒ Z d „  Z	 RS(   s    syncdata command sR   Makes the current database have the same data as the fixture(s), no more, no less.s   fixture [fixture ...]c      
   C   sw   t  t |  ƒ j | ƒ | j d d d d d d t d d ƒ| j d	 d t d d
 ƒ| j d d d d t d d ƒd  S(   Ns   --skip-removet   actiont   store_falset   destt   removet   defaultt   helps   Avoid remove any object from dbs
   --databasesX   Nominates a specific database to load fixtures into. Defaults to the "default" database.t   fixture_labelst   nargst   ?t   types+   Specify the fixture label (comma separated)(   t   superR   t   add_argumentst   add_argumentt   TrueR   t   str(   t   selft   parser(    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/syncdata.pyR   $   s    c         C   sG  x@| j  ƒ  D]2} | j j ƒ  } t g  | D] } | j ^ q, ƒ } t g  | | D] } | j ^ qR ƒ } | j | ƒ } | rÐ xN | D]C }	 |	 j | k r† |	 j ƒ  | d k rÉ d t j |	 ƒ GHqÉ q† q† Wn  | d k r | r t	 | ƒ }
 |
 d k rt j | j
 j ƒ } n t j | j
 j ƒ } d t |
 ƒ | f GHq q Wd S(   så   
        Delete all the objects in the database that are not in objects_to_keep.
        - objects_to_keep: A map where the keys are classes, and the values are a
         set of the objects of that class we should keep.
        i   s   Deleted object: %si    i   s   Deleted %s %sN(   t   keyst   objectst   allt   sett   pkt
   differencet   deletet   sixt   ut   lent   _metat   verbose_name_pluralt   verbose_nameR   (   R   t   objects_to_keept	   verbosityt   class_t   currentt   xt   current_idst   keep_idst   remove_these_onest   objt   num_deletedt   type_deleted(    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/syncdata.pyt   remove_objects_not_in3   s"    "&
c         O   s´   t  ƒ  |  _ | d |  _ | d r6 | d j d ƒ n d } y' t j ƒ   |  j | | ƒ Wd  QXWn$ t k
 r‰ } |  j j | ƒ GHn Xt j	 |  j ƒ r° t
 |  j j ƒ  n  d  S(   Nt   databaseR   t   ,(    (   R   t   stylet   usingt   splitR   t   atomict   syncdataR
   t   ERRORt   get_autocommitR   t   close(   R   t   argst   optionsR   t   exc(    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/syncdata.pyt   handleO   s    #c       	   C   s"  | d } | d } d } d } g  } t  ƒ  } d „  }	 t |  j j ƒ  }
 g  t j ƒ  D] } | j ^ qX } g  | D]* } t j j	 t j j
 | j ƒ d ƒ ^ qt } xœ| D]”} | j d ƒ } t | ƒ d k rç | } t j ƒ  } nB d j	 | d  ƒ | d } } | t j ƒ  k r#| g } n g  } | rJ| d k r`d	 | GHq`n t d
 | | f ƒ ‚ t j j | ƒ r~| g } n | t t j ƒ d g } x¤| D]œ} | d k rÃd |	 | ƒ GHn  t } xo| D]g} | d k rýd |	 | ƒ | | f GHn  yçt j j	 | d j	 | | g ƒ ƒ } t | d ƒ } | rb| j ƒ  t d | |	 | ƒ f ƒ ‚ n| d 7} | j d ƒ | d k r d | | |	 | ƒ f GHn  y½ i  } t j | | ƒ } x{ | D]s } | d 7} | d c d 7<| j j } | | k r
t  ƒ  | | <n  | | j | j ƒ | j | ƒ | j ƒ  qÂW| d rV|  j | | ƒ n  t } Wny t  t! f k
 ry‚  n` t" k
 rØd d  l# } | j ƒ  t$ j% ƒ  | r¹| j& ƒ  n  t d | | j' ƒ  f ƒ ‚ n X| j ƒ  WqÐt k
 rÿ} | ‚ qÐt" k
 r6| d k r7d | | |	 | ƒ f GHq7qÐXqÐWqŸWq« Wd | k rbt d | ƒ ‚ n  | d k rËt |  j j( j) |  j* | ƒ } | rË| d k r§d GHn  x | D] } |
 j+ | ƒ q®WqËn  | d k rî| d k rd GHqn0 | d k rd | t, | ƒ | t, | ƒ f GHn  d  S(   NR-   t	   tracebacki    c         S   s   |  r d |  p d S(   Ns   '%s's   absolute path(    (   t   dirname(    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/syncdata.pyt   <lambda>k   t    t   fixturest   .i   iÿÿÿÿs   Loading '%s' fixtures...sH   Problem installing fixture '%s': %s is not a known serialization format.RI   s   Checking %s for fixtures...s    Trying %s for %s fixture '%s'...t   rs-   Multiple fixtures named '%s' in %s. Aborting.s#   Installing %s fixture '%s' from %s.R   s$   Problem installing fixture '%s': %s
s   No %s fixture '%s' in %s.s=   No fixture data found for '%s'. (File format may be invalid.)s   Resetting sequencess   No fixtures found.s'   Installed %d object%s from %d fixture%s(-   R"   R   R;   t   cursorR    t   get_app_configst   modulet   ost   patht   joinRG   t   __file__R<   R(   R   t   get_public_serializer_formatsR
   t   isabst   listR   t   FIXTURE_DIRSt   Falset   openRA   t   appendt   deserializet   objectt	   __class__t   addt   saveR7   R   t
   SystemExitt   KeyboardInterruptt	   ExceptionRF   R   t   rollbackt	   print_exct
   format_exct   opst   sequence_reset_sqlR:   t   executeR   (    R   R   RC   R-   t   show_tracebackt   fixture_countt   object_countt   objects_per_fixturet   modelst   humanizeRM   t   appt   app_modulest   app_fixturest   fixture_labelt   partst   fixture_namet   formatst   formatt   fixture_dirst   fixture_dirt   label_foundt	   full_patht   fixtureR,   R    R4   R.   RF   t   et   sequence_sqlt   line(    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/syncdata.pyR>   a   s²    

		"7$






 	+(
   R   R   t   __doc__R   RB   R   R7   R	   RE   R>   (    (    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/syncdata.pyR      s   		(   R   RP   R&   t   django.appsR    t   django.confR   t   django.coreR   t   django.core.management.baseR   t   django.core.management.colorR   t	   django.dbR   R   R   t   django.template.defaultfiltersR   t"   django_extensions.management.utilsR	   Rb   R
   R   (    (    (    sP   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/syncdata.pyt   <module>
   s   