Commit aec993f9 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Require a pre-existing concept scheme for LCSV command line import

Instead of creating a dummy concept scheme from the file name, we now
require a pre-existing concept scheme's URI to be passed as a --scheme
argument when running an LCSV import from the command line. This matches
with the web UI behavior and makes it easier to integrate downstream
with cubicweb-saem_ref as this application has a custom handling of ARK
identifier which we could assign to the concept scheme previously
created during import.

We move the "Importing Skos dataset" down a bit because we may fail
earlier if the --scheme option is not specified and having this message
displayed is meaningless in such cases.

Related to Extranet #44146518.
parent 056d3154cbe0
......@@ -18,7 +18,7 @@
from __future__ import print_function
import logging
from os import path
import sys
from cubicweb.toolsutils import Command, underline_title
from cubicweb.cwctl import CWCTL
......@@ -85,6 +85,11 @@ class ImportSkosData(Command):
'type': 'choice', 'choices': ('librdf', 'rdflib'), 'default': 'rdflib',
'help': 'RDF store type: librdf or rdflib (only with --format rdf).'
}),
('scheme',
{'type': 'string',
'help': ('URI of an existing concept scheme to import concepts in '
'(only relevant for LCSV import format)'),
}),
)
rdf_store_factories = {
......@@ -99,28 +104,19 @@ class ImportSkosData(Command):
}
def run(self, args):
print(u'\n%s' % underline_title('Importing Skos dataset'))
appid = args[0]
connection = admincnx(appid)
drop_scheme = None
if self.get('format') == 'lcsv':
scheme_uri = self.get('scheme')
if not scheme_uri:
print(u'command failed: --scheme option is required for LCSV import')
connection.repo.shutdown()
sys.exit(1)
try:
fpath, = args[1:]
except TypeError:
raise Exception('LCSV format expects exactly one input file')
with connection as cnx:
title = path.basename(fpath).decode('utf-8')
scheme = cnx.create_entity('ConceptScheme', title=title)
cnx.commit()
scheme_uri = scheme.cwuri
def drop_scheme(cnx):
"""Drop ConceptScheme created prior to LCSV import."""
rset = cnx.find('ConceptScheme', title=title)
if rset:
rset.one().cw_delete()
def extentities_generator():
"""ExtEntity generator function holding control on `fpath` file."""
......@@ -138,6 +134,7 @@ class ImportSkosData(Command):
extentities = graph_extentities(graph)
import_log = LoggingImportLog()
print(u'\n%s' % underline_title('Importing Skos dataset'))
with connection as cnx:
store = self.cw_store_factories[self.get('cw-store')](cnx)
try:
......@@ -146,9 +143,6 @@ class ImportSkosData(Command):
cnx.commit()
except BaseException as exc:
cnx.rollback()
if drop_scheme is not None:
drop_scheme(cnx)
cnx.commit()
print(u"Aborting due to: '%s'" % exc)
else:
print(u'Created: %d\nUpdated: %d' % (len(created), len(updated)))
......
......@@ -50,16 +50,9 @@ class ImportSkosDataCommandTC(testlib.CubicWebTC):
CubicWebConfiguration.config_for = self.orig_config_for
super(ImportSkosDataCommandTC, self).tearDown()
def run_import_skos(self, fpath, *args, **kwargs):
def run_import_skos(self, fpath, *args):
cmd = [self.appid, fpath] + list(args)
sys.stdout = StringIO()
try:
ccplugin.ImportSkosData(None).main_run(cmd)
finally:
stdout_check = kwargs.pop('stdout_check', None)
if stdout_check is not None:
self.assertIn(stdout_check, sys.stdout.getvalue())
sys.stdout = sys.__stdout__
ccplugin.ImportSkosData(None).main_run(cmd)
def _test_base(self):
with self.admin_access.repo_cnx() as cnx:
......@@ -87,25 +80,25 @@ class ImportSkosDataCommandTC(testlib.CubicWebTC):
self._test_base()
def test_lcsv(self):
with self.admin_access.cnx() as cnx:
scheme = cnx.create_entity('ConceptScheme', title=u'lcsv')
cnx.commit()
scheme_uri = scheme.cwuri
self.run_import_skos(self.datapath('lcsv_example_shortened.csv'),
'--format', 'lcsv')
'--format', 'lcsv', '--scheme', scheme_uri)
with self.admin_access.cnx() as cnx:
rset = cnx.find('ConceptScheme', title=u'lcsv_example_shortened.csv')
self.assertTrue(rset)
scheme = rset.one()
self.assertEqual(scheme.title, u'lcsv_example_shortened.csv')
scheme = cnx.find('ConceptScheme', cwuri=scheme_uri).one()
self.assertEqual(len(scheme.reverse_in_scheme), 5)
def test_lcsv_rollback(self):
self.run_import_skos(
self.datapath('lcsv_example_missing_prolog.csv'),
'--format', 'lcsv',
stdout_check=u"Aborting due to: 'missing prolog column (#)'",
)
with self.admin_access.cnx() as cnx:
rset = cnx.find('ConceptScheme',
title=u'lcsv_example_missing_prolog.csv')
self.assertFalse(rset)
def test_lcsv_missing_scheme(self):
sys.stdout = StringIO()
try:
with self.assertRaises(SystemExit):
self.run_import_skos('whatever', '--format', 'lcsv')
self.assertIn('command failed: --scheme option is required',
sys.stdout.getvalue())
finally:
sys.stdout = sys.__stdout__
if __name__ == '__main__':
......
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