L i )ddlmZddlZddlmZmZddlmZddlmZddl m Z m Z ddl m Z mZddlmZdd lmZdd lmZej*d ZGd d eZGddeZy)) annotationsN)datetimetimezone) ENOTEMPTY)BytesIO)PurePathPureWindowsPath)AnyClassVar)AbstractFileSystem)LocalFileSystem)stringify_pathzfsspec.memoryfsceZdZUdZiZded<dgZdZdZe dZ ddZ dd Z dd Z dd Zd Zd Z ddZdZddZdZdZdZdZddZy)MemoryFileSystemzA filesystem based on a dict of BytesIO objects This is a global filesystem so instances of this class all point to the same in memory filesystem. zClassVar[dict[str, Any]]storememory/c(t|tr0t|trtj|St |}|j d}d|vsd|vr|jdS|jdjd}|rd|zSdS)Nz memory://z::z://rr) isinstancerr r _strip_protocolr removeprefixrstriplstrip)clspaths c/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/fsspec/implementations/memory.pyrz MemoryFileSystem._strip_protocols dH %$0&66t<<%d+  - 4<5D=;;s# #{{3&&s+!sTz)r)c |j|}||jvrI|s|gS||j|jd|j|jj dgSt }|dz}g}t |jD]}|j|sd|t|dvrS|j||j|jd|j|jj dxt|t|kDs||t|djdddz}||vs|xsg}|j|ddd|j||jD]}|j|sd|t|dvr,||vs*|j|ddd|j|Q||t|djdddz}||vsz|j|ddd|j||s||jvrgSt||r|St|D cgc]} | d c} Scc} w) Nfilenamesizetypecreatedrr directoryr"r#r$r")rrr#r% timestampsettuple startswithlenappendsplitadd pseudo_dirsFileNotFoundErrorsorted) selfrdetailkwargspathsstarteroutp2ppathfs rlszMemoryFileSystem.ls+sa##D) 4:: v ! JJt,11"#zz$/77AAC  * # )B}}W%bW00JJ$&$(JJrN$7$7$*'+zz"~'='='G'G'I Ws7|+#bW&8&>&>sA&Fq&IIEE)!iR (-()(3 %(1 )2"" )B}}W%bW00 B;#OP " $bW&8&>&>sA&Fq&IIEE) E1k#RS %( )t''' #D) ) J#.Qqy.//.s I!c ,|j|}||jvs||jvr t||j |j dr:|j |j |rt|j ||rD|j |j dr$ |j|j ||fi||r+||jvr|jj|yyy#t$rY9wxYw)Nr) rrr1FileExistsError_parentstripisfileNotADirectoryErrormkdirr.)r4rcreate_parentsr6s rrDzMemoryFileSystem.mkdirks##D) 4:: )9)9!9!$' ' <<  # #C (T[[d9K-L$T\\$%78 8 dll4066s;  4<<-~HH D 0 00    # #D )14#  s6#D DDcP |j|dy#t$r|sYywxYw)NT)rE)rDr?)r4rexist_oks rmakedirszMemoryFileSystem.makedirsys1  JJtDJ 1  s  %%c >|dk(rdnd}|j|||y)zSSet the bytes of given file Avoids copies of the data if possible createxbwb)modedataN)open)r4rvaluerMr6s r pipe_filezMemoryFileSystem.pipe_files$ x'tT $T .rc|j|}|dk(ry||jvr>|j|s|jj|yt t d|t |)NrzDirectory not empty)rr1r=removeOSErrorrr2r4rs rrmdirzMemoryFileSystem.rmdirsd##D) 2:  4## #774=  ''-i)>EE#D) )rc jtjd|j|jvs4t fdt |j |jzDrdddS|j vr+|j }|jdt|dddSt) Nzinfo: %sc3FK|]}|jdzyw)rN)r,).0prs r z(MemoryFileSystem.info..s#+ )*ALL $+ s!rr'r(r r%r!) loggerdebugrr1anylistrr#getattrr2)r4rr6filelikes ` rinfozMemoryFileSystem.infos Z&##D) 4## #s+ .24::.>AQAQ.Q+ ( #  TZZ zz$'H "8Y=  $D) )rNc |j|}d|vr|j|rt||jvr t ||}t |dkDr<|j |}|j|r t|t |dkDr<|dvrS||jvr:|j|}d|vr|jdd|S|jd|St||dvrUd|vr|j|rtt|||jd} |js| j| S|jj } t#d | d |) Nxr&)rbabzr+bza+bar>w+bx+brLrKrNzunsupported file mode for z: )rexistsr?r1IsADirectoryErrorr-r@rBrseekr2 MemoryFileget_intranscommit __class____name__ ValueError) r4rrM block_size autocommit cache_optionsr6parentr<mr"s r_openzMemoryFileSystem._opensY##D) $;4;;t,! ! 4## ##D) )&kAo\\&)F{{6"%f--&kAo - -tzz!JJt$$;FF1aLFF1I'-- / /d{t{{40%%4vzz&'9:A== H>>**D9$r$JK Krc b|j|}|j|}|j|r6t|||j|j |j|<y|j |r+||j vr|j j|yyt|N) rrBrnrgetvalueisdirr1r.r2)r4path1path2r6s rcp_filezMemoryFileSystem.cp_files$$U+$$U+ ;;u  *eTZZ.779!DJJu ZZ D,,,  ''.-$E* *rc tjd||j|} t|j|j ||S#t $r}t||d}~wwxYw)Nzcat: %s)r\r]rbytesr getbufferKeyErrorr2)r4rstartendr6es rcat_filezMemoryFileSystem.cat_filesf Y%##D) 1D)335eC@A A 1#D)q 0 1s(A A, A''A,c||j|} |j|=y#t$r}t||d}~wwxYwr|)rrrr2r4rrs r_rmzMemoryFileSystem._rms@##D) 1 4  1#D)q 0 1s ! ; 6;c|j|} |j|jS#t$r}t ||d}~wwxYwr|)rrmodifiedrr2rs rrzMemoryFileSystem.modifiedsI##D) 1::d#,, , 1#D)q 0 1, A AAc|j|} |j|jS#t$r}t ||d}~wwxYwr|)rrr%rr2rs rr%zMemoryFileSystem.createdsI##D) 1::d#++ + 1#D)q 0 1rc@|j|}||jvSr|)rrrUs rrBzMemoryFileSystem.isfiles!##D)tzz!!rcbt|tr|j|}n|Dcgc]}|j|}}|j|||}t |D]H}|j |r|j |&|j|s8|j|Jycc}w)N) recursivemaxdepth) rstrr expand_pathreversedrBrm_filerkrV)r4rrrrZr7s rrmzMemoryFileSystem.rms dC ''-D59:D((+:D:  X N% A{{1~ Q [[^ 1  ;sB,)T)F) overwrite)reNTN)NN)FN)rs __module__ __qualname____doc__r__annotations__r1protocol root_marker classmethodrr=rDrHrQrVrbrzrrrrr%rBrrrrrs ')E #($KHK * *>0@ */ **2 (LT +1111"rrcLeZdZdZdfd ZedZdZdZdZ dZ xZ S) rnzA BytesIO which can't close and works as a context manager Can initialise with data. Each path should only be active once at any moment. No need to provide fs, path if auto-committing (default) c8tjd|||_||_t j t j|_t j t j|_ |r!t|-||jdyy)Nz open file %stzr) r\r]fsrrnowrutcr%rsuper__init__rm)r4rrrNrrs rrzMemoryFile.__init__sg ^T* ||x||4   5  G T " IIaL rc6|jjSr|)rnbytesr4s rr#zMemoryFile.size(s~~&&&rc|Sr|rrs r __enter__zMemoryFile.__enter__,s rcyr|rrs rclosezMemoryFile.close/ rcyr|rrs rdiscardzMemoryFile.discard2rrc||jj|j<tjt j |_y)Nr)rrrrrrrrrs rrqzMemoryFile.commit5s,#' dii   5 r)NNN) rsrrrrpropertyr#rrrrq __classcell__)rrs@rrnrns5''  6rrn) __future__rloggingrrerrnoriorpathlibrr typingr r fsspecr fsspec.implementations.localr fsspec.utilsr getLoggerr\rrnrrrrsR"'- %8'   , -B)BJ!6!6r