Skip to content

Commit

Permalink
#24: added validation to DatasetURI's save method, and changed valida…
Browse files Browse the repository at this point in the history
…tion function to only raise exception if the resource is not available
  • Loading branch information
mortenwh committed Feb 8, 2019
1 parent dce1a52 commit 862e721
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions geospaas/catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def protocol(self):
return self.uri.split(':')[0]

def save(self, *args, **kwargs):
validate_uri(uri)
# Validation is not usually done in the models but rather via form
# validation. We should discuss if we want it or not. Presently, we
# check only that the uri is valid but we may also check if it exists
Expand Down
4 changes: 2 additions & 2 deletions geospaas/nansat_ingestor/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def get_or_create(self, uri, n_points=10, uri_filter_args=None, *args, **kwargs)
if not uri_filter_args:
uri_filter_args = {}

# Validate uri - this should fail if the uri doesn't point to a valid
# Validate uri - this should raise an exception if the uri doesn't point to a valid
# file or stream
valid_uri = validate_uri(uri)
validate_uri(uri)

# Several datasets can refer to the same uri (e.g., scatterometers and svp drifters), so we
# need to pass uri_filter_args
Expand Down
18 changes: 10 additions & 8 deletions geospaas/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,27 @@ def product_path(module, filename):
def validate_uri(uri):
""" Validation of URI and its existence
URI should be either: file://localhost/some/path/filename.ext
or accessible: http://www.eee.rrr/some/path
URI conventions: URI = scheme:[//authority]path[?query][#fragment]
Examples:
file://localhost/some/path/filename.ext
http://www.eee.rrr/some/path
If URI is not valid, the function rasies a ValueError or urrlib error
If URI is not valid, the function raises a ValueError or urrlib error
"""
validation_result = False
uri_parts = urlparse(uri)
if uri_parts.scheme=='file' and uri_parts.netloc=='localhost':
if os.path.isfile(uri_parts.path):
validation_result = True
if not os.path.isfile(uri_parts.path):
raise FileNotFoundError(uri_parts.path)
else:
if URLLIB_VERSION == 2:
request = urllibN.Request(uri)
else:
request = urllibN.PoolManager().request('GET', uri)
if request.status==200:
validation_result = True
return validation_result
if not request.status==200:
raise ConnectionError(uri)

def nansat_filename(uri):
# Check if data should be read as stream or as file? Or just:
Expand Down

0 comments on commit 862e721

Please sign in to comment.