diff --git a/pulsar/apps/data/channels.py b/pulsar/apps/data/channels.py index f051f495..d9ce708d 100644 --- a/pulsar/apps/data/channels.py +++ b/pulsar/apps/data/channels.py @@ -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 @@ -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__ diff --git a/pulsar/apps/data/store.py b/pulsar/apps/data/store.py index f42db1a0..bcc2ed0a 100644 --- a/pulsar/apps/data/store.py +++ b/pulsar/apps/data/store.py @@ -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): @@ -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): @@ -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 @@ -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: @@ -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) diff --git a/tests/stores/channels.py b/tests/stores/channels.py index 467ec753..0bc26a7a 100644 --- a/tests/stores/channels.py +++ b/tests/stores/channels.py @@ -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()