
    gx                        d dl mZ d dlmZmZmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ dZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Ze	 G d
 d             ZddZy)    )annotations)IterableIteratorMapping)asdict	dataclassis_dataclass)date)Any)ElementTree)	BaseModel)format_as_xmlc                    t        ||      j                  | |      }|s-|j                  !|dnd}|j                  t	        ||            S |t        j                  ||       t        j                  |d      S )a  Format a Python object as XML.

    This is useful since LLMs often find it easier to read semi-structured data (e.g. examples) as XML,
    rather than JSON etc.

    Supports: `str`, `bytes`, `bytearray`, `bool`, `int`, `float`, `date`, `datetime`, `Mapping`,
    `Iterable`, `dataclass`, and `BaseModel`.

    Args:
        obj: Python Object to serialize to XML.
        root_tag: Outer tag to wrap the XML in, use `None` to omit the outer tag.
        item_tag: Tag to use for each item in an iterable (e.g. list), this is overridden by the class name
            for dataclasses and Pydantic models.
        include_root_tag: Whether to include the root tag in the output
            (The root tag is always included if it includes a body - e.g. when the input is a simple value).
        none_str: String to use for `None` values.
        indent: Indentation string to use for pretty printing.

    Returns: XML representation of the object.

    Example:
    ```python {title="format_as_xml_example.py" lint="skip"}
    from pydantic_ai.format_as_xml import format_as_xml

    print(format_as_xml({'name': 'John', 'height': 6, 'weight': 200}, root_tag='user'))
    '''
    <user>
      <name>John</name>
      <height>6</height>
      <weight>200</weight>
    </user>
    '''
    ```
    )item_tagnone_str 
spaceunicodeencoding)_ToXmlto_xmltextjoin_rootless_xml_elementsr   indenttostring)objroot_tagr   include_root_tagr   r   elr   s           N/var/www/openai/venv/lib/python3.12/site-packages/pydantic_ai/format_as_xml.pyr   r      st    T 
H	5	<	<S(	KB^ryy/F;<<r0##B;;    c                  2    e Zd ZU ded<   ded<   ddZddZy)	r   strr   r   c                    t        j                  || j                  n|      }|| j                  |_        |S t        |t              r	||_        |S t        |t        t        f      r|j                  d      |_        |S t        |t        t        t        f      rt        |      |_        |S t        |t              r|j                         |_        |S t        |t              r| j!                  ||       |S t#        |      rZt        |t$              sJ|)t        j                  |j&                  j(                        }t+        |      }| j!                  ||       |S t        |t,              rO|)t        j                  |j&                  j(                        }| j!                  ||j/                  d             |S t        |t0              r,|D ]%  }| j3                  |d       }|j5                  |       ' |S t7        dt%        |             )Nignore)errorspython)modez%Unsupported type for XML formatting: )r   Elementr   r   r   
isinstancer'   bytes	bytearraydecodeboolintfloatr
   	isoformatr   _mapping_to_xmlr	   type	__class____name__r   r   
model_dumpr   r   append	TypeError)selfvaluetagelementdc_dictitemitem_els          r$   r   z_ToXml.to_xmlG   s   %%s{dmmL===GL4 3 s# GL0 / y12 <<x<8GL, + c512u:GL( ' t$ ??,GL$ # w'  %0   % E4)@{%--eoo.F.FGUmG  '2  y){%--eoo.F.FG  %*:*:*:*IJ  x(++dD1w' 
  CDK=QRRr%   c                    |j                         D ]j  \  }}t        |t              rt        |      }n(t        |t              st	        dt        |       d      |j                  | j                  ||             l y )Nz)Unsupported key type for XML formatting: z, only str and int are allowed)itemsr.   r3   r'   r<   r7   r;   r   )r=   r@   mappingkeyr>   s        r$   r6   z_ToXml._mapping_to_xmlf   se    !--/JC#s##hS)"KDQTI;Vt uvvNN4;;uc23 *r%   N)r>   r   r?   
str | NonereturnElementTree.Element)r@   rJ   rF   zMapping[Any, Any]rI   None)r9   
__module____qualname____annotations__r   r6    r%   r$   r   r   B   s    MM>4r%   r   c              #  ~   K   | D ]4  }|t        j                  ||       t        j                  |d       6 y w)Nr   r   r   )r   r   r   )rootr   sub_elements      r$   r   r   o   s9     {&9"";CC s   ;=N)examplesexampleTnullz  )r    r   r!   r'   r   r'   r"   r2   r   r'   r   rH   rI   r'   )rQ   rJ   r   rH   rI   zIterator[str])
__future__r   _annotationscollections.abcr   r   r   dataclassesr   r   r	   datetimer
   typingr   	xml.etreer   pydanticr   __all__r   r   r   rO   r%   r$   <module>r_      s    2 7 7 7 7   ! 

 !1<	1<1< 1< 	1<
 1< 1< 	1<h )4 )4 )4XDr%   