§ <ˆjgXãóЗdZddlZddlZddlmZeZd„ZGd„d¦«Ze¦«Z dej ddfd „Zed ¦«Gd „d ¦«¦«Z Gd „de ¦«Z dS)a"A file interface for handling local and remote data files. The goal of datasource is to abstract some of the file system operations when dealing with data files so the researcher doesn't have to know all the low-level details. Through datasource, a researcher can obtain and use a file with one function call, regardless of location of the file. DataSource is meant to augment standard python libraries, not replace them. It should work seamlessly with standard file IO operations and the os module. DataSource files can originate locally or remotely: - local files : '/home/guido/src/local/data.txt' - URLs (http, ftp, ...) : 'http://www.scipy.org/not/real/data.txt' DataSource files can also be compressed or uncompressed. Currently only gzip, bz2 and xz are supported. Example:: >>> # Create a DataSource, use os.curdir (default) for local storage. >>> from numpy import DataSource >>> ds = DataSource() >>> >>> # Open a remote file. >>> # DataSource downloads the file, stores it locally in: >>> # './www.google.com/index.html' >>> # opens the file and returns a file object. >>> fp = ds.open('http://www.google.com/') # doctest: +SKIP >>> >>> # Use the file as you normally would >>> fp.read() # doctest: +SKIP >>> fp.close() # doctest: +SKIP éNé)Ú set_modulecó‚—d|vrd|vrtd|›�¦«‚dS|�td¦«‚|�td¦«‚dS)zàCheck mode and that encoding and newline are compatible. Parameters ---------- mode : str File open mode. encoding : str File encoding. newline : str Newline for text files. ÚtÚbzInvalid mode: Nz0Argument 'encoding' not supported in binary modez/Argument 'newline' not supported in binary mode)Ú ValueError©ÚmodeÚencodingÚnewlines úL/opt/cloudlinux/venv/lib64/python3.11/site-packages/numpy/lib/_datasource.pyÚ _check_moder.sf€ð ˆd€{€{Ø �$ˆ;ˆ;Ý�*°4°4Ð9Ñ:Ô:Ð :ð ˆ;ð Ð ÝÐOÑPÔPÐ PØ Ð ÝÐNÑOÔOÐ Oð Ð ócó*—eZdZdZd„Zd„Zd„Zd„ZdS)Ú _FileOpenersa  Container for different methods to open (un-)compressed files. `_FileOpeners` contains a dictionary that holds one method for each supported file format. Attribute lookup is implemented in such a way that an instance of `_FileOpeners` itself can be indexed with the keys of that dictionary. Currently uncompressed files as well as files compressed with ``gzip``, ``bz2`` or ``xz`` compression are supported. Notes ----- `_file_openers`, an instance of `_FileOpeners`, is made available for use in the `_datasource` module. Examples -------- >>> import gzip >>> np.lib._datasource._file_openers.keys() [None, '.bz2', '.gz', '.xz', '.lzma'] >>> np.lib._datasource._file_openers['.gz'] is gzip.open True có:—d|_dtji|_dS)NF)Ú_loadedÚioÚopenÚ _file_openers©Úselfs r Ú__init__z_FileOpeners.__init__cs€ØˆŒ Ø"¥B¤G˜_ˆÔÐÐrcó0—|jrdS ddl}|j|jd<n#t$rYnwxYw ddl}|j|jd<n#t$rYnwxYw ddl}|j|jd<|j|jd<n#ttf$rYnwxYwd|_dS)Nrz.bz2z.gzz.xzz.lzmaT)rÚbz2rrÚ ImportErrorÚgzipÚlzmaÚAttributeError)rrrrs r Ú_loadz_FileOpeners._loadgsô€Ø Œ<ð Ø ˆFð Ø ˆJˆJˆJØ),¬ˆDÔ ˜vÑ &Ð &øÝð ð ð Ø ˆDð øøøð Ø ˆKˆKˆKØ(,¬ ˆDÔ ˜uÑ %Ð %øÝð ð ð Ø ˆDð øøøð Ø ˆKˆKˆKØ(,¬ ˆDÔ ˜uÑ %Ø*.¬)ˆDÔ ˜wÑ 'Ð 'øÝ�^Ð,ð ð ð ð ˆDð øøøð ˆŒ ˆ ˆ s/‹Ÿ ,«,°AÁ AÁAÁ"A8Á8B  B cóv—| ¦«t|j ¦«¦«S)a[ Return the keys of currently supported file openers. Parameters ---------- None Returns ------- keys : list The keys are None for uncompressed files and the file extension strings (i.e. ``'.gz'``, ``'.xz'``) for supported compression methods. )r ÚlistrÚkeysrs r r#z_FileOpeners.keys‚s.€ð � Š ‰ Œ ˆ Ý�DÔ&×+Ò+Ñ-Ô-Ñ.Ô.Ð.rcóD—| ¦«|j|S©N)r r)rÚkeys r Ú __getitem__z_FileOpeners.__getitem__•s€Ø � Š ‰ Œ ˆ ØÔ! #Ô&Ð&rN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr r#r'©rr rrJsZ€€€€€ððð0-ð-ð-ðððð6/ð/ð/ð&'ð'ð'ð'ð'rrÚrcóR—t|¦«}| ||||¬¦«S)a… Open `path` with `mode` and return the file object. If ``path`` is an URL, it will be downloaded, stored in the `DataSource` `destpath` directory and opened from there. Parameters ---------- path : str Local file path or URL to open. mode : str, optional Mode to open `path`. Mode 'r' for reading, 'w' for writing, 'a' to append. Available modes depend on the type of object specified by path. Default is 'r'. destpath : str, optional Path to the directory where the source file gets downloaded to for use. If `destpath` is None, a temporary directory will be created. The default path is the current directory. encoding : {None, str}, optional Open text file with given encoding. The default encoding will be what `io.open` uses. newline : {None, str}, optional Newline to use when reading text file. Returns ------- out : file object The opened file. Notes ----- This is a convenience function that instantiates a `DataSource` and returns the file object from ``DataSource.open(path)``. ©r r )Ú DataSourcer)Úpathr Údestpathr r Údss r rr›s,€õJ �HÑ Ô €BØ �7Š7�4˜¨¸'ˆ7Ñ BÔ BÐBrÚnumpycóp—eZdZdZejfd„Zd„Zd„Zd„Z d„Z d„Z d„Z d „Z d „Zd „Zd „Zd „Zdd„ZdS)r0aû DataSource(destpath='.') A generic data source file (file, http, ftp, ...). DataSources can be local files or remote files/URLs. The files may also be compressed or uncompressed. DataSource hides some of the low-level details of downloading the file, allowing you to simply pass in a valid file path (or URL) and obtain a file object. Parameters ---------- destpath : str or None, optional Path to the directory where the source file gets downloaded to for use. If `destpath` is None, a temporary directory will be created. The default path is the current directory. Notes ----- URLs require a scheme string (``http://``) to be used, without it they will fail:: >>> repos = np.DataSource() >>> repos.exists('www.google.com/index.html') False >>> repos.exists('http://www.google.com/index.html') True Temporary directories are deleted when the DataSource is deleted. Examples -------- :: >>> ds = np.DataSource('/home/guido') >>> urlname = 'http://www.google.com/' >>> gfile = ds.open('http://www.google.com/') >>> ds.abspath(urlname) '/home/guido/www.google.com/index.html' >>> ds = np.DataSource(None) # use with temporary file >>> ds.open('/home/guido/foobar.txt') >>> ds.abspath('/home/guido/foobar.txt') '/tmp/.../home/guido/foobar.txt' có¬—|r-tj |¦«|_d|_dSddl}| ¦«|_d|_dS)z2Create a DataSource with a local path at destpath.FrNT)Úosr1ÚabspathÚ _destpathÚ _istmpdestÚtempfileÚmkdtemp)rr2r;s r rzDataSource.__init__ösQ€à ð #ÝœWŸ_š_¨XÑ6Ô6ˆDŒNØ#ˆDŒOˆOˆOà ˆOˆOˆOØ%×-Ò-Ñ/Ô/ˆDŒNØ"ˆDŒOˆOˆOrcóx—t|d¦«r'|jr"ddl}| |j¦«dSdSdS)Nr:r)Úhasattrr:ÚshutilÚrmtreer9)rr?s r Ú__del__zDataSource.__del__sT€å �4˜Ñ &Ô &ð *¨4¬?ð *Ø ˆMˆMˆMà �MŠM˜$œ.Ñ )Ô )Ð )Ð )Ð )ð *ð *ð *ð *rcó|—tj |¦«\}}|t ¦«vS)zNTest if the filename is a zip file by looking at the file extension. )r7r1Úsplitextrr#)rÚfilenameÚfnameÚexts r Ú_iszipzDataSource._iszips4€õ”W×%Ò% hÑ/Ô/‰ ˆˆsØ•m×(Ò(Ñ*Ô*Ð*Ð*rcó"—d}|D] }||vrdSŒ dS)z4Test if the given mode will open a file for writing.)Úwú+TFr,)rr Ú _writemodesÚcs r Ú _iswritemodezDataSource._iswritemodes4€ð!ˆ Øð ð ˆAØ�KÐÐØ�t�tð àˆurcór—| |¦«rtj |¦«S|dfS)z�Split zip extension from filename and return filename. Returns ------- base, zip_ext : {tuple} N)rGr7r1rC)rrDs r Ú _splitzipextzDataSource._splitzipexts8€ð �;Š;�xÑ Ô ð "Ý”7×#Ò# HÑ-Ô-Ð -à˜T�>Ð !rcó¢—|g}| |¦«s6t ¦«D]}|r| ||z¦«Œ|S)z9Return a tuple containing compressed filename variations.)rGrr#Úappend)rrDÚnamesÚzipexts r Ú_possible_nameszDataSource._possible_names&s\€à� ˆØ�{Š{˜8Ñ$Ô$ð 2Ý'×,Ò,Ñ.Ô.ð 2ð 2�Øð2Ø—L’L ¨&¡Ñ1Ô1Ð1øØˆ rcóT—ddlm}||¦«\}}}}}}t|o|¦«S)z=Test if path is a net location. Tests the scheme and netloc.r©Úurlparse)Ú urllib.parserWÚbool) rr1rWÚschemeÚnetlocÚupathÚuparamsÚuqueryÚufrags r Ú_isurlzDataSource._isurl/sF€ð *Ð)Ð)Ð)Ð)Ð)ð9A¸À¹¼Ñ5ˆ�˜˜w¨°Ý�FÐ%˜vÑ&Ô&Ð&rcó8—ddl}ddlm}| |¦«}tj tj |¦«¦«s1t jtj |¦«¦«|  |¦«rb||¦«5}t|d¦«5}|  ||¦«ddd¦«n #1swxYwYddd¦«n #1swxYwYn|  ||¦«|S)zhCache the file specified by path. Creates a copy of the file in the datasource cache. rN©ÚurlopenÚwb) r?Úurllib.requestrcr8r7r1ÚexistsÚdirnameÚmakedirsr`Ú_openÚ copyfileobjÚcopyfile)rr1r?rcr\Ú openedurlÚfs r Ú_cachezDataSource._cache>s~€ð ˆ ˆ ˆ Ø*Ð*Ð*Ð*Ð*Ð*à— ’ ˜TÑ"Ô"ˆõŒw�~Š~�bœgŸošo¨eÑ4Ô4Ñ5Ô5ð 0Ý ŒK�œŸš¨Ñ.Ô.Ñ /Ô /Ð /ð �;Š;�tÑ Ô ð )Ø�˜‘”ð 5 )ݘ5 $Ñ'Ô'ð5¨1Ø×&Ò& y°!Ñ4Ô4Ð4ð5ð5ð5ñ5ô5ð5ð5ð5ð5ð5ð5øøøð5ð5ð5ð5ð 5ð 5ð 5ñ 5ô 5ð 5ð 5ð 5ð 5ð 5ð 5øøøð 5ð 5ð 5ð 5øð �OŠO˜D %Ñ (Ô (Ð (؈ s6Â-C8Â>C!à C8Ã!C% Ã%C8Ã(C% Ã)C8Ã8C<Ã?C<có—| |¦«sA| |¦«}|| | |¦«¦«z }n@| | |¦«¦«}|| |¦«z}|D]E}| |¦«r.| |¦«r| |¦«}|cSŒFdS)aySearches for ``path`` and returns full path if found. If path is an URL, _findfile will cache a local copy and return the path to the cached file. If path is a local file, _findfile will return a path to that local file. The search will include possible compressed versions of the file and return the first occurrence found. N)r`rTr8rfrn)rr1ÚfilelistÚnames r Ú _findfilezDataSource._findfileXsç€ð�{Š{˜4Ñ Ô ð =à×+Ò+¨DÑ1Ô1ˆHà ˜×,Ò,¨T¯\ª\¸$Ñ-?Ô-?Ñ@Ô@Ñ @ˆHˆHð×+Ò+¨D¯LªL¸Ñ,>Ô,>Ñ?Ô?ˆHà $×"6Ò"6°tÑ"<Ô"<Ñ<ˆHàð ð ˆDØ�{Š{˜4Ñ Ô ð Ø—;’;˜tÑ$Ô$ð-ØŸ;š; tÑ,Ô,�DØ� � � ð ðˆtrcó>—ddlm}| |jd¦«}t |¦«dkr|d}||¦«\}}}}}} | |¦«}| |¦«}t j |j||¦«S)aF Return absolute path of file in the DataSource directory. If `path` is an URL, then `abspath` will return either the location the file exists locally or the location it would exist when opened using the `open` method. Parameters ---------- path : str Can be a local file or a remote URL. Returns ------- out : str Complete path, including the `DataSource` destination directory. Notes ----- The functionality is based on `os.path.abspath`. rrVré) rXrWÚsplitr9ÚlenÚ_sanitize_relative_pathr7r1Újoin) rr1rWÚ splitpathrZr[r\r]r^r_s r r8zDataSource.abspathws¡€ð0 *Ð)Ð)Ð)Ð)Ð)ð—J’J˜tœ~¨qÑ1Ô1ˆ Ý ˆy‰>Œ>˜AÒ Ð Ø˜Q”<ˆDØ8@¸À¹¼Ñ5ˆ�˜˜w¨°Ø×-Ò-¨fÑ5Ô5ˆØ×,Ò,¨UÑ3Ô3ˆÝŒw�|Š|˜DœN¨F°EÑ:Ô:Ð:rcóp—d}tj |¦«}||krŽ|}| tj¦« d¦«}| tj¦« d¦«}tj |¦«\}}||k°Ž|S)zvReturn a sanitised relative path for which os.path.abspath(os.path.join(base, path)).startswith(base) Nú/z..)r7r1ÚnormpathÚlstripÚsepÚpardirÚ splitdrive)rr1ÚlastÚdrives r rwz"DataSource._sanitize_relative_path¡s™€ðˆÝŒw×Ò Ñ%Ô%ˆØ�dŠlˆl؈Dà—;’;�rœvÑ&Ô&×-Ò-¨cÑ2Ô2ˆDØ—;’;�rœyÑ)Ô)×0Ò0°Ñ6Ô6ˆDÝœ'×,Ò,¨TÑ2Ô2‰KˆE�4ð �dŠlˆlð ˆ rcóT—tj |¦«rdSddlm}ddlm}| |¦«}tj |¦«rdS| |¦«r/ ||¦«}|  ¦«~dS#|$rYdSwxYwdS)a3 Test if path exists. Test if `path` exists as (and in this order): - a local file. - a remote URL that has been downloaded and stored locally in the `DataSource` directory. - a remote URL that has not been downloaded, but is valid and accessible. Parameters ---------- path : str Can be a local file or a remote URL. Returns ------- out : bool True if `path` exists. Notes ----- When `path` is an URL, `exists` will return True if it's either stored locally in the `DataSource` directory, or is a valid remote URL. `DataSource` does not discriminate between the two, the file is accessible if it exists in either location. Trrb)ÚURLErrorF) r7r1rfrercÚ urllib.errorr„r8r`Úclose)rr1rcr„r\Únetfiles r rfzDataSource.exists¯sØ€õ@ Œ7�>Š>˜$Ñ Ô ð Ø�4ð +Ð*Ð*Ð*Ð*Ð*Ø)Ð)Ð)Ð)Ð)Ð)ð— ’ ˜TÑ"Ô"ˆÝ Œ7�>Š>˜%Ñ Ô ð Ø�4ð �;Š;�tÑ Ô ð ð Ø!˜' $™-œ-�Ø— ’ ‘”�ØØ�tøØð ð ð Ø�u�uð øøøàˆusÁ: BÂB%Â$B%r-Ncó`—| |¦«r$| |¦«rtd¦«‚| |¦«}|rM| |¦«\}}|dkr| dd¦«t |||||¬¦«St|›d�¦«‚)aD Open and return file-like object. If `path` is an URL, it will be downloaded, stored in the `DataSource` directory and opened from there. Parameters ---------- path : str Local file path or URL to open. mode : {'r', 'w', 'a'}, optional Mode to open `path`. Mode 'r' for reading, 'w' for writing, 'a' to append. Available modes depend on the type of object specified by `path`. Default is 'r'. encoding : {None, str}, optional Open text file with given encoding. The default encoding will be what `io.open` uses. newline : {None, str}, optional Newline to use when reading text file. Returns ------- out : file object File object. zURLs are not writeablerrJÚr z not found.)r`rMrrrrOÚreplacerÚFileNotFoundError)rr1r r r ÚfoundÚ_fnamerFs r rzDataSource.opençs΀ðD �;Š;�tÑ Ô ð 7 ×!2Ò!2°4Ñ!8Ô!8ð 7ÝÐ5Ñ6Ô6Ð 6ð—’˜tÑ$Ô$ˆØ ð :Ø×+Ò+¨EÑ2Ô2‰KˆF�CØ�eŠ|ˆ|Ø— ’ ˜S "Ñ%Ô%Ð%Ý  Ô% e°$Ø/7ÀðJñJôJð Jõ$ tÐ$8Ð$8Ð$8Ñ9Ô9Ð 9r©r-NN)r(r)r*r+r7ÚcurdirrrArGrMrOrTr`rnrrr8rwrfrr,rr r0r0Äsõ€€€€€ð.ð.ð`!#¤ ð#ð#ð#ð#ð*ð*ð*ð+ð+ð+ðððð "ð "ð "ðððð 'ð 'ð 'ðððð4ððð>(;ð(;ð(;ðT ð ð ð6ð6ð6ðp.:ð.:ð.:ð.:ð.:ð.:rr0cóR—eZdZdZejfd„Zd„Zd„Zd„Z d„Z d„Z d d „Z d „Z d S) Ú Repositorya Repository(baseurl, destpath='.') A data repository where multiple DataSource's share a base URL/directory. `Repository` extends `DataSource` by prepending a base URL (or directory) to all the files it handles. Use `Repository` when you will be working with multiple files from one base URL. Initialize `Repository` with the base URL, then refer to each file by its filename only. Parameters ---------- baseurl : str Path to the local directory or remote location that contains the data files. destpath : str or None, optional Path to the directory where the source file gets downloaded to for use. If `destpath` is None, a temporary directory will be created. The default path is the current directory. Examples -------- To analyze all files in the repository, do something like this (note: this is not self-contained code):: >>> repos = np.lib._datasource.Repository('/home/user/data/dir/') >>> for filename in filelist: ... fp = repos.open(filename) ... fp.analyze() ... fp.close() Similarly you could use a URL for a repository:: >>> repos = np.lib._datasource.Repository('http://www.xyz.edu/data') cóL—t ||¬¦«||_dS)z>Create a Repository with a shared url or directory of baseurl.)r2N)r0rÚ_baseurl)rÚbaseurlr2s r rzRepository.__init__@s&€å×Ò˜D¨8ÐÑ4Ô4Ð4؈Œ ˆ ˆ rcó:—t |¦«dSr%)r0rArs r rAzRepository.__del__Es€Ý×Ò˜4Ñ Ô Ð Ð Ð rcó²—| |jd¦«}t|¦«dkr&tj |j|¦«}n|}|S)z>Return complete path for path. Prepends baseurl if necessary.rrt)rur“rvr7r1rx)rr1ryÚresults r Ú _fullpathzRepository._fullpathHsK€à—J’J˜tœ}¨aÑ0Ô0ˆ Ý ˆy‰>Œ>˜QÒ Ð Ý”W—\’\ $¤-°Ñ6Ô6ˆFˆFàˆF؈ rcó^—t || |¦«¦«S)z8Extend DataSource method to prepend baseurl to ``path``.)r0rrr˜©rr1s r rrzRepository._findfileQs$€å×#Ò# D¨$¯.ª.¸Ñ*>Ô*>Ñ?Ô?Ð?rcó^—t || |¦«¦«S)ak Return absolute path of file in the Repository directory. If `path` is an URL, then `abspath` will return either the location the file exists locally or the location it would exist when opened using the `open` method. Parameters ---------- path : str Can be a local file or a remote URL. This may, but does not have to, include the `baseurl` with which the `Repository` was initialized. Returns ------- out : str Complete path, including the `DataSource` destination directory. )r0r8r˜ršs r r8zRepository.abspathUs&€õ*×!Ò! $¨¯ª°tÑ(<Ô(<Ñ=Ô=Ð=rcó^—t || |¦«¦«S)aÕ Test if path exists prepending Repository base URL to path. Test if `path` exists as (and in this order): - a local file. - a remote URL that has been downloaded and stored locally in the `DataSource` directory. - a remote URL that has not been downloaded, but is valid and accessible. Parameters ---------- path : str Can be a local file or a remote URL. This may, but does not have to, include the `baseurl` with which the `Repository` was initialized. Returns ------- out : bool True if `path` exists. Notes ----- When `path` is an URL, `exists` will return True if it's either stored locally in the `DataSource` directory, or is a valid remote URL. `DataSource` does not discriminate between the two, the file is accessible if it exists in either location. )r0rfr˜ršs r rfzRepository.existsls'€õ@× Ò   t§~¢~°dÑ';Ô';Ñ<Ô<Ðð>ð>ð. =ð =ð =ðDCðCðCðCð@-ð-ð-ð-ð-rr‘) r+r7rÚ_utilsrrrirrrr�r0r‘r,rr úr¢s7ðð#ð#ðH € € € Ø € € € àÐÐÐÐÐð €ðPðPðPð8M'ðM'ðM'ðM'ðM'ñM'ôM'ðM'ð^� ‘”€ à "¤)°dÀDð&Cð&Cð&Cð&CðR €ˆGÑÔðP:ðP:ðP:ðP:ðP:ñP:ôP:ñÔðP:ðf h-ðh-ðh-ðh-ðh-�*ñh-ôh-ðh-ðh-ðh-r