Skip to content

Commit

Permalink
ScannerError: pass diagnostic information to ValidationMessage
Browse files Browse the repository at this point in the history
* 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 <graeme.watt@durham.ac.uk>
  • Loading branch information
GraemeWatt committed Sep 30, 2016
1 parent e39ee1c commit 64f806f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions hepdata_validator/__init__.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions hepdata_validator/submission_file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 64f806f

Please sign in to comment.