3
(h                 @   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>   y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-install-q3hcpn_q/Django/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   |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__)N)r   r   r   r   r   r   r   r   r   r      s   r   c               @   s$   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
 |j ddtddd
 |j ddddd |j ddtddd
 |j dddddd |j dddd |j d d!d"d#d$d |j d%d&tdd'd
 |j d(d)d*d |j d+dd,d-d. d S )/Ndata_sourcezPath to the data source.)help
model_namezName of the model to create.z--blankZ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.)r   actiondefaultr   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`))r   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.z--multi-geom
store_true
multi_geomz?Treat the geometry in the data source as a geometry collection.)r    r   r!   r   z--name-fieldZ
name_fieldzJSpecifies a field name to return for the `__unicode__`/`__str__` function.)r   r   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`.)r    r   r   )add_argumentr   r   )r   r   r   r   r   add_arguments*   sH    zCommand.add_argumentsc                sL  |j d|j d }}tjs$tdytj|}W n, tjk
r^ } zt|W Y d d }~X nX ddlm m}  fdd|j	 D }dd	  ||f|D }	|d
 r>|d |d |d d}
||f|
}dd |j	 D |	j
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   z4GDAL is required to inspect geospatial data sources.r   )_ogrinspectr)   c                s*   i | ]"\}}|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S r   r   )r-   sr   r   r   
<listcomp>t   s    z"Command.handle.<locals>.<listcomp>r)   r#   r$   r&   )r#   r$   r&   c             S   s   i | ]\}}||qS r   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   Actionr   r   r   r   r   r   r   <module>   s   