
    cG                        d dl mZmZmZ ddlm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d dlZd Zd)dZd*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d dlZ	 d dlZn# e$ r dZY nw xY wej        ZdZdZdZdZ dZ!g dZ"dZ#dZ$dZ%dZ& G d de'          Z(ddde!e ddfdZ)ej*        efdZ+ G d de'          Z,d Z-d Z.d*dZ/d+d!Z0d" Z1d# Z2d$ Z3d,d%Z4d& Z5d,d'Z6d( Z7dS )-    )print_functionabsolute_importdivision   )__version__)reportNc                    t           j                            t          j        t          j        |                               }t           j                            |d          t           j                            |dd          g}|D ]%}t           j                            |          r|c S &t          d| d|          )Nexamplesz..zCould not find examples for z at any of )
ospathdirnameinspectgetfile	importlibimport_modulejoinexists
ValueError)namemodule_path
candidates	candidates       (lib/python3.11/site-packages/pyct/cmd.py_find_examplesr      s    '//'/)2I$2O2O"P"PQQK 	[,,
[j11	3J    	7>>)$$ 		 *DDDT
U
UU    Fc                 R    t          | |||           t          | |d||           dS )z
    Copy examples and fetch data (if any) to the supplied path.
    See copy-examples and fetch-data for more flexibility.

    NOTE: force operates both on example and data over-writing
    pre-existing files.
    F)require_datasetsuse_test_dataforceN)copy_examples
fetch_data)r   r   verboser   r   s        r   r
   r
      s8     $gu---tD%mRWXXXXXXr   c                 X   t          |           }t          j                            |          }t          j                            |          r|st          d|z            |rt          d|z             t          j        	                    |||           t          d|z             dS )z#Copy examples to the supplied path.zSPath %s already exists; please move it away, choose a different path, or use force.zCopying examples from %s)r"   zCopied examples to %sN)
