
    IR-e                        d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
	 ddlZddlZddlZdZn# e$ r dZY nw xY wddlmZ ddlmZ ddlmZ g d	Zd
Z G d d          Zd,dZd Zd-dZd Zd Zd ZdddZd Z d Z!d Z" G d d          Z# G d d          Z$ G d d          Z% G d d           Z&d.d#Z' G d$ d%          Z( G d& d'          Z) G d( d)          Z* G d* d+          Z+dS )/z)
Utilities for console input and output.
    NTF)conf   )classproperty)
isiterable)	isattycolor_print
human_timehuman_file_sizeProgressBarSpinnerprint_code_lineProgressBarOrSpinnerterminal_sizezutf-8c                   j    e Zd ZdZed             Zed             Zed             Zed             Z	dS )_IPythonz5Singleton class given access to IPython streams, etc.c                 6    	 ddl m} n# t          $ r Y nw xY w|S )Nr   )get_ipython)IPythonr   ImportError)clsr   s     5lib/python3.11/site-packages/astropy/utils/console.pyr   z_IPython.get_ipython4   sB    	+++++++ 	 	 	D	s   	 
c                 \   t          | d          sd | _        	 |                                  n# t          $ r Y d S w xY w	 ddlm} nY# t          $ rL 	 ddlm} nA# t          $ r4 ddlm	} |d         dk    rY Y d S 	 ddl
m} n# t          $ r Y Y Y d S w xY wY nw xY wY nw xY w|| _        | j        S )N
_OutStreamr   )	OutStreamversion_info   )hasattrr   r   	NameErroripykernel.iostreamr   r   IPython.zmq.iostreamr   r   IPython.kernel.zmq.iostream)r   r   r   s      r   r   z_IPython.OutStream<   sE   sL)) 	'!CN!!!!   tt$8888888 $ $ $$>>>>>>>" 	$ 	$ 	$444444#A!++#ttt$IIIIIII& $ $ $#tttt$ JI	$$ 'CN~sr   . 
<< A 
BABB5B:B B
BBBBBBBBBc                 |    t          | d          s&	 ddlm} || _        n# t          $ r
 d | _        Y nw xY w| j        S )N_ipyior   )io)r   IPython.utilsr%   r$   r   )r   r%   s     r   ipyioz_IPython.ipyioY   sh    sH%% 	  ,,,,,,  

  " " "!


" zs     44c                 ,    t          | j        |          S N)getattrr'   )r   streams     r   
get_streamz_IPython.get_streamd   s    sy&)))    N)
__name__
__module____qualname____doc__r   r   r   r'   classmethodr,    r-   r   r   r   1   s        ??  ]   ]8   ] * * [* * *r-   r   c                 >    | rd}nd}t          t          |          }|S )a_  
    This utility function contains the logic to determine what streams to use
    by default for standard out/err.

    Typically this will just return `sys.stdout`, but it contains additional
    logic for use in IPython on Windows to determine the correct stream to use
    (usually ``IPython.util.io.stdout`` but only if sys.stdout is a TTY).
    stderrstdout)r*   sys)r5   r+   
sys_streams      r   _get_stdoutr9   i   s-      f%%Jr-   c                    t          j                    j        dk    st          j                    j        dk    rdS t          | d          r|                                 S t          j        t          | t          j                  sdS t          | dd          dk    rdS t          | d	          r.	 d
dlm} n# t          $ r Y dS w xY wt          | j        |          S dS )z
    Returns `True` if ``file`` is a tty.

    Most built-in Python file-like objects have an `isatty` member,
    but some user-defined types may not, so this assumes those are not
    ttys.
    MainProcess
MainThreadFr   Nnamer6   Tr+   r   )Console)multiprocessingcurrent_processr=   	threadingcurrent_threadr   r   r   r   
isinstancer*   pyreadline.consoler>   r   r+   )filePyreadlineConsoles     r   r   r   {   s     	')).-??#%%*l::utX {{}}!*T8;M*N*N!u
 tVT""h..ttX :	GGGGGGG 	 	 	55	 $+'89995s   /B6 6
CCc                 p   | t                      } 	 t          j        ddddd          }t          j        | t
          j        |          }t          j        d|          \  }}}}|dk    r|dz  }|dk    r|dz  }|dk    s|dk    rt          d          ||fS # t          $ r 	 t          t          j                            d	                    t          t          j                            d
                    fcY S # t          $ r) t          j        }t          j        }|d}|d}||fcY cY S w xY ww xY w)z
    Returns a tuple (height, width) containing the height and width of
    the terminal.

    This function will look for the width in height in multiple areas
    before falling back on the width and height in astropy's
    configuration.
    NHHHHr         
   r   zunable to get terminal sizeLINESCOLUMNS   P   )r9   structpackfcntlioctltermios
