ó
¦>dc           @   sï   d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z y0 d d l m	 Z	 d d l
 m
 Z
 d d l Z Wn e k
 r• n Xd d l Td d l m Z m Z m Z m Z d	 d d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   s   Main ProgressBar class.iÿÿÿÿ(   t   divisionN(   t   ioctl(   t   arrayi   (   t   *(   t   Bart
   Percentaget   WidgetHFillt   format_updatablet   UnknownLengthc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyR   *   s    t   ProgressBarc           B   sã   e  Z d  Z d$ Z d Z d Z e ƒ  d e ƒ  g Z d% d% d% d e
 e j d „ Z d „  Z d „  Z d „  Z e Z d „  Z d% d% d „ Z d „  Z e e ƒ Z d „  Z d „  Z d „  Z d  „  Z d% d! „ Z d" „  Z d# „  Z RS(&   sÌ  The ProgressBar class which updates and prints the bar.

    A common way of using it is like:
    >>> pbar = ProgressBar().start()
    >>> for i in range(100):
    ...    # do something
    ...    pbar.update(i+1)
    ...
    >>> pbar.finish()

    You can also use a ProgressBar as an iterator:
    >>> progress = ProgressBar()
    >>> for i in progress(some_iterable):
    ...    # do something
    ...

    Since the progress bar is incredibly customizable you can specify
    different widgets of any type in any order. You can even write your own
    widgets! However, since there are already a good number of widgets you
    should probably play around with them before moving on to create your own
    widgets.

    The term_width parameter represents the current terminal width. If the
    parameter is set to an integer then the progress bar will use that,
    otherwise it will attempt to determine the terminal width falling back to
    80 columns if the width cannot be determined.

    When implementing a widget's update method you are passed a reference to
    the current progress bar. As a result, you have access to the
    ProgressBar's methods and attributes. Although there is nothing preventing
    you from changing the ProgressBar you should treat it as read only.

    Useful methods and attributes include (Public API):
     - currval: current progress (0 <= currval <= maxval)
     - maxval: maximum (and final) value
     - finished: True if the bar has finished (reached 100%)
     - start_time: the time when start() method of ProgressBar was called
     - seconds_elapsed: seconds elapsed since start_time and last call to
                        update
     - percentage(): progress in percent [0..100]
    t   currvalt   fdt   finishedt   last_update_timet   left_justifyt   maxvalt   next_updatet   num_intervalst   pollt   seconds_elapsedt
   signal_sett
   start_timet
   term_widtht   update_intervalt   widgetst   _time_sensitivet
   __iterableid   iP   t    i   c         C   s!  | d k r t |  j ƒ } n  | |  _ | |  _ | |  _ | |  _ t |  _ | d k	 rc | |  _	 n_ y- |  j
 ƒ  t j t j |  j
 ƒ t |  _ Wn/ t t f k
 r¬ ‚  n |  j ƒ  |  _	 n Xd |  _ |  j ƒ  d |  _ t |  _ d |  _ | |  _ d |  _ d |  _ d |  _ d |  _ d S(   s.   Initializes a progress bar with sane defaults.i    i   N(   t   Nonet   listt   _DEFAULT_WIDGETSR   R   R   R   t   FalseR   R   t   _handle_resizet   signalt   SIGWINCHt   Truet
   SystemExitt   KeyboardInterruptt	   _env_sizet   _ProgressBar__iterablet   _update_widgetsR   R   R   R   R   R   R   R   (   t   selfR   R   R   R   R   R   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt   __init__b   s6    					
 	
							c         C   sK   y t  | ƒ |  _ Wn" |  j d k r8 t |  _ q8 n Xt | ƒ |  _ |  S(   s1   Use a ProgressBar to iterate through an iterable.N(   t   lenR   R   R   t   iterR)   (   R+   t   iterable(    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt   __call__‡   s    c         C   s   |  S(   N(    (   R+   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt   __iter__”   s    c         C   sˆ   yG t  |  j ƒ } |  j d  k r. |  j ƒ  n |  j |  j d ƒ | SWn: t k
 rƒ |  j d  k rs |  j ƒ  n  |  j ƒ  ‚  n Xd  S(   Ni   (	   t   nextR)   R   R   t   startt   updateR   t   StopIterationt   finish(   R+   t   value(    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt   __next__˜   s    
c         C   s    t  t j j d |  j ƒ ƒ d S(   s2   Tries to find the term_width from the environment.t   COLUMNSi   (   t   intt   ost   environt   gett   _DEFAULT_TERMSIZE(   R+   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyR(   ¬   s    c         C   s8   t  d t |  j t j d ƒ ƒ d  \ } } | |  _ d S(   s5   Tries to catch resize signals sent from the terminal.t   hs    i   i   Ns           (   R   R   R   t   termiost
   TIOCGWINSZR   (   R+   t   signumt   frameR?   t   w(    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyR"   ²   s    +c         C   s(   |  j  |  j k r d S|  j  d |  j S(   s%   Returns the progress as a percentage.g      Y@(   R   R   (   R+   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt
   percentage¹   s    c         C   s  g  } g  } |  j  } xx t |  j ƒ D]g \ } } t | t ƒ r` | j | ƒ | j d | ƒ q% t | |  ƒ } | j | ƒ | t | ƒ 8} q% Wt | ƒ } xv | rt	 t
 t j | d | ƒ ƒ d ƒ } | j ƒ  } | d 8} | | j |  | ƒ } | t | ƒ 8} | | | <qŸ W| S(   Ni    g      ð?i   (   R   t	   enumerateR   t
   isinstanceR   t   appendt   insertR   R-   t   maxR:   t   matht   ceilt   popR4   (   R+   t   resultt	   expandingt   widtht   indext   widgett   countt   portion(    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt   _format_widgetsÂ   s&    		&
c         C   sB   d j  |  j ƒ  ƒ } |  j r. | j |  j ƒ S| j |  j ƒ Sd S(   s)   Joins the widgets and justifies the line.t    N(   t   joinRU   R   t   ljustR   t   rjust(   R+   R   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt   _format_lineÝ   s    	 c         C   sH   |  j  |  j k s |  j r t St j ƒ  |  j } |  j oG | |  j k S(   s7   Returns whether the ProgressBar should redraw the line.(   R   R   R   R%   t   timeR   R   R   (   R+   t   delta(    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt   _need_updateæ   s     c         C   s    t  d „  |  j Dƒ ƒ |  _ d S(   s.   Checks all widgets for the time sensitive bit.c         s   s!   |  ] } t  | d  t ƒ Vq d S(   t   TIME_SENSITIVEN(   t   getattrR!   (   t   .0RD   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pys	   <genexpr>ñ   s   N(   t   anyR   R   (   R+   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyR*   î   s    	c         C   sæ   | d k	 rb | t k	 rb |  j t k	 rV d | k oA |  j k n rV t d ƒ ‚ n  | |  _ n  |  j ƒ  sr d S|  j d k r t d ƒ ‚ n  t j ƒ  } | |  j |  _	 |  j |  j
 |  _ |  j j |  j ƒ  d ƒ | |  _ d S(   s'   Updates the ProgressBar to a new value.i    s   Value out of rangeNs-   You must call "start" before calling "update"s   (   R   R   R   t
   ValueErrorR   R]   R   t   RuntimeErrorR[   R   R   R   R   t   writeRZ   R   (   R+   R7   t   now(    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyR4   õ   s      c         C   s¦   |  j  d k r |  j |  _  n  t d |  j ƒ |  _ d |  _ |  j  t k	 r |  j  d k  ri t d ƒ ‚ n  |  j  |  j |  _	 n  t
 j
 ƒ  |  _ |  _ |  j d ƒ |  S(   s  Starts measuring time, and prints the bar at 0%.

        It returns self so you can use it like this:
        >>> pbar = ProgressBar().start()
        >>> for i in range(100):
        ...    # do something
        ...    pbar.update(i+1)
        ...
        >>> pbar.finish()
        id   i    s   Value out of rangeN(   R   R   t   _DEFAULT_MAXVALRJ   R   R   R   R   Rb   R   R[   R   R   R4   (   R+   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyR3     s    	 c         C   s\   |  j  r d St |  _  |  j |  j ƒ |  j j d ƒ |  j rX t j t j t j	 ƒ n  d S(   s/   Puts the ProgressBar bar in the finished state.Ns   
(
   R   R%   R4   R   R   Rd   R   R#   R$   t   SIG_DFL(   R+   (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyR6   )  s    			(   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   N(   R	   R
   t   __doc__t	   __slots__Rf   R>   R   R   R    R   R%   t   syst   stderrR,   R0   R1   R8   R2   R(   R"   RE   t   propertyt   percentRU   RZ   R]   R*   R4   R3   R6   (    (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyR   -   s4   )    $											(    (   Rh   t
   __future__R    RK   R;   R#   Rj   R[   t   fcntlR   R   R@   t   ImportErrort   compatR   R   R   R   R   R   t   objectR   (    (    (    s@   /tmp/pip-install-m8tpmx/progressbar33/progressbar/progressbar.pyt   <module>   s    
"