PK! opgnotify/__init__.pyfrom __future__ import absolute_import, division, print_function, unicode_literals from logx import log log.set_null_handler() from .notify import ( # noqa await_pg_notifications, get_dbapi_connection, start_listening, ) PK!ddpgnotify/notify.pyfrom __future__ import absolute_import, division, print_function, unicode_literals import errno import fcntl import os import select import signal import sys import psycopg2 from logx import log from six import string_types def get_wakeup_fd(): pipe_r, pipe_w = os.pipe() flags = fcntl.fcntl(pipe_w, fcntl.F_GETFL, 0) flags = flags | os.O_NONBLOCK flags = fcntl.fcntl(pipe_w, fcntl.F_SETFL, flags) signal.set_wakeup_fd(pipe_w) return pipe_r def empty_signal_handler(signal, frame): pass try: import sqlalchemy except ImportError: # pragma: no cover sqlalchemy = None if sqlalchemy: from sqlalchemy.engine.base import Engine def get_dbapi_connection(x): if isinstance(x, string_types): c = psycopg2.connect(x) x = c elif sqlalchemy and isinstance(x, Engine): sqla_connection = x.connect() sqla_connection.execution_options(isolation_level="AUTOCOMMIT") sqla_connection.detach() x = sqla_connection.connection.connection else: pass x.autocommit = True return x def quote_table_name(name): return '"{}"'.format(name) def start_listening(connection, channels): names = (quote_table_name(each) for each in channels) listens = "; ".join(["listen {}".format(n) for n in names]) c = connection.cursor() c.execute(listens) c.close() def log_notification(_n): log.debug("NOTIFY: {}, {}, {}".format(_n.pid, _n.channel, _n.payload)) def await_pg_notifications( dburi_or_sqlaengine_or_dbapiconnection, channels=None, timeout=5, yield_on_timeout=False, handle_signals=None, notifications_as_list=False, ): """Subscribe to PostgreSQL notifications, and handle them in infinite-loop style. On an actual message, returns the notification (with .pid, .channel, and .payload attributes). If you've enabled 'yield_on_timeout', yields None on timeout. If you've enabled 'handle_keyboardinterrupt', yields False on interrupt. """ timeout_is_callable = callable(timeout) cc = get_dbapi_connection(dburi_or_sqlaengine_or_dbapiconnection) if channels: if isinstance(channels, string_types): channels = [channels] start_listening(cc, channels) signals_to_handle = handle_signals or [] original_handlers = {} try: if signals_to_handle: for s in signals_to_handle: original_handlers[s] = signal.signal(s, empty_signal_handler) wakeup = get_wakeup_fd() listen_on = [cc, wakeup] else: listen_on = [cc] while True: try: if timeout_is_callable: _timeout = timeout() log.debug("dynamic timeout of {_timeout} seconds") else: _timeout = timeout _timeout = max(0, _timeout) r, w, x = select.select(listen_on, [], [], _timeout) log.debug("select call awoken, returned: {}".format((r, w, x))) if (r, w, x) == ([], [], []): log.debug("idle timeout on select call, carrying on...") if yield_on_timeout: yield None if wakeup in r: signal_byte = os.read(wakeup, 1) signal_int = int.from_bytes(signal_byte, sys.byteorder) sig = signal.Signals(signal_int) signal_name = signal.Signals(sig).name log.info(f"woken from slumber by signal: {signal_name}") yield signal_int if cc in r: cc.poll() nlist = [] while cc.notifies: notify = cc.notifies.pop() nlist.append(notify) if notifications_as_list: for n in nlist: log_notification(n) yield nlist else: for n in nlist: log_notification(n) yield n except select.error as e: e_num, e_message = e if e_num == errno.EINTR: log.debug("EINTR happened during select") else: raise finally: if signals_to_handle: for s in signals_to_handle: signal_name = signal.Signals(s).name log.debug(f"restoring original handler for: {signal_name}") signal.signal(s, original_handlers[s]) PK!a)pgnotify-0.1.1541466735.dist-info/LICENSEThis is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to PK!H@ WX'pgnotify-0.1.1541466735.dist-info/WHEEL A н#f DI޾p}pfCSmֻ.,"檸m - |PK!H~(k" *pgnotify-0.1.1541466735.dist-info/METADATAVmO8_1*(RnY^HH{-tOZ!Ժ4^9No$-/M=o. oj6`s>Ю 6pRRCchp6BEg(hъ'$iuk=!16~^i<8$1(ےPXSxXiywW Ծ<؛^BfW7%)鰷_ѸًoZ 7-#ZzioXΔĥo jfSP3ҳ5 sBV 6y~7Wm)@V?e&6w쮷~TJ ӐPK!H&90(pgnotify-0.1.1541466735.dist-info/RECORDιv@gAaX"5Y&<}rRZ%o̞(*r"Ep:Z{5 Q4h&2i1-.;$Ӂ0\ݫG\51DCm+B1X?j hHC, :.6VV"֑̅jlvSoL0dn*{7l0 O#Ę} :y)hhϻ]S8eIjm (8,:Ct?םb1taZ:U}ȏ!n,\Q +HddZPK! opgnotify/__init__.pyPK!ddpgnotify/notify.pyPK!a)pgnotify-0.1.1541466735.dist-info/LICENSEPK!H@ WX'pgnotify-0.1.1541466735.dist-info/WHEELPK!H~(k" *Npgnotify-0.1.1541466735.dist-info/METADATAPK!H&90(pgnotify-0.1.1541466735.dist-info/RECORDPK.