U
    8ÄT_Q  ã                   @   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-dv63caxw/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dZdd„ Zdd„ ZdS )ÚCommandz³Inspects 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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--blankFz–Use 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	--decimalzžUse 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   zšThe 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--nullz•Use 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>o   s
     ÿ z"Command.handle.<locals>.<dictcomp>c                 S   s   g | ]}|‘qS r   r   )r.   Úsr   r   r   Ú
<listcomp>q   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   