
v^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   S/tmp/pip-build-8lau8j11/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   s<  | j  d d d | j  d d d | j  d d t d d	 d d
 | j  d d t d d	 d d | j  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 | j  d d d d d d d | j  d d t d d	 d d | j  d  d d! | j  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--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	--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-nameZgeomzDSpecifies the model name for the Geometry Field (defaults to `geom`)z--layerr   	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_truez?Treat the geometry in the data source as a geometry collection.z--name-fieldz:Specifies a field name to return for the __str__() method.z--no-importsstore_falseZimportszDDo not include `from django.contrib.gis.db import models` statement.z--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--sridzeThe SRID to use for the Geometry Field. If it can be determined, the SRID of the data source is used.z	--mappingz8Generate mapping dictionary for use with `LayerMapping`.)add_argumentr   r   )r   r   r   r   r   add_arguments*   sF    zCommand.add_argumentsc                s  | j  d  | j  d  } } y t j |  } Wn4 t j k
 rh } z t |   WYd  d  } ~ Xn Xd d l m   m }   f d d   | j   D } d d     | | |  D }	 | d	 rd
 | d
 d | d d | d i }
 | | |
  } d d   | j   D  |	 j	 d d d | d | j
   g  |	 j	  f d d   | | d j D  |	 j	 d | d
 | | d
 f d g  d j |	  d S)Nr   r   r   )_ogrinspectmappingc                s=   i  |  ]3 \ } } | t     k r | d  k	 r | |  q S)N)r   ).0kv)r'   r   r   
<dictcomp>o   s   	 	z"Command.handle.<locals>.<dictcomp>c             S   s   g  |  ] } |  q Sr   r   )r)   sr   r   r   
<listcomp>q   s   	 z"Command.handle.<locals>.<listcomp>r(   Z	geom_namer"   Z
multi_geomc             S   s   i  |  ] \ } } | |  q Sr   r   )r)   r*   r+   r   r   r   r,   ~   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
DataSourceZGDALExceptionr   Z#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'   r0   r   handleb   s(    


)zCommand.handleN)r   r   r   r   Zrequires_system_checksr&   r>   r   r   r   r   r   !   s   8r   )argparseZdjango.contrib.gisr   Zdjango.core.management.baser   r   Zdjango.utils.inspectr   ZActionr   r   r   r   r   r   r   <module>   s   