TIOCGWINSZunpack	Exceptionintosenvironget	TypeErrorr   	max_lines	max_width)rE   sxlineswidthxpixelsypixelss          r   r   r      s^    |}} K1a++Kg0!44+1=+C+C(w2::QJE2::QJEA::!9:::u~      	 
w//00#bjnnY6O6O2P2PQQQQ 		  		  		  NENE}}%<		 	 s1   BB 
D5$AC>;D5>.D1,D50D11D5c                     i ddddddddd	d
dddddddddddddddddddddd d!d"}t           j        d#k    rt          j        | S |                    |d          }d%| d&|  d'S )(ae  Returns a string wrapped in ANSI color codes for coloring the text in a terminal.

    ::

        colored_text = color_text('Here is a message', 'blue')

    This won't actually effect the text until it is printed to the
    terminal.

    Parameters
    ----------
    text : str
        The string to return, bounded by the color codes.
    color : str
        An ANSI terminal color name. Must be one of:
        black, red, green, brown, blue, magenta, cyan, lightgrey,
        default, darkgrey, lightred, lightgreen, yellow, lightblue,
        lightmagenta, lightcyan, white, or '' (the empty string).
    blackz0;30redz0;31greenz0;32brownz0;33bluez0;34magentaz0;35cyanz0;36	lightgreyz0;37defaultz0;39darkgreyz1;30lightredz1;31
lightgreenz1;32yellowz1;33	lightbluez1;34lightmagentaz1;35	lightcyanz1;36whitez1;37win32Nz[mz[0m)r7   platformr   r   r[   )textcolorcolor_mapping
color_codes       r   _color_textr~      s   (v 	 		
 	 	6 	 	V 	6 	F 	F 	f 	& 	V 	  	V!" 	#M( |w8#5#=""5&11J-:------r-   c                     t          j                    }	 	 |                     |          S # t          $ r
 t          }Y nw xY w|                     |          S # t
          $ r |                     d          cY S w xY w)zDecode the supplied byte string using the preferred encoding
    for the locale (`locale.getpreferredencoding`) or, if the default encoding
    is invalid, fall back first on utf-8, then on latin-1 if the message cannot
    be decoded with utf-8.
    latin-1)localegetpreferredencodingdecodeLookupError_DEFAULT_ENCODINGUnicodeDecodeError)r_   encs     r   _decode_preferred_encodingr      s     
%
'
'C#	$88C==  	$ 	$ 	$#CCC	$xx}} # # #xx	"""""#s"   + ?A ?A A98A9c                    	  ||            |S # t           $ r Y nw xY wt          j                    }	 t          j        |          }n)# t
          $ r t          j        t                    }Y nw xY w ||          }|j        }	  ||            |S # t           $ r) t          j        d          } ||          }|j        }Y nw xY w ||            |S )zWrite the supplied string with the given write function like
    ``write(s)``, but use a writer for the locale's preferred encoding in case
    of a UnicodeEncodeError.  Failing that attempt to write with 'utf-8' or
    'latin-1'.
    r   )UnicodeEncodeErrorr   r   codecs	getwriterr   r   write)r_   r   fileobjr   Writerfs         r   _write_with_fallbackr     s   a    
