[ckan-dev] Spatial Custom Validator

Pedro G Silva pedro.gracio at deimos.com.pt
Thu Feb 11 15:02:31 UTC 2016


Hi All
Im writing a custom validator for a XML profile based on ESA Products. I'm
having an error of not founding my validator.
I have updated the config to include 'ckan.spatial.validator.profiles =
esa-safe'
I think I might not be extending the validation interface or something
along those lines. Any suggestions?

below you'll find the error and code

ERROR:
2016-02-11 14:58:10,184 DEBUG [ckanext.spatial.validation.validation]
Starting validation against profile(s) esa-safe
Traceback (most recent call last):
  File "/usr/lib/ckan/default/bin/paster", line 11, in <module>
    sys.exit(run())
  File
"/usr/lib/ckan/default/local/lib/python2.7/site-packages/paste/script/command.py",
line 102, in run
    invoke(command, command_name, options, args[1:])
  File
"/usr/lib/ckan/default/local/lib/python2.7/site-packages/paste/script/command.py",
line 141, in invoke
    exit_code = runner.run(args)
  File
"/usr/lib/ckan/default/local/lib/python2.7/site-packages/paste/script/command.py",
line 236, in run
    result = self.command()
  File
"/usr/lib/ckan/default/src/ckanext-harvest/ckanext/harvest/commands/harvester.py",
line 179, in command
    fetch_callback(consumer, method, header, body)
  File
"/usr/lib/ckan/default/src/ckanext-harvest/ckanext/harvest/queue.py", line
386, in fetch_callback
    fetch_and_import_stages(harvester, obj)
  File
"/usr/lib/ckan/default/src/ckanext-harvest/ckanext/harvest/queue.py", line
403, in fetch_and_import_stages
    success_import = harvester.import_stage(obj)
  File
"/usr/lib/ckan/default/lib/python2.7/site-packages/ckanext_spatial-0.2-py2.7.egg/ckanext/spatial/harvesters/base.py",
line 496, in import_stage
    is_valid, profile, errors =
self._validate_document(harvest_object.content, harvest_object)
  File
"/usr/lib/ckan/default/lib/python2.7/site-packages/ckanext_spatial-0.2-py2.7.egg/ckanext/spatial/harvesters/base.py",
line 823, in _validate_document
    valid, profile, errors = validator.is_valid(xml)
  File
"/usr/lib/ckan/default/lib/python2.7/site-packages/ckanext_spatial-0.2-py2.7.egg/ckanext/spatial/validation/validation.py",
line 340, in is_valid
    validator = self.validators[name]
KeyError: 'esa-safe'


CODE:
validation/__init__.py

import os
#from lxml import etree
#import lxml

from ckanext.spatial.validation import BaseValidator, XsdValidator
from ckanext.spatial.validation import Validators

from ckan.lib.navl.dictization_functions import Invalid

custom_validators = [ESASafeValidator]


VALIDATION_PROFILES = [('', 'Autodetect')]

for custom_validator in custom_validators:
    VALIDATION_PROFILES.append((custom_validator.name, custom_validator.title))


def validate_profiles(profile):
    if profile not in [p[0] for p in VALIDATION_PROFILES]:
        raise Invalid('Unknown validation profile: {0}'.format(profile))
    return profile


class ESASafeValidator(XsdValidator):
    '''
    XSD based validation for ESA SAFE documents.

    Uses XSD schema from ESA:

    http://earth.esa.int/SAFE/download/repinfo/BASE_20150212_RPI-BAS_5A4F.SAFE.zip

    '''
    name = 'esa-safe'
    title = 'ESA SAFE XSD Schema'

    @classmethod
    def is_valid(cls, xml):
        xsd_path = 'xml/esa-safe'

        xsd_filepath =
os.path.join(os.path.dirname(__file__),xsd_path, 'xfdu-safe/xfdu.xsd')

        xsd_name = 'Dataset schema (xfdu.xsd)'

        is_valid, errors = cls._is_valid(xml, xsd_filepath, xsd_name)

        if not is_valid:
            #TODO: not sure if we need this one, keeping for backwards
compatibility
            errors.insert(0, ('{0} Validation Error'.format(xsd_name), None))
        return is_valid, errors


-- 


[image: DEIMOS Engenharia, SA] <http://www.deimos.pt>
*Pedro Gracio Silva*
Aerospace Systems / Production Center

*Av. D. Joao II, Lt 1.17.01, 10 • 1998-023 Lisboa, Portugal*
Ph: +351 21 893 3020 • Fax: +351 21 896 9099
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20160211/7da15162/attachment-0002.html>


More information about the ckan-dev mailing list