§
    öÛg¸  ã                   óJ   — d Z ddlmZ ddlmZ ddlmZ  G d„ de¦  «        ZdS )a  
Notification Management Tools

Primarily this module houses the `NotificationManager` class which provides an
iterator for a NOTIFY event loop against a set of connections.

	>>> import postgresql
	>>> db = postgresql.open(...)
	>>> from postgresql.notifyman import NotificationManager
	>>> nm = NotificationManager(db, timeout = 10) # idle events every 10 seconds
	>>> for x in nm:
	...  if x is None:
	...   # idle event
	...   ...
	...  db, notifies = x
	...  for channel, payload, pid in notifies:
	...   ...
é    )Útime)Úselect)Úchainc                   ól   — e Zd ZdZdZddœd„Zeefd„Zd„ Z	d„ Z
d	„ Zd
„ Zefd„Zd„ Zd„ Zd„ Zefd„ZdS )ÚNotificationManagera_  
	A class for managing the asynchronous notifications received by a
	set of connections.

	Instances provide the iterator for an event loop that responds to NOTIFYs
	received by the connections being watched. There is no thread safety, so
	when a connection is being managed, it should not be used concurrently in
	other threads while being managed.
	)ÚconnectionsÚgarbageÚincomingÚtimeoutÚ
_last_timeÚ_pulledN©r   c                óÀ   — |                       |¦  «         t          |¦  «        | _        t          ¦   «         | _        d | _        d | _        t          ¦   «         | _        d S ©N)Ú
settimeoutÚsetr   r	   r
   r   Údictr   )Úselfr   r   s      úJ/granary/edev/quantum/lib/python3.11/site-packages/postgresql/notifyman.pyÚ__init__zNotificationManager.__init__-   sL   € Ø‡/‚/�'ÑÔÐÝ˜Ñ%Ô%€$Ôå‘”€$„,à€$„-Ø€$„/å‘”€$„,€,€,ó    c                 ó4  — | j         dk    rd}nD| j        �%| j         pd |¦   «         | j        z
  z
  }|dk     rd}n |¦   «         | _        | j         pd}| j        | j        z
  }|D ]=}|j        r4| j                             |¦  «         | j                             |¦  «         Œ>| j        | j        z
  } ||d||¦  «        \  }}}|D ]=}	 |                     d¦  «         Œ# t          $ r | 	                    |¦  «         Y Œ:w xY w|  
                    |¦  «         d S )Nr   é
   © Ú )r   r   r   r	   ÚclosedÚremoveÚaddÚexecuteÚ	ExceptionÚappendÚtrash)	r   r   r   Úmax_durationÚcheckÚdbÚrÚwÚxs	            r   Ú_wait_on_wiresz"NotificationManager._wait_on_wires9   sV  € Ø	„\�QÒÐà€<€<ð „oÐ!Ø”LÐ& B¨4¨4©6¬6°D´OÑ+CÑD€LØ�aÒÐØ€\øà�d‘f”f€D„OØ”<Ð% 2€Lð Ô
˜Tœ\Ñ
)€%Øð ð €bØ„ið ØÔ×Ò˜BÑÔÐØ„L×Ò�RÑÔÐøØ
Ô
˜Tœ\Ñ
)€%àˆF�5˜"˜e \Ñ2Ô2�'€!€Qˆàð 
ð 
€bðð ‡J‚Jˆr�N„N€N€NøÝ
ð ð ð ð ‡H‚HˆR�L„L€L€L€Lðøøøð ‡*‚*ˆQ�-„-€-€-€-s   ÃCÃD Ã?D c                 ón   — | j                              |¦  «         | j                             |¦  «         dS )zÎ
		Remove the given connections from the set of good connections, and add
		them to the `garbage` set.

		This method can be overridden by subclasses to take a callback approach
		to connection failures.
		N)r	   Úupdater   Údifference_update)r   r   s     r   r"   zNotificationManager.trash_   s7   € ð „,×Ò�kÑ"Ô"Ð"ØÔ×$Ò$ [Ñ1Ô1Ð1Ð1Ð1r   c                 ó~   — | j                              |t          ¦   «         ¦  «        }|                     |¦  «         dS )z²
		Queue the notifies for the specified connection. Upon success, the 

		This method can be overridden by subclasses to take a callback approach
		to notification management.
		N)r   Ú
setdefaultÚlistÚextend)r   r%   ÚnotifiesÚls       r   ÚqueuezNotificationManager.queuek   s7   € ð „l×Ò˜b¥$¡&¤&Ñ)Ô)€!Ø‡(‚(ˆ8ÑÔÐÐÐr   c                 óÄ   ‡— | j         D ]V}|j        sŒ
|j        j        Šˆfd„|j        D ¦   «         }|                      ||¦  «         |j        d t          |¦  «        …= ŒWd S )Nc                 ó\   •— g | ](} ‰|j         ¦  «         ‰|j        ¦  «        |j        f‘Œ)S r   )ÚchannelÚpayloadÚpid)Ú.0r(   Údecodes     €r   ú
<listcomp>z>NotificationManager._pull_from_connections.<locals>.<listcomp>}   sJ   ø€ ð ð ð à	ð €VˆAŒIÑÔ˜˜˜qœyÑ)Ô)¨1¬5Ð1ðð ð r   )r   Ú	_notifiesÚtypior:   r3   Úlen)r   r%   r1   r:   s      @r   Ú_pull_from_connectionsz*NotificationManager._pull_from_connectionsv   sŒ   ø€ ØÔð $ð $€bØ
Œ,ð ààŒHŒO€6ðð ð ð àŒ\ðñ ô €8ð ‡:‚:ˆb�(ÑÔÐØ	„|�N•S˜‘]”]�NÐ#Ð#ð$ð $r   c                 ót  — g }| j         D ]0}|j        j        }|                     || j         |         f¦  «         Œ1|rV| j        r1t          | j        t          |¦  «        ¦  «        i c| _        | _         d S t          |¦  «        i c| _        | _         d S | j        €t          d¦  «        i c| _        | _         d S d S )Nr   )r   r=   r:   r!   r
   r   Úiter)r   Únew_seqsr%   r:   s       r   Ú_queue_nextzNotificationManager._queue_next…   sÀ   € Ø€(ØŒLð +ð +€bØŒHŒO€6Ø‡?‚?�B˜œ RÔ(Ð)Ñ*Ô*Ð*Ð*àð 	.Ø
„mð 5õ #(¨¬µt¸H±~´~Ñ"FÔ"FÈÐ€D„M�4”<�<�<å"& x¡.¤.°"Ð€D„M�4”<�<�<Ø„}Ðå!% b¡¤¨2Ð€4„=�$”,�,�,ð Ðr   c                 óŒ   —  |¦   «         }| j         €|| _         n*| j        r|| j         | j        z   k    r	d | _         dS d | _         dS )NTF)r   r   )r   r   Únows      r   Ú	_timedoutzNotificationManager._timedout–   sY   € ð 	ˆ‰Œ€#Ø	„_ÐØ€4„?€?Ø„|ð ˜ ¤°$´,Ñ >Ò?Ð?ð €4„?à
ˆ$ð €4„?Ø	ˆr   c                 óB   — |�|dk     rt          d¦  «        ‚|| _        dS )a“  
		Set the maximum duration, in seconds, for waiting for NOTIFYs on the
		set of managed connections. The given `seconds` argument can be a number
		or `None`.

		A timeout of `None` means no timeout, and "idle" events will never
		occur.

		A timeout of `0` means to never wait for NOTIFYs. This has the effect of
		a StopIteration being raised by `__next__` when there are no more
		Notifications available for any of the connections in the set. "Idle"
		events will never occur in this situation as well.

		A timeout greater than zero means to emit `None` as "idle" events into
		the loop at the specified interval. Idle events are guaranteed to occur.
		Nr   z!cannot set timeout less than zero)Ú
ValueErrorr   )r   Úsecondss     r   r   zNotificationManager.settimeout§   s,   € ð" Ð˜W qš[˜[Ý	Ð7Ñ	8Ô	8Ð8Ø€$„,€,€,r   c                 ó   — | j         S )zGet the timeout.r   ©r   s    r   Ú
gettimeoutzNotificationManager.gettimeout¼   s
   € à	ŒÐr   c                 ó   — | S r   r   rK   s    r   Ú__iter__zNotificationManager.__iter__À   s   € Ø	€+r   c                 óV  — d}	 | j         �?	 t          | j         ¦  «        S # t          $ r d | _         | j        dk    s| j        s‚ Y nw xY w|                      ¦   «         rd S |s| j        r|                      ¦   «          d}nd}|                      ¦   «          |                      ¦   «          Œ§)NTr   F)	r
   ÚnextÚStopIterationr   r   rF   r)   r?   rC   )r   r   Úchecked_wires      r   Ú__next__zNotificationManager.__next__Ã   sß   € Ø€,ðØ
„mÐð
Ý�”ÑÔÐøÝð ð ð à€T„]ð
 	Œ˜ÒÐ Ô!1ÐØð 	Ððøøøð ‡n‚nÑÔð Øˆ4à
ð ˜4Ô+ð à×ÒÑÔÐØ€L€Là€LØ×ÒÑ Ô Ð Ø×ÒÑÔÐð7s   Œ   $AÁA)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r)   r"   r3   r?   rC   rF   r   rL   rN   rS   r   r   r   r   r      sú   € € € € € ðð ð€ð -1ð 	ð 	ð 	ð 	ð 	ð "&°ð $ð $ð $ð $ðL
2ð 
2ð 
2ðð ð ð$ð $ð $ð.ð .ð .ð" !ð ð ð ð ð"ð ð ð*ð ð ðð ð ð  ð ð ð ð ð ð r   r   N)rW   r   r   Ú	itertoolsr   Úobjectr   r   r   r   ú<module>r[      s…   ððð ð$ Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð ðGð Gð Gð Gð G˜&ñ Gô Gð Gð Gð Gr   