%
'
'C5!#&& 5 5 5!"3445 	wAGEa   !),,F7OO 
E!HHHLs/    
A #A.-A.B 0CC
)endc                    |                     dt                                }|j        }t          |          rt          j        r}t          dt          |          d          D ]Q}||         }|dz   t          |          k    rd}n||dz            }|rt          ||          }t          |||          }R ||            dS t          dt          |          d          D ]}||         } ||            ||            dS )a  
    Prints colors and styles to the terminal uses ANSI escape
    sequences.

    ::

       color_print('This is the color ', 'default', 'GREEN', 'green')

    Parameters
    ----------
    positional args : str
        The positional arguments come in pairs (*msg*, *color*), where
        *msg* is the string to display and *color* is the color to
        display it in.

        *color* is an ANSI terminal color name.  Must be one of:
        black, red, green, brown, blue, magenta, cyan, lightgrey,
        default, darkgrey, lightred, lightgreen, yellow, lightblue,
        lightmagenta, lightcyan, white, or '' (the empty string).

    file : writable file-like, optional
        Where to write to.  Defaults to `sys.stdout`.  If file is not
        a tty (as determined by calling its `isatty` member, if one
        exists), no coloring will be included.

    end : str, optional
        The ending of the message.  Defaults to ``\n``.  The end will
        be printed after resetting any color or font state.
    rE   r      r    N)
r[   r9   r   r   r   	use_colorrangelenr~   r   )r   argskwargsrE   r   imsgr{   s           r   r   r   0  s   < ::fkmm,,DJEd||  q#d))Q'' 	; 	;Aq'C1uD		!!QU .!#u-- )eT::EEc




q#d))Q'' 	 	Aq'CE#JJJJc




r-   c                 .    t          j        dd|           S )z2
    Remove ANSI color codes from the string.
    z\[([0-9]+)(;[0-9]+)*mr   )resub)r_   s    r   strip_ansi_codesr   j  s     6.A666r-   c                    g d}t          |           } | dk     rd| ddS t          t          |          dz
            D ]D}||         \  }}||dz            \  }}| |k    r#d                    | |z  || |z  |z  |          c S EdS )	a  
    Returns a human-friendly time string that is always exactly 6
    characters long.

    Depending on the number of seconds given, can be one of::

        1w 3d
        2d 4h
        1h 5m
        1m 4s
          15s

    Will be in color if console coloring is turned on.

    Parameters
    ----------
    seconds : int
        The number of seconds to represent

    Returns
    -------
    time : str
        A human-friendly representation of the given number of seconds
        that is always exactly 6 characters.
    ))yi )wi:	 )diQ )hi  )rx   <   )r_   r   r   z   2dr_   r   z{:2d}{}{:2d}{}z  ~inf)rX   r   r   format)secondsunitsr   unit1limit1unit2limit2s          r   r	   r	   q  s    4  E 'llG||"W"""""3u::>""  ava!evf#**6!57V+;*F      8r-   c                 :   t          | d          r&ddlm} |                    | |j                  j        } d}| dk    rd}nHt          t          j        t          j	        |           t          j	        d          z                      }|dk    rd}n||         }t          t          j
        d|                    }| |z  }t          |          }|dk    r|d	|                    d
                   }n!|d         d
k    r|d	d         }n
|d	d         }|d| S )a  
    Returns a human-friendly string representing a file size
    that is 2-4 characters long.

    For example, depending on the number of bytes given, can be one
    of::

        256b
        64k
        1.1G

    Parameters
    ----------
    size : int
        The size of the file (in bytes)

    Returns
    -------
    size : str
        A human-friendly representation of the size of the file
    unitr   )r   z	 kMGTPEZYi     ? N.r      z>3s)r   astropyr   QuantitybytevaluerX   mathfloorlogpowstrindex)sizeusuffixes	num_scalesuffixr   	str_values          r   r
   r
     s1   , tV . 	'&&&&&zz$''-Hqyy		
48D>>DHTNN#BCCDD	1}})$DHT9--..I9EE

I}}4	 4 445			1		bqbM		bqbM	%%V%%%r-   c                       e Zd ZdZd Zd ZdS )_mapfuncz:
    A function wrapper to support ProgressBar.map().
    c                     || _         d S r)   _func)selffuncs     r   __init__z_mapfunc.__init__  s    


