U C^!@sddlmZmZmZddlmZddlZddlmZm Z ddl m Z Gddde Z Gd d d e ZGd d d eZdddZdS))FTPError error_permtimeoutN)AbstractBufferedFileAbstractFileSystem)infer_storage_optionscseZdZdZdZdZd$fdd Zd d Zed d Z e ddZ d%ddZ d&ddZ ddZd'ddZddZddZddZd d!Zd"d#ZZS)( FTPFileSystemzA filesystem over classic /FN/tmpc  sXtt|jf| ||_||_||_|||f|_||_|dk rF||_nd|_| dS)a You can use _get_kwargs_from_urls to get some kwargs from a reasonable FTP url. Authentication will be anonymous if username/password are not given. Parameters ---------- host: str The remote server name/ip to connect to port: int Port to connect with username: str or None If authenticating, the user's identifier password: str of None User's password on the server, if using acct: str or None Some servers also need an "account" string for auth block_size: int or None If given, the read-ahead or write buffer size. tempdir: str Directory on remote to put temporary files when in a transaction Ni) superr __init__hostporttempdircredr blocksize_connect) selfrrusernamepasswordZacct block_sizerrkwargs __class__>/tmp/pip-install-6_kvzl1k/fsspec/fsspec/implementations/ftp.pyrs$ zFTPFileSystem.__init__cCs2t|jd|_|j|j|j|jj|jdS)Nr)rrftpconnectrrloginrrrrr r>szFTPFileSystem._connectcCsdt|dddS)Nr path)r lstriprstrip)clsr%rrr _strip_protocolCszFTPFileSystem._strip_protocolcCs$t|}|dd|dd|S)Nr%protocol)r pop)Zurlpathoutrrr _get_kwargs_from_urlsGs  z#FTPFileSystem._get_kwargs_from_urlscCs&|dk r|j|dn |jdSN)dircacher+clearrr%rrr invalidate_cacheNszFTPFileSystem.invalidate_cacheTc CsL||}g}||jkrzzdd|j|D}Wn tk rVt|j|}YnX|D]R\}}|dkrpd}d||dg|d<|ddkrt|d|d<q\d |d<q\||j|<WnZt k rz$| |}|ddkr||fg}Wnt t fk rt YnXYnX|j ||}|s>td d|DSd d|DS) NcSs,g|]$\}}|dkr|ddkr||fqS)).z..type)Zpdircdirr.0fndetailsrrr Zs z$FTPFileSystem.ls..r namer4filesizercSsg|] \}}|qSrrr6rrr r:tscSsg|] \}}|qSrrr6rrr r:us)r)r/r!Zmlsdr_mlsd2joinr&intrinfo IndexErrorFileNotFoundErrorgetsorted)rr%Zdetailr,r8r9rBfilesrrr lsTs:        zFTPFileSystem.lsc s^|||dd}zfdd|Dd}Wntk rXtYnX|S)Nr Tcsg|]}|dkr|qS)r<r)r7fr%rr r:|s z&FTPFileSystem.info..r)r)rH_parentr&rCrD)rr%rrGr,rrJr rBws zFTPFileSystem.inforbc Ks,||}|p|j}t|||||j||dS)N)moderr autocommit cache_options)r)rFTPFiler)rr%rMrrOrNrrrr _opens  zFTPFileSystem._opencCs0||}|j|||ddddS)Nr r)r)r!deleter2rsplitr1rrr _rms  zFTPFileSystem._rmcKs||}|j|dSr.)r)r!Zmkd)rr%rrrr mkdirs zFTPFileSystem.mkdircCs||}|j|dSr.)r)r!Zrmdr1rrr rmdirs zFTPFileSystem.rmdircKsF||}||}|j||||||||dSr.)r)r!renamer2rK)rpath1path2rrrr mvs   zFTPFileSystem.mvcCs|jdSr.)r!closer$rrr __del__szFTPFileSystem.__del__)r NNNNrr)N)T)rLNNT)__name__ __module__ __qualname____doc__Z root_markerZcachablerr classmethodr) staticmethodr-r2rHrBrQrUrVrWr[r] __classcell__rrrr r s:0    #  r c@seZdZdZdS) TransferDonez+Internal exception to break out of transferN)r^r_r`rarrrr resrecsDeZdZdZdfdd Zd d Zd d Zd dZdddZZ S)rPz9Interact with a remote FTP file with read/write bufferingrLdefaultT readaheadNc  sNtj||f|||||d||sJ|j|_d|dttg|_dS)N)rMrrN cache_typerOr r)rrr%targetr@struuidZuuid4) rfsr%rMrrNrhrOrrrr rs  zFTPFile.__init__cCs|j|j|jdSr.)rlr[r%rir$rrr commitszFTPFile.commitcCs|j|jdSr.)rlZrmr%r$rrr discardszFTPFile.discardc sgdgfdd}z"|jjjd|j|j|dWnPtk rz|jj|jjWntk r|j YnXYnXd S)aGet bytes between given byte limits Implemented by raising an exception in the fetch callback when the number of bytes received reaches the requested amount. Will fail if the server does not respect the REST command on retrieve requests. rcsjdt|7<dkrH|ddtn |dkrftdS)Nr)lenappendre)xendr,starttotalrr callbacks z&FTPFile._fetch_range..callbackzRETR %s)rrestrv) rlr!Z retrbinaryr%rreabortZvoidresprrr@)rrtrsrvrrrr _fetch_ranges"    zFTPFile._fetch_rangeFcCs2|jd|jjjd|j|j|j|jddS)NrzSTOR )rrwT)bufferseekrlr!Z storbinaryr%roffset)rfinalrrr _upload_chunks zFTPFile._upload_chunk)rLrfTrgN)F) r^r_r`rarrmrnrzrrdrrrr rPs&rPr3cCsg}g}|||j|D]z}|}|dd|dd|d|d|d|dd f}d |d d dkr~d |d d<n d|d d<||q|S)a( Fall back to using `dir` instead of `mlsd` if not supported. This parses a Linux style `ls -l` response to `dir`, but the response may be platform dependent. Parameters ---------- ftp: ftplib.FTP path: str Expects to be given path, but defaults to ".".  rr)modifyz unix.ownerz unix.group unix.moder>drRrdirr4r=)rrpsplitr@)r!r%linesZminfolinethisrrr r?s$    r?)r3)ftplibrrrsocketrrkspecrr utilsr r ExceptionrerPr?rrrr s  'P