o
    <g                     @   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   d/var/www/html/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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-name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_false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              
      s8  | 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|	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>}   s
    z"Command.handle.<locals>.<dictcomp>c                 S   s   g | ]}|qS r   r   )r1   sr   r   r   
<listcomp>   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   handleo   sN   

	
zCommand.handleN)r   r   r   r#   requires_system_checksr-   rP   r   r   r   r   r!   #   s    Cr!   )argparsedjango.contrib.gisr   django.core.management.baser   r   django.utils.inspectr   Actionr   r   r!   r   r   r   r   <module>   s    