From 64f806f494fe5a9cb67cdf5174c413020f03480e Mon Sep 17 00:00:00 2001 From: Graeme Watt Date: Fri, 30 Sep 2016 16:18:59 +0100 Subject: [PATCH] ScannerError: pass diagnostic information to ValidationMessage * submission file validator: append ScannerError exception to message * data file validator: catch ScannerError exception and add message * README: explain how to print error messages * release: version 0.1.11 Signed-off-by: Graeme Watt --- README.md | 6 ++++++ hepdata_validator/__init__.py | 13 +++++++++++-- hepdata_validator/submission_file_validator.py | 5 +++-- setup.py | 4 ++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 14df0c7..32c21d0 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ is_valid_submission_file = submission_file_validator.validate(file_path=submissi # if there are any error messages, they are retrievable through this call submission_file_validator.get_messages() + +# the error messages can be printed +submission_file_validator.print_errors(submission_file_path) ``` Data file validation is exactly the same. @@ -51,6 +54,9 @@ data_file_validator.validate(file_path='data.yaml') # if there are any error messages, they are retrievable through this call data_file_validator.get_messages() + +# the error messages can be printed +data_file_validator.print_errors('data.yaml') ``` Optionally, if you have already loaded the YAML object, then you can pass it through diff --git a/hepdata_validator/__init__.py b/hepdata_validator/__init__.py index c81124c..6e93499 100644 --- a/hepdata_validator/__init__.py +++ b/hepdata_validator/__init__.py @@ -1,6 +1,7 @@ import json from jsonschema import validate, ValidationError import yaml +from yaml.scanner import ScannerError from yaml.parser import ParserError __author__ = 'eamonnmaguire' @@ -33,9 +34,17 @@ def validate(self, **kwargs): if data is None: try: - data = yaml.load(open(file_path, 'r'), Loader=yaml.CLoader) + try: + data = yaml.load(open(file_path, 'r'), Loader=yaml.CLoader) + except ScannerError as se: + self.add_validation_message(ValidationMessage(file=file_path, message=str(se))) + return False except: #pragma: no cover - data = yaml.load(open(file_path, 'r')) #pragma: no cover + try: #pragma: no cover + data = yaml.load(open(file_path, 'r')) #pragma: no cover + except ScannerError as se: #pragma: no cover + self.add_validation_message(ValidationMessage(file=file_path, message=str(se))) #pragma: no cover + return False #pragma: no cover try: validate(data, schema) diff --git a/hepdata_validator/submission_file_validator.py b/hepdata_validator/submission_file_validator.py index cfc93cb..b3b55df 100644 --- a/hepdata_validator/submission_file_validator.py +++ b/hepdata_validator/submission_file_validator.py @@ -65,7 +65,8 @@ def validate(self, **kwargs): except ScannerError as se: self.add_validation_message( ValidationMessage(file=file_path, - message='There was a problem parsing the file. ' + message='There was a problem parsing the file. ' 'This can be because you forgot spaces ' - 'after colons in your YAML file for instance.') + 'after colons in your YAML file for instance. ' + 'Diagnostic information follows.\n' + str(se)) ) diff --git a/setup.py b/setup.py index caff477..206ea4b 100644 --- a/setup.py +++ b/setup.py @@ -49,8 +49,8 @@ def run_tests(self): setup( name='hepdata_validator', - version='0.1.10', - summary='0.1.10 release', + version='0.1.11', + summary='0.1.11 release', url='https://github.com/hepdata/hepdata-validator', license='GPLv2', author='Eamonn Maguire',