Skip to content

Commit

Permalink
chore: flake8 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
duttonw committed Dec 13, 2024
1 parent ffe9090 commit fde0c39
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 55 deletions.
3 changes: 2 additions & 1 deletion ckanext/validation/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

from six.moves.urllib.parse import urlparse
from six import string_types
from ckantoolkit import url_for, _, config, asbool, literal, h
from ckantoolkit import url_for, _, config, asbool, literal

from ckanext.validation.utils import get_default_schema


def get_helpers():
validators = (
get_validation_badge,
Expand Down
4 changes: 2 additions & 2 deletions ckanext/validation/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def run_validation_job(resource):

t.get_action('resource_patch')(
{'ignore_auth': True,
'user': t.get_action('get_site_user')({'ignore_auth': True})['name'],
'_validation_performed': True},
'user': t.get_action('get_site_user')({'ignore_auth': True})['name'],
'_validation_performed': True},
data_dict)
utils.send_validation_report(utils.validation_dictize(validation))

Expand Down
15 changes: 6 additions & 9 deletions ckanext/validation/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ def get_actions():
return {"{}".format(func.__name__): func for func in validators}




# Actions


def resource_validation_run(context, data_dict):
u'''
Start a validation job against a resource.
Expand Down Expand Up @@ -67,7 +63,7 @@ def resource_validation_run(context, data_dict):
# Ensure format is supported
if not resource.get(u'format', u'').lower() in settings.get_supported_formats():
raise t.ValidationError(
{u'format': u'Unsupported resource format.' +
{u'format': u'Unsupported resource format.'
u'Must be one of {}'.format(
u','.join(settings.get_supported_formats()))})

Expand Down Expand Up @@ -108,6 +104,7 @@ def resource_validation_run(context, data_dict):
def enqueue_job(*args, **kwargs):
return t.enqueue_job(*args, **kwargs)


@t.side_effect_free
def resource_validation_show(context, data_dict):
u'''
Expand Down Expand Up @@ -285,9 +282,9 @@ def resource_validation_run_batch(context, data_dict):

except t.ValidationError as e:
log.warning(
u'Could not run validation for resource %s ' +
u'Could not run validation for resource %s '
u'from dataset %s: %s',
resource['id'], dataset['name'], e)
resource['id'], dataset['name'], e)

if len(query['results']) < page_size:
break
Expand Down Expand Up @@ -363,8 +360,8 @@ def _update_search_params(search_data_dict, user_search_params=None):
else:
search_data_dict['fq'] = user_search_params['fq']

if (user_search_params.get('fq_list') and
isinstance(user_search_params['fq_list'], list)):
if (user_search_params.get('fq_list')
and isinstance(user_search_params['fq_list'], list)):
search_data_dict['fq_list'].extend(user_search_params['fq_list'])


Expand Down
36 changes: 13 additions & 23 deletions ckanext/validation/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
import json
import logging



import ckan.plugins as p
import ckantoolkit as tk

from . import settings as s, utils, validators, utils
from . import settings as s, utils, validators
from .helpers import get_helpers
from .logic import action, auth
from .model import tables_exist

from ckanext.validation.interfaces import IDataValidation
from ckanext.validation import views, cli


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -82,14 +79,12 @@ def get_validators(self):
resources_to_validate = {}
packages_to_skip = {}


# CKAN < 2.10
def before_create(self, context, data_dict):
return self.before_resource_create(context, data_dict)

# CKAN >= 2.10
def before_resource_create(self, context, data_dict):

is_dataset = self._data_dict_is_dataset(data_dict)
if not is_dataset:
context["_resource_create_call"] = True
Expand All @@ -104,7 +99,6 @@ def after_create(self, context, data_dict):

# CKAN >= 2.10
def after_resource_create(self, context, data_dict):

is_dataset = self._data_dict_is_dataset(data_dict)

if not s.get_create_mode_from_config() == u'async':
Expand All @@ -130,14 +124,13 @@ def _handle_validation_for_resource(self, context, resource):
needs_validation = False
if ((
# File uploaded
resource.get(u'url_type') == u'upload' or
resource.get(u'url_type') == u'upload'
# URL defined
resource.get(u'url')
) and (
or resource.get(u'url')
) and (
# Make sure format is supported
resource.get(u'format', u'').lower() in
s.get_supported_formats()
)):
resource.get(u'format', u'').lower() in s.get_supported_formats()
)):
needs_validation = True

