import requests

from pylons import config

import ckan.plugins as p

from ckanext.dcat.interfaces import IDCATRDFHarvester
from ckanext.sweden.dcat import template_helpers


VALIDATION_SERVICE = 'https://validator.dcat-editor.com/service'


class SwedenDCATRDFHarvester(p.SingletonPlugin):

    p.implements(IDCATRDFHarvester, inherit=True)
    p.implements(p.IConfigurer)
    p.implements(p.ITemplateHelpers)

    def after_download(self, content, harvest_job):

        if not p.toolkit.asbool(config.get('ckanext.sweden.harvest.use_validation', True)):
            return content, []

        validation_service = config.get('ckanext.sweden.harvest.validation_service', VALIDATION_SERVICE)

        stop_on_errors = p.toolkit.asbool(config.get('ckanext.sweden.harvest.stop_on_validation_errors', False))

        errors = []
        try:
            r = requests.post(validation_service, data=content)
        except requests.exceptions.RequestException, e:
            errors.append('Error contacting the validation service: {0}'.format(str(e)))

            if stop_on_errors:
                return None, errors
            else:
                return content, errors

        if r.status_code != 200:

            errors.append('The validation service returned an error: {0}'.format(
                r.status_code))

            if stop_on_errors:
                return None, errors
            else:
                return content, errors

        else:
            response = r.json()

            if not response.get('rdfError') and not response.get('errors'):
                # All clear
                return content, []

            if response.get('rdfError'):
                errors.append(response.get('rdfError'))
            else:
                if response.get('mandatoryError'):
                    for _class in response['mandatoryError']:
                        errors.append('Mandatory class {0} missing'.format(_class))
                for resource in response.get('resources', []):
                    for error in resource.get('errors', []):
                        msg = '[error][{uri}][{type}] {reason} for {path}'
                        code = error.get('code')
                        if code == 1 or 'few':
                            reason = 'Too few values'
                        elif code == 2 or 'many':
                            reason = 'Too many values'
                        else:
                            reason = ''

                        errors.append(msg.format(
                            uri=resource.get('uri'),
                            type=resource.get('type'),
                            path=error.get('path'),
                            reason=reason))

            if stop_on_errors:
                return None, errors
            else:
                return content, errors

    # IConfigurer
    def update_config(self, config):
        p.toolkit.add_template_directory(config, 'templates')

    # ITemplateHelpers
    def get_helpers(self):
        return {
            'json_loads': template_helpers.json_loads,
        }

@@ -1,12 +1,24 @@
+import json%0A
 import reque
@@ -1955,16 +1955,17 @@
 class))%0A
+%0A
         
@@ -2043,701 +2043,41 @@
     
-for 
 error
- in resource.get('errors', %5B%5D):%0A                        msg = '%5Berror%5D%5B%7Buri%7D%5D%5B%7Btype%7D%5D %7Breason%7D for %7Bpath%7D'%0A                        code = error.get('code')%0A                        if code == 1 or 'few':%0A                            reason = 'Too few values'%0A                        elif code == 2 or 'many':%0A                            reason = 'Too many values'%0A                        else:%0A                            reason = ''%0A%0A                        errors.append(msg.format(%0A                            uri=resource.get('uri'),%0A                            type=resource.get('type'),%0A                            path=error.get('path'),%0A                            reason=reason
+s.append(json.dumps(resource
 ))%0A%0A

change dcat harvester to save resource errors as json