
 X                 @   s   d  d l  Z  d  d l m Z d  d l m Z m Z d  d l m Z Gd d   d e  j  Z	 Gd d   d e  j  Z
 Gd	 d
   d
 e  Z d S)    N)gdal)BaseCommandCommandError)get_func_argsc               @   s%   e  Z d  Z d Z d d d  Z d S)LayerOptionActionzy
    Custom argparse action for the `ogrinspect` `layer_key` keyword option
    which may be an integer or a string.
    Nc             C   sI   y t  | |  j t |   Wn% t k
 rD t  | |  j |  Yn Xd  S)N)setattrdestint
ValueError)selfparser	namespacevalueoption_string r   _/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/management/commands/ogrinspect.py__call__   s    zLayerOptionAction.__call__)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c               @   s%   e  Z d  Z d Z d d d  Z d S)ListOptionActionz
    Custom argparse action for `ogrinspect` keywords that require
    a string list. If the string is 'True'/'true' then the option
    value will be a boolean instead.
    Nc             C   sH   | j    d k r( t | |  j d  n t | |  j | j d   d  S)NtrueT,)lowerr   r   split)r   r   r   r   r   r   r   r   r      s    zListOptionAction.__call__)r   r   r   r   r   r   r   r   r   r      s   r   c               @   s4   e  Z d  Z d Z d Z d d   Z d d   Z d S)CommandzInspects the given OGR-compatible data source (e.g., a shapefile) and outputs
a GeoDjango model with the given model name. For example:
 ./manage.py ogrinspect zipcode.shp ZipcodeFc          
   C   sx  | j  d d d | j  d d d | j  d d d d	 t d
 d d d | j  d d d d	 t d
 d d d | j  d d d d
 d d d | j  d d d d	 t d
 d d d | j  d d	 d d d d
 d d d | j  d d d d d | j  d d	 d  d d! d
 d" d d# | j  d$ d d% d	 t d
 d d d& | j  d' d d( d d) | j  d* d	 d d d+ d d, d  S)-Ndata_sourcehelpzPath to the data source.
model_namezName of the model to create.z--blankr   ZblankactiondefaultFzUse a comma separated list of OGR field names to add the `blank=True` option to the field definition. Set to `true` to apply to all applicable fields.z	--decimaldecimalzUse a comma separated list of OGR float fields to generate `DecimalField` instead of the default `FloatField`. Set to `true` to apply to all OGR float fields.z--geom-name	geom_nameZgeomzDSpecifies the model name for the Geometry Field (defaults to `geom`)z--layer	layer_keyr   zThe key for specifying which layer in the OGR data source to use. Defaults to 0 (the first layer). May be an integer or a string identifier for the layer.z--multi-geom
store_true
multi_geomz?Treat the geometry in the data source as a geometry collection.z--name-fieldZ
name_fieldzJSpecifies a field name to return for the `__unicode__`/`__str__` function.z--no-importsstore_falseZimportsTzDDo not include `from django.contrib.gis.db import models` statement.z--nullnullzUse a comma separated list of OGR field names to add the `null=True` option to the field definition. Set to `true` to apply to all applicable fields.z--sridZsridzeThe SRID to use for the Geometry Field. If it can be determined, the SRID of the data source is used.z	--mappingmappingz8Generate mapping dictionary for use with `LayerMapping`.)add_argumentr   r   )r   r   r   r   r   add_arguments*   sH    zCommand.add_argumentsc                s  | j  d  | j  d  } } t j s7 t d   n  y t j |  } Wn4 t j k
 r } z t |   WYd  d  } ~ Xn Xd d l m   m }   f d d   | j	   D } d d	     | | |  D }	 | d
 ri | d d 6| d d 6| d d 6}
 | | |
  } d d   | j	   D  |	 j
 d d | d | j   g  |	 j
  f d d   | | d j D  |	 j
 d | d | | d f d g  n  d j |	  d S)Nr   r   z4GDAL is required to inspect geospatial data sources.r   )_ogrinspectr)   c                s=   i  |  ]3 \ } } | t     k r | d  k	 r | |  q S)N)r   ).0kv)r,   r   r   
<dictcomp>r   s   	 	z"Command.handle.<locals>.<dictcomp>c             S   s   g  |  ] } |  q Sr   r   )r-   sr   r   r   
<listcomp>t   s   	 z"Command.handle.<locals>.<listcomp>r)   r#   r$   r&   c             S   s   i  |  ] \ } } | |  q Sr   r   )r-   r.   r/   r   r   r   r0      s   	  z7# Auto-generated `LayerMapping` dictionary for %s modelz%s_mapping = {c             3   s#   |  ] } d    | | f Vq d S)z    '%s' : '%s',Nr   )r-   Zogr_fld)rev_mappingr   r   	<genexpr>   s   z!Command.handle.<locals>.<genexpr>z    '%s' : '%s',}
)popr   ZHAS_GDALr   Z
DataSourceZGDALExceptionZ#django.contrib.gis.utils.ogrinspectr,   r)   itemsextendr   fieldsjoin)r   argsoptionsr   r   Zdsmsgr)   Zogr_optionsoutputkwargsZmapping_dictr   )r,   r4   r   handlec   s.    	
,zCommand.handleN)r   r   r   r   Zrequires_system_checksr+   rB   r   r   r   r   r   !   s   9r   )argparseZdjango.contrib.gisr   Zdjango.core.management.baser   r   Zdjango.utils.inspectr   ZActionr   r   r   r   r   r   r   <module>   s   