
    M@cD                     P   d dl Z d dlZd dlZd dlZd dlZd dlmZ 	 d dlZd dlZd dlZdZ	ddlmZmZ  G d de      Z G d	 d
      Z G d d      Z G d d      Z ee      Z ee      Z ee      Zd Zd Zd Z G d de      Z G d de      Zd Zd Zd Zd Zd8dZd9dZ d Z!d:dZ"ejF                  dfdZ$ddejF                  fdZ%e&d k(  rejO                  d       d! Z(d"  e)e      D        D ]  \  Z*Z+ejF                  jY                  e+e*d#ejZ                  d$e+d$       d%  e)e      D        D ]f  \  Z.Z/ejF                  jY                  e/       d&  e)e      D        D ]4  \  Z0Z1ejF                  jY                  e1e0d$ e2ed'e0z        d$e+       6 h ejF                  jY                  d(ejf                  z          ejF                  jY                  d)        eejh                  d*z         5 Z5 e6d+      D ]  Z. ejn                  d,        e8e5        	 ddd        eejr                  d-z   ejZ                  z   d.      5 Z5 e6d+      D ]  Z. ejn                  d,        e8e5        	 ddd        ed/dd0      5 Z5 e6d+      D ]  Z. e8e5        ejn                  d,        	 ddd        ed1dd23      5 Z5 e6d+      D ]  Z. e8e5        ejn                  d,        	 ddd        e6d4      D  cg c]  } |  c} Z: e e;e:            5 Z< e=e:      D ]$  Z.e<j}                           ejn                  d,       & 	 ddd        e? ee:            D ]  Z. ejn                  d,        d Z@d5ZA eeA      5 Z<e@eAk  r.e@d6z  Z@e<j}                  e@        ejn                  d7       e@eAk  r.ddd       yy# e
$ r dZ	Y w xY w# 1 sw Y   xY w# 1 sw Y   ixY w# 1 sw Y   :xY w# 1 sw Y   xY wc c} w # 1 sw Y   xY w# 1 sw Y   yxY w);    N)UnionTF   )
formatSize
formatTimec                   b     e Zd ZdZdZd Zed        Z fdZd Z	e
d        Zed        Z xZS )		AnsiCodesFz[c                 T   d }t        |      D ]  }|j                  d      rt        ||      }t        | | ||             d|z  dfD ]Z  }t	        ||      st        ||      }t        | d|j                         z  t        j                   ||       ||                      y )Nc                 @    t         j                  t        |       z   dz   S )Nm)r   _CSIstr)codes    5/usr/lib/python3/dist-packages/eyed3/utils/console.pycode_to_charsz)AnsiCodes.__init__.<locals>.code_to_chars   s    >>CI-33    _RESET_%sRESETz%s)dir
startswithgetattrsetattrhasattrlowerr   _mkfunc)selfcodesr   namevalue
reset_namereset_values          r   __init__zAnsiCodes.__init__   s    	4 J 	D??3't,dM%$89 $.#4g"> Juj1&-eZ&@dTZZ\&9 ) 1 1-2F2?2L!NO 	r   c                       fd}|S )Nc                     t         j                  s| S d}|D ]  }||z  }	 || z   z   z  }|r|t        j                  z  }|S N )r   	_USE_ANSIStyle	RESET_ALL)textstylessstcolorresets       r   _cwrapz!AnsiCodes._mkfunc.<locals>._cwrap+   sW    &&A R%%AU__$Hr    )r.   r/   r0   s   `` r   r   zAnsiCodes._mkfunc)   s    
	 r   c                     t         t        |   |      }t        |d      r0|j	                  t        j
                        rt        j                  sy|S )Nr   r&   )superr   __getattribute__r   r   r   r'   )r   r   attr	__class__s      r   r4   zAnsiCodes.__getattribute__8   s?    Y6t<D,'	/''Kr   c                 6    t        | |j                               S N)r   upper)r   r   s     r   __getitem__zAnsiCodes.__getitem__A   s    tTZZ\**r   c                 6    |xr | j                         | _        y r8   )_term_supports_colorr'   )clsallow_colorss     r   initzAnsiCodes.initD   s    $C)A)A)Cr   c                      t         j                  j                  d      dk(  s"t         j                  j                  d      dk(  ryt        t        j
                  d      xr t        j
                  j                         S )NTERMdumbOS
Windows_NTFisatty)osenvirongetr   sysstdoutrE   r1   r   r   r<   zAnsiCodes._term_supports_colorH   sR    JJNN6"f,

t$4szz8,D1B1B1DDr   )__name__
__module____qualname__r'   r   r"   staticmethodr   r4   r:   classmethodr?   r<   __classcell__)r6   s   @r   r   r      s\    ID$  + D D E Er   r   c                   0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)AnsiFore          !   "   #   $   %   '   NrK   rL   rM   GREYREDGREENYELLOWBLUEMAGENTACYANWHITEr   r1   r   r   rR   rR   P   /    DCEFDGDEEr   rR   c                   0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)AnsiBack(   )   *   +   ,   -   .   /   1   Nr\   r1   r   r   rg   rg   \   re   r   rg   c                   P    e Zd ZdZdZdZdZeZdZdZ	dZ
dZd	Zd
ZdZdZdZdZdZdZy)	AnsiStyler   r                                       	      N)rK   rL   rM   r)   BRIGHTRESET_BRIGHTDIM	RESET_DIMITALICSRESET_ITALICS	UNDERLINERESET_UNDERLINE
BLINK_SLOWRESET_BLINK_SLOW
BLINK_FASTRESET_BLINK_FASTINVERSERESET_INVERSESTRIKE_THRURESET_STRIKE_THRUr1   r   r   rr   rr   h   sZ    IFLC$IGMIOJJGMKr   rr   c                  "    t         j                  S r8   )Forer^   r1   r   r   ERROR_COLORr      s    88Or   c                  "    t         j                  S r8   )r   r`   r1   r   r   WARNING_COLORr      s    ;;r   c                  "    t         j                  S r8   )r   r_   r1   r   r   HEADER_COLORr      s    ::r   c                   <    e Zd ZdZdZdZ	 	 d
dZd Zd Zd Z	d	 Z
y)Spinnerz
    A class to display a spinner in the terminal.

    It is designed to be used with the `with` statement::

        with Spinner("Reticulating splines", "green") as s:
            for item in enumerate(items):
                s.next()
    u   ◓◑◒◐z-/|\Nc                     || _         |xs t        j                  | _        || _        |s|r| j
                  }n| j                  }|| _        | j                  j                          | _	        || _
        y r8   )_msgrI   rJ   _file_step_default_unicode_chars_default_ascii_chars_charsrE   _silent_print_done)r   msgfilestepcharsuse_unicode
print_dones          r   r"   zSpinner.__init__   sd     	'SZZ

3311::,,..%r   c              #   X  K   | j                   }d}| j                  j                  }| j                  j                  }	  |d        || j                          |d        |||           |        d  t        | j                        D ]  }d   |dz  }|t        |      k(  rd}lw)Nr    r   )r   r   writeflushr   ranger   len)r   r   indexr   r   r   s         r   	_iteratorzSpinner._iterator   s     

  

  $K$))#J%,G4::&  QJEE
" s   B(B*c                 Z    | j                   r| j                         S | j                         S r8   )r   _silent_iteratorr   r   s    r   	__enter__zSpinner.__enter__   s%    <<((**>>##r   c                 J   | j                   j                  }| j                   j                  }| j                  s |d        || j                         | j
                  r6| |t        j                  dz          n" |t        j                  dz          n |d        |        y )Nr   z [Done]
z
 [Failed]

)	r   r   r   r   r   r   r   r_   r^   )r   exc_type	exc_value	tracebackr   r   s         r   __exit__zSpinner.__exit__   sw    

  

  ||$K$))djj;./dhh./$Kr   c              #      K   | j                   j                  | j                         | j                   j                          	 d  wr8   )r   r   r   r   r   s    r   r   zSpinner._silent_iterator   s7     

#

 s   AA)Nr   NTT)rK   rL   rM   __doc__r   r   r"   r   r   r   r   r1   r   r   r   r      s4     ,",-:>& *$ r   r   c                       e Zd ZdZddeeej                  f   fdZddZ	d Z
d Zd Zd	 Zdd
Zd ZddZedd       Zy)ProgressBaraP  
    A class to display a progress bar in the terminal.

    It is designed to be used either with the `with` statement::

        with ProgressBar(len(items)) as bar:
            for item in enumerate(items):
                bar.update()

    or as a generator::

        for item in ProgressBar(items):
            item.process()
    Ntotal_or_itemsc                    |xs t         j                  | _        | j                  j                         s| j                  | _        d| _        nd| _        	 t        |      | _        t        |      | _
        t        j                         | _        t         xr | j                  j                         | _        | j%                          | j"                  r6t'        j&                  t&        j(                  | j$                         d| _        nd| _        | j                  d       y# t        $ rQ 	 t        |      | _
        t        t        | j                              | _        n# t        $ r t        d      w xY wY w xY w)a  
        total_or_items : int or sequence
            If an int, the number of increments in the process being
            tracked.  If a sequence, the items to iterate over.

        file : writable file-like object, optional
            The file to write the progress bar to.  Defaults to
            `sys.stdout`.  If `file` is not a tty (as determined by
            calling its `isatty` member, if any), the scrollbar will
            be completely silent.
        TFz&First argument must be int or sequencer   N)rI   rJ   r   rE   _silent_updateupdater   iter_itemsr   _total	TypeErrorintr   time_start_time_CAN_RESIZE_TERMINAL_should_handle_resize_handle_resizesignalSIGWINCH_signal_set)r   r   r   s      r   r"   zProgressBar.__init__   s0    'SZZ
zz  "--DKDL DL	J~.DKn-DK  99; !8TZZ%6%6%8 	"%%MM&//4+>+>?#D$DA%  	JJ!.1"5#56 J HIIJ 	Js*    D   	E:*3EE:E33E:9E:c                 T    t        | j                  | j                        d   | _        y )Nr   )
getTtySizer   r   _terminal_width)r   signumframes      r   r   zProgressBar._handle_resize  s(    )$***.*D*D FFG Ir   c                     | S r8   r1   r   s    r   r   zProgressBar.__enter__      r   c                 @   | j                   s|| j                  | j                         | j                  j	                  d       | j                  j                          | j                  r3t        j                  t        j                  t        j                         y y y Nr   )
r   r   r   r   r   r   r   r   r   SIG_DFL)r   r   r   r   s       r   r   zProgressBar.__exit__  sj    ||DKK(JJT"JJfoov~~>   r   c                     | S r8   r1   r   s    r   __iter__zProgressBar.__iter__'  r   r   c                     	 t        | j                        }| j                          |S # t        $ r | j	                  d d d         w xY wr8   )nextr   r   StopIterationr   )r   rvs     r   __next__zProgressBar.__next__*  sI    	dkk"B
 KKMI  	MM$d+	s	   ) Ac                    || j                   dz   x}| _         n|| _         | j                  dk(  rd}n!t        |      t        | j                        z  }| j                  }|j                  }| j                  ||      }| j                  dz
  | _        t        t        | j                        |z        } |d        |t        j                  d|z  z   t        j                  z          || j                  k  rE |t        j                  dz   t        j                  z           |d	| j                  |z
  dz
  z          |d
        ||       | j                  j                          y)zq
        Update the progress bar to the given value (out of the total
        given to the constructor).
        Nr   r         ?rZ   z|=>-|)_current_valuer   floatr   r   _formatSuffixr   _bar_lengthr   r   ra   r   r_   r   )r   r   fracr   r   suffixbar_fills          r   r   zProgressBar.update4  s*   
 =*.*=*=*AAED'"'D;;!D<%"44Dzz

##E40//"4uT--.56edii#.(4::56d&&&$**s"TZZ/0#))H4q89:c
f

r   c                    || j                   k\  r$t        j                         | j                  z
  }d}n6|dk  rd }d}n,t        j                         | j                  z
  d|z
  z  |z  }d}||t        |d      z  }dj	                  t        |d      t        | j                   d            }|d	j	                  d
j	                  |dz              z  }||z  }|S )Nz     r   r&   r   z ETA T)shortz {0:>4s}/{1:>4s}z ({0:>6s}%)z{0:.2f}g      Y@)r   r   r   r   formatr   )r   r   r   ttime_strr   s         r   r   zProgressBar._formatSuffixS  s    DKK		d...AHaZAH))+ 0 00S4Z@DHAH=
1D11H#**:e4+H+5dkk+NP-&&y'7'7u'EFF(r   c                      y r8   r1   )r   r   s     r   r   zProgressBar._silent_updateh  s    r   c           
      $   g }|t         j                  } | t        |      |      5 }t        d|j                        }t        t        t        t        |            |z        d      }|s6t        |      D ]'  \  }	}
 ||
       |	|z  dk(  s|j                  |	       ) nZddl	}|j                         }t        |j                  |||            D ]'  \  }	}|j                  |	       |j                  |       ) ddd       |S # 1 sw Y   |S xY w)as  
        Does a `map` operation while displaying a progress bar with
        percentage complete.

        ::

            def work(i):
                print(i)

            ProgressBar.map(work, range(50))

        Parameters:

        function : function
            Function to call for each step

        items : sequence
            Sequence where each element is a tuple of arguments to pass to
            *function*.

        multiprocess : bool, optional
            If `True`, use the `multiprocessing` module to distribute each
            task to a different processor core.

        file : writeable file-like object, optional
            The file to write the progress bar to.  Defaults to
            `sys.stdout`.  If `file` is not a tty (as determined by
            calling its `isatty` member, if any), the scrollbar will
            be completely silent.
        Nr      r   r   )rI   rJ   r   maxr   r   r   	enumerater   multiprocessingPoolimap_unorderedappend)r=   functionitemsmultiprocessr   resultsbar	step_sizestepsiitemr   presults                 r   mapzProgressBar.mapk  s   @ <::DU$' 	+3C1IE#e*-	9:A>E(/ &GAtTNE	a'

1&
 '#((*!*1+;+;He<A,C "D +IAvJJqMNN6*+	+  !	+  s   A$DA.DDr8   NNFN)rK   rL   rM   r   r   r   typingSequencer"   r   r   r   r   r   r   r   r   rO   r	  r1   r   r   r   r      s_    )uS&//-A'B )VI?>* 4 4r   r   c           
         t         j                  }t        | t              sJ 	 |j	                  d| z         |j                          y # t
        $ r1 |j	                  dt        | j                  dd      d      z         Y Jw xY w)N%s
zutf-8replace)rI   rJ   
isinstancer   r   UnicodeEncodeErrorencoder   )r,   fps     r   printMsgr    sr    	BaF
! HHJ  F
#ahhw	:GDDEFs   A	 	7BBc                 J    t        | t               t        j                         y r8   )_printWithColorr   rI   stderrr,   s    r   
printErrorr    s    A{}cjj1r   c                 J    t        | t               t        j                         y r8   )r  r   rI   rJ   r  s    r   printWarningr    s    A}

3r   c                 J    t        | t               t        j                         y r8   )r  r   rI   rJ   r  s    r   printHeaderr    s    A|~szz2r   c                     t        | d|      S )NT)bc)
formatText)r,   r!  s     r   boldTextr#    s    a41%%r   c                     |rt         j                  nd|xs dz   | z   |rt        j                  ndz   |rt         j                  z   S dz   S r%   )r(   r   r   r   r   )r,   r   r!  s      r   r"  r"    sY    U\\BW" TZZ"& $%U	. / +-	. /r   c                     t        | t              sJ |j                  || z   t        j                  z   dz          |j                          y r   )r  r   r   r   r   r   )r,   r.   r   s      r   r  r    s9    aJJuqy4::%,-JJLr   c                     |xs d}|xs d}dj                  |xs g       }|s|s|r2t        j                  t        j                  z   t        j
                  z   nd}dt               z  }|S )zFormat ``msg`` with foreground and optional background. Optional
    ``styles`` lists will also be applied. The formatted string is returned.r&   z&%(fg)s%(bg)s%(styles)s%(msg)s%(reset)s)joinr   r   Backr(   r)   locals)r   fgbgr+   r/   outputs         r   cformatr-    sb     
rB	rBWWV\r"F:<fDJJ#eoo5;= 
 6@FMr   c                    d }|r<	 t        j                  | t        j                  d      }t	        j
                  d|      }|sS	 t        t        j                  j                  d            t        t        j                  j                  d            f}|S |S # t        t        f$ r Y hw xY w# t        t        f$ r d}Y |S w xY w)Nz    hhLINESCOLUMNS)N   rz   )fcntlioctltermios
TIOCGWINSZstructunpackOSError	NameErrorr   rF   rG   rH   r   
ValueError)fd	check_ttyhwdatas       r   r   r     s    	B	;;r7#5#5x@DtT*B 	bjjnnW-.bjjnnY/02B I2I # 		 :& 	BI	s$   ;B AB- B*)B*-CCc                 8    t        t        | |||      |       y)zBCalls ``cformat`` and prints the result to output stream ``file``.)r+  r+   r   N)printr-  )r   r*  r+  r+   r   s        r   cprintrB    s    	'#rb
0t<r   __main__c                 T    | d   dk7  xr d| vxr | d   | d   j                         k(  S )Nr   r   r   )r9   )r!  s    r   	checkCoderE    s:    ! %q %!!

$	r   c              #   V   K   | ]!  }t        |      r|t        t        |      f # y wr8   )rE  r   r(  .0r!  s     r   	<genexpr>rI    s,      A!"9Q<  q!12 A   ')7r   c              #   V   K   | ]!  }t        |      r|t        t        |      f # y wr8   )rE  r   r   rG  s     r   rI  rI    s,      E%&y| wtQ/0 ErJ  c              #   V   K   | ]!  }t        |      r|t        t        |      f # y wr8   )rE  r   r(   rG  s     r   rI  rI    s,      %J)*IaL '():%; %JrJ  r   r  r   zPhase #12   g?zPhase #2)r   zPhase #3)r   r   zPhase #4u   .oO°Oo.)r   r   r   i ih[  gMbP?r8   r  r
  )BrF   r7  rI   r   r  r   r3  r5  r   r   ImportErrorr&   r   r   objectr   rR   rg   rr   r   r(  r(   r   r   r   r   r   r  r  r  r  r#  r"  r  r-  rJ   r   rB  rK   r?   rE  r   bg_namebg_coder   r   r   fg_codest_namest_coder   r)   r_   spinnerr   sleepr   r^   r   r   r  r   r   r   progressmax_val)xs   0r   <module>r[     sl   	  
   ! %:E :Ez	 		 	 ( )Mf M`C& CL243&/
 ** " Dszz =
 zNN4A&)$iA 3

!7DJJA 	BE*-d)E 	,JAwJJW%%J.1%j%J , 

  ")7")%g1E"F")"+ ,,	, 	

%//123 JJT	j(	) Wr 	ADJJsOM	 
J&3!
# &-r 	ADJJsOM	
 
5	A Wr 	AMDJJsO	 
Z	@ Gr 	AMDJJsO	
 c
#1Q#E	SZ	  C5! 	AJJLDJJsO	
 +e$% 

3 HG	W	  HJJx DJJt   o C  ! !v  
  
 $  s_   O	 ?(O(O$(O1(O>	P!0P3P	OOO!$O.1O;>PPP%