# Path: gnsq/lookupd.py
# class LookupdClient(HTTPClient):
#     """Low level http client for nsqlookupd.
# 
#     :param host: nsqlookupd host address (default: localhost)
# 
#     :param port: nsqlookupd http port (default: 4161)
# 
#     :param useragent: useragent sent to nsqlookupd (default:
#         ``<client_library_name>/<version>``)
# 
#     :param connection_class: override the http connection class
#     """
#     def __init__(self, host='localhost', port=4161, **kwargs):
#         super(LookupdClient, self).__init__(host, port, **kwargs)
# 
#     def lookup(self, topic):
#         """Returns producers for a topic."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('GET', '/lookup', fields={'topic': topic})
# 
#     def topics(self):
#         """Returns all known topics."""
#         return self._request('GET', '/topics')
# 
#     def channels(self, topic):
#         """Returns all known channels of a topic."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('GET', '/channels', fields={'topic': topic})
# 
#     def nodes(self):
#         """Returns all known nsqd."""
#         return self._request('GET', '/nodes')
# 
#     def create_topic(self, topic):
#         """Add a topic to nsqlookupd's registry."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('POST', '/topic/create', fields={'topic': topic})
# 
#     def delete_topic(self, topic):
#         """Deletes an existing topic."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('POST', '/topic/delete', fields={'topic': topic})
# 
#     def create_channel(self, topic, channel):
#         """Add a channel to nsqlookupd's registry."""
#         nsq.assert_valid_topic_name(topic)
#         nsq.assert_valid_channel_name(channel)
#         return self._request('POST', '/channel/create',
#                              fields={'topic': topic, 'channel': channel})
# 
#     def delete_channel(self, topic, channel):
#         """Deletes an existing channel of an existing topic."""
#         nsq.assert_valid_topic_name(topic)
#         nsq.assert_valid_channel_name(channel)
#         return self._request('POST', '/channel/delete',
#                              fields={'topic': topic, 'channel': channel})
# 
#     def tombstone_topic(self, topic, node):
#         """Tombstones a specific producer of an existing topic."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('POST', '/topic/tombstone',
#                              fields={'topic': topic, 'node': node})
# 
#     def ping(self):
#         """Monitoring endpoint.
# 
#         :returns: should return `"OK"`, otherwise raises an exception.
#         """
#         return self._request('GET', '/ping')
# 
#     def info(self):
#         """Returns version information."""
#         return self._request('GET', '/info')


import random
import six
from .lookupd import LookupdClient

def normalize_nsqd_address(address):
    if not isinstance(address, six.string_types):
        raise TypeError('nsqd address must be a string')

    host, _, port = address.partition(':')
    if port:
        try:
            port = int(port, 10)
        except ValueError:
            raise ValueError('invalid nsqd port')

    else:
        port = 4150

    return '{host}:{port}'.format(host=host, port=port)


def parse_nsqds(nsqd_tcp_addresses):
    if isinstance(nsqd_tcp_addresses, six.string_types):
        return set([normalize_nsqd_address(nsqd_tcp_addresses)])

    elif isinstance(nsqd_tcp_addresses, (list, tuple, set)):
        return set(normalize_nsqd_address(addr) for addr in nsqd_tcp_addresses)

    raise TypeError('nsqd_tcp_addresses must be a list, set or tuple')


def parse_lookupds(lookupd_http_addresses):
    if isinstance(lookupd_http_addresses, six.string_types):

# Path: gnsq/lookupd.py
# class LookupdClient(HTTPClient):
#     """Low level http client for nsqlookupd.
# 
#     :param host: nsqlookupd host address (default: localhost)
# 
#     :param port: nsqlookupd http port (default: 4161)
# 
#     :param useragent: useragent sent to nsqlookupd (default:
#         ``<client_library_name>/<version>``)
# 
#     :param connection_class: override the http connection class
#     """
#     def __init__(self, host='localhost', port=4161, **kwargs):
#         super(LookupdClient, self).__init__(host, port, **kwargs)
# 
#     def lookup(self, topic):
#         """Returns producers for a topic."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('GET', '/lookup', fields={'topic': topic})
# 
#     def topics(self):
#         """Returns all known topics."""
#         return self._request('GET', '/topics')
# 
#     def channels(self, topic):
#         """Returns all known channels of a topic."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('GET', '/channels', fields={'topic': topic})
# 
#     def nodes(self):
#         """Returns all known nsqd."""
#         return self._request('GET', '/nodes')
# 
#     def create_topic(self, topic):
#         """Add a topic to nsqlookupd's registry."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('POST', '/topic/create', fields={'topic': topic})
# 
#     def delete_topic(self, topic):
#         """Deletes an existing topic."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('POST', '/topic/delete', fields={'topic': topic})
# 
#     def create_channel(self, topic, channel):
#         """Add a channel to nsqlookupd's registry."""
#         nsq.assert_valid_topic_name(topic)
#         nsq.assert_valid_channel_name(channel)
#         return self._request('POST', '/channel/create',
#                              fields={'topic': topic, 'channel': channel})
# 
#     def delete_channel(self, topic, channel):
#         """Deletes an existing channel of an existing topic."""
#         nsq.assert_valid_topic_name(topic)
#         nsq.assert_valid_channel_name(channel)
#         return self._request('POST', '/channel/delete',
#                              fields={'topic': topic, 'channel': channel})
# 
#     def tombstone_topic(self, topic, node):
#         """Tombstones a specific producer of an existing topic."""
#         nsq.assert_valid_topic_name(topic)
#         return self._request('POST', '/topic/tombstone',
#                              fields={'topic': topic, 'node': node})
# 
#     def ping(self):
#         """Monitoring endpoint.
# 
#         :returns: should return `"OK"`, otherwise raises an exception.
#         """
#         return self._request('GET', '/ping')
# 
#     def info(self):
#         """Returns version information."""
#         return self._request('GET', '/info')


# Path: gnsq/util.py
import random
import six
from .lookupd import LookupdClient


def normalize_nsqd_address(address):
    if not isinstance(address, six.string_types):
        raise TypeError('nsqd address must be a string')

    host, _, port = address.partition(':')
    if port:
        try:
            port = int(port, 10)
        except ValueError:
            raise ValueError('invalid nsqd port')

    else:
        port = 4150

    return '{host}:{port}'.format(host=host, port=port)


def parse_nsqds(nsqd_tcp_addresses):
    if isinstance(nsqd_tcp_addresses, six.string_types):
        return set([normalize_nsqd_address(nsqd_tcp_addresses)])

    elif isinstance(nsqd_tcp_addresses, (list, tuple, set)):
        return set(normalize_nsqd_address(addr) for addr in nsqd_tcp_addresses)

    raise TypeError('nsqd_tcp_addresses must be a list, set or tuple')


def parse_lookupds(lookupd_http_addresses):
    if isinstance(lookupd_http_addresses, six.string_types):

        return [LookupdClient.from_url(lookupd_http_addresses)]