o
    Zh	                     @  s   d Z ddlmZ ddlZddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZmZmZ ddlmZ ddlmZ e	r>ddlZdZG d	d
 d
eZdS )z)Document Loader for ArcGIS FeatureLayers.    )annotationsN)datetimetimezone)TYPE_CHECKINGAnyIteratorListOptionalUnion)Document)
BaseLoaderz(Not Provided)c                   @  s>   e Zd ZdZ						ddddZd d!ddZd"ddZdS )#ArcGISLoaderz)Load records from an ArcGIS FeatureLayer.N1=1Flayer(Union[str, arcgis.features.FeatureLayer]gisOptional[arcgis.gis.GIS]wherestr
out_fieldsOptional[Union[List[str], str]]return_geometryboolresult_record_countOptional[int]lyr_descOptional[str]kwargsr   c              
   K  s6  zdd l }	W n ty }
 ztd|
d }
~
ww zddlm} || _W n ty4   td d | _Y nw |p;|	j | _t	|t
rO|| _|	jj||d| _n|j| _|| _| || _|| _t	|t
rh|| _n|d u rpd| _nd|| _|| _|| _t	|t | _t| j| j| j| j| jd}|| || _d S )	Nr   zJarcgis is required to use the ArcGIS Loader. Install it with pip or conda.)BeautifulSoupz1BeautifulSoup not found. HTML will not be parsed.)r   *,)r   r   r   return_all_recordsr   )arcgisImportErrorZbs4r   BEAUTIFULSOUPwarningswarnr   ZGIS
isinstancer   urlfeaturesFeatureLayerr   _get_layer_propertieslayer_propertiesr   r   joinr   r   intr!   dictupdatequery_params)selfr   r   r   r   r   r   r   r   r"   er   r1    r4   i/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/arcgis_loader.py__init__   sT   






zArcGISLoader.__init__returnr/   c           	      C  s  ddl }td}| jj}|du r3z| jr| |d j}n|d }|p%t}W n ty2   t}Y nw z<|d }| j	j
|pJ|jt|d| j}z|j}W n ty]   |jj}Y nw | jrh| |j}n|}|pmt}W n tyz   t}Y nw |||dS )z/Get the layer properties from the FeatureLayer.r   Nz/\d+$descriptionZserviceItemId )layer_descriptionitem_descriptionr,   )r"   recompiler   
propertiesr$   text_NOT_PROVIDEDKeyErrorr   contentgetr)   r*   subr(   r8   AttributeError)	r2   r   r"   Zlayer_number_patternpropsZitem_iditemZraw_descZ	item_descr4   r4   r5   r+   V   sB   

z"ArcGISLoader._get_layer_propertiesIterator[Document]c              	   c  s    | j jdi | j}dd |D }|D ]M}|d }t|}ttj	  d| j
d d | j| j
d | j
d | j
d d	}| jrZz|d
 |d
< W n tyY   td Y nw t||dV  qdS )z$Lazy load records from FeatureLayer.c                 s  s    | ]}|j V  qd S N)as_dict).0featurer4   r4   r5   	<genexpr>   s    z)ArcGISLoader.lazy_load.<locals>.<genexpr>
attributesZr,   namer:   r;   )ZaccessedrP   r(   r:   r;   r,   Zgeometryz7Geometry could not be retrieved from the feature layer.)page_contentmetadataNr4   )r   queryr1   jsondumpsr   nowr   utc	isoformatr,   r(   r   rA   r%   r&   r   )r2   Zquery_responser)   rL   rN   rQ   rR   r4   r4   r5   	lazy_load}   s.   
	zArcGISLoader.lazy_load)Nr   NFNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rI   )r   r   r7   r/   )r7   rH   )__name__
__module____qualname____doc__r6   r+   rY   r4   r4   r4   r5   r      s    >'r   )r]   
__future__r   rT   r<   r%   r   r   typingr   r   r   r   r	   r
   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r"   r@   r   r4   r4   r4   r5   <module>   s     