Skip to content

Commit

Permalink
fix URL based validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Jan 20, 2024
1 parent 323ba43 commit a163b4f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pywcmp/ets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

# executable test for WCMP2

from io import BytesIO
import json

import click
Expand Down Expand Up @@ -60,9 +59,10 @@ def validate(ctx, file_or_url, logfile, verbosity,
click.echo(f'Opening {file_or_url}')

if file_or_url.startswith('http'):
content = BytesIO(urlopen_(file_or_url).read())
content = urlopen_(file_or_url).read()
else:
content = file_or_url
with open(file_or_url) as fh:
content = fh.read()

click.echo(f'Validating {file_or_url}')

Expand Down
6 changes: 3 additions & 3 deletions pywcmp/kpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

# WMO Core Metadata Profile Key Performance Indicators (KPIs)

from io import BytesIO
import json
import logging

Expand Down Expand Up @@ -66,9 +65,10 @@ def validate(ctx, file_or_url, summary, kpi, logfile, verbosity,
setup_logger(verbosity, logfile)

if file_or_url.startswith('http'):
content = BytesIO(urlopen_(file_or_url).read())
content = urlopen_(file_or_url).read()
else:
content = file_or_url
with open(file_or_url) as fh:
content = fh.read()

click.echo(f'Validating {file_or_url}')

Expand Down
7 changes: 3 additions & 4 deletions pywcmp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,16 @@ def check_url(url: str, check_ssl: bool, timeout: int = 30) -> dict:

def parse_wcmp(content: str) -> dict:
"""
Parse a buffer into a JSON dict (WCMP2)
Parse a string of WCMP into a JSON dict (WCMP2)
:param content: str of JSON
:param content: `str` of JSON
:returns: `dict` object of WCMP
"""

LOGGER.debug('Attempting to parse as JSON')
try:
with open(content) as fh:
data = json.load(fh)
data = json.loads(content)
except json.decoder.JSONDecodeError as err:
LOGGER.error(err)
raise RuntimeError(f'Encoding error: {err}')
Expand Down
8 changes: 7 additions & 1 deletion tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ def test_parse_wcmp(self):
"""test invalid input"""

with self.assertRaises(RuntimeError):
_ = parse_wcmp(get_test_file_path('data/not-json.csv'))
file_ = 'data/not-json.csv'
with open(get_test_file_path(file_)) as fh:
_ = parse_wcmp(fh.read())

file_ = 'data/wcmp2-passing.json'
with open(get_test_file_path(file_)) as fh:
_ = parse_wcmp(fh.read())


if __name__ == '__main__':
Expand Down

0 comments on commit a163b4f

Please sign in to comment.