
    =wgPT                        d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZmZ d dlmZmZ d dlmZ d dlmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zd Zy)    )with_statementN)Lock)BytesIOmemoryview_)
BufferFile
StructFile)_DEF_INDEX_NAMEEmptyIndexError)random_name)FileLockc                       e Zd Zy)StorageErrorN__name__
__module____qualname__     N/var/www/horilla/myenv/lib/python3.12/site-packages/whoosh/filedb/filestore.pyr   r   )       r   r   c                       e Zd Zy)ReadOnlyErrorNr   r   r   r   r   r   -   r   r   r   c                       e Zd ZdZdZdZd Zd Zd Zd Z	d Z
edfd	Zeddfd
ZddZd Zd Zd Zd Zd Zd Zd ZddZd Zd Zd ZddZy)StorageaE  Abstract base class for storage objects.

    A storage object is a virtual flat filesystem, allowing the creation and
    retrieval of file-like objects
    (:class:`~whoosh.filedb.structfile.StructFile` objects). The default
    implementation (:class:`FileStorage`) uses actual files in a directory.

    All access to files in Whoosh goes through this object. This allows more
    different forms of storage (for example, in RAM, in a database, in a single
    file) to be used transparently.

    For example, to create a :class:`FileStorage` object::

        # Create a storage object
        st = FileStorage("indexdir")
        # Create the directory if it doesn't already exist
        st.create()

    The :meth:`Storage.create` method makes it slightly easier to swap storage
    implementations. The ``create()`` method handles set-up of the storage
    object. For example, ``FileStorage.create()`` creates the directory. A
    database implementation might create tables. This is designed to let you
    avoid putting implementation-specific setup code in your application.
    Fc                 4    t        | j                               S N)iterlistselfs    r   __iter__zStorage.__iter__P   s    DIIK  r   c                 &    | j                          | S r   )creater   s    r   	__enter__zStorage.__enter__S   s    r   c                 $    | j                          y r   )close)r    exc_typeexc_valexc_tbs       r   __exit__zStorage.__exit__W   s    

r   c                     | S )a  Creates any required implementation-specific resources. For example,
        a filesystem-based implementation might create a directory, while a
        database implementation might create tables. For example::

            from whoosh.filedb.filestore import FileStorage
            # Create a storage object
            st = FileStorage("indexdir")
            # Create any necessary resources
            st.create()

        This method returns ``self`` so you can also say::

            st = FileStorage("indexdir").create()

        Storage implementations should be written so that calling create() a
        second time on the same storage

        :return: a :class:`Storage` instance.
        r   r   s    r   r#   zStorage.createZ   s	    * r   c                      y)a  Removes any implementation-specific resources related to this storage
        object. For example, a filesystem-based implementation might delete a
        directory, and a database implementation might drop tables.

        The arguments are implementation-specific.
        Nr   r    argskwargss      r   destroyzStorage.destroyq   s     	r   Nc                     | j                   rt        |ddl}|j                  j                  }|j                  | ||      S )a  Creates a new index in this storage.

        >>> from whoosh import fields
        >>> from whoosh.filedb.filestore import FileStorage
        >>> schema = fields.Schema(content=fields.TEXT)
        >>> # Create the storage directory
        >>> st = FileStorage.create("indexdir")
        >>> # Create an index in the storage
        >>> ix = st.create_index(schema)

        :param schema: the :class:`whoosh.fields.Schema` object to use for the
            new index.
        :param indexname: the name of the index within the storage object. You
            can use this option to store multiple indexes in the same storage.
        :param indexclass: an optional custom ``Index`` sub-class to use to
            create the index files. The default is
            :class:`whoosh.index.FileIndex`. This method will call the
            ``create`` class method on the given class to create the index.
        :return: a :class:`whoosh.index.Index` instance.
        Nr   )readonlyr   whoosh.indexindex	FileIndexr#   )r    schema	indexname
indexclasswhooshs        r   create_indexzStorage.create_index{   s>    , ==//J  vy99r   c                 P    |ddl }|j                  j                  } || ||      S )a  Opens an existing index (created using :meth:`create_index`) in this
        storage.

        >>> from whoosh.filedb.filestore import FileStorage
        >>> st = FileStorage("indexdir")
        >>> # Open an index in the storage
        >>> ix = st.open_index()

        :param indexname: the name of the index within the storage object. You
            can use this option to store multiple indexes in the same storage.
        :param schema: if you pass in a :class:`whoosh.fields.Schema` object
            using this argument, it will override the schema that was stored
            with the index.
        :param indexclass: an optional custom ``Index`` sub-class to use to
            open the index files. The default is
            :class:`whoosh.index.FileIndex`. This method will instantiate the
            class with this storage object.
        :return: a :class:`whoosh.index.Index` instance.
        Nr   )r6   r7   )r3   r4   r5   )r    r7   r6   r8   r9   s        r   
