Commit 9ba601f0 authored by Christophe Benz's avatar Christophe Benz

Use new version of validata-core

parent 4b93ef4f
......@@ -27,11 +27,10 @@ setup(
'ezodf',
'flask',
'lxml',
'requests',
'goodtables',
'tabulator',
'validata_core >= 0.1, < 0.2',
'validata_core >= 0.2, < 0.3',
]
)
......@@ -7,6 +7,8 @@ from io import BytesIO
import validata_core
from validata_core.source_helpers import build_tabulator_params
from .validate_helper import ValidatorHelper
log = logging.getLogger(__name__)
......@@ -36,14 +38,3 @@ def bytes_data(f):
f.save(iob)
iob.seek(0)
return iob.getvalue()
def core_validate(schema_code, **args):
""" Validate source against schema using custom-checks and pre-checks"""
return validata_core.validate(
source=args['source'],
schema=schema_code,
force_strings=True,
**{k: v for k, v in args.items() if k != 'source'}
)
......@@ -3,7 +3,7 @@
import logging
import requests
import validata_core
#
# MEMENTO: json.load(pkg_resources.resource_stream('validata_core', 'spec.json'))
......@@ -18,27 +18,15 @@ class ValidatorHelper:
schema_dict = {}
@classmethod
def init(cls, schema_info):
def init(cls, schemas_config):
""" Register and download schema and custom_checks info """
cls.validator = validata_core.Validator(schemas_config)
cls.schema_dict = {}
for code in schema_info:
log.info('Downloading schema %s', code)
schema = schema_info[code].copy()
# schema download
schema['schema'] = cls.json_download(schema['schema_json_url'],
'{}_schema.json'.format(code))
# custom_checks
if 'goodtables_checks_json_url' in schema:
schema['goodtables_checks'] = cls.json_download(schema['goodtables_checks_json_url'],
'{}_goodtables_checks.json'.format(code))
cls.schema_dict[code] = schema
@classmethod
def json_download(cls, url, filename):
""" Download url content as JSON """
return requests.get(url).json()
for code, schema_config in schemas_config.items():
schema = schema_config['schema']
log.info('Loading schema %r from %r', code, schema)
schema_instance = cls.validator.load_schema(schema)
cls.schema_dict[code] = {**schema_config, "schema_instance": schema_instance}
@classmethod
def schema_exist(cls, schema_code):
......@@ -52,11 +40,15 @@ class ValidatorHelper:
return None
# First schema keys but 'fields'
d1 = dict([(k, v) for k, v in cls.schema_dict[schema_code]['schema'].items() if k != 'fields'])
d1 = {
k: v
for k, v in cls.schema(schema_code).descriptor.items()
if k != 'fields'
}
# All keys but schema* and custom_checks*
d2 = {k: v for k, v in cls.schema_dict[schema_code].items()
if not k.startswith('schema') and not k.startswith('goodtables_checks')}
if not k.startswith('schema')}
return {**d1, 'code': schema_code, **d2}
......@@ -65,9 +57,17 @@ class ValidatorHelper:
""" Return schema from schema code """
if not cls.schema_exist(schema_code):
return None
return cls.schema_dict[schema_code]['schema']
return cls.schema_dict[schema_code]['schema_instance']
@classmethod
def schema_info_list(cls):
""" Computes and return schema info list """
return [cls.schema_info(code) for code in sorted(cls.schema_dict.keys())]
@classmethod
def validate(cls, schema_code, **options):
""" Try to retrieve cached schema from `schema_code`, otherwise pass `schema_code` it as-is """
schema = cls.schema(schema_code)
if schema is None:
schema = schema_code
return cls.validator.validate(schema=schema, **options)
......@@ -19,7 +19,7 @@ from validata_core import csv_helpers
from validata_core.loaders import custom_loaders
from validata_ui import app
from validata_ui.ui_util import flash_error, flash_warning
from validata_ui.validata_util import ValidataSource, core_validate
from validata_ui.validata_util import ValidataSource
from validata_ui.validate_helper import ValidatorHelper
......@@ -170,7 +170,11 @@ def validate(schema_code, source: ValidataSource):
""" Validate source and display report """
try:
goodtables_report = core_validate(schema_code, **source.get_tabulator_params())
goodtables_report = ValidatorHelper.validate(
schema_code=schema_code,
force_strings=True,
**source.get_tabulator_params(),
)
except tabulator.exceptions.FormatError:
flash_error('Erreur : format de fichier non supporté')
return redirect(url_for('scdl_validator', val_code=schema_code))
......@@ -182,7 +186,7 @@ def validate(schema_code, source: ValidataSource):
report_date = datetime.strptime(date_str[:date_str.find('.')], '%Y-%m-%dT%H:%M:%S')
# Enhance goodtables_report
validata_report = create_validata_report(goodtables_report, ValidatorHelper.schema(schema_code))
validata_report = create_validata_report(goodtables_report, ValidatorHelper.schema(schema_code).descriptor)
# Display report to the user
val_info = ValidatorHelper.schema_info(schema_code)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment