/usr/lib/python2.7/dist-packages/ws4redis/publisher.py is in python-django-websocket-redis 0.4.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #-*- coding: utf-8 -*-
from redis import ConnectionPool, StrictRedis
from ws4redis import settings
from ws4redis.redis_store import RedisStore
redis_connection_pool = ConnectionPool(**settings.WS4REDIS_CONNECTION)
class RedisPublisher(RedisStore):
def __init__(self, **kwargs):
"""
Initialize the channels for publishing messages through the message queue.
"""
connection = StrictRedis(connection_pool=redis_connection_pool)
super(RedisPublisher, self).__init__(connection)
for key in self._get_message_channels(**kwargs):
self._publishers.add(key)
def fetch_message(self, request, facility, audience='any'):
"""
Fetch the first message available for the given ``facility`` and ``audience``, if it has
been persisted in the Redis datastore.
The current HTTP ``request`` is used to determine to whom the message belongs.
A unique string is used to identify the bucket's ``facility``.
Determines the ``audience`` to check for the message. Must be one of ``broadcast``,
``group``, ``user``, ``session`` or ``any``. The default is ``any``, which means to check
for all possible audiences.
"""
prefix = self.get_prefix()
channels = []
if audience in ('session', 'any',):
if request and request.session:
channels.append('{prefix}session:{0}:{facility}'.format(request.session.session_key, prefix=prefix, facility=facility))
if audience in ('user', 'any',):
if request and request.user and request.user.is_authenticated():
channels.append('{prefix}user:{0}:{facility}'.format(request.user.get_username(), prefix=prefix, facility=facility))
if audience in ('group', 'any',):
try:
if request.user.is_authenticated():
groups = request.session['ws4redis:memberof']
channels.extend('{prefix}group:{0}:{facility}'.format(g, prefix=prefix, facility=facility)
for g in groups)
except (KeyError, AttributeError):
pass
if audience in ('broadcast', 'any',):
channels.append('{prefix}broadcast:{facility}'.format(prefix=prefix, facility=facility))
for channel in channels:
message = self._connection.get(channel)
if message:
return message
|