open_indexzStorage.open_index   s+    * //J$vCCr   c                     |t         }	 | j                  |      }|j                         }|j                          |dkD  S # t        $ r Y yw xY w)zReturns True if a non-empty index exists in this storage.

        :param indexname: the name of the index within the storage object. You
            can use this option to store multiple indexes in the same storage.
        :rtype: bool
        F)r	   r<   latest_generationr&   r
   )r    r7   ixgens       r   index_existszStorage.index_exists   sW     'I	+B&&(CHHJ8O 		s   5A   	AAc                     t         )zCreates a file with the given name in this storage.

        :param name: the name for the new file.
        :return: a :class:`whoosh.filedb.structfile.StructFile` instance.
        NotImplementedErrorr    names     r   create_filezStorage.create_file   
     "!r   c                     t         )zOpens a file with the given name in this storage.

        :param name: the name for the new file.
        :return: a :class:`whoosh.filedb.structfile.StructFile` instance.
        rD   r    rG   r.   r/   s       r   	open_filezStorage.open_file   rI   r   c                     t         )zZReturns a list of file names in this storage.

        :return: a list of strings
        rD   r   s    r   r   zStorage.list   s
    
 "!r   c                     t         )z}Returns True if the given file exists in this storage.

        :param name: the name to check.
        :rtype: bool
        rD   rF   s     r   file_existszStorage.file_exists   rI   r   c                     t         )zReturns the last-modified time of the given file in this storage (as
        a "ctime" UNIX timestamp).

        :param name: the name to check.
        :return: a "ctime" number.
        rD   rF   s     r   file_modifiedzStorage.file_modified   s
     "!r   c                     t         )zReturns the size (in bytes) of the given file in this storage.

        :param name: the name to check.
        :rtype: int
        rD   rF   s     r   file_lengthzStorage.file_length   rI   r   c                     t         )z\Removes the given file from this storage.

        :param name: the name to delete.
        rD   rF   s     r   delete_filezStorage.delete_file   s
     "!r   c                     t         )zRenames a file in this storage.

        :param frm: The current name of the file.
        :param to: The new name for the file.
        :param safe: if True, raise an exception if a file with the new name
            already exists.
        rD   )r    frmtosafes       r   rename_filezStorage.rename_file  s
     "!r   c                     t         )a  Return a named lock object (implementing ``.acquire()`` and
        ``.release()`` methods). Different storage implementations may use
        different lock types with different guarantees. For example, the
        RamStorage object uses Python thread locks, while the FileStorage
        object uses filesystem-based locks that are valid across different
        processes.

        :param name: a name for the lock.
        :return: a lock-like object.
        rD   rF   s     r   lockzStorage.lock  s
     "!r   c                      y)a  Closes any resources opened by this storage object. For some storage
        implementations this will be a no-op, but for others it is necessary
        to release locks and/or prevent leaks, so it's a good idea to call it
        when you're done with a storage object.
        Nr   r   s    r   r&   zStorage.close  s     	r   c                      y)zOptimizes the storage object. The meaning and cost of "optimizing"
        will vary by implementation. For example, a database implementation
        might run a garbage collection procedure on the underlying database.
        Nr   r   s    r   optimizezStorage.optimize$  s     	r   c                     t         )aI  Creates a new storage object for temporary files. You can call
        :meth:`Storage.destroy` on the new storage when you're finished with
        it.

        :param name: a name for the new storage. This may be optional or
            required depending on the storage implementation.
        :rtype: :class:`Storage`
        rD   rF   s     r   temp_storagezStorage.temp_storage,  s
     "!r   r   F)r   r   r   __doc__r2   supports_mmapr!   r$   r*   r#   r0   r	   r:   r<   rB   rH   rL   r   rO   rQ   rS   rU   rZ   r\   r&   r_   ra   r   r   r   r   r   3   s    2 HM!. .= :: $34D D4&"""""""	""
