def _read_from_plugin(plugin_type, filename, format=None, **kwargs): """ Reads a single file from a plug-in's readFormat function. """ if isinstance(filename, (str, native_str)): if not os.path.exists(filename): msg = "[Errno 2] No such file or directory: '{}'".format( filename) raise FileNotFoundError(msg) eps = ENTRY_POINTS[plugin_type] # get format entry point format_ep = None if not format: # auto detect format - go through all known formats in given sort order for format_ep in eps.values(): # search isFormat for given entry point is_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.%s.%s' % (plugin_type, format_ep.name), 'isFormat') # If it is a file-like object, store the position and restore it # later to avoid that the isFormat() functions move the file # pointer. if hasattr(filename, "tell") and hasattr(filename, "seek"): position = filename.tell() else: position = None # check format is_format = is_format(filename) if position is not None: filename.seek(0, 0) if is_format: break else: raise TypeError('Unknown format for file %s' % filename) else: # format given via argument format = format.upper() try: format_ep = eps[format] except (KeyError, IndexError): msg = "Format \"%s\" is not supported. Supported types: %s" raise TypeError(msg % (format, ', '.join(eps))) # file format should be known by now try: # search readFormat for given entry point read_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.%s.%s' % (plugin_type, format_ep.name), 'readFormat') except ImportError: msg = "Format \"%s\" is not supported. Supported types: %s" raise TypeError(msg % (format_ep.name, ', '.join(eps))) # read list_obj = read_format(filename, **kwargs) return list_obj, format_ep.name
def _read_from_plugin(plugin_type, filename, format=None, **kwargs): """ Reads a single file from a plug-in's readFormat function. """ if isinstance(filename, (str, native_str)): if not os.path.exists(filename): msg = "[Errno 2] No such file or directory: '{}'".format( filename) raise FileNotFoundError(msg) eps = ENTRY_POINTS[plugin_type] # get format entry point format_ep = None if not format: # auto detect format - go through all known formats in given sort order for format_ep in eps.values(): # search isFormat for given entry point is_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.%s.%s' % (plugin_type, format_ep.name), 'isFormat') # If it is a file-like object, store the position and restore it # later to avoid that the isFormat() functions move the file # pointer. if hasattr(filename, "tell") and hasattr(filename, "seek"): position = filename.tell() else: position = None # check format is_format = is_format(filename) if position is not None: filename.seek(0, 0) if is_format: break else: raise TypeError('Unknown format for file %s' % filename) else: # format given via argument format = format.upper() try: format_ep = eps[format] except (KeyError, IndexError): msg = "Format \"%s\" is not supported. Supported types: %s" raise TypeError(msg % (format, ', '.join(eps))) # file format should be known by now try: # search readFormat for given entry point read_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.%s.%s' % (plugin_type, format_ep.name), 'readFormat') except ImportError: msg = "Format \"%s\" is not supported. Supported types: %s" raise TypeError(msg % (format_ep.name, ', '.join(eps))) # read list_obj = read_format(filename, **kwargs) return list_obj, format_ep.name
def test_auto_file_format_during_writing(self): """ The file format is either determined by directly specifying the format or deduced from the filename. The former overwrites the latter. """ # Get format name and name of the write function. formats = [ (key, value.module_name) for key, value in _get_default_eps( 'obspy.plugin.waveform', 'writeFormat').items() # Only test plugins that are actually part of ObsPy. if value.dist.key == "obspy" ] # Test for stream as well as for trace. stream_trace = [read(), read()[0]] # get mseed cache name and mseed function mseed_name = "obspy/obspy.plugin.waveform.MSEED/writeFormat" mseed_func = _ENTRY_POINT_CACHE.get(mseed_name, _write_mseed) for suffix, module_name in formats: # get a list of dist, group, name. entry_point_list = [ "obspy", "obspy.plugin.waveform.%s" % suffix, "writeFormat" ] # load entry point to make sure it is in the cache. buffered_load_entry_point(*entry_point_list) # get the cache name for monkey patching. entry_point_name = '/'.join(entry_point_list) # For stream and trace. for obj in stream_trace: # Various versions of the suffix. for s in [suffix.capitalize(), suffix.lower(), suffix.upper()]: # create a mock function and patch the entry point cache. write_func = _ENTRY_POINT_CACHE[entry_point_name] mocked_func = mock.MagicMock(write_func) mock_dict = {entry_point_name: mocked_func} with mock.patch.dict(_ENTRY_POINT_CACHE, mock_dict): obj.write("temp." + s) # Make sure the fct has actually been called. self.assertEqual(mocked_func.call_count, 1) # Specifying the format name should overwrite this. mocked_mseed_func = mock.MagicMock(mseed_func) mseed_mock_dict = {mseed_name: mocked_mseed_func} with mock.patch.dict(_ENTRY_POINT_CACHE, mseed_mock_dict): obj.write("temp." + s, format="mseed") self.assertEqual(mocked_mseed_func.call_count, 1) self.assertEqual(mocked_func.call_count, 1) # An unknown suffix should raise. with self.assertRaises(ValueError): for obj in stream_trace: obj.write("temp.random_suffix")
def test_auto_file_format_during_writing(self): """ The file format is either determined by directly specifying the format or deduced from the filename. The former overwrites the latter. """ # Get format name and name of the write function. formats = [(key, value.module_name) for key, value in _get_default_eps('obspy.plugin.waveform', 'writeFormat').items() # Only test plugins that are actually part of ObsPy. if value.dist.key == "obspy"] # Test for stream as well as for trace. stream_trace = [read(), read()[0]] # get mseed cache name and mseed function mseed_name = "obspy/obspy.plugin.waveform.MSEED/writeFormat" mseed_func = _ENTRY_POINT_CACHE.get(mseed_name, _write_mseed) for suffix, module_name in formats: # get a list of dist, group, name. entry_point_list = ["obspy", "obspy.plugin.waveform.%s" % suffix, "writeFormat"] # load entry point to make sure it is in the cache. buffered_load_entry_point(*entry_point_list) # get the cache name for monkey patching. entry_point_name = '/'.join(entry_point_list) # For stream and trace. for obj in stream_trace: # Various versions of the suffix. for s in [suffix.capitalize(), suffix.lower(), suffix.upper()]: # create a mock function and patch the entry point cache. write_func = _ENTRY_POINT_CACHE[entry_point_name] mocked_func = mock.MagicMock(write_func) mock_dict = {entry_point_name: mocked_func} with mock.patch.dict(_ENTRY_POINT_CACHE, mock_dict): obj.write("temp." + s) # Make sure the fct has actually been called. self.assertEqual(mocked_func.call_count, 1) # Specifying the format name should overwrite this. mocked_mseed_func = mock.MagicMock(mseed_func) mseed_mock_dict = {mseed_name: mocked_mseed_func} with mock.patch.dict(_ENTRY_POINT_CACHE, mseed_mock_dict): obj.write("temp." + s, format="mseed") self.assertEqual(mocked_mseed_func.call_count, 1) self.assertEqual(mocked_func.call_count, 1) # An unknown suffix should raise. with self.assertRaises(ValueError): for obj in stream_trace: obj.write("temp.random_suffix")
def write(self, filename, format, **kwargs): """ Saves catalog into a file. :type filename: str :param filename: The name of the file to write. :type format: str :param format: The file format to use (e.g. ``"QUAKEML"``). See the `Supported Formats`_ section below for a list of supported formats. :param kwargs: Additional keyword arguments passed to the underlying plugin's writer method. .. rubric:: Example >>> from obspy import read_events >>> catalog = read_events() >>> catalog.write("example.xml", format="QUAKEML") # doctest: +SKIP Writing single events into files with meaningful filenames can be done e.g. using event.id >>> for ev in catalog: # doctest: +SKIP ... filename = str(ev.resource_id) + ".xml" ... ev.write(filename, format="QUAKEML") # doctest: +SKIP .. rubric:: _`Supported Formats` Additional ObsPy modules extend the parameters of the :meth:`~obspy.core.event.Catalog.write` method. The following table summarizes all known formats with write capability currently available for ObsPy. Please refer to the `Linked Function Call`_ of each module for any extra options available. %s """ format = format.upper() try: # get format specific entry point format_ep = EVENT_ENTRY_POINTS_WRITE[format] # search writeFormat method for given entry point write_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.event.%s' % (format_ep.name), 'writeFormat') except (IndexError, ImportError, KeyError): msg = "Writing format \"%s\" is not supported. Supported types: %s" raise ValueError(msg % (format, ', '.join(EVENT_ENTRY_POINTS_WRITE))) return write_format(self, filename, **kwargs)
def write(self, filename, format, **kwargs): """ Saves catalog into a file. :type filename: str :param filename: The name of the file to write. :type format: str :param format: The file format to use (e.g. ``"QUAKEML"``). See the `Supported Formats`_ section below for a list of supported formats. :param kwargs: Additional keyword arguments passed to the underlying plugin's writer method. .. rubric:: Example >>> from obspy import read_events >>> catalog = read_events() >>> catalog.write("example.xml", format="QUAKEML") # doctest: +SKIP Writing single events into files with meaningful filenames can be done e.g. using event.id >>> for ev in catalog: # doctest: +SKIP ... filename = str(ev.resource_id) + ".xml" ... ev.write(filename, format="QUAKEML") # doctest: +SKIP .. rubric:: _`Supported Formats` Additional ObsPy modules extend the parameters of the :meth:`~obspy.core.event.Catalog.write` method. The following table summarizes all known formats with write capability currently available for ObsPy. Please refer to the `Linked Function Call`_ of each module for any extra options available. %s """ format = format.upper() try: # get format specific entry point format_ep = EVENT_ENTRY_POINTS_WRITE[format] # search writeFormat method for given entry point write_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.event.%s' % (format_ep.name), 'writeFormat') except (IndexError, ImportError, KeyError): msg = "Writing format \"%s\" is not supported. Supported types: %s" raise ValueError(msg % (format, ', '.join(EVENT_ENTRY_POINTS_WRITE))) return write_format(self, filename, **kwargs)
def test_raise_on_empty_file(self): """ Test case ensures that empty files do raise warnings. """ with NamedTemporaryFile() as tf: tmpfile = tf.name # create empty file open(tmpfile, 'wb').close() formats_ep = _get_default_eps('obspy.plugin.waveform', 'readFormat') # using format keyword for ep in formats_ep.values(): is_format = buffered_load_entry_point( ep.dist.key, 'obspy.plugin.waveform.' + ep.name, 'isFormat') self.assertFalse(False, is_format(tmpfile))
def test_raise_on_empty_file(self): """ Test case ensures that empty files do raise warnings. """ with NamedTemporaryFile() as tf: tmpfile = tf.name # create empty file open(tmpfile, 'wb').close() formats_ep = _get_default_eps('obspy.plugin.waveform', 'readFormat') # using format keyword for ep in formats_ep.values(): is_format = buffered_load_entry_point( ep.dist.key, 'obspy.plugin.waveform.' + ep.name, 'isFormat') self.assertFalse(False, is_format(tmpfile))
def write(self, path_or_file_object, format, **kwargs): """ Writes the inventory object to a file or file-like object in the specified format. :param path_or_file_object: File name or file-like object to be written to. :type format: str :param format: The file format to use (e.g. ``"STATIONXML"``). See the `Supported Formats`_ section below for a list of supported formats. :param kwargs: Additional keyword arguments passed to the underlying plugin's writer method. .. rubric:: Example >>> from obspy import read_inventory >>> inventory = read_inventory() >>> inventory.write("example.xml", ... format="STATIONXML") # doctest: +SKIP .. rubric:: _`Supported Formats` Additional ObsPy modules extend the parameters of the :meth:`~obspy.core.inventory.inventory.Inventory.write()` method. The following table summarizes all known formats with write capability currently available for ObsPy. Please refer to the `Linked Function Call`_ of each module for any extra options available. %s """ format = format.upper() try: # get format specific entry point format_ep = ENTRY_POINTS['inventory_write'][format] # search writeFormat method for given entry point write_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.inventory.%s' % (format_ep.name), 'writeFormat') except (IndexError, ImportError, KeyError): msg = "Writing format '{}' is not supported. Supported types: {}" msg = msg.format(format, ', '.join(ENTRY_POINTS['inventory_write'])) raise ValueError(msg) return write_format(self, path_or_file_object, **kwargs)
def write(self, path_or_file_object, format, **kwargs): """ Writes the inventory object to a file or file-like object in the specified format. :param path_or_file_object: File name or file-like object to be written to. :type format: str :param format: The file format to use (e.g. ``"STATIONXML"``). See the `Supported Formats`_ section below for a list of supported formats. :param kwargs: Additional keyword arguments passed to the underlying plugin's writer method. .. rubric:: Example >>> from obspy import read_inventory >>> inventory = read_inventory() >>> inventory.write("example.xml", ... format="STATIONXML") # doctest: +SKIP .. rubric:: _`Supported Formats` Additional ObsPy modules extend the parameters of the :meth:`~obspy.core.inventory.inventory.Inventory.write()` method. The following table summarizes all known formats with write capability currently available for ObsPy. Please refer to the `Linked Function Call`_ of each module for any extra options available. %s """ format = format.upper() try: # get format specific entry point format_ep = ENTRY_POINTS['inventory_write'][format] # search writeFormat method for given entry point write_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.inventory.%s' % (format_ep.name), 'writeFormat') except (IndexError, ImportError, KeyError): msg = "Writing format '{}' is not supported. Supported types: {}" msg = msg.format(format, ', '.join(ENTRY_POINTS['inventory_write'])) raise ValueError(msg) return write_format(self, path_or_file_object, **kwargs)
def _get_function_from_entry_point(group, type): """ A "automagic" function searching a given dict of entry points for a valid entry point and returns the function call. Otherwise it will raise a default error message. .. rubric:: Example >>> _get_function_from_entry_point( ... 'detrend', 'simple') # doctest: +ELLIPSIS <function simple at 0x...> >>> _get_function_from_entry_point('detrend', 'XXX') # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: Detrend type "XXX" is not supported. Supported types: ... """ ep_dict = ENTRY_POINTS[group] try: # get entry point if type in ep_dict: entry_point = ep_dict[type] else: # search using lower cases only entry_point = [ v for k, v in ep_dict.items() if k.lower() == type.lower() ][0] except (KeyError, IndexError): # check if any entry points are available at all if not ep_dict: msg = "Your current ObsPy installation does not support " + \ "any %s functions. Please make sure " + \ "SciPy is installed properly." raise ImportError(msg % (group.capitalize())) # ok we have entry points, but specified function is not supported msg = "%s type \"%s\" is not supported. Supported types: %s" raise ValueError(msg % (group.capitalize(), type, ', '.join(ep_dict))) # import function point # any issue during import of entry point should be raised, so the user has # a chance to correct the problem func = buffered_load_entry_point(entry_point.dist.key, 'obspy.plugin.%s' % (group), entry_point.name) return func
def _get_function_from_entry_point(group, type): """ A "automagic" function searching a given dict of entry points for a valid entry point and returns the function call. Otherwise it will raise a default error message. .. rubric:: Example >>> _get_function_from_entry_point( ... 'detrend', 'simple') # doctest: +ELLIPSIS <function simple at 0x...> >>> _get_function_from_entry_point('detrend', 'XXX') # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: Detrend type "XXX" is not supported. Supported types: ... """ ep_dict = ENTRY_POINTS[group] try: # get entry point if type in ep_dict: entry_point = ep_dict[type] else: # search using lower cases only entry_point = [v for k, v in ep_dict.items() if k.lower() == type.lower()][0] except (KeyError, IndexError): # check if any entry points are available at all if not ep_dict: msg = "Your current ObsPy installation does not support " + \ "any %s functions. Please make sure " + \ "SciPy is installed properly." raise ImportError(msg % (group.capitalize())) # ok we have entry points, but specified function is not supported msg = "%s type \"%s\" is not supported. Supported types: %s" raise ValueError(msg % (group.capitalize(), type, ', '.join(ep_dict))) # import function point # any issue during import of entry point should be raised, so the user has # a chance to correct the problem func = buffered_load_entry_point(entry_point.dist.key, 'obspy.plugin.%s' % (group), entry_point.name) return func
def write(self, path_or_file_object, format, **kwargs): """ Writes the inventory object to a file or file-like object in the specified format. :param path_or_file_object: File name or file-like object to be written to. :param format: The format of the written file. """ format = format.upper() try: # get format specific entry point format_ep = ENTRY_POINTS['inventory_write'][format] # search writeFormat method for given entry point write_format = buffered_load_entry_point( format_ep.dist.key, 'obspy.plugin.inventory.%s' % (format_ep.name), 'writeFormat') except (IndexError, ImportError, KeyError): msg = "Writing format \"%s\" is not supported. Supported types: %s" raise TypeError( msg % (format, ', '.join(ENTRY_POINTS['inventory_write']))) return write_format(self, path_or_file_object, **kwargs)
def test_is_format(self): """ Tests all isFormat methods against all data test files from the other modules for false positives. """ known_false = [ os.path.join('seisan', 'tests', 'data', '2011-09-06-1311-36S.A1032_001BH_Z.mseed'), os.path.join('seisan', 'tests', 'data', 'D1360930.203.mseed'), os.path.join('seisan', 'tests', 'data', '2005-07-23-1452-04S.CER___030.mseed'), os.path.join('seisan', 'tests', 'data', '9701-30-1048-54S.MVO_21_1.ascii'), os.path.join('core', 'tests', 'data', 'IU_ULN_00_LH1_2015-07-18T02.mseed'), # That file is not in obspy.io.mseed as it is used to test an # issue with the uncompress_data() decorator. os.path.join('core', 'tests', 'data', 'tarfile_impostor.mseed'), # these files are not in /mseed because they hold the data to # validate the read output of the reftek file os.path.join('io', 'reftek', 'tests', 'data', '2015282_225051_0ae4c_1_1.msd'), os.path.join('io', 'reftek', 'tests', 'data', '2015282_225051_0ae4c_1_2.msd'), os.path.join('io', 'reftek', 'tests', 'data', '2015282_225051_0ae4c_1_3.msd'), os.path.join('core', 'tests', 'data', 'ffbx_unrotated_gaps.mseed'), os.path.join('core', 'tests', 'data', 'ffbx_rotated.slist'), os.path.join('io', 'ascii', 'tests', 'data', 'miniseed_record.mseed'), ] formats_ep = _get_default_eps('obspy.plugin.waveform', 'isFormat') formats = list(formats_ep.values()) # Get all the test directories. paths = {} all_paths = [] for f in formats: path = os.path.join(f.dist.location, *f.module_name.split('.')[:-1]) path = os.path.join(path, 'tests', 'data') all_paths.append(path) if os.path.exists(path): paths[f.name] = path msg = 'Test data directories do not exist:\n ' self.assertTrue(len(paths) > 0, msg + '\n '.join(all_paths)) # Collect all false positives. false_positives = [] # Big loop over every format. for format in formats: # search isFormat for given entry point is_format = buffered_load_entry_point( format.dist.key, 'obspy.plugin.waveform.' + format.name, 'isFormat') for f, path in paths.items(): if format.name in paths and paths[f] == paths[format.name]: continue # Collect all files found. filelist = [] # Walk every path. for directory, _, files in os.walk(path): filelist.extend([os.path.join(directory, _i) for _i in files]) for file in filelist: if any([n in file for n in known_false]): continue if is_format(file) is True: # pragma: no cover false_positives.append((format.name, file)) # Use try except to produce a meaningful error message. try: self.assertEqual(len(false_positives), 0) except Exception: # pragma: no cover msg = 'False positives for isFormat:\n' msg += '\n'.join(['\tFormat %s: %s' % (_i[0], _i[1]) for _i in false_positives]) raise Exception(msg)
def make_format_plugin_table(group="waveform", method="read", numspaces=4, unindent_first_line=True): """ Returns a markdown formatted table with read waveform plugins to insert in docstrings. >>> table = make_format_plugin_table("event", "write", 4, True) >>> print(table) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS ======... ===========... ========================================... Format Used Module _`Linked Function Call` ======... ===========... ========================================... CMTSOLUTION :mod:`...io.cmtsolution` :func:`..._write_cmtsolution` CNV :mod:`...io.cnv` :func:`obspy.io.cnv.core._write_cnv` JSON :mod:`...io.json` :func:`obspy.io.json.core._write_json` KML :mod:`obspy.io.kml` :func:`obspy.io.kml.core._write_kml` NLLOC_OBS :mod:`...io.nlloc` :func:`obspy.io.nlloc.core.write_nlloc_obs` NORDIC :mod:`obspy.io.nordic` :func:`obspy.io.nordic.core.write_select` QUAKEML :mod:`...io.quakeml` :func:`obspy.io.quakeml.core._write_quakeml` SC3ML :mod:`...io.seiscomp` :func:`obspy.io.seiscomp.event._write_sc3ml` SCARDEC :mod:`obspy.io.scardec` :func:`obspy.io.scardec.core._write_scardec` SHAPEFILE :mod:`obspy.io.shapefile` :func:`obspy.io.shapefile.core._write_shapefile` ZMAP :mod:`...io.zmap` :func:`obspy.io.zmap.core._write_zmap` ======... ===========... ========================================... :type group: str :param group: Plugin group to search (e.g. "waveform" or "event"). :type method: str :param method: Either 'read' or 'write' to select plugins based on either read or write capability. :type numspaces: int :param numspaces: Number of spaces prepended to each line (for indentation in docstrings). :type unindent_first_line: bool :param unindent_first_line: Determines if first line should start with prepended spaces or not. """ method = method.lower() if method not in ("read", "write"): raise ValueError("no valid type: %s" % method) method = "%sFormat" % method eps = _get_ordered_entry_points("obspy.plugin.%s" % group, method, WAVEFORM_PREFERRED_ORDER) mod_list = [] for name, ep in eps.items(): module_short = ":mod:`%s`" % ".".join(ep.module_name.split(".")[:3]) ep_list = [ep.dist.key, "obspy.plugin.%s.%s" % (group, name), method] func = buffered_load_entry_point(*ep_list) func_str = ':func:`%s`' % ".".join((ep.module_name, func.__name__)) mod_list.append((name, module_short, func_str)) mod_list = sorted(mod_list) headers = ["Format", "Used Module", "_`Linked Function Call`"] maxlens = [max([len(x[0]) for x in mod_list] + [len(headers[0])]), max([len(x[1]) for x in mod_list] + [len(headers[1])]), max([len(x[2]) for x in mod_list] + [len(headers[2])])] info_str = [" ".join(["=" * x for x in maxlens])] info_str.append( " ".join([headers[i].ljust(maxlens[i]) for i in range(3)])) info_str.append(info_str[0]) for mod_infos in mod_list: info_str.append( " ".join([mod_infos[i].ljust(maxlens[i]) for i in range(3)])) info_str.append(info_str[0]) ret = " " * numspaces + ("\n" + " " * numspaces).join(info_str) if unindent_first_line: ret = ret[numspaces:] return ret
def make_format_plugin_table(group="waveform", method="read", numspaces=4, unindent_first_line=True): """ Returns a markdown formatted table with read waveform plugins to insert in docstrings. >>> table = make_format_plugin_table("event", "write", 4, True) >>> print(table) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS ======... ===========... ========================================... Format Used Module _`Linked Function Call` ======... ===========... ========================================... CMTSOLUTION :mod:`...io.cmtsolution` :func:`..._write_cmtsolution` CNV :mod:`...io.cnv` :func:`obspy.io.cnv.core._write_cnv` JSON :mod:`...io.json` :func:`obspy.io.json.core._write_json` KML :mod:`obspy.io.kml` :func:`obspy.io.kml.core._write_kml` NLLOC_OBS :mod:`...io.nlloc` :func:`obspy.io.nlloc.core.write_nlloc_obs` NORDIC :mod:`obspy.io.nordic` :func:`obspy.io.nordic.core.write_select` QUAKEML :mod:`...io.quakeml` :func:`obspy.io.quakeml.core._write_quakeml` SC3ML :mod:`...io.seiscomp` :func:`obspy.io.seiscomp.event._write_sc3ml` SCARDEC :mod:`obspy.io.scardec` :func:`obspy.io.scardec.core._write_scardec` SHAPEFILE :mod:`obspy.io.shapefile` :func:`obspy.io.shapefile.core._write_shapefile` ZMAP :mod:`...io.zmap` :func:`obspy.io.zmap.core._write_zmap` ======... ===========... ========================================... :type group: str :param group: Plugin group to search (e.g. "waveform" or "event"). :type method: str :param method: Either 'read' or 'write' to select plugins based on either read or write capability. :type numspaces: int :param numspaces: Number of spaces prepended to each line (for indentation in docstrings). :type unindent_first_line: bool :param unindent_first_line: Determines if first line should start with prepended spaces or not. """ method = method.lower() if method not in ("read", "write"): raise ValueError("no valid type: %s" % method) method = "%sFormat" % method eps = _get_ordered_entry_points("obspy.plugin.%s" % group, method, WAVEFORM_PREFERRED_ORDER) mod_list = [] for name, ep in eps.items(): module_short = ":mod:`%s`" % ".".join(ep.module_name.split(".")[:3]) ep_list = [ep.dist.key, "obspy.plugin.%s.%s" % (group, name), method] func = buffered_load_entry_point(*ep_list) func_str = ':func:`%s`' % ".".join((ep.module_name, func.__name__)) mod_list.append((name, module_short, func_str)) mod_list = sorted(mod_list) headers = ["Format", "Used Module", "_`Linked Function Call`"] maxlens = [ max([len(x[0]) for x in mod_list] + [len(headers[0])]), max([len(x[1]) for x in mod_list] + [len(headers[1])]), max([len(x[2]) for x in mod_list] + [len(headers[2])]) ] info_str = [" ".join(["=" * x for x in maxlens])] info_str.append(" ".join([headers[i].ljust(maxlens[i]) for i in range(3)])) info_str.append(info_str[0]) for mod_infos in mod_list: info_str.append(" ".join( [mod_infos[i].ljust(maxlens[i]) for i in range(3)])) info_str.append(info_str[0]) ret = " " * numspaces + ("\n" + " " * numspaces).join(info_str) if unindent_first_line: ret = ret[numspaces:] return ret
def test_is_format(self): """ Tests all isFormat methods against all data test files from the other modules for false positives. """ known_false = [ os.path.join('seisan', 'tests', 'data', '2011-09-06-1311-36S.A1032_001BH_Z.mseed'), os.path.join('seisan', 'tests', 'data', 'D1360930.203.mseed'), os.path.join('seisan', 'tests', 'data', '2005-07-23-1452-04S.CER___030.mseed'), os.path.join('seisan', 'tests', 'data', '9701-30-1048-54S.MVO_21_1.ascii'), os.path.join('core', 'tests', 'data', 'IU_ULN_00_LH1_2015-07-18T02.mseed'), # That file is not in obspy.io.mseed as it is used to test an # issue with the uncompress_data() decorator. os.path.join('core', 'tests', 'data', 'tarfile_impostor.mseed'), # these files are not in /mseed because they hold the data to # validate the read output of the reftek file os.path.join('io', 'reftek', 'tests', 'data', '2015282_225051_0ae4c_1_1.msd'), os.path.join('io', 'reftek', 'tests', 'data', '2015282_225051_0ae4c_1_2.msd'), os.path.join('io', 'reftek', 'tests', 'data', '2015282_225051_0ae4c_1_3.msd'), os.path.join('core', 'tests', 'data', 'ffbx_unrotated_gaps.mseed'), os.path.join('core', 'tests', 'data', 'ffbx_rotated.slist'), ] formats_ep = _get_default_eps('obspy.plugin.waveform', 'isFormat') formats = list(formats_ep.values()) # Get all the test directories. paths = {} all_paths = [] for f in formats: path = os.path.join(f.dist.location, *f.module_name.split('.')[:-1]) path = os.path.join(path, 'tests', 'data') all_paths.append(path) if os.path.exists(path): paths[f.name] = path msg = 'Test data directories do not exist:\n ' self.assertTrue(len(paths) > 0, msg + '\n '.join(all_paths)) # Collect all false positives. false_positives = [] # Big loop over every format. for format in formats: # search isFormat for given entry point is_format = buffered_load_entry_point( format.dist.key, 'obspy.plugin.waveform.' + format.name, 'isFormat') for f, path in paths.items(): if format.name in paths and paths[f] == paths[format.name]: continue # Collect all files found. filelist = [] # Walk every path. for directory, _, files in os.walk(path): filelist.extend( [os.path.join(directory, _i) for _i in files]) for file in filelist: if any([n in file for n in known_false]): continue if is_format(file) is True: # pragma: no cover false_positives.append((format.name, file)) # Use try except to produce a meaningful error message. try: self.assertEqual(len(false_positives), 0) except Exception: # pragma: no cover msg = 'False positives for isFormat:\n' msg += '\n'.join( ['\tFormat %s: %s' % (_i[0], _i[1]) for _i in false_positives]) raise Exception(msg)