Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
validation with logs
"""Data-package validation report"""
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from logging import getLogger
from future import standard_library
from os.path import dirname
from jsonschema import ValidationError
from munch import Munch
standard_library.install_aliases()
from collections import OrderedDict
from os import rename
log = getLogger(__name__)
class Validator(object):
"""Data-package validation for humans"""
def __init__(self, package, file=None):
self.package = package
self.file = file
self.facts = OrderedDict()
self.is_okay = None
def run(self):
self.configure_logger()
self.log_header()
self.log_errors()
self.stamp_logfile()
def stamp_logfile(self):
stamp = 'pass' if self.is_okay else 'fail'
rename(self.filepath, self.filepath + stamp)
@staticmethod
def simplify(feedback):
return str(feedback).split('{schema:')[0].replace('\n', ' ')
def log_errors(self):
try:
self.package.log_errors()
self.is_okay = True
log.info('Valid data-package')
except ValidationError:
self.is_okay = False
for error in self.package.iter_errors():
log.warn(self.simplify(error))
def log_header(self):
self.facts.update(self.package.metadata)
self.facts.update({'validation': self.log_errors()})
@property
def filepath(self):
original = dirname(self.package.base_path)
return original.replace('.json', '.log')
def configure_logger(self):
if __name__ == '__main__':
from gobble.config import FISCAL_SCHEMA, EXAMPLES_DIR
from datapackage import DataPackage
from os.path import join
example = join(EXAMPLES_DIR, 'dp-invalid', 'datapackage.json')
dp = DataPackage(example, schema=FISCAL_SCHEMA)
report = Validator(dp)
print(report.is_okay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment