From f49301bfab5af44c65e883cb65e7f93c224b8bf9 Mon Sep 17 00:00:00 2001 From: "J.R. Leeman" Date: Thu, 21 Sep 2017 08:57:40 -0600 Subject: [PATCH 1/4] Add string representations for catalogref and dataset. --- environment.yml | 1 + siphon/catalog.py | 14 ++++++++++++++ siphon/tests/test_catalog.py | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/environment.yml b/environment.yml index b6005bf34..7265b1d76 100755 --- a/environment.yml +++ b/environment.yml @@ -13,6 +13,7 @@ dependencies: - sphinx-gallery - doc8 - pytest + - pytest-catchlog - pytest-cov - pytest-flake8 - pytest-runner diff --git a/siphon/catalog.py b/siphon/catalog.py index 9fa9f37ac..0d96be9ca 100644 --- a/siphon/catalog.py +++ b/siphon/catalog.py @@ -133,6 +133,8 @@ def __str__(self): """Return a string representation of the collection.""" return str(list(self)) + __repr__ = __str__ + class TDSCatalog(object): """ @@ -312,6 +314,10 @@ def __init__(self, base_url, element_node): href = element_node.attrib['{http://www.w3.org/1999/xlink}href'] self.href = urljoin(base_url, href) + def __str__(self): + """Return a string representation of the catalog reference.""" + return str(self.title) + def follow(self): """Follow the catalog reference and return a new :class:`TDSCatalog`. @@ -323,6 +329,8 @@ def follow(self): """ return TDSCatalog(self.href) + __repr__ = __str__ + class Dataset(object): """ @@ -371,6 +379,10 @@ def __init__(self, element_node, catalog_url=''): log.warning('Must pass along the catalog URL to resolve ' 'the latest.xml dataset!') + def __str__(self): + """Return a string representation of the dataset.""" + return str(self.name) + def resolve_url(self, catalog_url): """Resolve the url of the dataset when reading latest.xml. @@ -576,6 +588,8 @@ def access_with_service(self, service): except KeyError: raise ValueError(service + ' is not available for this dataset') + __repr__ = __str__ + class SimpleService(object): """Hold information about an access service enabled on a dataset. diff --git a/siphon/tests/test_catalog.py b/siphon/tests/test_catalog.py index 9b5497a25..14241cffb 100644 --- a/siphon/tests/test_catalog.py +++ b/siphon/tests/test_catalog.py @@ -147,6 +147,16 @@ def test_datasets_str(): "'Latest Collection for NAM CONUS 20km']") +@recorder.use_cassette('top_level_20km_rap_catalog') +def test_datasets_sliced_str(): + """Test that datasets are printed as expected when sliced.""" + url = ('http://thredds.ucar.edu/thredds/catalog/grib/NCEP/NAM/' + 'CONUS_20km/noaaport/catalog.xml') + cat = TDSCatalog(url) + assert str(cat.datasets[-2:]) == ('[Best NAM CONUS 20km Time Series, ' + 'Latest Collection for NAM CONUS 20km]') + + @recorder.use_cassette('top_level_20km_rap_catalog') def test_datasets_nearest_time(): """Test getting dataset by time using filenames.""" From 7b85be34df8248ed207ff43f54e31965910eea7a Mon Sep 17 00:00:00 2001 From: "J.R. Leeman" Date: Thu, 2 Nov 2017 13:46:47 -0600 Subject: [PATCH 2/4] Add string representation for the catalog object. --- siphon/catalog.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/siphon/catalog.py b/siphon/catalog.py index 0d96be9ca..2ef9eb4f8 100644 --- a/siphon/catalog.py +++ b/siphon/catalog.py @@ -239,6 +239,10 @@ def __init__(self, catalog_url): self._process_datasets() + def __str__(self): + """Return a string representation of the catalog name.""" + return str(self.catalog_name) + def _process_dataset(self, element): catalog_url = '' if 'urlPath' in element.attrib: @@ -279,6 +283,8 @@ def latest(self): return TDSCatalog(latest_cat).datasets[0] raise AttributeError('"latest" not available for this catalog') + __repr__ = __str__ + class CatalogRef(object): """ From a348ec8801a0c8771ef2cdf82dddac44c99ae255 Mon Sep 17 00:00:00 2001 From: "J.R. Leeman" Date: Thu, 2 Nov 2017 13:46:58 -0600 Subject: [PATCH 3/4] Add test of catalog string representation. --- siphon/tests/test_catalog.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/siphon/tests/test_catalog.py b/siphon/tests/test_catalog.py index 14241cffb..5687dfac0 100644 --- a/siphon/tests/test_catalog.py +++ b/siphon/tests/test_catalog.py @@ -26,6 +26,13 @@ def test_basic(): assert 'Forecast Model Data' in cat.catalog_refs +@recorder.use_cassette('thredds-test-toplevel-catalog') +def test_catalog_representation(): + """Test string representation of the catalog object.""" + url = 'http://thredds-test.unidata.ucar.edu/thredds/catalog.xml' + cat = TDSCatalog(url) + assert str(cat) == 'Unidata THREDDS Data Server' + @recorder.use_cassette('thredds-test-latest-gfs-0p5') def test_access(): """Test catalog parsing of access methods.""" From 80b8d0e5cf563b8d32066a1cbd31b4e3b796f918 Mon Sep 17 00:00:00 2001 From: "J.R. Leeman" Date: Thu, 2 Nov 2017 13:56:12 -0600 Subject: [PATCH 4/4] Add test of catalog reference string representation. --- siphon/tests/test_catalog.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/siphon/tests/test_catalog.py b/siphon/tests/test_catalog.py index 5687dfac0..0d850299f 100644 --- a/siphon/tests/test_catalog.py +++ b/siphon/tests/test_catalog.py @@ -33,6 +33,7 @@ def test_catalog_representation(): cat = TDSCatalog(url) assert str(cat) == 'Unidata THREDDS Data Server' + @recorder.use_cassette('thredds-test-latest-gfs-0p5') def test_access(): """Test catalog parsing of access methods.""" @@ -283,3 +284,11 @@ def test_tds50_catalogref_follow(): """Test following a catalog ref url on TDS 5.""" cat = TDSCatalog('http://thredds-test.unidata.ucar.edu/thredds/catalog.xml') assert len(cat.catalog_refs[0].follow().catalog_refs) == 59 + + +@recorder.use_cassette('top_level_cat') +def test_catalog_ref_str(): + """Test that catalog references are properly represented as strings.""" + url = 'http://thredds.ucar.edu/thredds/catalog.xml' + cat = TDSCatalog(url) + assert str(cat.catalog_refs[0]) == 'Forecast Model Data'