Skip to content

Commit

Permalink
Merge pull request #166 from jrleeman/Improve_String_Representations
Browse files Browse the repository at this point in the history
Add string representations for catalogref and dataset
  • Loading branch information
dopplershift authored Nov 2, 2017
2 parents bd0a6e3 + 80b8d0e commit 7c3df4f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
- sphinx-gallery
- doc8
- pytest
- pytest-catchlog
- pytest-cov
- pytest-flake8
- pytest-runner
Expand Down
20 changes: 20 additions & 0 deletions siphon/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def __str__(self):
"""Return a string representation of the collection."""
return str(list(self))

__repr__ = __str__


class TDSCatalog(object):
"""
Expand Down Expand Up @@ -237,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:
Expand Down Expand Up @@ -277,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):
"""
Expand Down Expand Up @@ -312,6 +320,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`.
Expand All @@ -323,6 +335,8 @@ def follow(self):
"""
return TDSCatalog(self.href)

__repr__ = __str__


class Dataset(object):
"""
Expand Down Expand Up @@ -371,6 +385,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.
Expand Down Expand Up @@ -576,6 +594,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.
Expand Down
26 changes: 26 additions & 0 deletions siphon/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ 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."""
Expand Down Expand Up @@ -147,6 +155,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."""
Expand Down Expand Up @@ -266,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'

0 comments on commit 7c3df4f

Please sign in to comment.