a
    J5d(                     @   s   d dl mZ d dl mZ d dl mZ g dZG dd dZG dd deZG d	d
 d
eZG dd deZG dd deZ	G dd deZ
G dd deZdS )   )bisep)
exceptions)utils)TransformerFuncTNumberT
ContainerTListTMapTUnionTc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   z+Reusable transformer between text and data.c                 C   s   |S )zReturn the value of the text.

        :param str text: the text
        :return: the value
        :raises ~tsplib95.exceptions.ParsingError: if the text cannot be parsed
                                                   into a value
         selftextr   r   Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/tsplib95/transformers.pyparse   s    zTransformer.parsec                 C   s   |du rdS t |S )zfReturn the text for the value.

        :param str value: the value
        :return: the text
        N )strr   valuer   r   r   render   s    zTransformer.renderc                 C   s   dS )z=Validate the value.

        :param value: the value
        Nr   r   r   r   r   validate)   s    zTransformer.validateN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   

r   c                       s(   e Zd ZdZ fddZdd Z  ZS )r   ad  Transformer that simply wraps a parsing function.

    The parsing function must accept a single positional argument for the text
    to parse. It must parse and return the value.

    Values are rendered back into values using the builtin :func:`str`, so it's
    generally best to use this for primitives.

    :param callable func: parsing function
    c                   s   t    || _d S N)super__init__func)r   r   	__class__r   r   r   <   s    
zFuncT.__init__c              
   C   sD   z|  |W S  ty> } ztj|dW Y d }~n
d }~0 0 d S )Nzfunc transformer error)r   	Exceptionr   ParsingErrorwrap)r   r   er   r   r   r   @   s    zFuncT.parse)r   r   r   r   r   r   __classcell__r   r   r    r   r   0   s   r   c                   @   s   e Zd ZdZdd ZdS )r   z)Transformer for any number, int or float.c              	   C   sH   t tfD ]&}z||W   S  ty,   Y q0 qd| }t|d S )Nz"could not convert text to number: )intfloat
ValueErrorr   r#   )r   r   r   errorr   r   r   r   J   s    
zNumberT.parseN)r   r   r   r   r   r   r   r   r   r   G   s   r   c                   @   sh   e Zd ZdZdddddddddZdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd ZdS )r   a  Transformer that acts as a generic container.

    :param value: transformer for each item
    :type value: :class:`~tsplib95.transformers.Transformer`
    :param str sep: separator between items
    :param str terminal: text that marks the end
    :param bool terminal_required: whether the terminal is required
    :param int size: required number of items
    :param bool filter_empty: filter out empty items (zero-length/blank)
    NT)r   septerminalterminal_requiredsizefilter_emptyc                C   s4   |pt  | _t|| _|| _|| _|| _|| _d S r   )	r   child_tfr   
