L iZUddlmZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z ddl mZddlmZddlmZddlmZmZmZmZmZddlmZerddlZdd lmZdd lmZd Z ed Z! d) d*d Z" d) d+dZ#iZ$de%d<d,dZ&d-dZ'd.dZ( d/ d0dZ)d1dZ*d2dZ+ d3dZ,d4dZ- d5 d6dZ.d7dZ/d8dZ0d9dZ1d9dZ2d:dZ3d;dZ4 d< d=d Z5d>d!Z6 d?d"Z7ejpd@d#Z9 dA dBd$Z:dCd%Z;ejpdDdEd&Zy)F) annotationsN)IterableIteratorSequence)partial)md5)version)IO TYPE_CHECKINGAnyCallableTypeVar)urlsplit) TypeGuard)AbstractFileSystemiPTctjd|stjd|d|dSt|}|jxsd}|jr(dj |j |jg}n |j }|dk(r2tjd|}|r|j\}}|d|}|dvr||dS||d}|jr|jjd d d jdd d |d <|dvr|d |dz|d<n|d |d <|jr|j|d<|jr|j|d<|jr|j|d<|jr|j|d<|jr|j|d<|r t|||S)aInfer storage options from URL path and merge it with existing storage options. Parameters ---------- urlpath: str or unicode Either local absolute file path or URL (hdfs://namenode:8020/file.csv) inherit_storage_options: dict (optional) Its contents will get merged with the inferred information from the given path Returns ------- Storage options dict. Examples -------- >>> infer_storage_options('/mnt/datasets/test.csv') # doctest: +SKIP {"protocol": "file", "path", "/mnt/datasets/test.csv"} >>> infer_storage_options( ... 'hdfs://username:pwd@node:123/mnt/datasets/test.csv?q=1', ... inherit_storage_options={'extra': 'value'}, ... ) # doctest: +SKIP {"protocol": "hdfs", "username": "username", "password": "pwd", "host": "node", "port": 123, "path": "/mnt/datasets/test.csv", "url_query": "q=1", "extra": "value"} z^[a-zA-Z]:[\\/]z^[a-zA-Z0-9]+://file)protocolpath#z^/([a-zA-Z])[:|]([\\/].*)$:)httphttps@rhost)s3s3agcsgsrportusernamepassword url_query url_fragment)rematchrschemefragmentjoinrgroupsnetlocrsplitr#r$r%queryupdate_storage_options)urlpathinherit_storage_options parsed_pathrr windows_pathdriveoptionss R/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/fsspec/utils.pyinfer_storage_optionsr9$s@ #W- 88' 1 9"G447#K!!+VHxx));+?+?@A6xx =tD &--/KE4WAdV$D$$$g66+3TBG&,,33C;B?FFsANqQ 1 1%fo?GFO%foGFO   )..GFO   "-"6"6GJ    "-"6"6GJ *00 "-"6"6w(?@ Nc|si}t|t|z}|r8|D]3}|j||j|k7s'td||j|y)Nz9Collision between inferred and specified storage option: )setgetKeyErrorupdate)r7 inherited collisions collisions r8r1r1yst  WI.J# I{{9%y)AA ){,   NN9r:zdict[str, str] compressionsctjj|djdj }|t vr t |Sy)aInfer compression, if available, from filename. Infer a named compression type, if registered and available, from filename extension. This includes builtin (gz, bz2, zip) compressions, as well as optional compressions. See fsspec.compression.register_compression. r.N)osrsplitextstriplowerrC)filename extensions r8infer_compressionrLsG  *2.44S9??AIL I&& r:c|dz }ttjtj|dfd }|S)aReturns a function that receives a single integer and returns it as a string padded by enough zero characters to align with maximum possible integer >>> name_f = build_name_function(57) >>> name_f(7) '07' >>> name_f(31) '31' >>> build_name_function(1000)(42) '0042' >>> build_name_function(999)(42) '042' >>> build_name_function(0)(0) '0' g:0yE>c8t|jSN)strzfill)i pad_lengths r8 name_functionz*build_name_function..name_functions1v||J''r:)rRintreturnrP)rUmathceillog10)max_intrTrSs @r8build_name_functionr[s7& tOGTYYtzz'234J( r:cv|jdk(ryd} |j|}|sy|r||zn|} ||vrL|j|}|j|jt ||z z t |zyt ||kry |t | d}#t t f$rY!wxYw)aSeek current file to file start, file end, or byte after delimiter seq. Seeks file to next chunk delimiter, where chunks are defined on file start, a delimiting sequence, and file end. Use file.tell() to see location afterwards. Note that file start is a valid split, so must be at offset > 0 to seek for delimiter. Parameters ---------- file: a file delimiter: bytes a delimiter like ``b'\n'`` or message sentinel, matching file .read() type blocksize: int Number of bytes to read from the file at once. Returns ------- Returns True if a delimiter was found, False if at file start or end. rFNT)tellreadindexseeklenOSError ValueError)r delimiter blocksizelastcurrentfullrRs r8seek_delimiterris. yy{aD ))I&!%tg~7 D JJy) $))+TQ7#i.HIW )* S^O%&! $   sAB&B&&B87B8c|r|j|t||d}||jS|j}|||z z}|j||zt||d}|j}|r|r|t |z}|r|r|t |z}|}||z }|j||J|j|} | S)aRead a block of bytes from a file Parameters ---------- f: File Open file offset: int Byte offset to start read length: int Number of bytes to read, read through end of file if None delimiter: bytes (optional) Ensure reading starts and stops at delimiter bytestring split_before: bool (optional) Start/stop read *before* delimiter bytestring. If using the ``delimiter=`` keyword argument we ensure that the read starts and stops at delimiter boundaries that follow the locations ``offset`` and ``offset + length``. If ``offset`` is zero then we start at zero, regardless of delimiter. The bytestring returned WILL include the terminating delimiter string. Examples -------- >>> from io import BytesIO # doctest: +SKIP >>> f = BytesIO(b'Alice, 100\nBob, 200\nCharlie, 300') # doctest: +SKIP >>> read_block(f, 0, 13) # doctest: +SKIP b'Alice, 100\nBo' >>> read_block(f, 0, 13, delimiter=b'\n') # doctest: +SKIP b'Alice, 100\nBob, 200\n' >>> read_block(f, 10, 10, delimiter=b'\n') # doctest: +SKIP b'Bob, 200\nCharlie, 300' i)r`rir^r]ra) foffsetlengthrd split_beforefound_start_delimstartfound_end_delimendbs r8 read_blockrtsV v*1i? >668O%&.  uv~(Iu=ffh  S^ #E | 3y> !CuFF6N    vA Hr:c|r||fz } tt|j}|j S#t$r6tt|jd}Y|j SwxYw)zDeterministic token (modified from dask.base) >>> tokenize([1, 2, '3']) '9d71491b50023b06fc76928e6eddb952' >>> tokenize('Hello') == tokenize('Hello') True F)usedforsecurity)rrPencoderc hexdigest)argskwargshs r8tokenizer|2ss  ; D   " # ;;= ; D   "E : ;;=;s"<-A;:A;ct|tr|St|dr|jSt|dr |jS|S)a6Attempt to convert a path-like object to a string. Parameters ---------- filepath: object to be converted Returns ------- filepath_str: maybe a string version of the object Notes ----- Objects supporting the fspath protocol are coerced according to its __fspath__ method. For backwards compatibility with older Python version, pathlib.Path objects are specially coerced. Any other object is passed through unchanged, which includes bytes, strings, buffers, or anything else that's not even path-like. __fspath__r) isinstancerPhasattrr~r)filepaths r8stringify_pathrGsE,(C  < (""$$ 6 "}}r:c6||i|}|j|SrO)_determine_worker)clsryrzinsts r8 make_instancergs$   D Kr:c|Dcgc]}|jdc}tdD}d}t|D]tfdD}|rn|z dj ddScc}w)z;For a list of paths, find the shortest prefix common to all/c32K|]}t|ywrO)ra).0ps r8 z common_prefix..rs%!s1v%rc3:K|]}|dk(yw)rN)rrrRpartss r8rz common_prefix..us"5!!A$%(1+%5sN)splitminrangeallr,)pathsrlmaxrrrRrs @@r8 common_prefixros#( )aQWWS\ )E %u% %D C 4[5u55 HA 88E!HRaL !! *sA;c t|tr|jd}|r4|Dcgc]'}dj||j ddf)}}|St |}|r|j ddd}|s4td|Dr"|Dcgc]}dj||g}}|S|Dcgc]}|j||d}}|St|t|k(sJ|Scc}wcc}wcc}w)aIn bulk file operations, construct a new file tree from a list of files Parameters ---------- paths: list of str The input file tree path2: str or list of str Root to construct the new list in. If this is already a list of str, we just assert it has the right number of elements. exists: bool (optional) For a str destination, it is already exists (and is a dir), files should end up inside. flatten: bool (optional) Whether to flatten the input directory tree structure so that the output files are in the same directory. Returns ------- list of str rrrrc3@K|]}|jd yw)rN) startswithrss r8rzother_paths..sC!,,s"33Cs) rrPrstripr,rrr/rreplacera)rpath2existsflattenrcps r8 other_pathsr|s6% S! BGHQSXXuaggcl2&678HEH Lu%BYYsA&q)#CUCC7<=!5!*-== L;@@Q2ua0@@ L5zSZ''' LI >@s,C-C24C7c"t|tSrO)r BaseExceptionobjs r8 is_exceptionrs c= ))r:c,tfddDS)Nc36K|]}t|ywrO)r)rattrrks r8rzisfilelike..sFDwq$Fs)r^closer])r)rks`r8 isfilelikers F,EF FFr:cpt|}tjd|d}t|dkDr|dSy)Nz (\:\:|\://)r)maxsplitrr)rr(rra)urlrs r8 get_protocolrs5  C HH^S1 5E 5zA~Qx r:cdt|}|jdd}t|dkDr|dSy)NrErr)rr/ra)r ext_partss r8get_file_extensionrs5  C 3"I 9~} r:crddlm} t|t|ddS#tt f$rYywxYw)z*Can the given URL be used with open_local?r)get_filesystem_class local_fileF)fsspecrgetattrrrc ImportError)rrs r8 can_be_localrs;++L,>?uUU  $s $66c|tjvr+tj|}t|dr |jS t |S#YnxYw ddl}|j |}|jS#ttf$rYywxYw)aFor given package name, try to find the version without importing it Import and package.__version__ is still the backup here, so an import *might* happen. Returns either the version string, or None if the package or the version was not readily found. __version__rN) sysmodulesrrr importlib import_modulerAttributeError)namemodrs r8"get_package_version_without_importrs s{{kk$ 3 &?? " t}  %%d+  (s A A A33BBcF| | td|xstj|}tj}tjd}|j ||r|j j|j||j||S)Nz+Provide either logger object or logger namezD%(asctime)s - %(name)s - %(levelname)s - %(funcName)s -- %(message)s) rclogging getLogger StreamHandler Formatter setFormatterhandlersclear addHandlersetLevel)logger logger_namelevelrhandle formatters r8 setup_loggingrs ~+-FGG  5w((5F  " " $F!!NI  "  f OOE Mr:c$|j|SrO)unstrip_protocol)rfss r8_unstrip_protocolrs  t $$r:c&dfd dfd }|S)zqMirror attributes and methods from the given origin_name attribute of the instance to the decorated classc4t|}t||SrO)r)methodselforigin origin_names r8 origin_getterz"mirror_from..origin_getters{+vv&&r:cZD]$}t|}t||t|&|SrO)rsetattrproperty)rrwrapped_methodmethodsrs r8wrapperzmirror_from..wrapper s6 ;F$]F;N C.!9 : ; r:)rrPrr rVr )rtype[T]rVrr)rrrrs`` @r8 mirror_fromrs' Nr:c#K|ywrOrrs r8 nullcontextrs Isc .t|tstt|ts|gt|z}t|ts|gt|z}t|t|k7st|t|k7rtt|dkr|||fS|Dcgc]}|xsd }}|r'dt t t |||D\}}}|r|dd}|dd}|dd} tdt|D]} || || dz k(r| d|| || dz k7s|| | dz |kDs|K|| |dz |kDr=|j|| |j|| | j|| || | d<||| fS|||fScc}w)a}Merge adjacent byte-offset ranges when the inter-range gap is <= `max_gap`, and when the merged byte range does not exceed `max_block` (if specified). By default, this function will re-order the input paths and byte ranges to ensure sorted order. If the user can guarantee that the inputs are already sorted, passing `sort=False` will skip the re-ordering. rrc32K|]}t|ywrO)list)rvs r8rz&merge_offset_ranges..8s  G rNr) rr TypeErrorrarczipsortedrappend) rstartsendsmax_gap max_blocksortr new_paths new_startsnew_endsrRs r8merge_offset_rangesrs eT " fd #CJ& dD !vE " 6{c%j CIU$; 6{afd""$ %af1f %F %  vt, vt "1I BQZ 8q#e*% 'AQx5Q<'HRL,@aE!a%L(AY"-8)tAwB/G9.T  q*!!&),Q( $Aw ! '"*h.. &$ M&s Fc|j} |jdd|j|S#|j|wxYw)z+Find length of any open read-mode file-liker)r]r`)filelikeposs r8 file_sizer^s9 --/C}}Q" c cs 5Ac#Ktjtjj |tjj |dz\}} t ||5}|dddtj||y#1swY xYw#t$rEtjt5tj|ddd#1swYxYwwxYww)z A context manager that opens a temporary file next to `path` and, on exit, replaces `path` with the temporary file, thereby updating `path` atomically. -)dirprefixN) tempfilemkstemprFrdirnamebasenameopenrr contextlibsuppressFileNotFoundErrorunlink)rmodefdfnfps r8 atomic_writergs   GGOOD !"''*:*:4*@3*FFB "d^ rH  2t     !2 3  IIbM    sNAC( B'B ,B4C( BB"C%9C C%C! C%%C(c`g}|j}dt|}}||kr||}|dz}|dk(r|r|d|ur||n|dk(r ||n|dk(r|}||kr ||dk(r|dz}||kr ||dk(r|dz}||kr||dk7r|dz}||kr ||dk7r||k\r |d nq|||} d | vr| jd d } ng} ||dk(r|d zn|dz} |jd | |} | dkrn| j||| | dz}| dz} 8|||} | r| j| n | dxxd z cc<t t| dz ddD]4} | | dz d| | dkDs| | dz dd| | ddz| | dz <| | =6d j d| D} t jdd| } |dz}| s |dnS| dk(r |dnE| ddk(r d| ddz} n | ddvrd | z} |d| dn|t j|||kr||k(sJ|S)Nrr*r?[!]z\[r\\\rc3`K|]&}|jddjdd(yw)rrrz\-N)rrs r8rz_translate..s-%GH $.66sEB%s,.z([&~|])z\\\1z(?!)rE^)rr) rrarfindrr,r(subescape) patSTAR QUESTION_MARKresaddrRncjstuffchunkskchunks r8 _translater/|sC **C c#hqA a% F E 8CG4/D #X   #XA1uQ3E1uQ3Ea%CFcMEa%CFcMAvE Aae#!MM$6EF!$Q3AAEAHHS!Q/q5! c!Ah/EE  !HE e,r c) "3v;?Ar:*!!a%=,vay|;,21q5M#2,>12,NF1q5M &q *  HH%LR%Ez7E:EKc\HQx3 #eABiqZ/ $u !E7! %  ! A a%B 6M6 Jr:c,tjjr6tjjtjjz}ntjj}dj t t j|}t|dkDrd|dn|}d|d}|d}||}d|d}d }g} t j||} t| dz } t| D]\} } | d k(r| j| | kr|n|$| d k(r| j| | kr|n|Bd | vr td | r| jt| |d || | ksx| j|dj | }d |dS)zBTranslate a pathname with shell wildcards to a regular expression.rrrrz[^+z(?:.+z)?z.*rz**z:Invalid pattern: '**' can only be an entire path componentz(?s:z)\Z)rFraltsepsepr,mapr(r"rar enumeraterrcextendr/)r#seps escaped_sepsany_sepnot_sepone_last_segment one_segment any_segmentsany_last_segmentsresultsr last_part_idxidxpartr&s r8glob_translaterCs  ww~~ww{{RWW^^+ww{{773ryy$/0L%(Y],q! G<."G!!}%&wi0K7)2&LG HHWc "EJNMu%$ T 3; NN# *=;CS T  4< NN3+>r_s" 88&"+. CLDHR R+@RRlAE   (=   " " n! :.'j# G G G  G  G  G  G T*@ !.8F "  * * * * *  *Z*G6%)"  !     *%,!(   C C C C C  C  C,CL (GT#r: