Skip to content

Commit

Permalink
global: accepts file_path and/or pre loaded YAML object
Browse files Browse the repository at this point in the history
* API - validation method now to be used with file_path= parameter,
  and optionally a data object that represents the loaded YAML structure.
  This avoids loaded the YAML file twice which can be an overhead for
  larger YAML files.

* README - updated readme.

* Release - version 0.1.10

Signed-off-by: Eamonn Maguire <eamonnmag@gmail.com>
  • Loading branch information
eamonnmag committed Jul 1, 2016
1 parent 148a54d commit e39ee1c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ submission_file_validator = SubmissionFileValidator()
submission_file_path = 'submission.yaml'

# the validate method takes a string representing the file path.
is_valid_submission_file = submission_file_validator.validate(submission_file_path)
is_valid_submission_file = submission_file_validator.validate(file_path=submission_file_path)

# if there are any error messages, they are retrievable through this call
submission_file_validator.get_messages()
Expand All @@ -47,8 +47,25 @@ from hepdata_validator.data_file_validator import DataFileValidator
data_file_validator = DataFileValidator()

# the validate method takes a string representing the file path.
data_file_validator.validate('data.yaml')
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()
```

Optionally, if you have already loaded the YAML object, then you can pass it through
as a data object. You must also pass through the file_path since this is used as a key
for the error message lookup map.

```python

from hepdata_validator.data_file_validator import DataFileValidator
import yaml

file = yaml.load(open('data.yaml', 'r'))
data_file_validator = DataFileValidator()

data_file_validator.validate(file_path='data.yaml', data=file_contents)