r-   c                 :    |\  }}||                      |          fS r)   r   )r   i_argr   args       r   __call__z_mapfunc.__call__  s     3$**S//!!r-   Nr.   r/   r0   r1   r   r   r3   r-   r   r   r     s<           " " " " "r-   r   c                       e Zd ZdZddZddZd Zd Zd Zd	 Z	dd
Z
ddZddZddZe	 	 	 	 	 dd            Ze	 	 	 	 	 dd            ZdS )r   aR  
    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()
    FNc                 0   |t                      }|s#t          |          s| j        | _        d| _        nd| _        t          |          r)t          |          | _        t          |          | _	        nY	 t          |          | _	        t          t          | j	                            | _        n# t          $ r t          d          w xY w|| _        t          j                    | _        t!          | j	                  | _        || _        d| _        |skt(          o| j                                        | _        |                                  | j        r+t/          j        t.          j        | j                   d| _        |                     d           dS )a  
        Parameters
        ----------
        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.

        ipython_widget : bool, optional
            If `True`, the progress bar will display as an IPython
            notebook widget.

        file : writable file-like, 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, or special case hacks
            to detect the IPython console), the progress bar will be
            completely silent.
        NTFz&First argument must be int or sequencer   )r9   r   _silent_updateupdate_silentr   iter_itemsr   _totalrX   r   r\   _filetime_start_timer
   _human_total_ipython_widget_signal_set_CAN_RESIZE_TERMINAL_should_handle_resize_handle_resizesignalSIGWINCH)r   total_or_itemsipython_widgetrE   s       r   r   zProgressBar.__init__  s}   & <==D 	!fTll 	!-DKDLL DLn%% 		7~..DKn--DKK7!.11 #5#5#566  J J J HIIIJ
 
9;;+DK88-  	()=)U$*BSBSBUBUD&!!!) (fot/BCCC#' As   6B1 1Cc                 N    t          | j                  d         }|dz
  | _        d S )Nr   %   )r   r   _bar_length)r   signumframeterminal_widths       r   r   zProgressBar._handle_resize'  s)    &tz2215)B.r-   c                     | S r)   r3   r   s    r   	__enter__zProgressBar.__enter__+      r-   c                    | j         s||                     | j                   | j                            d           | j                                         | j        r-t          j        t          j        t          j	                   d S d S d S )Nr   )