from_valuer+   r,   r-   r.   r/   )r   r   r+   r,   r-   r.   r/   r   r   r   r   `   s    zContainerT.__init__c                 C   s  |  }| jrb|| jsJ| jrJtd| j d|t| j d  d|dt| j    }z| |}W n6 ty } zd}tj	||W Y d}~n
d}~0 0 | j
rttd|}| jdur.z|| j}W n ty   Y nD0 ||d d }dt| dt| j d	t|d
  }t|g }g }	t|D ]d\}
}z| |}W n@ ty } z&|	d|
 dt|  W Y d}~nd}~0 0 || q>|	rtj|	dd}t|| jrt|| jkrd| j dt| }t|| |S )zyParse the text into a container of items.

        :param str text: the text to parse
        :return: container
        zmust end with z, not "N"zcould not split the textr   zfound z extra items after terminal z, first is     zitem.=>   )limitz	expected z items, found )stripr,   endswithr-   r   r#   lensplit_itemsr"   r$   r/   listfilterindexr)   repr	enumerate
parse_itemappendr   friendly_joinr.   pack)r   r   Ztextsr%   contextr=   extrar*   itemserrorsiitemr   r   r   r   i   sR    
$

0

zContainerT.parsec                    s<     |}t fdd|D } jr2| j  |S )zmRender the container into text.

        :param container: container to render
        :return: text
        c                 3   s   | ]}  |V  qd S r   )render_item).0rH   r   r   r   	<genexpr>       z$ContainerT.render.<locals>.<genexpr>)unpackr;   r,   rA   
join_items)r   	containerrF   renderedr   rL   r   r      s
    
zContainerT.renderc                 C   s   | j |S )zrParse the text into a single item.

        :param str text: the text to parse
        :return: container
        r0   r   r   r   r   r   r@      s    zContainerT.parse_itemc                 C   s   | j |S )z^Render the item into text.

        :param item: item to render
        :return: text
        r0   r   )r   rI   r   r   r   rJ      s    zContainerT.render_itemc                 C   s   | j |S )zSplit the text into multiple items.

        :param str text: text to split
        :return: muliple items
        :rtype: list
        )r+   splitr   r   r   r   r:      s    zContainerT.split_itemsc                 C   s   | j |S )zJoin zero or more items into a single text.

        :param list items: items to join
        :return: joined text
        :rtype: str
        )r+   joinr   rF   r   r   r   rP      s    zContainerT.join_itemsc                 C   s
   t  dS )zPack the given items into a container.

        :param list items: items to pack
        :return: container with items in it
        NNotImplementedErrorrW   r   r   r   rC      s    zContainerT.packc                 C   s
   t  dS )zUnpack items from the container.

        :param container: container with items in it
        :return: items from container
        :rtype: list
        NrX   r   rQ   r   r   r   rO      s    zContainerT.unpack)r   r   r   r   r   r   r   r@   rJ   r:   rP   rC   rO   r   r   r   r   r   T   s   	@		r   c                   @   s    e Zd ZdZdd Zdd ZdS )r	   z Transformer for a list of items.c                 C   s   t |S r   r;   rW   r   r   r   rC      s    z
ListT.packc                 C   s   t |S r   r[   rZ   r   r   r   rO      s    zListT.unpackN)r   r   r   r   rC   rO   r   r   r   r   r	      s   r	   c                       sh   e Zd ZdZddd fdd
Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Z  ZS )r
   zTransformer for a key-value mapping of items.

    :param str kv_sep: separator between keys and values
    :param key: transformer for the keys
    :type key: :class:`~tsplib95.transformers.Transformer`
    N)kv_sepkeyc                   s.   t  jf i | |pt | _t|| _d S r   )r   r   r   key_tfr   r1   r\   )r   r\   r]   kwargsr    r   r   r      s    zMapT.__init__c                 C   sJ   z| j j|dd\}}W n ty4   tdY n0 | || |fS )Nr   )maxsplitzexpected key-value pair)r\   rU   r)   r   r#   	parse_keyparse_value)r   r   r]   r   r   r   r   r@     s
    zMapT.parse_itemc                 C   s,   |\}}|  |}| |}| j||gS r   )
render_keyrender_valuer\   rV   )r   rI   kvr]   r   r   r   r   rJ     s    

zMapT.render_itemc                 C   s   | j |S )zdParse the text into a key.

        :param str text: the text to parse
        :return: key
        )r^   r   r   r   r   r   ra     s    zMapT.parse_keyc                 C   s   | j |S )zsRender the key into text.

        :param key: the key to render
        :return: text
        :rtype: str
        )r^   r   )r   r]   r   r   r   rc      s    zMapT.render_keyc                 C   s   | j |S )zhParse the text into a value.

        :param str text: the text to parse
        :return: value
        rS   r   r   r   r   rb   )  s    zMapT.parse_valuec                 C   s   | j |S )zyRender the value into text.

        :param value: the value to render
        :return: text
        :rtype: str
        rT   r   r   r   r   rd   1  s    zMapT.render_valuec                 C   s   dd |D S )Nc                 S   s   i | ]\}}||qS r   r   )rK   re   rf   r   r   r   
<dictcomp><  rN   zMapT.pack.<locals>.<dictcomp>r   rW   r   r   r   rC   ;  s    z	MapT.packc                 C   s   |  S r   )rF   rZ   r   r   r   rO   >  s    zMapT.unpack)r   r   r   r   r   r@   rJ   ra   rc   rb   rd   rC   rO   r&   r   r   r    r   r
      s   
		
r
   c                       s,   e Zd Z fddZdd Zdd Z  ZS )r   c                    s   t  jf i | || _d S r   )r   r   transformers)r   Ztfsr_   r    r   r   r   C  s    zUnionT.__init__c                 C   s|   g }| j D ]V}z||W   S  ty^ } z(||jj dt|  W Y d }~q
d }~0 0 q
tdt	
| d S )Nr4   zUno transformer in the union could parse the text, resulting in the following errors: )rh   r   r"   rA   r!   r   r>   r   r#   r   rB   )r   r   rG   tfr%   r   r   r   r   G  s    
4zUnionT.parsec                 C   s|   g }| j D ]V}z||W   S  ty^ } z(||jj dt|  W Y d }~q
d }~0 0 q
tdt	
| d S )Nr4   zWno transformer in the union could render the value, resulting in the following errors: )rh   r   r"   rA   r!   r   r>   r   ZRenderingErrorr   rB   )r   r   rG   ri   r%   r   r   r   r   S  s    
4zUnionT.render)r   r   r   r   r   r   r&   r   r   r    r   r   B  s   r   N)r   r   r   r   __all__r   r   r   r   r	   r
   r   r   r   r   r   <module>   s    
J