U
    gO                     @   sh   d dl Z d dlmZ d dlmZmZ d dlmZ G dd de jZ	G dd de jZ
G d	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   s>   zt || jt| W n" tk
r8   t || j| Y nX d S N)setattrdestint
ValueErrorselfparser	namespacevalueoption_string r   U/tmp/pip-unpacked-wheel-ye1bytdm/django/contrib/gis/management/commands/ogrinspect.py__call__   s    zLayerOptionAction.__call__)N__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   s4   |  dkrt|| jd nt|| j|d d S )NtrueT,)lowerr	   r
   splitr   r   r   r   r      s    zListOptionAction.__call__)Nr   r   r   r   r   r      s   r   c                   @   s$   e Zd ZdZg 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 Zipcodec                 C   s   |j ddd |j ddd |j dtddd	 |j d
tddd	 |j dddd |j ddtddd |j dddd |j ddd |j ddddd |j d tdd!d	 |j d"d#d |j d$dd%d d S )&Ndata_sourcezPath to the data source.)help
model_namezName of the model to create.z--blankFzUse 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.)actiondefaultr"   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`))r%   r"   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.)r
   r$   r%   r"   z--multi-geom
store_truez?Treat the geometry in the data source as a geometry collection.)r$   r"   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.)r$   r
   r"   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*   sp               zCommand.add_argumentsc              
      s<  | d| d }}zt|}W n, tjk
rP } zt|W 5 d }~X Y nX ddlm m}  fdd| D }dd  ||f|D }	|d	 r2|d
 |d |d d}
||f|
}dd | D |		ddd| d|
  g |		fdd||d  jD  |		d|d
 ||d
  f dg d|	S )Nr!   r#   r   )_ogrinspectmappingc                    s*   i | ]"\}}|t  kr|d k	r||qS r   r   .0kv)r+   r   r   
<dictcomp>p   s
      z"Command.handle.<locals>.<dictcomp>c                 S   s   g | ]}|qS r   r   )r.   sr   r   r   
<listcomp>r   s     z"Command.handle.<locals>.<listcomp>r,   	geom_namer&   
multi_geom)r4   r&   r5   c                 S   s   i | ]\}}||qS r   r   r-   r   r   r   r1      s       z7# Auto-generated `LayerMapping` dictionary for %s modelz%s_mapping = {c                 3   s   | ]}d  | |f V  qdS )    '%s': '%s',Nr   )r.   Zogr_fld)rev_mappingr   r   	<genexpr>   s
    z!Command.handle.<locals>.<genexpr>r7   }
)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+   r8   r   handleb   s.    

"zCommand.handleN)r   r   r   r"   Zrequires_system_checksr*   rF   r   r   r   r   r    !   s
   8r    )argparseZdjango.contrib.gisr   Zdjango.core.management.baser   r   Zdjango.utils.inspectr   Actionr   r   r    r   r   r   r   <module>   s   