def test_reading_wadls_without_type(self): """ Tests the reading of WADL files that have no type. """ filename = os.path.join(self.data_path, "station_no_types.wadl") with open(filename, "rt") as fh: wadl_string = fh.read() parser = WADLParser(wadl_string) params = parser.parameters # Assert that types have been assigned. self.assertEqual(params["starttime"]["type"], UTCDateTime) self.assertEqual(params["endtime"]["type"], UTCDateTime) self.assertEqual(params["startbefore"]["type"], UTCDateTime) self.assertEqual(params["startafter"]["type"], UTCDateTime) self.assertEqual(params["endbefore"]["type"], UTCDateTime) self.assertEqual(params["endafter"]["type"], UTCDateTime) self.assertEqual(params["network"]["type"], str) self.assertEqual(params["station"]["type"], str) self.assertEqual(params["location"]["type"], str) self.assertEqual(params["channel"]["type"], str) self.assertEqual(params["minlatitude"]["type"], float) self.assertEqual(params["maxlatitude"]["type"], float) self.assertEqual(params["latitude"]["type"], float) self.assertEqual(params["minlongitude"]["type"], float) self.assertEqual(params["maxlongitude"]["type"], float) self.assertEqual(params["longitude"]["type"], float) self.assertEqual(params["minradius"]["type"], float) self.assertEqual(params["maxradius"]["type"], float) self.assertEqual(params["level"]["type"], str) self.assertEqual(params["includerestricted"]["type"], bool) self.assertEqual(params["includeavailability"]["type"], bool) self.assertEqual(params["updatedafter"]["type"], UTCDateTime) # Now read a dataselect file with no types. filename = os.path.join(self.data_path, "dataselect_no_types.wadl") with open(filename, "rt") as fh: wadl_string = fh.read() parser = WADLParser(wadl_string) params = parser.parameters # Assert that types have been assigned. self.assertEqual(params["starttime"]["type"], UTCDateTime) self.assertEqual(params["endtime"]["type"], UTCDateTime) self.assertEqual(params["network"]["type"], str) self.assertEqual(params["station"]["type"], str) self.assertEqual(params["location"]["type"], str) self.assertEqual(params["channel"]["type"], str) self.assertEqual(params["quality"]["type"], str) self.assertEqual(params["minimumlength"]["type"], float) self.assertEqual(params["longestonly"]["type"], bool)
def test_parsing_event_wadls_with_missing_attributes(self): """ Some WADL file miss required attributes. In this case a warning will be raised. """ # This event WADL misses the includeallorigins and the updatedafter # parameters. filename = os.path.join(self.data_path, "event_missing_attributes.wadl") with open(filename, "rb") as fh: wadl_string = fh.read() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") parser = WADLParser(wadl_string) # Assert that the warning raised is correct. self.assertEqual(len(w), 1) msg = str(w[0].message) self.assertTrue("includeallorigins" in msg) self.assertTrue("updatedafter" in msg) # Assert that some other parameters are still existent. params = parser.parameters self.assertTrue("starttime" in params) self.assertTrue("endtime" in params) self.assertTrue("minlatitude" in params) self.assertTrue("maxlatitude" in params) self.assertTrue("minlongitude" in params) self.assertTrue("maxlongitude" in params) self.assertTrue("minmagnitude" in params) self.assertTrue("maxmagnitude" in params) self.assertTrue("magnitudetype" in params) self.assertTrue("catalog" in params)
def test_parsing_dataselect_wadls_with_missing_attributes(self): """ Some WADL file miss required attributes. In this case a warning will be raised. """ # This dataselect WADL misses the quality, minimumlength, and # longestonly parameters. filename = os.path.join(self.data_path, "dataselect_missing_attributes.wadl") with open(filename, "rb") as fh: wadl_string = fh.read() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") parser = WADLParser(wadl_string) # Assert that the warning raised is correct. self.assertEqual(len(w), 1) msg = str(w[0].message) self.assertTrue("quality" in msg) self.assertTrue("minimumlength" in msg) self.assertTrue("longestonly" in msg) # Assert that some other parameters are still existent. params = parser.parameters self.assertTrue("starttime" in params) self.assertTrue("endtime" in params) self.assertTrue("network" in params) self.assertTrue("station" in params) self.assertTrue("location" in params) self.assertTrue("channel" in params)
def _parse_wadl_file(self, filename): """ Parses wadl, returns WADLParser and any catched warnings. """ filename = os.path.join(self.data_path, filename) with open(filename, "rb") as fh: wadl_string = fh.read() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") parser = WADLParser(wadl_string) return parser, w
def test_station_wadl_parsing(self): """ Tests the parsing of a station wadl. """ filename = os.path.join(self.data_path, "station.wadl") with open(filename, "rt") as fh: wadl_string = fh.read() parser = WADLParser(wadl_string) params = parser.parameters self.assertTrue("starttime" in params) self.assertTrue("endtime" in params) self.assertTrue("startbefore" in params) self.assertTrue("startafter" in params) self.assertTrue("endbefore" in params) self.assertTrue("endafter" in params) self.assertTrue("network" in params) self.assertTrue("station" in params) self.assertTrue("location" in params) self.assertTrue("channel" in params) self.assertTrue("minlatitude" in params) self.assertTrue("maxlatitude" in params) self.assertTrue("latitude" in params) self.assertTrue("minlongitude" in params) self.assertTrue("maxlongitude" in params) self.assertTrue("longitude" in params) self.assertTrue("minradius" in params) self.assertTrue("maxradius" in params) self.assertTrue("level" in params) self.assertTrue("includerestricted" in params) self.assertTrue("includeavailability" in params) self.assertTrue("updatedafter" in params) self.assertTrue("matchtimeseries" in params) # The nodata attribute should not be parsed. self.assertFalse("nodata" in params) # Same for the format attribute. self.assertFalse("format" in params) self.assertEqual( params["endbefore"]["doc_title"], "limit to stations ending before the specified time") self.assertEqual( params["endbefore"]["doc"], "Examples: endbefore=2012-11-29 or 2012-11-29T00:00:00 or " "2012-11-29T00:00:00.000")
def test_usgs_event_wadl_parsing(self): """ Tests the parsing of an event wadl. """ filename = os.path.join(self.data_path, "usgs_event.wadl") with open(filename, "rb") as fh: wadl_string = fh.read() parser = WADLParser(wadl_string) params = parser.parameters # The WADL contains some short forms. In the parameters dictionary # these should be converted to the long forms. self.assertTrue("starttime" in params) self.assertTrue("endtime" in params) self.assertTrue("minlatitude" in params) self.assertTrue("maxlatitude" in params) self.assertTrue("minlongitude" in params) self.assertTrue("maxlongitude" in params) self.assertTrue("minmagnitude" in params) self.assertTrue("maxmagnitude" in params) self.assertTrue("magnitudetype" in params) self.assertTrue("catalog" in params) self.assertTrue("contributor" in params) self.assertTrue("maxdepth" in params) self.assertTrue("mindepth" in params) self.assertTrue("latitude" in params) self.assertTrue("longitude" in params) self.assertTrue("maxradius" in params) self.assertTrue("minradius" in params) self.assertTrue("orderby" in params) self.assertTrue("updatedafter" in params) self.assertTrue("eventid" in params) self.assertTrue("includearrivals" in params) self.assertTrue("includeallmagnitudes" in params) self.assertTrue("includeallorigins" in params) self.assertTrue("limit" in params) self.assertTrue("offset" in params)
def test_dataselect_wadl_parsing(self): """ Tests the parsing of a dataselect wadl. """ filename = os.path.join(self.data_path, "dataselect.wadl") with open(filename, "rb") as fh: wadl_string = fh.read() parser = WADLParser(wadl_string) params = parser.parameters self.assertTrue("starttime" in params) self.assertTrue("endtime" in params) self.assertTrue("network" in params) self.assertTrue("station" in params) self.assertTrue("location" in params) self.assertTrue("channel" in params) self.assertTrue("quality" in params) self.assertTrue("minimumlength" in params) self.assertTrue("quality" in params) self.assertTrue("longestonly" in params) self.assertEqual(params["starttime"]["type"], UTCDateTime) self.assertEqual(params["starttime"]["required"], True) self.assertEqual(params["endtime"]["type"], UTCDateTime) self.assertEqual(params["endtime"]["required"], True) self.assertEqual(params["network"]["type"], str) self.assertEqual(params["station"]["type"], str) self.assertEqual(params["location"]["type"], str) self.assertEqual(params["channel"]["type"], str) self.assertEqual(sorted(params["quality"]["options"]), sorted(["D", "R", "Q", "M", "B"])) # Check that the default values did get read correctly. self.assertEqual(params["quality"]["default_value"], "B") self.assertEqual(params["minimumlength"]["default_value"], 0.0) self.assertEqual(params["longestonly"]["default_value"], False)
def test_event_wadl_parsing(self): """ Tests the parsing of an event wadl. """ filename = os.path.join(self.data_path, "event.wadl") with open(filename, "rt") as fh: wadl_string = fh.read() parser = WADLParser(wadl_string) params = parser.parameters # The WADL contains some short forms. In the parameters dictionary # these should be converted to the long forms. self.assertTrue("starttime" in params) self.assertTrue("endtime" in params) self.assertTrue("minlatitude" in params) self.assertTrue("maxlatitude" in params) self.assertTrue("minlongitude" in params) self.assertTrue("maxlongitude" in params) self.assertTrue("minmagnitude" in params) self.assertTrue("maxmagnitude" in params) # XXX hack for IRIS wadl that contains the abbreviated "magtype" # XXX instead of the normal "magnitudetype" currently. Emailed them # XXX about it, expecting that to be changed since no other # XXX abbreviations are used in the WADL otherwise. # XXX When it is changed at IRIS, we should update data/event.wadl # XXX and remove this. key_magnitudetype = "magnitudetype" # XXX see above, remove following line again when event.wadl is fixed # XXX at IRIS and data/event.wadl is updated key_magnitudetype = "magtype" self.assertTrue(key_magnitudetype in params) self.assertTrue("catalog" in params) self.assertTrue("contributor" in params) self.assertTrue("maxdepth" in params) self.assertTrue("mindepth" in params) self.assertTrue("latitude" in params) self.assertTrue("longitude" in params) self.assertTrue("maxradius" in params) self.assertTrue("minradius" in params) self.assertTrue("orderby" in params) self.assertTrue("updatedafter" in params) self.assertTrue("eventid" in params) self.assertTrue("originid" in params) self.assertTrue("includearrivals" in params) self.assertTrue("includeallmagnitudes" in params) self.assertTrue("includeallorigins" in params) self.assertTrue("limit" in params) self.assertTrue("offset" in params) # The nodata attribute should not be parsed. self.assertFalse("nodata" in params) # Same for the format attribute. self.assertFalse("format" in params) key_magnitudetype = "magnitudetype" # XXX see above, remove following line again when event.wadl is fixed # XXX at IRIS and data/event.wadl is updated key_magnitudetype = "magtype" self.assertEqual( params[key_magnitudetype]["doc_title"], "type of Magnitude used to test minimum and maximum limits " "(case insensitive)") self.assertEqual(params[key_magnitudetype]["doc"], "Examples: Ml,Ms,mb,Mw\"")
def _discover_services(self): """ Automatically discovers available services. They are discovered by downloading the corresponding WADL files. If a WADL does not exist, the services are assumed to be non-existent. """ dataselect_url = self._build_url("dataselect", "application.wadl") station_url = self._build_url("station", "application.wadl") event_url = self._build_url("event", "application.wadl") catalog_url = self._build_url("event", "catalogs") contributor_url = self._build_url("event", "contributors") urls = (dataselect_url, station_url, event_url, catalog_url, contributor_url) # Request all in parallel. wadl_queue = Queue.Queue() headers = self.request_headers debug = self.debug def get_download_thread(url): class ThreadURL(threading.Thread): def run(self): code, data = download_url(url, headers=headers, debug=debug) if code == 200: wadl_queue.put((url, data)) else: wadl_queue.put((url, None)) return ThreadURL() threads = map(get_download_thread, urls) for thread in threads: thread.start() for thread in threads: thread.join(15) self.services = {} for _ in range(wadl_queue.qsize()): item = wadl_queue.get() url, wadl = item if wadl is None: continue if "dataselect" in url: self.services["dataselect"] = WADLParser(wadl).parameters if self.debug is True: print "Discovered dataselect service" elif "event" in url and "application.wadl" in url: self.services["event"] = WADLParser(wadl).parameters if self.debug is True: print "Discovered event service" elif "station" in url: self.services["station"] = WADLParser(wadl).parameters if self.debug is True: print "Discovered station service" elif "event" in url and "catalogs" in url: try: self.services["available_event_catalogs"] = \ parse_simple_xml(wadl)["catalogs"] except ValueError: msg = "Could not parse the catalogs at '%s'." warnings.warn(msg) elif "event" in url and "contributors" in url: try: self.services["available_event_contributors"] = \ parse_simple_xml(wadl)["contributors"] except ValueError: msg = "Could not parse the contributors at '%s'." warnings.warn(msg) if not self.services: msg = ("No FDSN services could be discoverd at '%s'. This could " "be due to a temporary service outage or an invalid FDSN " "service address." % self.base_url) raise FDSNException(msg)