def has_network_access(*locations): for url in locations: try: urlopen(url, timeout=5) except (URLError, OSError): pass else: return True return False
def test_xml_resource_from_resource(self): xml_file = urlopen('file://{}'.format(add_leading_slash(self.vh_xml_file))) try: resource = XMLResource(xml_file) self.assertEqual(resource.source, xml_file) self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles') self.check_url(resource.url, self.vh_xml_file) self.assertIsNone(resource.document) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<?xml')) finally: xml_file.close()
def test_xml_resource_from_resource(self): xml_file = urlopen('file://{}'.format( add_leading_slash(self.vh_xml_file))) try: resource = XMLResource(xml_file) self.assertEqual(resource.source, xml_file) self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles') self.check_url(resource.url, self.vh_xml_file) self.assertIsNone(resource.document) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<?xml')) finally: xml_file.close()
def test_xml_resource_from_resource(self): xml_file = urlopen(FILE_SCHEME.format(self.vh_xml_file)) try: resource = XMLResource(xml_file) self.assertEqual(resource.source, xml_file) self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles') self.assertEqual(resource.url, FILE_SCHEME.format(self.vh_xml_file)) self.assertIsNone(resource.document) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<?xml')) finally: xml_file.close()
def test_fetch_resource(self): wrong_path = casepath('resources/dummy_file.txt') self.assertRaises(XMLSchemaURLError, fetch_resource, wrong_path) right_path = casepath('resources/dummy file.txt') self.assertTrue(fetch_resource(right_path).endswith('dummy file.txt')) ambiguous_path = casepath('resources/dummy file #2.txt') self.assertTrue( fetch_resource(ambiguous_path).endswith('dummy file %232.txt')) res = urlopen(fetch_resource(ambiguous_path)) try: self.assertEqual(res.read(), b'DUMMY CONTENT') finally: res.close()