Skip to content
This repository has been archived by the owner on Nov 23, 2020. It is now read-only.

Commit

Permalink
channels inherit namespace from store and set its own dns
Browse files Browse the repository at this point in the history
  • Loading branch information
lsbardel committed Oct 23, 2016
1 parent bc2bb69 commit 1cf35ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
11 changes: 9 additions & 2 deletions pulsar/apps/data/channels.py
Expand Up @@ -77,7 +77,14 @@ def __init__(self, pubsub, namespace=None, status_channel=None,
self._connection_error = False
self.channels = OrderedDict()
self.logger = logger or LOGGER
self.namespace = '%s_' % (namespace or DEFAULT_NAMESPACE).lower()
self.namespace = (
namespace or
pubsub.store.urlparams.get('namespace') or
DEFAULT_NAMESPACE
).lower()
if not self.namespace.endswith('_'):
self.namespace = '%s_' % self.namespace
self.dns = pubsub.store.buildurl(namespace=self.namespace)
self.status_channel = (status_channel or DEFAULT_CHANNEL).lower()
self.status = StatusType.initialised
self.pubsub = pubsub
Expand All @@ -89,7 +96,7 @@ def _loop(self):
return self.pubsub._loop

def __repr__(self):
return '%s/%s' % (self.pubsub.store.dns, self.prefixed('*'))
return self.dns

__str__ = __repr__

Expand Down
15 changes: 10 additions & 5 deletions pulsar/apps/data/store.py
Expand Up @@ -73,7 +73,7 @@ def __init__(self, name, host, database=None,
self._password = password
self._urlparams = {}
self._init(**kw)
self._dns = self._buildurl()
self._dns = self.buildurl()

@property
def name(self):
Expand All @@ -88,7 +88,7 @@ def database(self):
@database.setter
def database(self, value):
self._database = value
self._dns = self._buildurl()
self._dns = self.buildurl()

@property
def encoding(self):
Expand All @@ -101,6 +101,11 @@ def dns(self):
'''Domain name server'''
return self._dns

@property
def urlparams(self):
"""url parameters in dns query"""
return self._urlparams

@classmethod
def register(cls):
pass
Expand Down Expand Up @@ -145,7 +150,7 @@ def _init(self, **kw): # pragma nocover
'''Internal initialisation'''
pass

def _buildurl(self, **kw):
def buildurl(self, **kw):
pre = ''
if self._user:
if self._password:
Expand All @@ -160,8 +165,8 @@ def _buildurl(self, **kw):
host = '%s:%s' % host
host = '%s%s' % (pre, host)
path = '/%s' % self._database if self._database else ''
kw.update(self._urlparams)
query = urlencode(kw)
self._urlparams.update(kw)
query = urlencode(self._urlparams)
scheme = self._name
if self._scheme:
scheme = '%s+%s' % (self._scheme, scheme)
Expand Down
14 changes: 11 additions & 3 deletions tests/stores/channels.py
Expand Up @@ -16,9 +16,17 @@ def __call__(self, *args, **kwargs):

class ChannelsTests:

def channels(self):
return Channels(self.store.pubsub(protocol=Json()),
namespace=self.namespace())
def channels(self, **kw):
return Channels(self.store.pubsub(protocol=Json()), **kw)

def test_channels_dns(self):
channels = self.channels()
self.assertEqual(channels.namespace, '%s_' % self.namespace())
channels = self.channels(namespace='foo')
self.assertEqual(channels.namespace, 'foo_')
channels = self.channels(namespace='foo_')
self.assertEqual(channels.namespace, 'foo_')
self.assertTrue(str(channels).endswith('?namespace=foo_'))

async def test_channels(self):
channels = self.channels()
Expand Down

0 comments on commit 1cf35ac

Please sign in to comment.