def test_send_to_pager_stringio(clobber): """Check we can create a StringIO object We ignore the clobber parameter in this approach. """ out = StringIO() testdata = "test\ntest" utils.send_to_pager(testdata, out, clobber=clobber) assert out.getvalue() == testdata + "\n"
def test_send_to_pager_file(tmpdir): """Check we can create a file""" # would like to use tmp_path but this needs a recent pytest fileexists = tmpdir.join('tmp.txt') fileexists.write_text('x', 'ascii') assert fileexists.read() == 'x' testdata = "test\ntest" utils.send_to_pager(testdata, str(fileexists), clobber=True) assert fileexists.read_text('ascii') == testdata + "\n"
def test_send_to_pager_file_clobber(tmpdir): """Ensure clobber=False doesn't overwrite things""" # would like to use tmp_path but this needs a recent pytest fileexists = tmpdir.join('tmp.txt') fileexists.write_text('x', 'ascii') with pytest.raises(IOErr) as exc: utils.send_to_pager("test", str(fileexists)) assert str(exc.value).startswith("file '") assert str(exc.value).endswith("/tmp.txt' exists and clobber is not set")
def show_stack(self, outfile=None, clobber=False): """Display basic information about the contents of the data stack. A brief summary of each data set in the stack is printed to the standard output channel. The information displayed depends on the type of each data set. Parameters ---------- outfile : str, optional If not given the results are displayed to the screen, otherwise it is taken to be the name of the file to write the results to. clobber : bool, optional If `outfile` is not ``None``, then this flag controls whether an existing file can be overwritten (``True``) or if it raises an exception (``False``, the default setting). """ header, data = self._summary_for_display() txt = '|'.join(header) + '\n' for row in data: txt += '|'.join(row) + '\n' send_to_pager(txt, outfile, clobber)
def citation(version='current', filename=None, clobber=False): """Return citatation information for Sherpa. The citation information is taken from Zenodo [1]_, using the Sherpa "latest release" identifier [2]_, and so requires an internet connection. The message is displayed on screen, using pagination, or can be written to a file. Parameters ---------- version : str, optional The version to retrieve the citation for. The supported values are limited to 'current', to return the citation for the installed version of Sherpa, 'latest' which will return the latest release, and the current set of releases available on Zenodo (this goes back to '4.8.0'). filename : str or StringIO or None, optional If not None, write the output to the given file or filelike object. clobber : bool, optional If filename is a string, then - when clobber is set - refuse to overwrite the file if it already exists. Notes ----- If there is no internet connection, or there was a problem in downloading the data, or the Zenodo API has started to return different informatioon than expected, then the code will return information on why the call failed and other citation options. Zenodo only lets you perform a limited number of calls in a set time span, so if you call this routine too many times then it may start to fail. If a specific version is given then a hard-coded list of versions is checked, and if it matches then this information is used, rather than requiring a call to Zenodo. References ---------- .. [1] https://zenodo.org/ .. [2] https://doi.org/10.5281/zenodo.593753 Examples -------- Display the citation information for the current release on Zenodo. The information is paged to the display: >>> import sherpa >>> sherpa.citation() Write out the citation information for Sherpa 4.12.1 to the file ``cite.txt``: >>> sherpa.citation('4.12.1', outfile='cite.txt') Display the information for the latest release: >>> sherpa.citation('latest') """ from sherpa.utils import send_to_pager cite = _get_citation(version=version) send_to_pager(cite, filename=filename, clobber=clobber)