
:>"^                 @   s   d  Z  d d l m Z d d l Z d d   Z d d   Z d d	   Z d
 d   Z d d d d  Z d d d  Z	 d d d  Z
 e d k r e
   d S)z4Common utility functions for various Bio submodules.    )print_functionNc             C   sR   y t  |   SWn= t k
 rM x  t |   D] \ } } | } q+ W| d SYn Xd S)a`  Count the number of items in an iterable.

    If the argument supports len(items), and some iterators do, then
    this returns len(items). Otherwise it will scan over the entries
    in order to count them.

    Exhausts a generator, but doesn't require creating a full list.

    >>> iterlen("abcde")
    5
    >>> iterlen(iter("abcde"))
    5

       N)len	TypeError	enumerate)itemsixcount r   //tmp/pip-build-ww9dw3qa/biopython/Bio/_utils.pyiterlen   s    
r   c             C   s4   x- |  j    } | s( | r | j   r | Sq Wd S)z?Read through whitespaces, return the first non-whitespace line.N)readlinestrip)handleliner   r   r   read_forward'   s    r   c             C   sG   |  j  |  } | s! t d   t |  | k  rC t d |   | S)aZ  Read the specified number of characters from the given handle.

    Raise a ValueError("Empty file.") if the length of data read is zero. The
    reason for having a separate function for the header is it enables raising
    an empty file error if the length of the data read is zero. This might
    not always be the case later in the file.
    zEmpty file.z1Improper header, cannot read %d bytes from handle)read
ValueErrorr   )r   lengthdatar   r   r   _read_header1   s    r   c             C   s2   t  |   | k r. |  d | t  |   | S|  S)z&Truncate the given string for display.N)r   )stringmax_lenZconcat_charr   r   r   trim_strA   s    r   ?c             C   sC   t  |  |  r? | d k	 r, | t |  |  St t |  |   S| S)zzReturn string of the given object's attribute.

    Defaults to the given fallback value if attribute is not present.
    N)hasattrgetattrstr)objattrfmtfallbackr   r   r   getattr_strH   s
    r#   c             C   s   |  s d }  t  j j |   } x t  j j t  j j | d   r t  j j t  j j | d   r t  j j t  j j | d   St  j j |  \ } } | | k r P| } q! Wt d t  j j |     d S)a   Find the absolute path of Biopython's Tests directory.

    Arguments:
    start_dir -- Initial directory to begin lookup (default to current dir)

    If the directory is not found up the filesystem's root directory, an
    exception will be raised.

    .ZBioZTestsz$Not within Biopython source tree: %rN)ospathabspathisdirjoinsplitr   )	start_dirtargetnewtmpr   r   r   find_test_dirT   s    
!!
r/   c             O   s   d d l  } d | j i } | j |  t j j t j  } t d  z' t j t	 |    | j
 | |   Wd t j |  Xt d  d S)z%Run doctest for the importing module.r   NZoptionflagszRunning doctests...Done)doctestELLIPSISupdater%   r&   r'   curdirprintchdirr/   testmod)
target_dirargskwargsr1   Zdefault_kwargsZcur_dirr   r   r   run_doctestu   s    
r;   __main__)__doc__
__future__r   r%   r   r   r   r   r#   r/   r;   __name__r   r   r   r   <module>   s   
!