"r   r   c                   l    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd ZddZy)OverlayStoragezOverlays two storage objects. Reads are processed from the first if it
    has the named file, otherwise the second. Writes always go to the second.
    c                      || _         || _        y r   )ab)r    rh   ri   s      r   __init__zOverlayStorage.__init__>  s    r   c                 <     | j                   j                  |i | y r   )ri   r:   r-   s      r   r:   zOverlayStorage.create_indexB  s    T,V,r   c                 <     | j                   j                  |i | y r   )rh   r<   r-   s      r   r<   zOverlayStorage.open_indexE  s    4*6*r   c                 :     | j                   j                  |i |S r   )ri   rH   r-   s      r   rH   zOverlayStorage.create_fileH  s    !tvv!!42622r   c                     | j                   j                  |      r  | j                   j                  |g|i |S  | j                  j                  |g|i |S r   )rh   rO   rL   ri   rK   s       r   rL   zOverlayStorage.open_fileK  sV    66d##466##D:4:6::#466##D:4:6::r   c                     t        t        | j                  j                               t        | j                  j                               z        S r   )r   setrh   ri   r   s    r   r   zOverlayStorage.listQ  s0    C&TVV[[]);;<<r   c                 r    | j                   j                  |      xs | j                  j                  |      S r   )rh   rO   ri   rF   s     r   rO   zOverlayStorage.file_existsT  s+    vv!!$'C466+=+=d+CCr   c                     | j                   j                  |      r| j                   j                  |      S | j                  j                  |      S r   )rh   rO   rQ   ri   rF   s     r   rQ   zOverlayStorage.file_modifiedW  s>    66d#66''--66''--r   c                     | j                   j                  |      r| j                   j                  |      S | j                  j                  |      S r   )rh   rO   rS   ri   rF   s     r   rS   zOverlayStorage.file_length]  s>    66d#66%%d++66%%d++r   c                 8    | j                   j                  |      S r   )ri   rU   rF   s     r   rU   zOverlayStorage.delete_filec  s    vv!!$''r   c                     t         r   rD   r-   s      r   rZ   zOverlayStorage.rename_filef  s    !!r   c                 8    | j                   j                  |      S r   )ri   r\   rF   s     r   r\   zOverlayStorage.locki  s    vv{{4  r   c                 l    | j                   j                          | j                  j                          y r   )rh   r&   ri   r   s    r   r&   zOverlayStorage.closel  s    r   c                 l    | j                   j                          | j                  j                          y r   )rh   r_   ri   r   s    r   r_   zOverlayStorage.optimizep  s    r   Nc                 :    | j                   j                  |      S )N)rG   )ri   ra   rF   s     r   ra   zOverlayStorage.temp_storaget  s    vv"""--r   r   )r   r   r   rc   rj   r:   r<   rH   rL   r   rO   rQ   rS   rU   rZ   r\   r&   r_   ra   r   r   r   rf   rf   9  sS    -+3;=D.,("!.r   rf   c                   ~    e Zd ZdZdZddZd Zd Zd ZddZ	d Z
d	 Zdd
Zd Zd Zd Zd Zd ZddZd ZddZy)FileStorageaS  Storage object that stores the index as files in a directory on disk.

    Prior to version 3, the initializer would raise an IOError if the directory
    did not exist. As of version 3, the object does not check if the
    directory exists at initialization. This change is to support using the
    :meth:`FileStorage.create` method.
    Tc                 J    || _         || _        || _        || _        i | _        y)a  
        :param path: a path to a directory.
        :param supports_mmap: if True (the default), use the ``mmap`` module to
            open memory mapped files. You can open the storage object with
            ``supports_mmap=False`` to force Whoosh to open files normally
            instead of with ``mmap``.
        :param readonly: If ``True``, the object will raise an exception if you
            attempt to create or rename a file.
        N)folderrd   r2   _debuglocks)r    pathrd   r2   debugs        r   rj   zFileStorage.__init__  s(     * 
r   c                 N    | j                   j                  d| j                  dS )N())	__class__r   r}   r   s    r   __repr__zFileStorage.__repr__  s    >>22DKK@@r   c                    t         j                  j                  | j                        }	 t        j                  |       t         j                  j                  |      s%t        d|z        }t        j                  |_        || S # t
        $ r8 t        j                         d   }|j                  t        j                  k7  r Y w xY w)a  Creates this storage object's directory path using ``os.makedirs`` if
        it doesn't already exist.

        >>> from whoosh.filedb.filestore import FileStorage
        >>> st = FileStorage("indexdir")
        >>> st.create()

        This method returns ``self``, you can say::

            st = FileStorage("indexdir").create()

        Note that you can simply create handle the creation of the directory
        yourself and open the storage object using the initializer::

            dirname = "indexdir"
            os.mkdir(dirname)
            st = FileStorage(dirname)

        However, using the ``create()`` method allows you to potentially swap in
        other storage implementations more easily.

        :return: a :class:`Storage` instance.
           z%r is not a directory)osr   abspathr}   makedirsOSErrorsysexc_infoerrnoEEXISTisdirIOErrorENOTDIR)r    dirpathes      r   r#   zFileStorage.create  s    2 ''//$++.	KK  ww}}W%/'9:AmmAGG  	q!Aww%,,& '		s   B >CCc                     | j                          	 t        j                  | j                         y# t        $ r9 t        j                         d   }|j                  t        j                  k(  rY y|w xY w)zRemoves any files in this storage object and then removes the
        storage object's directory. What happens if any of the files or the
        directory are in use depends on the underlying platform.
        r   N)	cleanr   rmdirr}   r   r   r   r   ENOENT)r    r   s     r   r0   zFileStorage.destroy  sY     	

	HHT[[! 	q!Aww%,,&	s   2 =A41A4c                    | j                   rt        | j                  |      }|rt        j                  t        j
                  z  t        j                  z  }t        t        d      r|t        j                  z  }t        j                  ||      }t        j                  ||      }nt        ||      }t        |fd|i|}	|	S )ae  Creates a file with the given name in this storage.

        :param name: the name for the new file.
        :param excl: if True, try to open the file in "exclusive" mode.
        :param mode: the mode flags with which to open the file. The default is
            ``"wb"``.
        :return: a :class:`whoosh.filedb.structfile.StructFile` instance.
        O_BINARYrG   )r2   r   _fpathr   O_CREATO_EXCLO_RDWRhasattrr   openfdopenr   )
