L i;dZddlZddlZddlZddlmZmZddlZddlZ ddl m Z ddl m Z Gdde Zd Zd Zd ZGd d Zy)z This module contains SMBFileSystem class responsible for handling access to Windows Samba network shares by using package smbprotocol N)S_ISDIRS_ISLNK)AbstractFileSysteminfer_storage_optionsceZdZdZdZ dfd ZedZdZe dZ e dZ ddZ dd Zd Zd Zd Zd ZddZ ddZdZdZddZxZS) SMBFileSystemaaAllow reading and writing to Windows and Samba network shares. When using `fsspec.open()` for getting a file-like object the URI should be specified as this format: ``smb://workgroup;user:password@server:port/share/folder/file.csv``. Example:: >>> import fsspec >>> with fsspec.open( ... 'smb://myuser:mypassword@myserver.com/' 'share/folder/file.csv' ... ) as smbfile: ... df = pd.read_csv(smbfile, sep='|', header=None) Note that you need to pass in a valid hostname or IP address for the host component of the URL. Do not use the Windows/NetBIOS machine name for the host component. The first component of the path in the URL points to the name of the shared folder. Subsequent path components will point to the directory/folder/file. The URL components ``workgroup`` , ``user``, ``password`` and ``port`` may be optional. .. note:: For working this source require `smbprotocol`_ to be installed, e.g.:: $ pip install smbprotocol # or # pip install smbprotocol[kerberos] .. _smbprotocol: https://github.com/jborean93/smbprotocol#requirements Note: if using this with the ``open`` or ``open_files``, with full URLs, there is no way to tell if a path is relative, so all paths are assumed to be absolute. smbc  Lt |di| ||_||_||_||_||_||_| jdd|_ ||_ ||_ | dkr td| |_ | dkr td| |_| |_|j!y) a& You can use _get_kwargs_from_urls to get some kwargs from a reasonable SMB url. Authentication will be anonymous or integrated if username/password are not given. Parameters ---------- host: str The remote server name/ip to connect to port: int or None Port to connect with. Usually 445, sometimes 139. username: str or None Username to connect with. Required if Kerberos auth is not being used. password: str or None User's password on the server, if using username timeout: int Connection timeout in seconds encrypt: bool Whether to force encryption or not, once this has been set to True the session cannot be changed back to False. share_access: str or None Specifies the default access applied to file open operations performed with this file system object. This affects whether other processes can concurrently open a handle to the same file. - None (the default): exclusively locks the file until closed. - 'r': Allow other handles to be opened with read access. - 'w': Allow other handles to be opened with write access. - 'd': Allow other handles to be opened with delete access. register_session_retries: int Number of retries to register a session with the server. Retries are not performed for authentication errors, as they are considered as invalid credentials and not network issues. If set to negative value, no register attempts will be performed. register_session_retry_wait: int Time in seconds to wait between each retry. Number must be non-negative. register_session_retry_factor: int Base factor for the wait time between each retry. The wait time is calculated using exponential function. For factor=1 all wait times will be equal to `register_session_retry_wait`. For any number of retries, the last wait time will be equal to `register_session_retry_wait` and for retries>1 the first wait time will be equal to `register_session_retry_wait / factor`. Number must be equal to or greater than 1. Optimal factor is 10. auto_mkdir: bool Whether, when opening a file, the directory containing it should be created (if it doesn't already exist). This is assumed by pyarrow and zarr-python code. temppathrz:register_session_retry_wait must be a non-negative integerzSregister_session_retry_factor must be a positive integer equal to or greater than 1N)super__init__hostportusernamepasswordtimeoutencryptpopr share_accessregister_session_retries ValueErrorregister_session_retry_waitregister_session_retry_factor auto_mkdir_connect)selfrrrrrrrrrrrkwargs __class__s `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/fsspec/implementations/smb.pyrzSMBFileSystem.__init__?sB "6"         :r2 ((@% & *L ,G( (1 ,5 .K*$ c6|jdS|jS)Nrr!s r$_portzSMBFileSystem._portsii's6TYY6r%c Zddl}|jdkryg}|j|jdz |jt fdt ddzD}t |jdzD][} t j|j|j|j|j|j|jy|d#tjj tjj"f$rt$$r<}t'j(dt+|r|j-|nYd}~n*d}~wt.$r}|j-|Yd}~nd}~wwxYw||jks|j1t3|1)Nrrc3:K|]}|z dz zzyw)rNr).0nfactorn_waits wait_times r$ z)SMBFileSystem._connect..s( 89Fq7{Q ') 3 s)rrrrconnection_timeoutz \[Errno -\d+])timerrriterrange smbclientregister_sessionrrrr*rr smbprotocol exceptionsSMBAuthenticationError LogonFailurerrefindallstrappend Exceptionsleepnext) r!r5retried_errors wait_timesattemptexcr0r1r2s @@@r$r zSMBFileSystem._connects  ( (B . 44  ) )A - 33  =B1gPQk=R  T::Q>?" -G +**II!]]!]] LL'+|| " -LR  5&&==&&33   ::.C9#))#. /  +%%c** + 666 4 +,s&ACAE=2E E="E88E=ct|dS)Npathr)clsrJs r$_strip_protocolzSMBFileSystem._strip_protocols$T*622r%cdt|}|jdd|jdd|S)NrJprotocol)rr)rJouts r$_get_kwargs_from_urlsz#SMBFileSystem._get_kwargs_from_urlss/$D)   D! r%c t|j|}|r$tj|fd|jd|ytj |fd|ji|y)NFexist_okrr) _as_unc_pathrr8makedirsr*mkdir)r!rJcreate_parentsr"wpaths r$rVzSMBFileSystem.mkdirsMTYY-    u Pu4:: P P OOE = =f =r%ct|r9t|j|}tj|||j yy)NrR)_share_has_pathrTrr8rUr*)r!rJrSrXs r$rUzSMBFileSystem.makedirss5 4  D1E   uxdjj I !r%ct|r8t|j|}tj||j yyNr()rZrTrr8rmdirr*)r!rJrXs r$r]zSMBFileSystem.rmdirs1 4  D1E OOE 3 !r%c ft|j|}tj|fd|ji|}t |j rd}nt|j rd}nd}|dk(r|dzn||j||j|j|j|jd}|S)Nr directorylinkfile/)namesizetypeuidgidr5mtime) rTrr8statr*rst_moderst_sizest_uidst_gidst_atimest_mtime)r!rJr"rXstatsstyperess r$infozSMBFileSystem.infosTYY-u@4::@@ 5== !E U]] #EE"';"6D3JDMM<<<<NN^^  r%ct|j|}tj||j}t j j |jt jjS)z=Return the created timestamp of a file as a datetime.datetimer(tz) rTrr8rir*datetime fromtimestampst_ctimetimezoneutcr!rJrXrps r$createdzSMBFileSystem.created RTYY-u4::6  ..u~~(BSBSBWBW.XXr%ct|j|}tj||j}t j j |jt jjS)z>Return the modified timestamp of a file as a datetime.datetimer(ru) rTrr8rir*rwrxrorzr{r|s r$modifiedzSMBFileSystem.modifiedr~r%c (t|j|}tj|fd|ji|}|Dcgc]$}dj |j d|g&}}|r|Dcgc]}|j|}}|Scc}wcc}w)Nrrb)rTrr8listdirr*joinrstriprs) r!rJdetailr"unclistedpdirsds r$lszSMBFileSystem.lss499d+""3BTZZB6B9?@A$++c*A./@@ *./QDIIaL/D/ A/s )B .Bc |jr&d|vr"|j|j|d||dk\r|nd}t|j|}|j d|j } d|vr@|dur1, buffer that many bytes Notes ----- By specifying 'share_access' in 'kwargs' it is possible to override the default shared access setting applied in the constructor of this object. wTrSrr,rF)r block_size) bufferingrr) rrU_parentrTrrr _as_temp_pathr SMBFileOpenerr*r8 open_file) r!rJmoder autocommit cache_optionsr"blsrXrtemps r$_openzSMBFileSystem._open s$ ??sd{ MM$,,t,tM <&2zQjBTYY-zz.$2C2CD $;:. D$--@D tT(, sFL ""   %     r%c t|j|}t|j|}|jr"|j|j |dt j ||fd|ji|y)z0Copy within two locations in the same filesystemTrrN)rTrrrUrr8copyfiler*)r!path1path2r"wpath1wpath2s r$copyzSMBFileSystem.copyEs`dii/dii/ ?? MM$,,u-M =66E EfEr%c:t|rt|j|}tj||j }t |jr"tj||j ytj||j yyr\) rZrTrr8rir*rrjr]remover|s r$_rmzSMBFileSystem._rmMse 4  D1ENN5tzz:Eu}}%DJJ7  TZZ8 !r%c t|j|}t|j|}tj||fd|ji|y)Nr)rTrr8renamer*)r!rr recursivemaxdepthr"rrs r$mvzSMBFileSystem.mvVs@dii/dii/CdjjCFCr%) NNN<NNr F)T)F)rbr,TN)NN)__name__ __module__ __qualname____doc__rNrpropertyr*r classmethodrL staticmethodrPrVrUr]rsr}rrrrrr __classcell__)r#s@r$r r s%NH !"$%&(Wr77<!|33>J 4 (Y Y  # JF9Dr%r c8|jdd}d||}|S)Nrb\z\\)replace)rrJrpathrs r$rTrT\s' LLd #E ug C Jr%c~|jdd}d||dtj}t||}|S)Nrbr)splituuiduuid4rT)rrJr share temp_filers r$rrbsA JJsOA EE78*Adjjl^4I tY 'C Jr%cZ|jd}|jdr|dkDS|dkDS)Nrbrr)countendswith)rJpartss r$rZrZis/ JJsOE }}Sqy 19r%cHeZdZdZd dZdZdZdZdZdZ dZ d Z d Z y ) rz/writes to remote temporary file, move on commitc ||_||_||_||_||_d|_d|_||_|jyNF) rJrrrr"smbfile _incontextrr)r!rJrrrrr"s r$rzSMBFileOpener.__init__ssC   $    r%c|j|jjrRtj|j|j f|j |jd|j|_yy)N)rr) rclosedr8rrrrrr"r)s r$rzSMBFileOpener._open~s` << 4<<#6#6$..  YY//  ++ DL$7r%cptj|j|j|jy)z(Move temp file to definitive on success.r(N)r8rrrJrr)s r$commitzSMBFileOpener.commits" $))TYYTYY?r%cZtj|j|jy)z Remove the temp file on failure.r(N)r8rrrr)s r$discardzSMBFileOpener.discards3r%c|jSN)rJr)s r$ __fspath__zSMBFileOpener.__fspath__s yyr%c6|jjSr)r__iter__r)s r$rzSMBFileOpener.__iter__s||$$&&r%c.t|j|Sr)getattrr)r!items r$ __getattr__zSMBFileOpener.__getattr__st||T**r%cDd|_|jjS)NT)rr __enter__r)s r$rzSMBFileOpener.__enter__s||%%''r%cLd|_|jj|||yr)rr__exit__)r!exc_type exc_value tracebacks r$rzSMBFileOpener.__exit__s h 9=r%N)r'r,) rrrrrrrrrrrrrrr%r$rrps39 @ 4'+(>r%r)rrwr>rrirrr8smbprotocol.exceptionsr:rrutilsrr rTrrZrrr%r$rsO  !!) ED&EDP  0>0>r%