B
    ¦0H[Ø  ã               @   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   úuC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\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   |  ¡ dkrt|| jdƒ nt|| 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 )Ú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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� |j dddd� |j 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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.)r   ÚactionÚdefaultr   z	--decimalÚ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-nameÚ	geom_nameZgeomzDSpecifies the model name for the Geometry Field (defaults to `geom`))r   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.z--multi-geomÚ
store_trueÚ
multi_geomz?Treat the geometry in the data source as a geometry collection.)r    r   r   z--name-fieldZ
name_fieldz:Specifies a field name to return for the __str__() method.)r   r   z--no-importsÚstore_falseZimportszDDo not include `from django.contrib.gis.db import models` statement.z--nullÚ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--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*   sF    zCommand.add_argumentsc          
      s@  |  d¡|  d¡ }}yt |¡}W n, tjk
rP } zt|ƒ‚W d 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 |	¡d S )Nr   r   r   )Ú_ogrinspectr)   c                s*   i | ]"\}}|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S r   r   )r-   Úsr   r   r   ú
<listcomp>q   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
DataSourceZGDALExceptionr   Z#django.contrib.gis.utils.ogrinspectr,   r)   ÚitemsÚextendr   ÚfieldsÚjoin)r   ÚargsÚoptionsr   r   ÚdsÚmsgr)   Zogr_optionsÚoutputÚkwargsZmapping_dictr   )r,   r4   r   Úhandleb   s(    
"zCommand.handleN)r   r   r   r   Zrequires_system_checksr+   rC   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   