diff --git a/pywcmp/ets.py b/pywcmp/ets.py index 356dd73..ea90075 100644 --- a/pywcmp/ets.py +++ b/pywcmp/ets.py @@ -28,7 +28,6 @@ # executable test for WCMP2 -from io import BytesIO import json import click @@ -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}') diff --git a/pywcmp/kpi.py b/pywcmp/kpi.py index cf42be3..c66b3b7 100644 --- a/pywcmp/kpi.py +++ b/pywcmp/kpi.py @@ -28,7 +28,6 @@ # WMO Core Metadata Profile Key Performance Indicators (KPIs) -from io import BytesIO import json import logging @@ -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}') diff --git a/pywcmp/util.py b/pywcmp/util.py index c57fcd0..eb5f57d 100644 --- a/pywcmp/util.py +++ b/pywcmp/util.py @@ -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}') diff --git a/tests/run_tests.py b/tests/run_tests.py index 3c7aa2c..c4bb6ba 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -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__':