data_file_validator.get_messages('data.yaml')
```
10 changes: 8 additions & 2 deletions hepdata_validator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@ class Validator(object):
def __init__(self):
self.messages = {}

def validate(self, file_path):
def validate(self, **kwargs):
"""
Validates a file.
:param file_path: path to file to be loaded.
:param data: pre loaded YAML object (optional).
:return: true if valid, false otherwise
"""
schema = json.load(open(self.schema_file, 'r'))

try:
data = kwargs.pop("data", None)
file_path = kwargs.pop("file_path", None)

if data is None:

try:
data = yaml.load(open(file_path, 'r'), Loader=yaml.CLoader)
except: #pragma: no cover
data = yaml.load(open(file_path, 'r')) #pragma: no cover

try:
validate(data, schema)

except ValidationError as ve:
Expand Down
25 changes: 19 additions & 6 deletions hepdata_validator/submission_file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ class SubmissionFileValidator(Validator):
schema_file = base_path + '/schemas/submission_schema.json'
additonal_info_schema = base_path + '/schemas/additional_info_schema.json'

def validate(self, file_path):
def validate(self, **kwargs):
"""
Validates a submission file
:param file_path: path to file to be loaded.
:param data: pre loaded YAML object (optional).
:return: Bool to indicate the validity of the file.
"""
try:
submission_file_schema = json.load(
open(self.schema_file, 'r'))
Expand All @@ -26,11 +32,18 @@ def validate(self, file_path):

# even though we are using the yaml package to load,
# it supports JSON and YAML
try:
# We try to load using the CLoader for speed improvements.
data = yaml.load_all(open(file_path, 'r'), Loader=yaml.CLoader)
except: #pragma: no cover
data = yaml.load_all(open(file_path, 'r')) #pragma: no cover
data = kwargs.pop("data", None)
file_path = kwargs.pop("file_path", None)

if file_path is None:
raise LookupError("file_path argument must be supplied")

if data is None:
try:
# We try to load using the CLoader for speed improvements.
data = yaml.load_all(open(file_path, 'r'), Loader=yaml.CLoader)
except Exception as e: #pragma: no cover
data = yaml.load_all(open(file_path, 'r')) #pragma: no cover

for data_item in data:
if data_item is None:
Expand Down
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.9',
summary='0.1.9 release',
version='0.1.10',
summary='0.1.10 release',
url='https://github.com/hepdata/hepdata-validator',
license='GPLv2',
author='Eamonn Maguire',
Expand Down
22 changes: 13 additions & 9 deletions testsuite/validation_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import unittest

import yaml

from hepdata_validator.data_file_validator import DataFileValidator
from hepdata_validator.submission_file_validator import SubmissionFileValidator

Expand All @@ -24,7 +27,8 @@ def test_valid_submission_yaml(self):
self.validator = SubmissionFileValidator()
valid_sub_yaml = os.path.join(self.base_dir, self.valid_file)

self.validator.validate(valid_sub_yaml)
sub_yaml_obj = yaml.load_all(open(valid_sub_yaml, 'r'))
self.validator.validate(file_path=valid_sub_yaml, data=sub_yaml_obj)
self.validator.print_errors(valid_sub_yaml)

print 'Valid\n'
Expand All @@ -37,7 +41,7 @@ def test_valid_submission_yaml_with_empty_section(self):
self.validator = SubmissionFileValidator()
valid_sub_yaml = os.path.join(self.base_dir, self.valid_empty_file)

self.assertEqual(self.validator.validate(valid_sub_yaml), True)
self.assertEqual(self.validator.validate(file_path=valid_sub_yaml), True)
self.validator.print_errors(valid_sub_yaml)


Expand All @@ -50,7 +54,7 @@ def test_valid_submission_yaml_with_license(self):
valid_sub_yaml = os.path.join(self.base_dir,
self.valid_license_file)

self.assertEqual(self.validator.validate(valid_sub_yaml), True)
self.assertEqual(self.validator.validate(file_path=valid_sub_yaml), True)

self.validator.print_errors(valid_sub_yaml)

Expand All @@ -62,7 +66,7 @@ def test_invalid_submission_yaml(self):
invalid_sub_yaml = os.path.join(self.base_dir, self.invalid_file)

self.assertEqual(self.validator.validate(
invalid_sub_yaml), False
file_path=invalid_sub_yaml), False
)

self.validator.print_errors(invalid_sub_yaml)
Expand Down Expand Up @@ -101,13 +105,13 @@ def setUp(self):

def test_valid_yaml_file(self):
print '___DATA_VALIDATION: Testing valid yaml submission___'
is_valid = self.validator.validate(self.valid_file_yaml)
is_valid = self.validator.validate(file_path=self.valid_file_yaml)
self.validator.print_errors(self.valid_file_yaml)
self.assertEqual(is_valid, True)

def test_invalid_yaml_file(self):
print '___DATA_VALIDATION: Testing invalid yaml submission___'
self.assertEqual(self.validator.validate(self.invalid_file_yaml),
self.assertEqual(self.validator.validate(file_path=self.invalid_file_yaml),
False)

self.validator.print_errors(self.invalid_file_yaml)
Expand All @@ -116,14 +120,14 @@ def test_invalid_yaml_file(self):

def test_valid_file_with_percent_errors(self):
print '___DATA_VALIDATION: Testing valid yaml percent error ___'
self.assertEqual(self.validator.validate(self.valid_file_error_percent_yaml),
self.assertEqual(self.validator.validate(file_path=self.valid_file_error_percent_yaml),
False)
self.validator.print_errors(self.valid_file_error_percent_yaml)
print 'Invalid\n'

def test_valid_json_file(self):
print '___DATA_VALIDATION: Testing valid json submission___'
is_valid = self.validator.validate(self.valid_file_json)
is_valid = self.validator.validate(file_path=self.valid_file_json)
self.validator.print_errors(self.valid_file_json)
self.assertEqual(is_valid, True)

Expand All @@ -132,7 +136,7 @@ def test_valid_json_file(self):

def test_invalid_json_file(self):
print '___DATA_VALIDATION: Testing invalid json submission___'
self.assertEqual(self.validator.validate(self.invalid_file_json),
self.assertEqual(self.validator.validate(file_path=self.invalid_file_json),
False)
self.validator.print_errors(self.invalid_file_json)
print 'Invalid\n'
Expand Down

0 comments on commit e39ee1c

Please sign in to comment.