o
    Zh                     @  s|   d dl 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 d dlmZ er4d dlmZmZmZ G dd deZd	S )
    )annotations)TYPE_CHECKINGAnyIteratorLiteralOptionalTuple)Document)get_from_env)
BaseLoader)BoardCardTrelloClientc                   @  s\   e Zd ZdZddddddd+ddZedddd,ddZd-dd Zd.d"d#Zd/d)d*Z	dS )0TrelloLoaderz!Load cards from a `Trello` board.Tall)due_datelabelslistclosed)include_card_nameinclude_commentsinclude_checklistcard_filterextra_metadataclientr   
board_namestrr   boolr   r   r    Literal['closed', 'open', 'all']r   Tuple[str, ...]c                C  s.   || _ || _|| _|| _|| _|| _|| _dS )a  Initialize Trello loader.

        Args:
            client: Trello API client.
            board_name: The name of the Trello board.
            include_card_name: Whether to include the name of the card in the document.
            include_comments: Whether to include the comments on the card in the
                document.
            include_checklist: Whether to include the checklist on the card in the
                document.
            card_filter: Filter on card status. Valid values are "closed", "open",
                "all".
            extra_metadata: List of additional metadata fields to include as document
                metadata.Valid values are "due_date", "labels", "list", "closed".

        N)r   r   r   r   r   r   r   )selfr   r   r   r   r   r   r    r!   b/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/trello.py__init__   s   
zTrelloLoader.__init__Napi_keytokenr%   Optional[str]r&   kwargsr   returnc             
   K  sn   zddl m} W n ty } ztd|d}~ww |p tdd}|p'tdd}|||d	}| ||fi |S )
a  Convenience constructor that builds TrelloClient init param for you.

        Args:
            board_name: The name of the Trello board.
            api_key: Trello API key. Can also be specified as environment variable
                TRELLO_API_KEY.
            token: Trello token. Can also be specified as environment variable
                TRELLO_TOKEN.
            include_card_name: Whether to include the name of the card in the document.
            include_comments: Whether to include the comments on the card in the
                document.
            include_checklist: Whether to include the checklist on the card in the
                document.
            card_filter: Filter on card status. Valid values are "closed", "open",
                "all".
            extra_metadata: List of additional metadata fields to include as document
                metadata.Valid values are "due_date", "labels", "list", "closed".
        r   )r   zWCould not import trello python package. Please install it with `pip install py-trello`.Nr%   ZTRELLO_API_KEYr&   ZTRELLO_TOKENr$   )trellor   ImportErrorr
   )clsr   r%   r&   r(   r   exr   r!   r!   r"   from_credentials4   s   zTrelloLoader.from_credentialsIterator[Document]c              
   c  sz    zddl m} W n ty } ztd|d}~ww |  }dd | D }|j| jd}|D ]	}| ||V  q1dS )zLoads all cards from the specified Trello board.

        You can filter the cards, metadata and text included by using the optional
            parameters.

         Returns:
            A list of documents, one for each card in the board.
        r   BeautifulSoupzK`beautifulsoup4` package not found, please run `pip install beautifulsoup4`Nc                 S  s   i | ]}|j |jqS r!   )idname).0Z	list_itemr!   r!   r"   
<dictcomp>o   s    z*TrelloLoader.lazy_load.<locals>.<dictcomp>)r   )bs4r1   r+   
_get_boardZ
list_listsZ	get_cardsr   _card_to_doc)r    r1   r-   board	list_dictZcardscardr!   r!   r"   	lazy_load\   s"   	zTrelloLoader.lazy_loadr   c                   s8   t  fdd j D d }|std j d|S )Nc                 3  s     | ]}|j  jkr|V  qd S )N)r3   r   )r4   br    r!   r"   	<genexpr>x   s    z*TrelloLoader._get_board.<locals>.<genexpr>zBoard `z` not found.)nextr   Zlist_boards
ValueErrorr   )r    r9   r!   r>   r"   r7   u   s   zTrelloLoader._get_boardr;   r   r:   dictr	   c                   s4  ddl m  d}| jr|jd }|j r| |jd 7 }| jrA|jD ]}|j	r@dd |j	D }|d|j dd
| 7 }q%| jrW fdd|jD }|d	d
| 7 }|j|j|jd
}d| jv rodd |jD |d< d| jv r|j|v r||j |d< d| jv r|j|d< d| jv r|j|d< t||dS )Nr   r0    
lxmlc                 S  s"   g | ]}|d   d|d  qS )r3   :stater!   )r4   itemr!   r!   r"   
<listcomp>   s    z-TrelloLoader._card_to_doc.<locals>.<listcomp>c                   s"   g | ]} |d  d d  qS )datatextrE   )get_text)r4   commentr0   r!   r"   rI      s    z	Comments:)titler2   urlr   c                 S  s   g | ]}|j qS r!   )r3   )r4   labelr!   r!   r"   rI      s    r   r   r   )Zpage_contentmetadata)r6   r1   r   r3   descriptionstriprL   r   Z
checklistsitemsjoinr   commentsr2   rO   r   r   Zlist_idr   r   r	   )r    r;   r:   Ztext_contentZ	checklistrT   rV   rQ   r!   r0   r"   r8   ~   sB   










zTrelloLoader._card_to_doc)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )
r   r   r%   r'   r&   r'   r(   r   r)   r   )r)   r/   )r)   r   )r;   r   r:   rB   r)   r	   )
__name__
__module____qualname____doc__r#   classmethodr.   r<   r7   r8   r!   r!   r!   r"   r      s    #
'
	r   N)
__future__r   typingr   r   r   r   r   r   Zlangchain_core.documentsr	   Zlangchain_core.utilsr
   Z)langchain_community.document_loaders.baser   r*   r   r   r   r   r!   r!   r!   r"   <module>   s     