
     hv                        d 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 ddl	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 dZd                     eee                    Zej        dk    r2ddlZddlZddlZej        ZdZ ej!        Z" ej#                    Z$d Z%d Z&n+ej        dk    rddl'Z'ddl'mZm"Z"m Z  d Z%d Z&n e(d           G d dej)                  Z) G d dej*                  Z*dS )a=	  File-system Transport module for kombu.

Transport using the file-system as the message store. Messages written to the
queue are stored in `data_folder_in` directory and
messages read from the queue are read from `data_folder_out` directory. Both
directories must be created manually. Simple example:

* Producer:

.. code-block:: python

    import kombu

    conn = kombu.Connection(
        'filesystem://', transport_options={
            'data_folder_in': 'data_in', 'data_folder_out': 'data_out'
        }
    )
    conn.connect()

    test_queue = kombu.Queue('test', routing_key='test')

    with conn as conn:
        with conn.default_channel as channel:
            producer = kombu.Producer(channel)
            producer.publish(
                        {'hello': 'world'},
                        retry=True,
                        exchange=test_queue.exchange,
                        routing_key=test_queue.routing_key,
                        declare=[test_queue],
                        serializer='pickle'
            )

* Consumer:

.. code-block:: python

    import kombu

    conn = kombu.Connection(
        'filesystem://', transport_options={
            'data_folder_in': 'data_out', 'data_folder_out': 'data_in'
        }
    )
    conn.connect()

    def callback(body, message):
        print(body, message)
        message.ack()

    test_queue = kombu.Queue('test', routing_key='test')

    with conn as conn:
        with conn.default_channel as channel:
            consumer = kombu.Consumer(
                conn, [test_queue], accept=['pickle']
            )
            consumer.register_callback(callback)
            with consumer:
                conn.drain_events(timeout=1)

Features
========
* Type: Virtual
* Supports Direct: Yes
* Supports Topic: Yes
* Supports Fanout: No
* Supports Priority: No
* Supports TTL: No

Connection String
=================
Connection string is in the following format:

.. code-block::

    filesystem://

Transport Options
=================
* ``data_folder_in`` - directory where are messages stored when written
  to queue.
* ``data_folder_out`` - directory from which are messages read when read from
  queue.
* ``store_processed`` - if set to True, all processed messages are backed up to
  ``processed_folder``.
* ``processed_folder`` - directory where are backed up processed files.
    N)Empty)	monotonic)ChannelError)bytes_to_strstr_to_bytes)dumpsloads)cached_property   )virtual)r   r   r   .ntc                     t          j        |                                           }t          j        ||ddt                     dS )Create file lock.r         N)	win32file_get_osfhandlefileno
LockFileEx__overlapped)fileflagshfiles      V/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/kombu/transport/filesystem.pylockr   y   s9    (77UE1j,GGGGG    c                     t          j        |                                           }t          j        |ddt                     dS )Remove file lock.r   r   N)r   r   r   UnlockFileExr   )r   r   s     r   unlockr    ~   s7    (77ua\BBBBBr   posix)LOCK_EXLOCK_NBLOCK_SHc                 T    t          j        |                                 |           dS )r   N)fcntlflockr   )r   r   s     r   r   r      s"    DKKMM5)))))r   c                 h    t          j        |                                 t           j                   dS )r   N)r&   r'   r   LOCK_UN)r   s    r   r    r       s$    DKKMM5=11111r   z9Filesystem plugin only defined for NT and POSIX platformsc                       e Zd ZdZd Zd Zd Zd Zed             Z	e
d             Ze
d             Ze
d	             Ze
d
             ZdS )ChannelzFilesystem Channel.c                 x   d                     t          t          t                      dz                      t	          j                    |          }t          j                            | j	        |          }	 t          |d          }t          |t                     |                    t          t          |                               n!# t           $ r t#          d|d          w xY w	 t%          |           |                                 dS # t%          |           |                                 w xY w)zPut `message` onto `queue`.z{}_{}.{}.msgi  wbzCannot add file z to directoryN)formatintroundr   uuiduuid4ospathjoindata_folder_outopenr   r"   writer   r   OSErrorr   r    close)selfqueuepayloadkwargsfilenamefs         r   _putzChannel._put   s   !((U9;;3E-F-F)G)G)-u> >7<< 4h??		Xt$$AGGGLw001111 	> 	> 	><8<<<> > >	> 2
 1IIIGGIIIII 1IIIGGIIIIs   8AC D C++D %D9c                 @   d|z   dz   }t          j        | j                  }t          |          }t	          |          dk    rM|                    d          }|                    |          dk     rC| j        r| j        }nt          j
                    }	 t          j        t           j                            | j        |          |           n# t          $ r Y nw xY wt           j                            ||          }	 t!          |d          }|                                }|                                 | j        st          j        |           n!# t          $ r t)          d|d          w xY wt+          t-          |                    S t/                      )zGet next message from `queue`.r   .msgr   rbzCannot read file z from queue.)r3   listdirdata_folder_insortedlenpopfindstore_processedprocessed_foldertempfile