r   r   r   r   r   flushr   r   r   SIG_DFLr   exc_type	exc_value	tracebacks       r   __exit__zProgressBar.__exit__.  s    | 	?DK(((JT"""J ?fov~>>>>>	? 	?
? ?r-   c                     | S r)   r3   r   s    r   __iter__zProgressBar.__iter__7  r   r-   c                     	 t          | j                  }|                                  |S # t          $ r |                     d d d             w xY wr)   )nextr   r   StopIterationr   )r   rvs     r   __next__zProgressBar.__next__:  s`    	dk""B
 KKMMMI  	 	 	MM$d+++	s	   , #Ac                     |
| j         dz   }|| _         | j        r|                     |           dS |                     |           dS )zN
        Update progress bar via the console or notebook accordingly.
        Nr   )_current_valuer   _update_ipython_widget_update_consoler   r   s     r   r   zProgressBar.updateD  s_    
 ='!+E#  	(''.....  '''''r-   c                 d   | j         dk    rd}n$t          |          t          | j                   z  }| j        }|j        }|dk    rt	          | j                  }n$t	          t          | j                  |z            } |d           t          d|z  d|d           || j        k     r,t          d	d
|d            |d| j        |z
  dz
  z              |d           || j         k    rt          j                    | j        z
  }d}n1|dk    rd}d}n&t          j                    | j        z
  d|z
  z  |z  }d} |dt          |          dd| j
        d            |d|dd            ||           | |t          |                     | j                                         dS )zq
        Update the progress bar to the given value (out of the total
        given to the constructor).
        r   g      ?r   z|=rj   r   rE   r   >rh   -|z     Nz ETA r   z>4s/ (>6.2%))r   floatr   r   rX   r   r   r   r   r
   r   r	   r   )r   r   fracrE   r   bar_filltprefixs           r   r  zProgressBar._update_consoleS  s   
 ;!DD<<%"4"44Dz
!884+,,HH5!122T9::HeC(NF2>>>>d&&&W4R8888E#)H4q89:::c


DK	d..AFFaZZAFF)++ 00S4Z@DHAFF/%((FFFt/@FFFGGG 4    !!!f=E*Q--   
r-   c                    t          | d          sddlm} |d         dk     r ddlm} |                                | _        n8t                                           ddl	m} |
                                | _        ddlm}  || j                   d| j        _        || j        z  }|dz  | j        _        d|d	d
| j        _        dS )z
        Update the progress bar to the given value (out of a total
        given to the constructor).

        This method is for use in the IPython notebook 2+.
        _widgetr   r   r   )widgets)displayd   r  r  r  N)r   r   r   IPython.htmlr  FloatProgressWidgetr  r   r   
ipywidgetsFloatProgressIPython.displayr  r   r   description)r   r   r   r  r  r  s         r   r  z"ProgressBar._update_ipython_widget{  s     tY'' 	#,,,,,,A""000000&::<<$$&&&......&4466//////GDL!!!!"DL t{"!CZ#5#5#5#5#5   r-   c                     d S r)   r3   r  s     r   r   zProgressBar._silent_update  s    r-   r  c           	          |r+t          |          }t          t          |                    }|                     |||||||          }|r(t	          t          |           \  }	}t          |          }|S )a  Map function over items while displaying a progress bar with percentage complete.

        The map operation may run in arbitrary order on the items, but the results are
        returned in sequential order.

        ::

            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, int, optional
            If `True`, use the `multiprocessing` module to distribute each task
            to a different processor core. If a number greater than 1, then use
            that number of cores.

        ipython_widget : bool, optional
            If `True`, the progress bar will display as an IPython
            notebook widget.

        file : writable file-like, 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.

        step : int, optional
            Update the progress bar at least every *step* steps (default: 100).
            If ``multiprocess`` is `True`, this will affect the size
            of the chunks of ``items`` that are submitted as separate tasks
            to the process pool.  A large step size may make the job
            complete faster if ``items`` is very long.

        multiprocessing_start_method : str, optional
            Useful primarily for testing; if in doubt leave it as the default.
            When using multiprocessing, certain anomalies occur when starting
            processes with the "spawn" method (the only option on Windows);
            other anomalies occur with the "fork" method (the default on
            Linux).
        )multiprocessrE   stepr   multiprocessing_start_method)r   list	enumeratemap_unorderedzipsorted)
r   functionitemsr#  rE   r$  r   r%  results_s
             r   mapzProgressBar.map  s    z  	+))H5))**E##%))E $ 
 
  	$fWoo.JAw7mmGr-   c           
         ddl m}m}	 g }
|t                      } | t	          |          ||          5 }|j        r|}nOt          t          t          t	          |                    |j	        z            d          }t          ||          }|r|dk     rRt          |          D ]A\  }}|
                     |                     ||z  dk    r|                    |           Bnt          j        |          }t!          |          } |d
d|durt          |          ndi|5 t           |	fd	|D                                 D ]A\  }}|                    |           |
                    |                                           B	 ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |
S )a  Map function over items, reporting the progress.

        Does a `map` operation while displaying a progress bar with
        percentage complete. The map operation may run on arbitrary order
        on the items, and the results may be returned in arbitrary order.

        ::

            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, int, optional
            If `True`, use the `multiprocessing` module to distribute each task
            to a different processor core. If a number greater than 1, then use
            that number of cores.

        ipython_widget : bool, optional
            If `True`, the progress bar will display as an IPython
            notebook widget.

        file : writable file-like, 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.

        step : int, optional
            Update the progress bar at least every *step* steps (default: 100).
            If ``multiprocess`` is `True`, this will affect the size
            of the chunks of ``items`` that are submitted as separate tasks
            to the process pool.  A large step size may make the job
            complete faster if ``items`` is very long.

        multiprocessing_start_method : str, optional
            Useful primarily for testing; if in doubt leave it as the default.
            When using multiprocessing, certain anomalies occur when starting
            processes with the "spawn" method (the only option on Windows);
            other anomalies occur with the "fork" method (the default on
            Linux).
        r   )ProcessPoolExecutoras_completedN)r   rE   r   )
mp_contextmax_workersTc              3   D   K   | ]}                     |          V  d S r)   )submit).0itemr+  ps     r   	<genexpr>z,ProgressBar.map_unordered.<locals>.<genexpr>L  s1      $P$P$QXXh%=%=$P$P$P$P$P$Pr-   r3   )concurrent.futuresr1  r2  r9   r   r   maxrX   r  r   minr'  appendr   r?   get_contextdictresult)r   r+  r,  r#  rE   r$  r   r%  r1  r2  r-  bar	chunksizedefault_stepr   r8  ctxr   r   r9  s    `                 @r   r(  zProgressBar.map_unordered  s   @ 	IHHHHHHH<==DSUNFFF 	3#" 4 		"3uSZZ'8'83?'J#K#KQOOd33	 3<!#3#3(// & &GAtNN88D>>222I!++

1&
 &12NOO---((  -9-E-EL)))4 	  
3
  )$$P$P$P$P$P%$P$P$PPP! ! 3 31 

1qxxzz2222	3
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	36 s7   C5F9-A(F"F9"F&	&F9)F&	*F99F= F=)FN)NNr)   )FNr  FN)r.   r/   r0   r1   r   r   r   r   r   r  r   r  r  r   r2   r/  r(  r3   r-   r   r   r     sB        4 4 4 4l/ / / /  ? ? ?    ( ( ( (& & & &P6 6 6 6@    
 %)N N N [N` 
 %)a a a [a a ar-   r   c                   N    e Zd ZdZdZdZddZd Zd	 Zd
 Z	d Z
d ZddZd ZdS )r   z
    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.update()
    u   ◓◑◒◐z-/|\rn   Nr   c                 F   |t                      }|| _        || _        || _        || _        |t
          j        r| j        }n| j        }|| _	        t          |           | _        | j        r|                                 | _        dS |                                 | _        dS )aw  
        Parameters
        ----------
        msg : str
            The message to print

        color : str, optional
            An ANSI terminal color name.  Must be one of: black, red,
            green, brown, blue, magenta, cyan, lightgrey, default,
            darkgrey, lightred, lightgreen, yellow, lightblue,
            lightmagenta, lightcyan, white.

        file : writable file-like, optional
            The file to write the spinner to.  Defaults to
            `sys.stdout`.  If ``file`` is not a tty (as determined by
            calling its `isatty` member, if any, or special case hacks
            to detect the IPython console), the spinner will be
            completely silent.

        step : int, optional
            Only update the spinner every *step* steps

        chars : str, optional
            The character sequence to use for the spinner
        N)r9   _msg_colorr   _stepr   unicode_output_default_unicode_chars_default_ascii_chars_charsr   r   _silent_iterator_iter	_iterator)r   r   r{   rE   r$  charss         r   r   zSpinner.__init__b  s    4 <==D	

=" 231!$<<'< 	*..00DJJJ))DJJJr-   c              #     K   | j         }d}| j        }|j        }|j        }d}	  |d           t	          | j        | j        |d            |d           	 |rt          ||         ||          }n |||                    n*# t          $ r | j	        } |||                    d}Y nw xY w |             d V  t          | j                  D ]}d V  |dz   t          |          z  })	Nr   Tr   r	  r   Fr   )rN  r   r   r   r   rH  rI  r   UnicodeErrorrM  r   rJ  r   )r   rR  r   rE   r   r   try_fallbackr   s           r   rQ  zSpinner._iterator  s4     z

	-E$KKK	4;TrBBBBE#JJJ
% (0uudKKEEE%,''' % % % 1eEl###$% EGGGEEE4:&&  QY#e**,E+	-s   +B $B+*B+c                     | S r)   r3   r   s    r   r   zSpinner.__enter__  r   r-   c                     | j         }|j        }|j        }| j        s( |d           t	          | j        | j        |d           |t	          dd|           nt	          dd|            |             d S )	NrT  r   r	  z [Done]rh   rE   z	 [Failed]rg   )r   r   r   r   r   rH  rI  )r   r   r   r   rE   r   r   s          r   r   zSpinner.__exit__  s    z

| 	CE$KKK	4;TrBBBB	766666U6666r-   c                     | S r)   r3   r   s    r   r   zSpinner.__iter__  r   r-   c                 .    t          | j                   d S r)   )r   rP  r   s    r   r  zSpinner.__next__  s    TZr-   c                 $    t          |            dS )zUpdate the spin wheel in the terminal.

        Parameters
        ----------
        value : int, optional
            Ignored (present just for compatibility with `ProgressBar.update`).

        N)r   r  s     r   r   zSpinner.update  s     	T




r-   c              #      K   t          | j        | j        | j        d           | j                                         	 d V  )Nr   r	  )r   rH  rI  r   r   r   s    r   rO  zSpinner._silent_iterator  sJ      DIt{DDDD
	EEE	r-   )rn   Nr   Nr)   )r.   r/   r0   r1   rL  rM  r   rQ  r   r   r   r  r   rO  r3   r-   r   r   r   T  s          ,"-* -* -* -*^- - ->        	 	 	 	    r-   r   c                   ,    e Zd ZdZddZd Zd Zd ZdS )	r   a  
    A class that displays either a `ProgressBar` or `Spinner`
    depending on whether the total size of the operation is
    known or not.

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

        if file.has_length():
            length = file.get_length()
        else:
            length = None
        bytes_read = 0
        with ProgressBarOrSpinner(length) as bar:
            while file.read(blocksize):
                bytes_read += blocksize
                bar.update(bytes_read)
    rn   Nc                     |t                      }|t          |          s d| _        t          |||          | _        dS d| _        t          |||           t          ||          | _        dS )a  
        Parameters
        ----------
        total : int or None
            If an int, the number of increments in the process being
            tracked and a `ProgressBar` is displayed.  If `None`, a
            `Spinner` is displayed.

        msg : str
            The message to display above the `ProgressBar` or
            alongside the `Spinner`.

        color : str, optional
            The color of ``msg``, if any.  Must be an ANSI terminal
            color name.  Must be one of: black, red, green, brown,
            blue, magenta, cyan, lightgrey, default, darkgrey,
            lightred, lightgreen, yellow, lightblue, lightmagenta,
            lightcyan, white.

        file : writable file-like, optional
            The file to write the to.  Defaults to `sys.stdout`.  If
            ``file`` is not a tty (as determined by calling its `isatty`
            member, if any), only ``msg`` will be displayed: the
            `ProgressBar` or `Spinner` will be silent.
        NT)r{   rE   FrY  )r9   r   _is_spinnerr   _objr   r   )r   totalr   r{   rE   s        r   r   zProgressBarOrSpinner.__init__  sx    4 <==D=t=#D5t<<<DIII$DU....#E555DIIIr-   c                     | S r)   r3   r   s    r   r   zProgressBarOrSpinner.__enter__  r   r-   c                 :    | j                             |||          S r)   )ra  r   r   s       r   r   zProgressBarOrSpinner.__exit__  s    y!!(IyAAAr-   c                 :    | j                             |           dS )zp
        Update the progress bar to the given value (out of the total
        given to the constructor.
        N)ra  r   r  s     r   r   zProgressBarOrSpinner.update  s     
 		r-   )rn   N)r.   r/   r0   r1   r   r   r   r   r3   r-   r   r   r     sc         $#6 #6 #6 #6J  B B B         r-   r      F   c                 <   |t                      }t          j        rd}nd}|j        }|J|t	          |           k    rt          d          | d|                             d          }|||dz
  z  z  }|                     d          } |                     dd|z            } |t||k    rnt          |d	z  t	          |           |z
            }||z
  }	| |	t	          |          z   d         } |t	          |          z  }|}||	z  }t          |d
|d           t	          |           |k    r6 || d|t	          |          z
                      t          |d
|           n ||             |d           |" |d|z             t          dd|           dS dS )ux  
    Prints a line of source code, highlighting a particular character
    position in the line.  Useful for displaying the context of error
    messages.

    If the line is more than ``width`` characters, the line is truncated
    accordingly and '…' characters are inserted at the front and/or
    end.

    It looks like this::

        there_is_a_syntax_error_here :
                                     ^

    Parameters
    ----------
    line : unicode
        The line of code to display

    col : int, optional
        The character in the line to highlight.  ``col`` must be less
        than ``len(line)``.

    file : writable file-like, optional
        Where to write to.  Defaults to `sys.stdout`.

    tabwidth : int, optional
        The number of spaces per tab (``'\t'``) character.  Default
        is 8.  All tabs will be converted to spaces to ensure that the
        caret lines up with the correct column.

    width : int, optional
        The width of the display, beyond which the line will be
        truncated.  Defaults to 70 (this matches the default in the
        standard library's `textwrap` module).
    Nu   …z...z%col must be less the the line length.	r   r   r   r   ro   r   r	  rY  ^rg   )r9   r   rK  r   r   
ValueErrorcountrstripreplacer=  r   )
linecolrE   tabwidthrb   ellipsisr   ntabsnew_coloffsets
             r   r   r      s   J |}} JE
#d))DEEETcT
  &&u1%%;;tD<<cHn--D
3;;eqj#d))c/22wFS]]*,,-XvHjt<<<<
4yy5d*US]]**+,,,Hjt44444dd
cCiCT****** r-   c                       e Zd ZdZd Zd ZdS )Getchz|Get a single character from standard input without screen echo.

    Returns
    -------
    char : str (one character)
    c                     	 t                      | _        d S # t          $ rD 	 t                      | _        Y d S # t          t          f$ r t                      | _        Y Y d S w xY ww xY wr)   )_GetchWindowsimplr   _GetchMacCarbonAttributeError
_GetchUnixr   s    r   r   zGetch.__init__|  s|    	)%DIII 	) 	) 	))+--				0 ) ) )&LL					)	)s$    
A%8$A!A% A!!A%c                 *    |                                  S r)   )rz  r   s    r   r   zGetch.__call__  s    yy{{r-   Nr   r3   r-   r   rw  rw  t  s<         ) ) )    r-   rw  c                       e Zd Zd Zd ZdS )r}  c                     dd l }dd l}dd l}d S Nr   )r7   rT   tty)r   r7   rT   r  s       r   r   z_GetchUnix.__init__  s#    


 	




r-   c                 f   dd l }dd l}dd l}|j                                        } |j        |          }	 |                    |j                                                   |j                            d          } |j        ||j	        |           n#  |j        ||j	        |           w xY w|S )Nr   r   )
r7   rT   r  stdinfileno	tcgetattrsetrawread	tcsetattr	TCSADRAIN)r   r7   rT   r  fdold_settingschs          r   r   z_GetchUnix.__call__  s    





Y(w(,,	CJJsy''))***""BGb'"3\BBBBGb'"3\BBBB	s   AB B.Nr.   r/   r0   r   r   r3   r-   r   r}  r}    s2              r-   r}  c                       e Zd Zd Zd ZdS )ry  c                     dd l }d S r  )msvcrtr   r  s     r   r   z_GetchWindows.__init__  s    r-   c                 2    dd l }|                                S r  )r  getchr  s     r   r   z_GetchWindows.__call__  s    ||~~r-   Nr  r3   r-   r   ry  ry    s2              r-   ry  c                       e Zd ZdZd Zd ZdS )r{  z
    A function which returns the current ASCII key that is down;
    if no ASCII key is down, the null string is returned.  The
    page http://www.mactech.com/macintosh-c/chap02-1.html was
    very helpful in figuring out how to do this.
    c                     dd l }|j         d S r  )CarbonEvt)r   r  s     r   r   z_GetchMacCarbon.__init__  s    



r-   c                     dd l }|j                            d          d         dk    rdS |j                            d          d         \  }}}}}t	          |dz            S )Nr   rf  r   r      )r  r  
EventAvailGetNextEventchr)r   r  whatr   whenwheremods          r   r   z_GetchMacCarbon.__call__  sj    :  ((+q002 -3J,C,CF,K,KA,N)T3eSsZ'(((r-   Nr   r3   r-   r   r{  r{    s<           
) ) ) ) )r-   r{  )Fr)   )NNrf  rg  ),r1   r   r   r   r?   rY   r   rP   r7   rA   r   rR   r   rT   r   r   r   r   
decoratorsr   miscr   __all__r   r   r9   r   r   r~   r   r   r   r   r	   r
   r   r   r   r   r   rw  r}  ry  r{  r3   r-   r   <module>r     so           				 				  



     
!LLLMMMNNN ! ! ! !       % % % % % %      
 
 
  5* 5* 5* 5* 5* 5* 5* 5*p   $' ' 'T$  $  $  $ N-. -. -.`# # #"     F   7 7 7 7 7t7 7 7. . .b/& /& /&d
" 
" 
" 
" 
" 
" 
" 
"p p p p p p p pfC C C C C C C CLC  C  C  C  C  C  C  C LJ+ J+ J+ J+h       *       0       ) ) ) ) ) ) ) ) ) )s   ; AA