r    rG   exclmoder/   r   flagsfdfileobjfs
             r   rH   zFileStorage.create_file  s     =={{4 JJ*RYY6Er:&$u%BiiD)G4&Gw4T4V4r   c                 T    t        t        | j                  |      d      fd|i|}|S )aC  Opens an existing file in this storage.

        :param name: the name of the file to open.
        :param kwargs: additional keyword arguments are passed through to the
            :class:`~whoosh.filedb.structfile.StructFile` initializer.
        :return: a :class:`whoosh.filedb.structfile.StructFile` instance.
        rbrG   )r   r   r   )r    rG   r/   r   s       r   rL   zFileStorage.open_file  s-     tDKK-t4J4J6Jr   c                     t         j                  j                  t         j                  j                  | j                  |            S r   )r   r   r   joinr}   )r    fnames     r   r   zFileStorage._fpath  s(    wwrww||DKK?@@r   c                     | j                   rt        | j                  }| j                         }|D ]6  }	 t	        j
                  t        j                  j                  ||             8 y # t        $ r |s Y Hw xY wr   )	r2   r   r}   r   r   remover   r   r   )r    ignorer   filesr   s        r   r   zFileStorage.clean  sn    =={{		 	E		"'',,tU34	   s   3A++A:9A:c                 j    	 t        j                  | j                        }|S # t        $ r g }Y |S w xY wr   )r   listdirr}   r   )r    r   s     r   r   zFileStorage.list  s;    	JJt{{+E   	E	s   # 22c                 ^    t         j                  j                  | j                  |            S r   )r   r   existsr   rF   s     r   rO   zFileStorage.file_exists  s    ww~~dkk$/00r   c                 ^    t         j                  j                  | j                  |            S r   )r   r   getmtimer   rF   s     r   rQ   zFileStorage.file_modified  s     wwD 122r   c                 ^    t         j                  j                  | j                  |            S r   )r   r   getsizer   rF   s     r   rS   zFileStorage.file_length  s    wwt{{4011r   c                 p    | j                   rt        t        j                  | j	                  |             y r   )r2   r   r   r   r   rF   s     r   rU   zFileStorage.delete_file  s$    ==
		$++d#$r   c                 T   | j                   rt        t        j                  j	                  | j                  |            r4|rt        d|z        t        j                  | j                  |             t        j                  | j                  |      | j                  |             y NzFile %r exists)	r2   r   r   r   r   r   	NameErrorr   rename)r    oldnamenewnamerY   s       r   rZ   zFileStorage.rename_file"  sq    ==77>>$++g./ 07 :;;		$++g./
		$++g&G(<=r   c                 6    t        | j                  |            S r   )r   r   rF   s     r   r\   zFileStorage.lock-  s    D)**r   Nc                     |xs dt               z  }t        j                  j                  | j                  |      }t        |      }|j                         S Nz%s.tmp)r   r   r   r   r}   r{   r#   )r    rG   r   	tempstores       r   ra   zFileStorage.temp_storage0  sD    /x+-/ww||DKK.%	!!r   )TFF)Fwbrb   r   )r   r   r   rc   rd   rj   r   r#   r0   rH   rL   r   r   r   rO   rQ   rS   rU   rZ   r\   ra   r   r   r   r{   r{   x  sa     M"A*X$4
A132%	>+"r   r{   c                   l    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZddZd Zd Zd ZddZy)
RamStoragez3Storage object that keeps the index in memory.
    Fc                 .    i | _         i | _        d| _        y )N )r   r   r}   r   s    r   rj   zRamStorage.__init__=  s    