if needs_validation:
Expand Down Expand Up @@ -173,20 +166,17 @@ def before_resource_update(self, context, current_resource, updated_resource):
needs_validation = False
if ((
# New file uploaded
updated_resource.get(u'upload') or
updated_resource.get(u'upload')
# External URL changed
updated_resource.get(u'url') != current_resource.get(u'url') or
or updated_resource.get(u'url') != current_resource.get(u'url')
# Schema changed
(updated_resource.get(u'schema') !=
current_resource.get(u'schema')) or
or updated_resource.get(u'schema') != current_resource.get(u'schema')
# Format changed
(updated_resource.get(u'format', u'').lower() !=
current_resource.get(u'format', u'').lower())
) and (
or updated_resource.get(u'format', u'').lower() != current_resource.get(u'format', u'').lower()
) and (
# Make sure format is supported
updated_resource.get(u'format', u'').lower() in
s.get_supported_formats()
)):
updated_resource.get(u'format', u'').lower() in s.get_supported_formats()
)):
needs_validation = True

if needs_validation:
Expand Down
3 changes: 1 addition & 2 deletions ckanext/validation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def get_supported_formats():
return supported_formats or DEFAULT_SUPPORTED_FORMATS



def get_update_mode_from_config():
if asbool(
config.get(u'ckanext.validation.run_on_update_sync', False)):
Expand All @@ -84,4 +83,4 @@ def get_create_mode_from_config():
config.get(u'ckanext.validation.run_on_create_async', True)):
return u'async'
else:
return None
return None
1 change: 0 additions & 1 deletion ckanext/validation/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def test_get_validation_badge_error(self):
assert '<span class="prefix">data</span><span class="status error">error</span>' in out
assert 'title="Error during validation {}"'.format(resource["validation_timestamp"]) in out


def test_get_validation_badge_other(self):

resource = factories.Resource(
Expand Down
29 changes: 12 additions & 17 deletions ckanext/validation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@

from six import string_types, ensure_str

import ckan.plugins as plugins
import ckan.plugins as p
import ckantoolkit as t
import ckan.lib.uploader as uploader
from ckan import model

from ckanext.validation.interfaces import IPipeValidation
from .interfaces import IPipeValidation
from . import settings

log = logging.getLogger(__name__)

from . import settings
import ckan.plugins as p
import ckantoolkit as t


def process_schema_fields(data_dict):
u'''
Expand All @@ -41,15 +38,16 @@ def process_schema_fields(data_dict):
data_dict["schema"] = data_dict["schema"].decode()
elif schema_url:

if (not isinstance(schema_url, string_types) or
not schema_url.lower()[:4] == u'http'):
if (not isinstance(schema_url, string_types)
or not schema_url.lower()[:4] == u'http'):
raise t.ValidationError({u'schema_url': 'Must be a valid URL'})
data_dict[u'schema'] = schema_url
elif schema_json:
data_dict[u'schema'] = schema_json

return data_dict


def run_async_validation(resource_id):

try:
Expand All @@ -60,8 +58,7 @@ def run_async_validation(resource_id):
except t.ValidationError as e:
log.warning(
u'Could not run validation for resource %s: %s',
resource_id, e)

resource_id, e)


def get_default_schema(package_id):
Expand All @@ -81,9 +78,9 @@ def should_remove_unsupported_resource_validation_reports(res_dict):
return False
return (not res_dict.get('format', u'').lower() in settings.get_supported_formats()
and (res_dict.get('url_type') == 'upload'
or not res_dict.get('url_type'))
or not res_dict.get('url_type'))
and (t.h.asbool(res_dict.get('validation_status', False))
or t.h.asbool(res_dict.get('extras', {}).get('validation_status', False))))
or t.h.asbool(res_dict.get('extras', {}).get('validation_status', False))))


def remove_unsupported_resource_validation_reports(resource_id):
Expand All @@ -102,7 +99,7 @@ def remove_unsupported_resource_validation_reports(resource_id):

if should_remove_unsupported_resource_validation_reports(res):
log.info('Unsupported resource format "%s". Deleting validation reports for resource %s',
res.get(u'format', u''), res['id'])
res.get(u'format', u''), res['id'])
try:
p.toolkit.get_action('resource_validation_delete')(context, {
"resource_id": res['id']})
Expand All @@ -111,8 +108,6 @@ def remove_unsupported_resource_validation_reports(resource_id):
log.error('Validation reports for resource %s do not exist', res['id'])




def get_local_upload_path(resource_id):
u'''
Returns the local path to an uploaded file give an id
Expand Down Expand Up @@ -173,7 +168,7 @@ def validation_dictize(validation):


def send_validation_report(validation_report):
for observer in plugins.PluginImplementations(IPipeValidation):
for observer in p.PluginImplementations(IPipeValidation):
try:
observer.receive_validation_report(validation_report)
except Exception as ex:
Expand Down

0 comments on commit fde0c39

Please sign in to comment.