gettempdirshutilmover4   r5   r9   r7   readr:   remover   r	   r   r   )r;   r<   
queue_findfolderr?   rL   r@   r=   s           r   _getzChannel._get   s   5[6)
D/00&kkAoozz!}}H }}Z((1,,# 9#'#8  #+#6#8#8 BGLL)<hGG,. . . .    w||$4h??HB4((&&((			+ (Ih''' B B B"@@@@B B BB g..///ggs   8C 
CCAE E3c                    d}d|z   dz   }t          j        | j                  }t          |          dk    r|                                }	 |                    |          dk     rBt           j                            | j        |          }t          j        |           |dz  }n# t          $ r Y nw xY wt          |          dk    |S )z!Remove all messages from `queue`.r   r   rC   r   )
r3   rE   rF   rH   rI   rJ   r4   r5   rR   r9   r;   r<   countrS   rT   r?   s         r   _purgezChannel._purge   s    5[6)
D/00&kkAoozz||H==,,q007<<(;XFF	(###
     &kkAoo" s   B% &>B% %
B21B2c                     d}d| d}t          j        | j                  }t          |          dk    rF|                                }|                    |          dk     rA|dz  }t          |          dk    F|S )z<Return the number of messages in `queue` as an :class:`int`.r   r   rC   r   )r3   rE   rF   rH   rI   rJ   rW   s         r   _sizezChannel._size   s    $___
D/00&kkAoozz||H }}Z((1,,QJE &kkAoo r   c                 $    | j         j        j        S N)
connectionclienttransport_optionsr;   s    r   r`   zChannel.transport_options   s    %77r   c                 8    | j                             dd          S )NrF   data_inr`   getra   s    r   rF   zChannel.data_folder_in   s    %))*:IFFFr   c                 8    | j                             dd          S )Nr6   data_outrd   ra   s    r   r6   zChannel.data_folder_out  s    %))*;ZHHHr   c                 8    | j                             dd          S )NrK   Frd   ra   s    r   rK   zChannel.store_processed  s    %))*;UCCCr   c                 8    | j                             dd          S )NrL   	processedrd   ra   s    r   rL   zChannel.processed_folder	  s    %))*<kJJJr   N)__name__
__module____qualname____doc__rA   rU   rY   r[   propertyr`   r
   rF   r6   rK   rL    r   r   r+   r+      s          "% % %N  2  " 8 8 X8 G G _G I I _I D D _D K K _K K Kr   r+   c                   V     e Zd ZdZeZ ej                    ZdZdZ	dZ
 fdZd Z xZS )	TransportzFilesystem Transport.r   
filesystemc                 T     t                      j        |fi | | j        | _        d S r]   )super__init__global_statestate)r;   r_   r>   	__class__s      r   rv   zTransport.__init__  s0    **6***&


r   c                     dS )NzN/Arp   ra   s    r   driver_versionzTransport.driver_version  s    ur   )rk   rl   rm   rn   r+   r   BrokerStaterw   default_portdriver_typedriver_namerv   r{   __classcell__)ry   s   @r   rr   rr     sq        G&7&((LLKK' ' ' ' '      r   rr   )+rn   r3   rO   rM   r1   r<   r   timer   kombu.exceptionsr   kombu.utils.encodingr   r   kombu.utils.jsonr   r	   kombu.utils.objectsr
    r   VERSIONr5   mapstr__version__name
pywintypeswin32conr   LOCKFILE_EXCLUSIVE_LOCKr"   r$   LOCKFILE_FAIL_IMMEDIATELYr#   
OVERLAPPEDr   r   r    r&   RuntimeErrorr+   rr   rp   r   r   <module>r      sJ  X Xt 
			                ) ) ) ) ) ) ; ; ; ; ; ; ; ; ) ) ) ) ) ) ) ) / / / / / /      
hhss3(()) 7d??OOO.GG0G(:(**LH H H
C C C C
 WLLL//////////* * *2 2 2 2 ,CE E EwK wK wK wK wKgo wK wK wKt    !     r   