Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve relative urls in catalog references #35

Merged
merged 4 commits into from
Jul 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions siphon/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
from .metadata import TDSCatalogMetadata
from .http_util import create_http_session, urlopen

try:
from urlparse import urljoin
except ImportError:
# Python 3
from urllib.parse import urljoin

log = logging.getLogger("siphon.catalog")
log.setLevel(logging.WARNING)

Expand Down Expand Up @@ -104,7 +110,7 @@ def _process_dataset(self, element):
self.datasets[ds.name] = ds

def _process_catalog_ref(self, element):
catalog_ref = CatalogRef(element)
catalog_ref = CatalogRef(self.catalog_url, element)
self.catalog_refs[catalog_ref.title] = catalog_ref

def _process_metadata(self, element, tag_type):
Expand Down Expand Up @@ -133,22 +139,31 @@ class CatalogRef(object):
title : string
Title of the catalogRef element
"""
def __init__(self, element_node):
def __init__(self, base_url, element_node):
r"""
Initialize the catalogRef object.

Parameters
----------
base_url : String
URL to the base catalog that owns this reference
element_node : Element
An Element Tree Element representing a catalogRef node

"""
self.name = element_node.attrib["name"]
self.href = element_node.attrib["{http://www.w3.org/1999/xlink}href"]
if self.href[0] == '/':
self.href = self.href[1:]
self.title = element_node.attrib["{http://www.w3.org/1999/xlink}title"]

# Resolve relative URLs
href = element_node.attrib["{http://www.w3.org/1999/xlink}href"]
self.href = urljoin(base_url, href)

def follow(self):
r"""
Follow the reference, returning a new TDSCatalog
"""
return TDSCatalog(self.href)


class Dataset(object):
r"""
Expand Down
6 changes: 6 additions & 0 deletions siphon/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ def test_html_link(self):
'grib/NCEP/RAP/CONUS_13km/catalog.html')
cat = TDSCatalog(url)
assert cat

def test_catalog_follow(self):
url = 'http://thredds-test.unidata.ucar.edu/thredds/testDatasets.xml'
ref_name = 'TestFmrc'
cat = TDSCatalog(url).catalog_refs[ref_name].follow()
assert cat