ó
ø¢TQc           @  sŽ   d  d l  m 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
 m Z d  d l m Z d e f d „  ƒ  YZ d S(	   iÿÿÿÿ(   t   unicode_literalsN(   t   make_option(   t   NoArgsCommandt   CommandError(   t   connectionst   DEFAULT_DB_ALIAS(   t   sixt   Commandc           B  su   e  Z d  Z e j e d d d d d d e d d ƒf Z e Z d	 Z	 d
 „  Z
 d „  Z d „  Z d „  Z d „  Z RS(   uX   Introspects the database tables in the given database and outputs a Django model module.u
   --databaset   actionu   storet   destu   databaset   defaultt   helpuN   Nominates a database to introspect.  Defaults to using the "default" database.u	   django.dbc         K  sV   y2 x+ |  j  | ƒ D] } |  j j d | ƒ q WWn t k
 rQ t d ƒ ‚ n Xd  S(   Nu   %s
uP   Database inspection isn't supported for the currently selected database backend.(   t   handle_inspectiont   stdoutt   writet   NotImplementedErrorR   (   t   selft   optionst   line(    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyt   handle_noargs   s
    c         c  s	  t  | j d ƒ } | j d ƒ } d „  } d „  } | j ƒ  } d Vd Vd Vd Vd	 Vd
 Vd Vd Vd Vd Vd |  j Vd Vg  } xy| j j | ƒ D]e} | d  k	 rÏ t | ƒ rÏ | | ƒ sÏ qœ qÏ n  d | | ƒ V| j | | ƒ ƒ y | j j	 | | ƒ }	 Wn t
 k
 r#i  }	 n Xy | j j | | ƒ }
 Wn t
 k
 rVi  }
 n Xg  } x‚t | j j | | ƒ ƒ D]e\ } } g  } i  } | d } | |	 k } |  j | | | ƒ \ } } } | j | ƒ | j | ƒ | j | ƒ | |
 k r1|
 | d rt | d <q1|
 | d r1t | d <q1n  | r‹|	 | d | k rQd pb| |	 | d ƒ } | | k r~d | } qÍd | } nB |  j | | | ƒ \ } } } | j | ƒ | j | ƒ | d 7} | d k rþ| d k rþ| i t d 6k rþqyn  | d r.t | d <| d' k r.t | d <q.n  d  | | f } | r«| j d ƒ s`| d! 7} n  | d! j g  | j ƒ  D]( \ } } d" | | t | ƒ ƒ f ^ qvƒ 7} n  | d# 7} | rÕ| d$ d% j | ƒ 7} n  d& | VqyWx |  j | ƒ D] } | VqòWqœ Wd  S((   Nu   databaseu   table_name_filterc         S  s.   |  j  ƒ  j d d ƒ j d d ƒ j d d ƒ S(   Nu   _u    u    u   -(   t   titlet   replace(   t
   table_name(    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyt   <lambda>$   s    c         S  s   |  j  d ƒ r |  d p |  S(   Nu   u'i   (   t
   startswith(   t   s(    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyR   %   s    u0   # This is an auto-generated Django model module.u<   # You'll have to do the following manually to clean this up:u   #     * Rearrange models' orderu@   #     * Make sure each model has one field with primary_key=TrueuR   # Feel free to rename the models, but don't rename db_table values or field names.u   #uV   # Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'u   # into your database.u'   from __future__ import unicode_literalsu    u   from %s import modelsu   class %s(models.Model):i    u   primary_keyu   uniquei   u   'self'u   ForeignKey(%su   ForeignKey('%s'u   (u   idu
   AutoField(i   u   blanku
   TextField(u
   CharField(u   nullu   %s = models.%su   , u   %s=%su   )u    # u    u       %s(   u
   TextField(u
   CharField((   R   t   gett   cursort	   db_modulet   introspectiont   table_namest   Nonet   callablet   appendt   get_relationsR   t   get_indexest	   enumeratet   get_table_descriptiont   normalize_col_namet   updatet   extendt   Truet   get_field_typet   endswitht   joint   itemst   reprt   get_meta(   R   R   t
   connectiont   table_name_filtert   table2modelt   strip_prefixR   t   known_modelsR   t	   relationst   indexest   used_column_namest   it   rowt   comment_notest   extra_paramst   column_namet   is_relationt   att_namet   paramst   notest   rel_tot
   field_typet   field_paramst   field_notest
   field_desct   kt   vt	   meta_line(    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyR      s˜    			

(
.
+

	?
c   	      C  s5  i  } g  } | j  ƒ  } | | k r4 | j d ƒ n  | rc | j d ƒ rV | d  } qc | | d <n  t j d d | ƒ \ } } | d k rš | j d ƒ n  | j d	 ƒ d k rx+ | j d	 ƒ d k rÜ | j d	 d ƒ } q² W| j  ƒ  j d	 ƒ d k r| j d
 ƒ qn  | j d ƒ r4d | } | j d ƒ n  | j d ƒ r]d | } | j d ƒ n  t j	 | ƒ r†| d 7} | j d ƒ n  | d j
 ƒ  r°d | } | j d ƒ n  | | k r	d } x$ d | | f | k rè| d 7} qÅWd | | f } | j d ƒ n  | | k r(| r(| | d <n  | | | f S(   uU   
        Modify the column name to make it Python-compatible as a field name
        u   Field name made lowercase.u   _idiýÿÿÿu	   db_columnu   \Wu   _i    u.   Field renamed to remove unsuitable characters.u   __u>   Field renamed because it contained more than one '_' in a row.u   field%su*   Field renamed because it started with '_'.u   %sfieldu(   Field renamed because it ended with '_'.u   _fieldu4   Field renamed because it was a Python reserved word.u	   number_%su:   Field renamed because it wasn't a valid Python identifier.u   %s_%di   u'   Field renamed because of name conflict.(   t   lowerR!   R+   t   ret   subnt   findR   R   t   keywordt	   iskeywordt   isdigit(	   R   t   col_nameR7   R=   RC   RD   t   new_namet   num_replt   num(    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyR&      sL    



c         C  sÝ   i  } g  } y | j  j | d | ƒ } Wn$ t k
 rO d } | j d ƒ n Xt | ƒ t k r~ | \ } } | j | ƒ n  | d k r¥ | d r¥ | d | d <n  | d k rÐ | d | d	 <| d
 | d <n  | | | f S(   uê   
        Given the database connection, the table name, and the cursor row
        description, this routine will return the given field type name, as
        well as any additional keyword parameters and notes for the field.
        i   u	   TextFieldu   This field type is a guess.u	   CharFieldi   u
   max_lengthu   DecimalFieldi   u
   max_digitsi   u   decimal_places(   R   R*   t   KeyErrorR!   t   typet   tupleR'   (   R   R0   R   R9   RC   RD   RB   t
   new_params(    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyR*   ¹   s     c         C  s   d d | d g S(   uº   
        Return a sequence comprising the lines of code necessary
        to construct the inner Meta class for the model corresponding
        to the given database table name.
        u       class Meta:u           db_table = '%s'u    (    (   R   R   (    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyR/   Ø   s    (   t   __name__t
   __module__R   R   t   option_listR   R   t   Falset   requires_model_validationR   R   R   R&   R*   R/   (    (    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyR      s   		b	8	(   t
   __future__R    RM   RJ   t   optparseR   t   django.core.management.baseR   R   t	   django.dbR   R   t   django.utilsR   R   (    (    (    sB   ../Django//lib/python/django/core/management/commands/inspectdb.pyt   <module>   s   