Commit ea7419e4 authored by Alexandre Bulté's avatar Alexandre Bulté
Browse files

Merge branch 'alternative-schemas-toml' into dev

parents 480256c9 de523c2a
Pipeline #561 passed with stage
in 38 seconds
...@@ -6,6 +6,7 @@ from datetime import datetime, timezone ...@@ -6,6 +6,7 @@ from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
import importlib_resources import importlib_resources
import os
import requests import requests
import toml import toml
from tableschema import Schema from tableschema import Schema
...@@ -37,6 +38,10 @@ import_custom_checks() ...@@ -37,6 +38,10 @@ import_custom_checks()
def get_schemas_config(): def get_schemas_config():
schemas_config_file = os.environ.get('VALIDATA_SCHEMAS_CONFIG')
if schemas_config_file is not None:
with open(schemas_config_file) as fp:
return toml.load(fp)
with importlib_resources.open_text('validata_core', 'schemas.toml') as fp: with importlib_resources.open_text('validata_core', 'schemas.toml') as fp:
return toml.load(fp) return toml.load(fp)
...@@ -173,8 +178,8 @@ class Validator: ...@@ -173,8 +178,8 @@ class Validator:
"""Return a `tableschema.Schema` instance from `source`.""" """Return a `tableschema.Schema` instance from `source`."""
if isinstance(source, Path): if isinstance(source, Path):
source = str(source) source = str(source)
if isinstance(source, str) and source.startswith("scdl-"): # try to load as a tag from schemas_config
# `source` seems to be a SCDL tag (i.e. `scdl-prenoms`) else:
schema_config = self.schemas_config.get(source) schema_config = self.schemas_config.get(source)
if schema_config is not None: if schema_config is not None:
source = schema_config["schema"] source = schema_config["schema"]
......
...@@ -8,17 +8,13 @@ import argparse ...@@ -8,17 +8,13 @@ import argparse
import json import json
import logging import logging
import sys import sys
from pathlib import Path
import toml from . import Validator, get_schemas_config
from . import Validator
def cli(): def cli():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('source', help='URL or path to tabular file (CSV, XLS, etc.) to validate') parser.add_argument('source', help='URL or path to tabular file (CSV, XLS, etc.) to validate')
parser.add_argument('--config', type=Path, help='use alternate `schemas.toml` config file')
parser.add_argument('--log', default='WARNING', help='level of logging messages') parser.add_argument('--log', default='WARNING', help='level of logging messages')
parser.add_argument('--schema', help='URL or path to table schema JSON file') parser.add_argument('--schema', help='URL or path to table schema JSON file')
args = parser.parse_args() args = parser.parse_args()
...@@ -32,10 +28,7 @@ def cli(): ...@@ -32,10 +28,7 @@ def cli():
stream=sys.stderr, # script outputs data stream=sys.stderr, # script outputs data
) )
schemas_config = None schemas_config = get_schemas_config()
if args.config is not None:
with args.config.open() as fp:
schemas_config = toml.load(fp)
report = Validator(schemas_config).validate(source=args.source, schema=args.schema) report = Validator(schemas_config).validate(source=args.source, schema=args.schema)
......
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