r   c                     | ` | `y r   )r   r   r   s    r   r0   zRamStorage.destroyB  s    JJr   c                 H    t        | j                  j                               S r   )r   r   keysr   s    r   r   zRamStorage.listF  s    DJJOO%&&r   c                     i | _         y r   r   r   s    r   r   zRamStorage.cleanI  s	    
r   c                 H     t         fd j                         D              S )Nc              3   @   K   | ]  }j                  |        y wr   )rS   ).0r   r    s     r   	<genexpr>z(RamStorage.total_size.<locals>.<genexpr>M  s     <14##A&<s   )sumr   r   s   `r   
total_sizezRamStorage.total_sizeL  s    <		<<<r   c                     || j                   v S r   r   rF   s     r   rO   zRamStorage.file_existsO  s    tzz!!r   c                 d    || j                   vrt        |      t        | j                   |         S r   )r   r   lenrF   s     r   rS   zRamStorage.file_lengthR  s,    tzz!D/!4::d#$$r   c                      y)Nr>   r   rF   s     r   rQ   zRamStorage.file_modifiedW  s    r   c                 P    || j                   vrt        |      | j                   |= y r   r   r   rF   s     r   rU   zRamStorage.delete_fileZ  s$    tzz!D/!JJtr   c                     || j                   vrt        |      |r|| j                   v rt        d|z        | j                   |   }| j                   |= || j                   |<   y r   r   )r    rG   r   rY   contents        r   rZ   zRamStorage.rename_file_  s_    tzz!D/!Gtzz),w677**T"JJt%

7r   c                 B      fd}t        t               |      }|S )Nc                 T    | j                   j                         j                  <   y r   )filegetvaluer   )sfilerG   r    s    r   
onclose_fnz*RamStorage.create_file.<locals>.onclose_fnj  s    $zz224DJJtr   )rG   onclose)r   r   )r    rG   r/   r   r   s   ``   r   rH   zRamStorage.create_filei  s    	5wytZ@r   c                     || j                   vrt        |      t        | j                   |         }t        |fd|i|S )NrG   )r   r   r   r   )r    rG   r/   bufs       r   rL   zRamStorage.open_fileo  s@    tzz!D/!$**T*+#3D3F33r   c                 j    || j                   vrt               | j                   |<   | j                   |   S r   )r   r   rF   s     r   r\   zRamStorage.locku  s-    tzz!#vDJJtzz$r   Nc                     t        j                         }|xs dt               z  }t        j                  j                  ||      }t        |      }|j                         S r   )tempfile
gettempdirr   r   r   r   r{   r#   )r    rG   tdirr   r   s        r   ra   zRamStorage.temp_storagez  sN    ""$/x+-/ww||D$'%	!!r   rb   r   )r   r   r   rc   rd   rj   r0   r   r   r   rO   rS   rQ   rU   rZ   rH   rL   r\   ra   r   r   r   r   r   7  sT     M
'="%

&4 
"r   r   c                     ddl m} | j                         D ]?  }| j                  |      5 }|j	                  |      5 } |||       ddd       ddd       A y# 1 sw Y   xY w# 1 sw Y   XxY w)zxCopies the files from the source storage object to the destination
    storage object using ``shutil.copyfileobj``.
    r   )copyfileobjN)shutilr   r   rL   rH   )sourcestore	deststorer   rG   sourcedests         r   copy_storager     sx     #  " *""4( 	*F&&t, *FD)*	* 	*** *	* 	*s"   A&
AA&A#A&&A/	c                 2    t               }t        | |       |S )zgCopies the given FileStorage object into a new RamStorage object.

    :rtype: :class:`RamStorage`
    )r   r   )storagerams     r   copy_to_ramr     s     ,C#Jr   )
__future__r   r   r   r   r   	threadingr   whoosh.compatr   r   whoosh.filedb.structfiler   r   r3   r	   r
   whoosh.utilr   whoosh.util.filelockr   	Exceptionr   r   objectr   rf   r{   r   r   r   r   r   r   <module>r      s~   8 &    . ; 9 # )
	9 		L 	C"f C"L<.W <.~|"' |"~H" H"V	*r   