spatial-d4science/ckanext/spatial/commands/validation.py

72 lines
1.8 KiB
Python
Raw Normal View History

2019-12-11 13:23:03 +01:00
from __future__ import print_function
import sys
2019-12-11 13:22:28 +01:00
import logging
2019-12-11 13:22:28 +01:00
from ckan.lib.cli import CkanCommand
2019-12-11 13:22:28 +01:00
import ckanext.spatial.util as util
log = logging.getLogger(__name__)
class Validation(CkanCommand):
'''Validation commands
Usage:
validation report [package-name]
Performs validation on the harvested metadata, either for all
packages or the one specified.
validation report-csv <filename>.csv
Performs validation on all the harvested metadata in the db and
writes a report in CSV format to the given filepath.
2019-12-11 13:22:28 +01:00
validation file <filename>.xml
Performs validation on the given metadata file.
'''
summary = __doc__.split('\n')[0]
usage = __doc__
max_args = 3
min_args = 0
def command(self):
if not self.args or self.args[0] in ['--help', '-h', 'help']:
2019-12-11 13:23:03 +01:00
print(self.usage)
sys.exit(1)
self._load_config()
cmd = self.args[0]
if cmd == 'report':
self.report()
elif cmd == 'report-csv':
self.report_csv()
elif cmd == 'file':
self.validate_file()
else:
2019-12-11 13:23:03 +01:00
print('Command %s not recognized' % cmd)
def report(self):
if len(self.args) >= 2:
2019-12-11 13:22:28 +01:00
pkg = self.args[1]
else:
pkg = None
2019-12-11 13:22:28 +01:00
return util.report(pkg)
def validate_file(self):
if len(self.args) > 2:
2019-12-11 13:23:03 +01:00
print('Too many parameters %i' % len(self.args))
sys.exit(1)
if len(self.args) < 2:
2019-12-11 13:23:03 +01:00
print('Not enough parameters %i' % len(self.args))
sys.exit(1)
2019-12-11 13:22:28 +01:00
return util.validate_file(self.args[1])
def report_csv(self):
if len(self.args) != 2:
2019-12-11 13:23:03 +01:00
print('Wrong number of arguments')
sys.exit(1)
2019-12-11 13:22:28 +01:00
return util.report_csv(self.args[1])