def connect(self, connection=None, host=None, port=None, force=False): """Connect to an NDS server If a connection is given, it is simply attached, otherwise a new connection will be opened Parameters ---------- connection : `nds2.connection`, optional an existing open connection to an NDS server host : `str`, optional the name of the NDS server host to connect port : `int`, optional the port number for the NDS server connection force : `bool`, optional force a new connection even if one is already open Returns ------- connection : `nds2.connection` the attached connection """ if force or (self.connection and (connection or host)): del self.connection self.logger.debug('Closed existing connection') # set up connection if connection: self.connection = connection self.logger.debug( 'Attached open connection to %s:%d...' % (self.connection.get_host(), self.connection.get_port())) else: if not host: ifos = list(set([c.ifo for c in self.channels if c.ifo])) if len(ifos) == 1: hosts = ndsio.host_resolution_order(ifos[0]) else: hosts = ndsio.host_resolution_order(None) try: host, port = hosts[0] except IndexError: raise ValueError("Cannot auto-select NDS server for " "ifos: %s" % ifos) if port: self.logger.debug('Opening connection to %s:%d...' % (host, port)) else: self.logger.debug('Opening connection to %s...' % host) self.connection = ndsio.auth_connect(host, port) self.logger.debug('Connection open') return self.connection
def connect(self, connection=None, host=None, port=None, force=False): """Connect to an NDS server If a connection is given, it is simply attached, otherwise a new connection will be opened Parameters ---------- connection : `nds2.connection`, optional an existing open connection to an NDS server host : `str`, optional the name of the NDS server host to connect port : `int`, optional the port number for the NDS server connection force : `bool`, optional force a new connection even if one is already open Returns ------- connection : `nds2.connection` the attached connection """ if force or (self.connection and (connection or host)): del self.connection self.logger.debug('Closed existing connection') # set up connection if connection: self.connection = connection self.logger.debug('Attached open connection to %s:%d...' % (self.connection.get_host(), self.connection.get_port())) else: if not host: ifos = list(set([c.ifo for c in self.channels if c.ifo])) if len(ifos) == 1: hosts = ndsio.host_resolution_order(ifos[0]) else: hosts = ndsio.host_resolution_order(None) try: host, port = hosts[0] except IndexError: raise ValueError("Cannot auto-select NDS server for " "ifos: %s" % ifos) if port: self.logger.debug('Opening connection to %s:%d...' % (host, port)) else: self.logger.debug('Opening connection to %s...' % host) self.connection = ndsio.auth_connect(host, port) self.logger.debug('Connection open') return self.connection
def test_nds2_host_order_ndsserver(self): """Test `host_resolution_order` with default env set """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) os.environ['NDSSERVER'] = 'test1.ligo.org:80,test2.ligo.org:43' hro = nds.host_resolution_order(None) self.assertListEqual( hro, [('test1.ligo.org', 80), ('test2.ligo.org', 43)]) hro = nds.host_resolution_order('L1') self.assertListEqual( hro, [('test1.ligo.org', 80), ('test2.ligo.org', 43)])
def test_nds2_host_order_ndsserver(self): """Test `host_resolution_order` with default env set """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) os.environ['NDSSERVER'] = 'test1.ligo.org:80,test2.ligo.org:43' hro = nds.host_resolution_order(None) self.assertListEqual(hro, [('test1.ligo.org', 80), ('test2.ligo.org', 43), ('nds.ligo.caltech.edu', 31200)]) hro = nds.host_resolution_order('L1') self.assertListEqual(hro, [('test1.ligo.org', 80), ('test2.ligo.org', 43), ('nds.ligo-la.caltech.edu', 31200), ('nds.ligo.caltech.edu', 31200)])
def test_nds2_host_order_none(self): """Test `host_resolution_order` with `None` IFO """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) hro = nds.host_resolution_order(None, env=None) self.assertListEqual(hro, [('nds.ligo.caltech.edu', 31200)])
def test_nds2_host_order_ifo(self): """Test `host_resolution_order` with `ifo` argument """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) hro = nds.host_resolution_order('L1', env=None) self.assertListEqual(hro, [('nds.ligo-la.caltech.edu', 31200), ('nds.ligo.caltech.edu', 31200)])
def test_nds2_host_order_ifo(self): """Test `host_resolution_order` with `ifo` argument """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) hro = nds.host_resolution_order('L1', env=None) self.assertListEqual( hro, [('nds.ligo-la.caltech.edu', 31200), ('nds.ligo.caltech.edu', 31200)])
def test_nds2_host_order_env(self): """Test `host_resolution_order` with non-default env set """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) os.environ['TESTENV'] = 'test1.ligo.org:80,test2.ligo.org:43' hro = nds.host_resolution_order(None, env='TESTENV') self.assertListEqual( hro, [('test1.ligo.org', 80), ('test2.ligo.org', 43)])
def test_nds2_host_order_env(self): """Test `host_resolution_order` with non-default env set """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) os.environ['TESTENV'] = 'test1.ligo.org:80,test2.ligo.org:43' hro = nds.host_resolution_order(None, env='TESTENV') self.assertListEqual(hro, [('test1.ligo.org', 80), ('test2.ligo.org', 43), ('nds.ligo.caltech.edu', 31200)])
def test_nds2_host_order_epoch(self): """Test `host_resolution_order` with old GPS epoch """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) # test kwarg doesn't change anything hro = nds.host_resolution_order('L1', epoch='now', env=None) self.assertListEqual(hro, [('nds.ligo-la.caltech.edu', 31200), ('nds.ligo.caltech.edu', 31200)]) # test old epoch puts CIT ahead of LLO hro = nds.host_resolution_order('L1', epoch='Jan 1 2015', env=None) self.assertListEqual(hro, [('nds.ligo.caltech.edu', 31200), ('nds.ligo-la.caltech.edu', 31200)]) # test epoch doesn't operate with env os.environ['TESTENV'] = 'test1.ligo.org:80,test2.ligo.org:43' hro = nds.host_resolution_order('L1', epoch='now', env='TESTENV') self.assertListEqual(hro, [('test1.ligo.org', 80), ('test2.ligo.org', 43), ('nds.ligo-la.caltech.edu', 31200), ('nds.ligo.caltech.edu', 31200)])
def test_nds2_host_order_epoch(self): """Test `host_resolution_order` with old GPS epoch """ try: from gwpy.io import nds except ImportError as e: self.skipTest(str(e)) # test kwarg doesn't change anything hro = nds.host_resolution_order('L1', epoch='now') self.assertListEqual( hro, [('nds.ligo-la.caltech.edu', 31200), ('nds.ligo.caltech.edu', 31200)]) # test old epoch puts CIT ahead of LLO hro = nds.host_resolution_order('L1', epoch='Jan 1 2015') self.assertListEqual( hro, [('nds.ligo.caltech.edu', 31200), ('nds.ligo-la.caltech.edu', 31200)]) # test epoch doesn't operate with env os.environ['TESTENV'] = 'test1.ligo.org:80,test2.ligo.org:43' hro = nds.host_resolution_order('L1', epoch='now', env='TESTENV') self.assertListEqual( hro, [('test1.ligo.org', 80), ('test2.ligo.org', 43)])