
^Q\                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z m	 Z	 d  d l
 m Z d  d l m Z m Z d  d l Z d  d l m Z m Z d  d l m Z d  d l m Z Gd	 d
   d
 e	  Z d d   Z d d   Z d S)    N)settings)CommandErrorBaseCommand)
connection)ContextTemplate)_make_writeablesignalcommand)REPLACEMENTS)
dia2djangoc               @   s@   e  Z d  Z d Z d Z d Z d d   Z e d d    Z d S)	CommandzNCreates an application directory structure for the specified application name.FTc          	   C   s   | j  d  | j  d d d d d d d d	 | j  d
 d d d d d d d | j  d d d d d d d | j  d d d d d d d d  S)Napp_namez
--templatez-tactionstoredestapp_templatehelpzThe path to the app templatez--parent_pathz-pparent_pathz0The parent path of the application to be createdz-d
store_true	dia_parsez7Generate model.py and admin.py from [APP_NAME].dia filez	--diagramdia_pathz8The diagram path of the app to be created. -d is implied)add_argument)selfparser r   a/var/www/dbchiro/venv/build/django-extensions/django_extensions/management/commands/create_app.pyadd_arguments   s    zCommand.add_argumentsc                s  d d l  m } t j d |  t j d  d  d k rH t d   n  t j   } t j	 j
 |  d } | d   | j d	  p t j	 j t j d d
 d	  } t j	 j | j d  p |    } | j d  p t j	 j | d    } t j	 j |  st d |   n  t j d    s8t d     n  | j d  pS| j d  }	 |	 rt j	 j |  st d |   n    t j k rt d     f   n    f d d   t j j   D }
 |
 rt d     qn  y t j |  Wn1 t k
 r+} z t |   WYd  d  } ~ Xn Xt | | |    |	 r[t | | |    n  |  j j d    |  j j d    d  S)Nr   )MarkedForDeprecationWarningzDeprecated: "create_app" is marked for depreciaton and will most likely be removed in future releases. Use "startapp --template" instead.      
   zEThis command is deprecated. Please use "startapp --template" instead.r   r   confr   r   z%s.diaz&The template path, %r, does not exist.z^\w+$zU%r is not a valid application name. Please use only numbers, letters and underscores.r   z%The diagram path, %r, does not exist.zyThe application %s should not be defined in the settings file. Please remove %s now, and add it after using this command.c                s)   g  |  ] } | j  d     r |  q S)z%s_)
startswith).0name)r   r   r   
<listcomp>G   s   	 z"Command.handle.<locals>.<listcomp>z>%r application has tables in the database. Please delete them.zApplication %r created.
zjPlease add now %r and any other dependent application in settings.INSTALLED_APPS, and run 'manage syncdb'
)r   r    )Z#django_extensions.utils.deprecationr   warningswarndjangoVERSIONr   osgetcwdpathsplitgetjoindjango_extensions__path__existsresearchr   ZINSTALLED_APPSr   ZintrospectionZtable_namesmakedirsOSErrorcopy_templategenerate_models_and_adminstdoutwrite)r   argsoptionsr   Zproject_dirproject_namer   app_dirr   r   Ztableser   )r   r   handle(   sD    
.$("zCommand.handleN)	__name__
__module____qualname__r   Zrequires_system_checksZcan_import_settingsr   r	   rA   r   r   r   r   r      s
   r   c             C   sp  d d l  } t j j |   }  xKt j |   D]:\ } } } | t |   d d  } t j j | |  j d |  }	 | r t j j |	  r t j	 |	  n  x3 t
 |  D]% \ }
 } | j d  r | |
 =q q Wi | d 6| d 6} | j t  xm| D]e} | j d  s | j d  r)q n  t j j | |  } t j j |	 | j d |   } t j j |  rt j j |	 |  } t j j |  rq qn  | j d	  r| d d  } n  t | d  } t | d  } | j t | j    j t |    | j   | j   y | j | |  t |  Wq t k
 rct j j d |  Yq Xq Wq. Wd S)z?copies the specified template directory to the copy_to locationr   Nr   r   .r>   z.pycz	.DS_Storez.tmpl   rwzlNotice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.
)shutilr+   r-   normpathwalklenr0   replacer3   mkdir	enumerater"   updater
   endswithopenr;   r   readrenderr   closecopymoder   r7   sysstderr)r   Zcopy_tor>   r   rJ   dsubdirsfilesrelative_dirZd_newisubdirreplacementsfZpath_oldZpath_newZfp_oldZfp_newr   r   r   r8   Y   sB    !!	(

r8   c       
      C   s   d d d  } t  j j | d  } t  j j | d  } d t |   } t | d  j |  t j d |  } d	 | d
 | | d j |  f d d }	 |	 | d d j t d d   |    7}	 t | d  j |	  d S)z*Generates the models.py and admin.py filesFc             S   s   d } x |  r |  d d  } | j  d  } | d	 k r{ t |   d k r{ | d |  d |  7} |  | d d  }  n | d |  7} d }  |  r	 | r	 d |  }  q	 q	 W| S)
z/format string in lines of 80 or less characters NM    r   z%s \
z%s
z    %sr&   )rfindrM   )stringindentretvallineZ
last_spacer   r   r   format_text   s    	z.generate_models_and_admin.<locals>.format_textz	models.pyzadmin.pyzfrom django.db import models
rH   zclass (\w+)z2from django.contrib.admin import site, ModelAdmin
zfrom %s.%s.models import %sz, rg   Tz

%s
c             S   s   d |  S)Nzsite.register(%s)r   )tr   r   r   <lambda>   s    z+generate_models_and_admin.<locals>.<lambda>N)	r+   r-   r0   r   rS   r;   r4   findallmap)
r   r?   r>   r   rj   Z
model_pathZ
admin_pathZ
models_txtZclassesZ	admin_txtr   r   r   r9      s    ,,r9   )r+   r4   rX   r'   r)   Zdjango.confr   Zdjango.core.management.baser   r   Z	django.dbr   Zdjango.templater   r   r1   Z"django_extensions.management.utilsr   r	   Zdjango_extensions.settingsr
   Z"django_extensions.utils.dia2djangor   r   r8   r9   r   r   r   r   <module>   s   F'