
    9i(                         S SK r S SKrS SKrS SKJrJr  S SKJr  S SKJ	r	J
r
  S SKr " S S\S9r " S S	\5      r " S
 S\5      r " S S\5      r0 r " S S\5      rg)    N)ABCMetaabstractmethod)Path)	GeneratorUnionc            
           \ rS rSrSr\S\4S j5       r\S\4S j5       r\S\	S\
\\4   SS4S	 j5       r\ SS\S\
\\4   S
\SS4S jj5       rSrg)Storage   zAbstract class of storage.

All backends need to implement two apis: ``read()`` and ``read_text()``.
``read()`` reads the file as a byte stream and ``read_text()`` reads
the file as texts.
filepathc                     g N selfr   s     V/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/fileio/file.pyreadStorage.read           c                     g r   r   r   s     r   	read_textStorage.read_text   r   r   objreturnNc                     g r   r   r   r   r   s      r   writeStorage.write   r   r   encodingc                     g r   r   r   r   r   r   s       r   
write_textStorage.write_text!   s    
 	r   r   zutf-8)__name__
__module____qualname____firstlineno____doc__r   strr   r   bytesr   r   r   r"   __static_attributes__r   r   r   r	   r	      s     S   #    %T	*: t    $+"39- ! 04 r   r	   )	metaclassc            	           \ rS rSrSrS\\\4   S\4S jr	 SS\\\4   S\S\4S jjr
S\S\\\4   SS	4S
 jr SS\S\\\4   S\SS	4S jjr\R                  S\\\4   S\\\\4   S	S	4   4S j5       rSrg	)LocalStorage)   zLocal hard disk storager   r   c                 t    [        US5       nUR                  5       nSSS5        U$ ! , (       d  f       W$ = f)Read data from a given ``filepath`` with 'rb' mode.

Args:
    filepath (str or Path): Path to read data.

Returns:
    bytes: Expected bytes object.
rbNopenr   )r   r   fcontents       r   r   LocalStorage.read,   s3     (D!QffhG " "!s   (
7r   c                 r    [        USUS9 nUR                  5       nSSS5        U$ ! , (       d  f       W$ = f)  Read data from a given ``filepath`` with 'r' mode.

Args:
    filepath (str or Path): Path to read data.
    encoding (str): The encoding format used to open the ``filepath``.
        Default: 'utf-8'.

Returns:
    str: Expected text reading from ``filepath``.
rr   Nr4   )r   r   r   r6   	value_bufs        r   r   LocalStorage.read_text9   s7     (C(3qI 4 43s   '
6r   Nc                 0   [         R                  R                  U5      nU(       a9  [         R                  R                  U5      (       d  [         R                  " USS9  [        US5       nUR                  U5        SSS5        g! , (       d  f       g= f)Write data to a given ``filepath`` with 'wb' mode.

Note:
    ``write`` will create a directory if the directory of ``filepath``
    does not exist.

Args:
    obj (bytes): Data to be written.
    filepath (str or Path): Path to write data.
Texist_okwbNospathdirnameexistsmakedirsr5   r   )r   r   r   rG   r6   s        r   r   LocalStorage.writeJ   s\     ''//(+277>>'22KK$/(D!QGGCL "!!s   ,B
Bc                 .   [         R                  R                  U5      nU(       a9  [         R                  R                  U5      (       d  [         R                  " USS9  [        USUS9 nUR                  U5        SSS5        g! , (       d  f       g= f)T  Write data to a given ``filepath`` with 'w' mode.

Note:
    ``write_text`` will create a directory if the directory of
    ``filepath`` does not exist.

Args:
    obj (str): Data to be written.
    filepath (str or Path): Path to write data.
    encoding (str): The encoding format used to open the ``filepath``.
        Default: 'utf-8'.
TrA   wr<   NrD   )r   r   r   r   rG   r6   s         r   r"   LocalStorage.write_text\   s^      ''//(+277>>'22KK$/(C(3qGGCL 433s   +B
Bc              #      #    Uv   g7fz$Only for unified API and do nothing.Nr   r   s     r   as_local_pathLocalStorage.as_local_paths   s      s   r   r$   )r%   r&   r'   r(   r)   r   r*   r   r+   r   r   r   r"   
contextlibcontextmanagerr   rQ   r,   r   r   r   r/   r/   )   s    !U39- %  #*!#t),.1" %T	*: t * $+"39- ! 04. C ! " '0c4i0@$0L&M r   r/   c            	           \ rS rSrSrS rS r\R                  S\	S\
\\	\4   SS4   4S j5       rS	\S
\\	\4   SS4S jr SS	\	S
\\	\4   S\	SS4S jjrSrg)HTTPStorage|   zHTTP and HTTPS storage.c                 f    [         R                  " U5      nUR                  5         UR                  $ r   )requestsgetraise_for_statusr7   r   urlr;   s      r   r   HTTPStorage.read   s&    LL	yyr   c                 f    [         R                  " U5      nUR                  5         UR                  $ r   )rY   rZ   r[   textr\   s      r   r   HTTPStorage.read_text   s%    LL	vvr   r   r   Nc              #   <  #     [         R                  " SS9nUR                  U R                  U5      5        UR	                  5         UR
                  v   [        R                  " UR
                  5        g! [        R                  " WR
                  5        f = f7f)a  Download a file from ``filepath``.

``as_local_path`` is decorated by :meth:`contextlib.contextmanager`. It
can be called with ``with`` statement, and when exists from the
``with`` statement, the temporary path will be released.

Args:
    filepath (str): Download a file from ``filepath``.

Examples:
    >>> storage = HTTPStorage()
    >>> # After existing from the ``with`` clause,
    >>> # the path will be removed
    >>> with storage.get_local_path('http://path/to/file') as path:
    ...     # do something here
FdeleteNtempfileNamedTemporaryFiler   r   closenamerE   remover   r   r6   s      r   rQ   HTTPStorage.as_local_path   a     &	++59AGGDIIh'(GGI&&LIIaffBIIaff   BAA7 !B7"BBr   r]   c                     [        S5      e)Nz&write is not supported by HTTP StorageNotImplementedError)r   r   r]   s      r   r   HTTPStorage.write   s    !"JKKr   r   c                     [        S5      e)Nz+write_text is not supported by HTTP Storagerp   )r   r   r]   r   s       r   r"   HTTPStorage.write_text   s     "9; 	;r   r   r$   )r%   r&   r'   r(   r)   r   r   rS   rT   r*   r   r   r   rQ   r+   r   r"   r,   r   r   r   rV   rV   |   s    !
 $-eCI.>d.J$K 4L LU39%5 L$ L $+;;c4i(; !; 04; ;r   rV   c            	           \ rS rSrSrSS jrS rSS jr\R                  S\
S\\\
\4   SS4   4S	 j5       rS
\S\\
\4   SS4S jr SS
\
S\\
\4   S\
SS4S jjrSrg)
OSSStorage   zOSS storage.Nc                     [        S5      e)Nz3OSSStorage.__init__ to be implemented in the futurerp   )r   oss_config_files     r   __init__OSSStorage.__init__   s    !AC 	Cr   c                     [        S5      e)Nz/OSSStorage.read to be implemented in the futurerp   r   s     r   r   OSSStorage.read   s    !=? 	?r   c                     [        S5      e)Nz4OSSStorage.read_text to be implemented in the futurerp   )r   r   r   s      r   r   OSSStorage.read_text   s    !BD 	Dr   r   r   c              #   <  #     [         R                  " SS9nUR                  U R                  U5      5        UR	                  5         UR
                  v   [        R                  " UR
                  5        g! [        R                  " WR
                  5        f = f7f)a  Download a file from ``filepath``.

``as_local_path`` is decorated by :meth:`contextlib.contextmanager`. It
can be called with ``with`` statement, and when exists from the
``with`` statement, the temporary path will be released.

Args:
    filepath (str): Download a file from ``filepath``.

Examples:
    >>> storage = OSSStorage()
    >>> # After existing from the ``with`` clause,
    >>> # the path will be removed
    >>> with storage.get_local_path('http://path/to/file') as path:
    ...     # do something here
Frc   Nre   rk   s      r   rQ   OSSStorage.as_local_path   rm   rn   r   c                     [        S5      e)Nz0OSSStorage.write to be implemented in the futurerp   r   s      r   r   OSSStorage.write   s    !>@ 	@r   r   c                     [        S5      e)Nz5OSSStorage.write_text to be implemented in the futurerp   r!   s       r   r"   OSSStorage.write_text   s     "CE 	Er   r   r   r$   )r%   r&   r'   r(   r)   rz   r   r   rS   rT   r*   r   r   r   rQ   r+   r   r"   r,   r   r   r   rv   rv      s    C
?D $-eCI.>d.J$K 4@ @%T	*: @t @ $+EE"39-E !E 04E Er   rv   c            
           \ rS rSr% \\\\S.r\\	S'   \
S 5       r\
S\S\4S j5       r\
SS\\\4   S\S\4S	 jj5       r\
S
\S\\\4   SS4S j5       r\
SS
\S\S\SS4S jj5       r\R*                  S\S\\\\4   SS4   4S j5       rSrg)File   )osshttphttpslocal_prefix_to_storagec                 ~   [        U [        5      (       d   S[        U 5       35       eSU ;  a  SnOU R                  S5      u  p#UnU[        R
                  ;   d2   SU  S[        [        R
                  R                  5       5       35       eU[        ;  a  [        R
                  U   " 5       [        U'   [        U   $ )Nz uri should be str type, but got z://r   zUnsupported uri z, valid prefixs: )	
isinstancer*   typesplitr   r   listkeys
G_STORAGES)uristorage_typeprefix_s       r   _get_storageFile._get_storage   s    #  	O"B49+ N	O  "L		%(IF!Lt666 	6se#4D++002346	66 z)'+'>'>|'L'NJ|$,''r   r   r   c                 N    [         R                  U 5      nUR                  U 5      $ )r2   )r   r   r   )r   storages     r   r   	File.read  s#     ##C(||C  r   r   c                 N    [         R                  U 5      nUR                  U 5      $ )r:   )r   r   r   )r   r   r   s      r   r   File.read_text  s%     ##C(  %%r   r   Nc                 N    [         R                  U5      nUR                  X5      $ )r@   )r   r   r   )r   r   r   s      r   r   
File.write#  s#     ##C(}}S&&r   c                 N    [         R                  U5      nUR                  X5      $ )rL   )r   r   r"   )r   r   r   r   s       r   r"   File.write_text2  s%     ##C(!!#++r   c              #      #    [         R                  U 5      nUR                  U 5       nUv   SSS5        g! , (       d  f       g= f7frP   )r   r   rQ   )r   r   
local_paths      r   rQ   File.as_local_pathC  s8      ##C(""3': (''s   'A7	A
AAr   r$   )r%   r&   r'   r(   rv   rV   r/   r   dict__annotations__staticmethodr   r*   r+   r   r   r   r   r   r"   rS   rT   r   rQ   r,   r   r   r   r   r      s$   	   ( (( 
!# 
!% 
! 
! &uS$Y' &3 &S & & '5 'uS$Y/ 'D ' ' , ,# , ,4 , ,  3 9U39-=tT-I#J  r   r   )rS   rE   rf   abcr   r   pathlibr   typingr   r   rY   r	   r/   rV   rv   r   objectr   r   r   r   <module>r      sh     	  '  #  8P7 Pf1;' 1;h4E 4En 
^6 ^r   