o
    uvXjX  ã                   @   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                   @   ó   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 d S  ty   t || j|ƒ Y d S w ©N)ÚsetattrÚdestÚintÚ
ValueError©ÚselfÚparserÚ	namespaceÚvalueÚoption_string© r   ún/var/www/html/myproject/venv/lib/python3.10/site-packages/django/contrib/gis/management/commands/ogrinspect.pyÚ__call__   s
   ÿzLayerOptionAction.__call__r	   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s    r   c                   @   r   )Ú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   s6   |  ¡ dkrt|| jdƒ d S t|| j| d¡ƒ d S )NÚtrueTú,)Úlowerr
   r   Úsplitr   r   r   r   r      s   zListOptionAction.__call__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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-nameÚ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_falseÚ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*   sZ   ýýþýþþþþþ
þzCommand.add_argumentsc              
      s<  |  d¡|  d¡}}zt |¡}W n tjy# } zt|ƒ‚d }~ww ddlm‰ m} ‡ fdd„| ¡ D ƒ}dd„ ˆ ||fi |¤ŽD ƒ}	|d	 r—|d
 |d |d dœ}
||fi |
¤Ž}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   )Ú_ogrinspectÚmappingc                    s*   i | ]\}}|t ˆ ƒv r|d u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   )r1   Úsr   r   r   Ú
<listcomp>q   s    z"Command.handle.<locals>.<listcomp>r/   Ú	geom_namer(   Ú
multi_geom)r7   r(   r8   c                 S   s   i | ]\}}||“qS r   r   r0   r   r   r   r4   ~   s    Ú z7# Auto-generated `LayerMapping` dictionary for %s modelz%s_mapping = {c                 3   s    � | ]}d ˆ | |f V  qdS )ú    '%s': '%s',Nr   )r1   Úogr_fld)Úrev_mappingr   r   Ú	<genexpr>�   s   € ÿÿz!Command.handle.<locals>.<genexpr>r:   Ú}Ú
)Úpopr   Ú
DataSourceÚGDALExceptionr   Ú#django.contrib.gis.utils.ogrinspectr.   r/   ÚitemsÚextendr   ÚfieldsÚjoin)r   ÚargsÚoptionsr"   r$   ÚdsÚmsgr/   Úogr_optionsÚoutputÚkwargsÚmapping_dictr   )r.   r<   r   Úhandleb   s2   €ÿý
ÿÿ"zCommand.handleN)r   r   r   r#   Úrequires_system_checksr-   rP   r   r   r   r   r!   !   s    ÿ8r!   )ÚargparseÚdjango.contrib.gisr   Údjango.core.management.baser   r   Údjango.utils.inspectr   ÚActionr   r   r!   r   r   r   r   Ú<module>   s    