r   r   r   abspathr   r   print	distutilsdir_util	copy_tree)r   r   r"   r   sources        r   r    r    *   s    D!!F7??4  D	w~~d uE unossttt 1(/000  w ???	
!$
&'''''r   )OrderedDictz%s[%s%s] %i/%i - %sz%s %s %i/%i.# )|/-\	   dataz.data_stubsc                   D    e Zd Zd Zd ZdddeeddfdZddZd	 Z	d
 Z
dS )Barc                     | S N selfs    r   	__enter__zBar.__enter__r   s    r   c                 .    |                                   dS )NF)done)r:   exc_typeexc_valexc_tbs       r   __exit__zBar.__exit__u   s    		ur        Nr   c                    || _         || _        || _        |8	 t                                           | _        n# t
          $ r
 d| _        Y nw xY w|| _        || _        || _        || _	        t          j
                    | _        g | _        d| _        t          j
                    | _        |                     | j                  | _        d| _        | j        r|                     d           dS dS )z3Bar is a class for printing the status of downloadsNTr   )labelwidthhideSTREAMisattyAttributeError
empty_charfilled_charexpected_sizeeverytimestartittimesetaetadeltaformat_timeetadisplast_progressshow)r:   rE   rF   rG   rK   rL   rM   rN   s           r   __init__zBar.__init__y   s     

	 	!! &/		! ! ! ! 			!'(*"
!Y[[
!Y[[!--dh77 	IIaLLLLL	 	s   9 AAc           	      f   ||| _         | j         t          d          || _        t          j                    | j        z
  t
          k    rt          j                    | _        | j        t           d          | j        t          j                    z
   |dz   z  gz   | _        t          | j                  t          t          | j                            z  | j         |z
  z  | _        |                     | j                  | _        t          | j        |z  | j         z            }| j        s|| j        z  dk    s|| j         k    rlt&                              t*          | j        | j        |z  | j        | j        |z
  z  || j         | j        fz             t&                                           d S d S d S )Nzexpected_size not initializedr   r   )rM   	ExceptionrV   rO   rS   ETA_INTERVALrQ   ETA_SMA_WINDOWrP   sumfloatlenrR   rT   rU   intrF   rG   rN   rH   writeBAR_TEMPLATErE   rL   rK   flush)r:   progresscountxs       r   rW   zBar.show   s    	'!&D 	=;<<<%IKK$-'<7 	6 IKKDMn_--.zDIKK/0HQJ?@A L DL!!E#dl*;*;$<$<<#h.0 H  ++DH55DL
X%(::;;y 	DJ&1, T//\J 01 4OtzA~6&-6 6 7 7 7 	 	 r   c           	         t          j                     | j        z
  | _        |                     | j                  }| j        st
                              t          | j        | j	        | j
        z  | j        dz  | j        | j        |fz             t
                              d           t
                                           d S d S )Nr   
)rO   rP   elapsedrT   rG   rH   ra   rb   rE   rL   rF   rK   rV   rM   rc   )r:   elapsed_disps     r   r=   zBar.done   s    y{{TZ/''55y 	LL
D,tz9!#T%7"L)2 2 3 3 3 LLLLNNNNN	 	r   c                 P    t          j        dt          j        |                    S )Nz%H:%M:%S)rO   strftimegmtime)r:   secondss     r   rT   zBar.format_time   s    }ZW)=)=>>>r   r7   )__name__
__module____qualname__r;   rA   BAR_EMPTY_CHARBAR_FILLED_CHARrX   rW   r=   rT   r8   r   r   r5   r5   q   s              r,D   2   2
 
 
? ? ? ? ?r   r5   rB   rC   c           	   #     K   |t          |           n|}t          |||t          t          ||          5 }	t	          |           D ]!\  }
}|V  |	                    |
dz              "	 ddd           dS # 1 swxY w Y   dS )z/Progress iterator. Wrap your iterables with it.N)rE   rF   rG   rK   rL   rM   rN   r   )r_   r5   rr   rs   	enumeraterW   )itrE   rF   rG   rK   rL   rM   rN   re   bariitems               r   rw   rw      s       %?CGGG-E	5D^(U
L 
L 
L  }} 	 	GAtJJJHHQUOOOO	                 s   2A66A:=A:c                      G d d|          }fd}|                     t          j        j        j        |           t          j        | |          S )Nc                       e Zd ZdS )#ordered_load.<locals>.OrderedLoaderN)ro   rp   rq   r8   r   r   OrderedLoaderr|      s        r   r}   c                 j    |                      |            |                     |                    S r7   )flatten_mappingconstruct_pairs)loadernodeobject_pairs_hooks     r   construct_mappingz'ordered_load.<locals>.construct_mapping   s6    t$$$  !7!7!=!=>>>r   )add_constructoryamlresolverBaseResolverDEFAULT_MAPPING_TAGload)streamLoaderr   r}   r   s     `  r   ordered_loadr      s           ? ? ? ? ? !!"6   9V]+++r   c                   $    e Zd ZdZd Zd Zd ZdS )DirectoryContextz2
    Context Manager for changing directories
    c                 D    t          j                    | _        || _        d S r7   )r   getcwdold_dirnew_dir)r:   r   s     r   rX   zDirectoryContext.__init__   s    y{{r   c                 8    t          j        | j                   d S r7   )r   chdirr   r9   s    r   r;   zDirectoryContext.__enter__       
r   c                 8    t          j        | j                   d S r7   )r   r   r   )r:   argss     r   rA   zDirectoryContext.__exit__   r   r   N)ro   rp   rq   __doc__rX   r;   rA   r8   r   r   r   r      sK                 r   r   c                 @   t          d                    |                     t          j        | d          }	 t	          |d          5 }t          |j                            d                    }t          |                    d          |dz  dz   d	
          D ]-}|r)|	                    |           |
                                 .	 ddd           dS # 1 swxY w Y   dS #  t          j                            |          rt          j        |            xY w)zGiven a url, output_path and title,
    write the contents of a requests get operation to
    the url in binary mode and print the title of operationzDownloading {0}T)r   wbzcontent-lengthi   )
chunk_sizer   i  )rM   rN   N)r%   formatrequestsgetopenr`   headersrw   iter_contentra   rc   r   r   r   remove)urloutput_pathtitlerespftotal_lengthchunks          r   _url_to_binary_writer      sb    


"
"5
)
)***<D)))D+t$$ 	t|//0@AABBLT..$.??P\]aPaefOfnrsss   GGENNNGGIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	7>>+&& 	#Ik"""s0   C' 
BCC' CC' !C"C' '6Dc                 f   |                      d          rWt          j        | d          5 }|                                 ddd           n# 1 swxY w Y   t	          j        |            dS |                      d          rWt          j        | d          5 }|                                 ddd           n# 1 swxY w Y   t	          j        |            dS |                      d          rWt          j        | d          5 }|                                 ddd           n# 1 swxY w Y   t	          j        |            dS |                      d          rWt          j        | d	          5 }|                                 ddd           n# 1 swxY w Y   t	          j        |            dS dS )
zEExtract a local archive, e.g. zip or tar, then
    delete the archiveztar.gzzr:gzNtarzr:ztar.bz2zr:bz2zipr)endswithtarfiler   
extractallr   r   zipfileZipFile)r   r   zipfs      r   _extract_downloaded_archiver      s    H%% \+v.. 	#NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
	+			e	$	$ \+t,, 	NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
	+			i	(	( \+w// 	3NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
	+			e	$	$ _[#.. 	$OO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
	+ sG   AAAB88B<?B<D$$D(+D(/FFFc                    t           j                            |          st          j        |           t	          |          5  d}|                     dg           D ]%}t           j                            |          sd} n&|du r8|s6t          d                    | d                              	 ddd           dS | d         | d         dz   }                    d	          r5fd
| d         D             }fd| d         D             }	d |	D             }
nPg}t           j        	                              d         g}	| d         }
t          |
t          t          f          s|
g}
t          ||	|
          }t          |          D ]\  }\  }}|                    |dz   t          |                    }|du rjt!          j        |          s3t           j                            |                    dd                    r#t          d                    |                     t           j                            |t&          |          }|ryt           j                            |          rZt           j                            ||          }t          d                    ||                     t)          j        ||           ?|rAt           j                            |          s"t          d                    |                     t-          ||           t/          |           	 ddd           n# 1 swxY w Y   t0          %t          d           t3          j        d           dS dS )ay  Process each download spec in datasets.yml

    Typically each dataset list entry in the yml has
    "files" and "url" and "title" keys/values to show
    local files that must be present / extracted from
    a decompression of contents downloaded from the url.

    If a url endswith '/', then all files given
    are assumed to be added to the url pattern at the
    end
    FfilesTzSkipping {0}r   Nr   z	 {} of {}r/   c                     g | ]}|z   S r8   r8   ).0r   r   s     r   
<listcomp>z$_process_dataset.<locals>.<listcomp>)  s    666C!G666r   c                 \    g | ](}t           j                            t          |          )S r8   )r   r   r   DATA_DIR)r   fnameheres     r   r   z$_process_dataset.<locals>.<listcomp>*  s;     ; ; ; % GLLx?? ; ; ;r   c           	          g | ]L}d                      |                    d           d|                    d          rdnd                   dz   MS )r+   Ngz*)r   splitr   )r   r   s     r   r   z$_process_dataset.<locals>.<listcomp>-  sl     9 9 9' !2!23!7!78c@T@TUY@Z@Z:b""`b8c!deehkk 9 9 9r   r   r   rB   z%Copying test data file '{0}' to '{1}'z6No test file found for: {}. Using regular file insteadzIthis download script requires the requests module: conda install requests)r   r   r   makedirsr   r   r%   r   r   r   
isinstancetuplelistr   ru   r_   globreplacer   DATA_STUBS_DIRshutilcopyfiler   r   r   sysexit)dataset
output_dirr   r   r   requires_downloadr   	title_fmturlsoutput_pathsunpackedzippedidxr   unpackrunning_titletesttargetr   s     `               @r   _process_datasetr     s    7>>*%%  
J	*	%	% *5 *5!Wb)) 	 	A7>>!$$ $(! E> 	"3 	.''(899:::*5 *5 *5 *5 *5 *5 *5 *5 enG${2	<< 	&6666WW%5666D; ; ; ;)0)9; ; ;L9 9+79 9 9HH 5DGMM#..q12Lw'Hh66 &$:T<22/8/@/@ 	5 	5+C+#{F%,,S1Wc$ii@@M~ 49V#4#4 v~~VYZ\G]G]8^8^  n++M::;;;7<<
NFCCD ]!5!5 ]j&99=DDT6RRSSSf--- ]rw~~d';'; ]NUUVZ[[\\\ k=AAA'4444!	55*5 *5 *5 *5 *5 *5 *5 *5 *5 *5 *5 *5 *5 *5 *5Z  YZZZ s   A.L> I1L>>MMdatasets.ymlTc           
      .   t           j                            |          }t           j                            ||          }t           j                            |          s-t           j                            t          |           |          }t           j                            |          s|du rt          d           dS t          d|dt           j                            |t                               t          |          5 }t          |
                                          }|                                D ]>\  }	}
t           j                            ||	          }|
D ]}t          |||||           ?	 ddd           dS # 1 swxY w Y   dS )zFetch sample datasets as defined by path/datasets if it exists or else module's own examples/datasets otherwise.

    Datasets are placed in path/data
    FzNo datasets to downloadNzFetching data defined in z and placing in )r   r   )r   r   r$   r   r   r   r%   r   r   r   readitemsr   )r   r   datasetsr   r   r   	info_filer   infotopic	downloadsr   ds                r   r!   r!   M  s   
 7??4  DT(++I7>>)$$ @GLL!5!5h??	7>>)$$ )9U)B '(((	E999RW\\RVW_E`E`E`
abbb	i `AAFFHH%% $

 	` 	`E9dE22J ` ` JMY^_____`	`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `s   A5F

FFc                 F   t           j                            |          }t           j                            |          st	          |           }t           j                            |t                    }t           j                            |t                    }t           j                            |          r%t          t          j	        |                    dk    rt          d           dS t          j	        |          D ]}t           j                            ||          }t           j                            |          st          d|           St           j                            ||          }t           j                            |          rt           j                            |          }t           j                            |          }||k    r%t          d|           t          j        |           t          d                    ||                      dS )z=Remove up any data files that are copied from test files
    r   zNo test files foundNz!Test file was not copied to data:zRemoving copied test file:z?Size of test file {:.2e} did not match size of data file {:.2e})r   r   r$   r   r   r   r   r   r_   listdirr%   isfilegetsizer   r   )	r   r   data_dirtest_dirr   	data_file	test_filedata_stest_ss	            r   
clean_datar   d  s    7??4  D7>>$ $d##w||D(++Hw||Hn55H7>>(## s2:h+?+?'@'@A'E #$$$Z!! I IGLL1--	w~~i(( 	5q999GLL1--	7>>)$$ 	IW__Y//FW__Y//F I2A666	)$$$$ 1171G1GI I II Ir   c                     d|v r|                      dddd           d|v r3|                      dt          dd                    |          	           d S d S )
N-vz	--verbosere   r   )actiondefault--pathzwhere to place outputz{}-examples)typehelpr   )add_argumentstrr   )parserr   r   s      r   _add_common_argsr     sz    t| JD+gqIII4 qH#4KUbUiUijnUoUopppppq qr   c                 >    |                      fd           d S )Nc                 J      fi  fdt                     D             S )Nc                 >    i | ]}|d k    |t          |          S func)getattr)r   kr   s     r   
<dictcomp>z3_set_defaults.<locals>.<lambda>.<locals>.<dictcomp>  s/    5j5j5jQ`aci`i5jaa5j5j5jr   )vars)r   fnr   s   `r   <lambda>z_set_defaults.<locals>.<lambda>  s9    ""T*l*l5j5j5j5jRVW[R\R\5j5j5j*l*l r   r  )set_defaults)r   r   r  s    ``r   _set_defaultsr    s.    
lllllmmmmmr   c                    |g d}d|v rn|                      dt          j        t                              }|                    ddd           t          ||d	d
           t          ||t                     d|v r|                      dt          j        t                              }|                    dt          dd           |                    ddd           |                    ddd           t          ||d
           t          ||t                     d|v r|                      dt          j        t                              }|                    ddd           |                    ddd           t          ||d	d
           t          ||t                     d|v rW|                      dt          j        t                              }t          ||d
           t          ||t                     dS dS )zF
    Add all commands in pyct.cmd unless specific cmds are listed
    N)r
   copy-examples
fetch-data
clean-datar  r   z--force
store_truezQif PATH already exists, force overwrite existing files if older than source files)r   r   r   r   r  z
--datasetsr   z^*name* of datasets file; must exist either in path specified by --path or in package/examples/)r   r   r   z,Force any existing data files to be replacedz--use-test-datazUse data's test files, if any, instead of fetching full data. If test file not in '.data_stubs', fall back to fetching full data.r
   zif PATH already exists, force overwrite existing examples if older than source examples. ALSO force any existing data files to be replacedr  )
add_parserr   getdocr    r   r   r  r!   r   r
   r   )r   r   cmdsr   	eg_parserd_parser
egd_parser	cd_parsers           r   add_commandsr    sv     FEEE $ 6%%oGN=<Y<Y%ZZ	y&H 	 	J 	J 	J 	D$999i}555t 
2$$\z8R8R$SSln%R 	 	T 	T 	T 	iCqrrr/%j 	 	l 	l 	l 	4222hj111T 	2&&zx8P8P&QQ
	,'p 	  	r 	r 	r 	 1,'l 	  	n 	n 	n 	T4:::j$111t 3%%l
9S9S%TT	D(333iz222223 3r   c                 n    t          j        |          }|                     ddd|j        z              d S )N	--versionversion	%(prog)s r   r  )r   r   r   r   )r   r   mods      r   add_versionr     s:    

!$
'
'C
I{S_?\]]]]]r   c                 F   t          j        d| z            }|                    d          }t          || ||           t	          ||            |                                }t          |d          r|                    |           dS |                    d           dS )z_
    If module has no other commands, use this function to add all of the ones in pyct.cmd
    z%s commandsdescriptionavailable commandsr   r  must supply command to runN)	argparseArgumentParseradd_subparsersr  r   
parse_argshasattrr  error)r   r  r   r   
subparserss        r   substitute_mainr.    s     $t1CDDDF&&-A&BBJT4...DtF++[DIIdOOOOO>Z1[1[1[1[1[r   c                      t          j        d          } |                     dddt          z              |                     d          }|                    d	t          j        t                    
          }|	                    t                     |                    ddt          dd           |                                 }t          |d          r |j        |j          d S |                     d           d S )NzCommands relating to versioningr"  r  r  r  r  r$  r%  r   r  r  packagespackage+zname of package)metavarr   nargsr   r  r&  )r'  r(  r   r   r)  r  r   r  r   r
  r   r*  r+  r  r0  r,  )r   r-  report_parserr   s       r   mainr6    s   $1RSSSF
I{;?VWWW&&-A&BBJ))(9O9O)PPMF+++z)3$5  7 7 7 DtF 3	4=!!!!122222r   )FFF)FF)r   TFF)NN)8
__future__r   r   r   rB   r   r   r   r   r   r'  distutils.dir_utilr&   r   r   r
   r    collectionsr*   r   r   r   rO   r   r   r   ImportErrorstderrrH   rb   MILL_TEMPLATE	DOTS_CHARrs   rr   
MILL_CHARSr[   r\   r   r   objectr5   rw   r   r   r   r   r   r   r!   r   r   r  r  r   r.  r6  r8   r   r   <module>r@     s  
 A @ @ @ @ @ @ @ @ @             				           V V V	Y 	Y 	Y 	Y	( 	( 	( 	(* $ # # # # #  



    OOOO   HHH 
&	"""
  G? G? G? G? G?& G? G? G?T BTn#4q    !%{ 	, 	, 	, 	,    v     (  *> > > >@` ` ` `.I I I<q q qn n n*3 *3 *3 *3X^ ^ ^
\ 
\ 
\ 
\3 3 3 3 3s   A A)(A)