o
    Rŀg                     @   sd   d Z ddlZdddZdd Zdd	 ZdddZdddZdddZdddZdddZ	dd Z
dS )z"Common SearchIO utility functions.    N?c                 C   s>   zt | |}W n ty   | Y S w |du rt|S || S )zzReturn string of the given object's attribute.

    Defaults to the given fallback value if attribute is not present.
    N)getattrAttributeErrorstr)objattrfmtfallbackvalue r   G/var/www/html/myenv/lib/python3.10/site-packages/Bio/SearchIO/_utils.pygetattr_str   s   r   c                 C   s    	 |   }|r|r| r|S q)z?Read through whitespaces, return the first non-whitespace line.)readlinestrip)handleliner   r   r   read_forward   s
   r   c              	   C   s   z||  }W n3 t y9   | du rtddt| ts tdd| |  kr-td|  dtd| d|f dw |\}}td| dgd	}t||S )
a  Return the object to process the given format according to the mapping.

    :param format: format name
    :type format: string, lower case
    :param mapping: mapping of format name and its processor object
    :type mapping: dictionary {string: object}

    Nz#Format required (lower case string)z.Need a string for the file format (lower case)z%Format string %r should be lower casez+Unknown format %r. Supported formats are %rz', 'zBio.SearchIO.%s )fromlist)	KeyError
ValueError
isinstancer   	TypeErrorlowerjoin
__import__r   )formatmappingobj_infomod_nameobj_namemodr   r   r   get_processor#   s(   




r"   r   c                        fdd}t ||dS )zProperty for fetching attribute from first entry of container.

    Returns a property that fetches the given attribute from
    the first item in a SearchIO container object.
    c                    s8   t | jdkrtd d u r| jd S t| jd  S )N   z.More than one HSPFragment objects found in HSPr   )len_itemsr   r   selfr   r   r   getterJ   s
   
zsingleitem.<locals>.getterfgetdocpropertyr   r-   r*   r   r)   r   
singleitemC   s   r1   c                    r#   )zProperty for fetching attribute from all entries of container.

    Returns a property that fetches the given attributes from
    all items in a SearchIO container object.
    c                    s"    d u r| j S  fdd| j D S )Nc                    s   g | ]}t | qS r   r   ).0fragr)   r   r   
<listcomp>^   s    z,allitems.<locals>.getter.<locals>.<listcomp>)r&   r'   r)   r   r   r*   [   s   zallitems.<locals>.getterr+   r.   r0   r   r)   r   allitemsT   s   r6   c                    s&    fdd} fdd}t |||dS )a  Return a getter property with a cascading setter.

    This is similar to ``optionalcascade``, but for SearchIO containers that have
    at least one item (HSP). The getter always retrieves the attribute
    value from the first item. If the items have more than one attribute values,
    an error will be raised. The setter behaves like ``partialcascade``, except
    that it only sets attributes to items in the object, not the object itself.

    c                    s   t | jd  S Nr   )r   r&   r'   r)   r   r   r*   n   s   zfullcascade.<locals>.getterc                    s   | D ]}t | | qd S Nsetattrr(   r
   itemr)   r   r   setterq   s   zfullcascade.<locals>.setterr,   fsetr-   r.   )r   r-   r*   r=   r   r)   r   fullcascadec   s   r@   c                    s*    fdd} fdd}t |||dS )aA  Return a getter property with a cascading setter.

    This is used for the ``id`` and ``description`` properties of the container
    objects with zero or more items. These items have their own private
    attributes that stores query and/or hit ID and description. When the
    container has zero items, attribute values are always retrieved from the
    container's attribute. Otherwise, the first item's attribute is used.

    To keep the container items' query and/or hit ID and description in-sync,
    the setter cascades any new value given to the items' values.

    c                    s   | j r
t| d S t|  S r7   )r&   r   r'   	cont_attr	item_attrr   r   r*      s   
zoptionalcascade.<locals>.getterc                    s&   t |  | | D ]}t || qd S r8   r9   r;   rA   r   r   r=      s   zoptionalcascade.<locals>.setterr>   r.   )rB   rC   r-   r*   r=   r   rA   r   optionalcascadex   s   rD   c                    sF   dv sJ d d  fdd} fdd}t |||dS )zReturn a getter property with cascading setter, for HSPFragment objects.

    Similar to ``partialcascade``, but for HSPFragment objects and acts on ``query``
    or ``hit`` properties of the object if they are not None.

    )hitquery_c                    s
   t |  S r8   r2   r'   )	attr_namer   r   r*      s   
zfragcascade.<locals>.getterc                    s2   t | | t| }|d urt | | d S d S r8   )r:   r   )r(   r
   seqr   rH   seq_typer   r   r=      s
   
zfragcascade.<locals>.setterr>   r.   )r   rK   r-   r*   r=   r   rJ   r   fragcascade   s
   rL   c                 C   sJ   t j^}}}|dkr |dkr |r| |r| dt|  S | S | |S )z0Remove the suffix from the string, if it exists.      N)sysversion_infoendswithr%   removesuffix)stringsuffixmajorminorrG   r   r   r   rR      s   
rR   )Nr   )Nr   )r   )__doc__rO   r   r   r"   r1   r6   r@   rD   rL   rR   r   r   r   r   <module>   s   


 



