[PATCH 5 of 5 saem_ref v2] Add a "naa-what" option to "skos-import" command

Denis Laxalde denis.laxalde at logilab.fr
Mon Jan 8 15:06:01 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1515419032 -3600
#      Mon Jan 08 14:43:52 2018 +0100
# Node ID 6a880b35214b33cf806c5cda5d4bd93b1ea1ed73
# Parent  53cbebb6a3f7ef27bb61fa8e8c98a49a5c475ab4
# EXP-Topic lcsv-skos-import
Add a "naa-what" option to "skos-import" command

This is required in LCSV import format in particular to allow imported
concepts to be assigned an ARK.

Closes Extranet #44146518.

diff --git a/cubicweb_saem_ref/__init__.py b/cubicweb_saem_ref/__init__.py
--- a/cubicweb_saem_ref/__init__.py
+++ b/cubicweb_saem_ref/__init__.py
@@ -66,13 +66,17 @@ def includeme(config):
     config.include('.pviews')
 
 
+def _metagen(cnx, config):
+    from .sobjects import SAEMMetadataGenerator
+    naa_what = getattr(config, 'naa_what', None)
+    return SAEMMetadataGenerator(cnx, naa_what=naa_what)
+
+
 def _massive_store_factory(cnx, config, **kwargs):
     from cubicweb.dataimport.massive_store import MassiveObjectStore
-    from .sobjects import SAEMMetadataGenerator
-    return MassiveObjectStore(cnx, metagen=SAEMMetadataGenerator(cnx), **kwargs)
+    return MassiveObjectStore(cnx, metagen=_metagen(cnx, config), **kwargs)
 
 
 def _nohook_store_factory(cnx, config):
     from cubicweb.dataimport.stores import NoHookRQLObjectStore
-    from .sobjects import SAEMMetadataGenerator
-    return NoHookRQLObjectStore(cnx, metagen=SAEMMetadataGenerator(cnx))
+    return NoHookRQLObjectStore(cnx, metagen=_metagen(cnx, config))
diff --git a/cubicweb_saem_ref/ccplugin.py b/cubicweb_saem_ref/ccplugin.py
--- a/cubicweb_saem_ref/ccplugin.py
+++ b/cubicweb_saem_ref/ccplugin.py
@@ -13,7 +13,7 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
-"""cubicweb-ctl plugin customizing eac-import command."""
+"""cubicweb-ctl plugin customizing data import commands."""
 from __future__ import print_function
 
 import sys
@@ -43,6 +43,15 @@ eac.ImportEacData.options = (
 )
 
 
+skos.ImportSkosData.options = skos.ImportSkosData.options + (
+    ("naa-what", {
+        'type': 'string',
+        'help': ('"what" number of the Name Assigning Authority to import '
+                 'concept through (only relevant for LSCV import format)'),
+    }),
+)
+
+
 def _skos_drop_rql_store():
     """Remove "rql" store from ImportSkosData command.
 
@@ -111,3 +120,14 @@ def run(self, args):
     finally:
         if repo is not None:
             repo.shutdown()
+
+
+_orig_run = skos.ImportSkosData.run
+
+
+ at monkeypatch(skos.ImportSkosData)  # noqa: F811
+def run(self, args):
+    if self.get('format') == 'lcsv' and self.get('naa-what') is None:
+        print(u'command failed: --naa-what option is required for LCSV import format')
+        sys.exit(1)
+    return _orig_run(self, args)
diff --git a/test/test_ccplugin.py b/test/test_ccplugin.py
--- a/test/test_ccplugin.py
+++ b/test/test_ccplugin.py
@@ -16,7 +16,7 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
 import sys
-from io import BytesIO
+from io import BytesIO, StringIO
 
 from cubicweb.devtools import testlib
 from cubicweb.cwconfig import CubicWebConfiguration
@@ -28,6 +28,8 @@ except ImportError as exc:
 else:
     from cubicweb_eac import ccplugin
 
+from cubes.skos import ccplugin as skos
+
 import testutils
 
 
@@ -100,6 +102,49 @@ class ImportEacDataCommandTC(CCPluginTC)
         self.assertIn('not associated to a ARK naming authority', output)
 
 
+class ImportSkosDataCommandTC(CCPluginTC):
+
+    def run_import_skos(self, *args):
+        cmd = [self.appid] + list(args)
+        sys.stdout = output = StringIO()
+        try:
+            skos.ImportSkosData(None).main_run(cmd)
+        except SystemExit as exc:
+            code = exc.code
+        else:
+            code = 0
+        finally:
+            sys.stdout = sys.__stdout__
+        return code, output.getvalue()
+
+    def test_lcsv(self):
+        with self.admin_access.cnx() as cnx:
+            scheme = cnx.create_entity('ConceptScheme', title=u'lcsv',
+                                       ark=u'123/345')
+            naa_what = testutils.naa(cnx).what
+            cnx.commit()
+            scheme_uri = scheme.cwuri
+        code, output = self.run_import_skos(
+            self.datapath('lcsv_example_shortened.csv'),
+            '--format', 'lcsv',
+            '--scheme', scheme_uri,
+            '--naa-what', naa_what,
+        )
+        self.assertEqual(code, 0, output)
+        with self.admin_access.cnx() as cnx:
+            scheme = cnx.find('ConceptScheme', cwuri=scheme_uri).one()
+            self.assertEqual(len(scheme.reverse_in_scheme), 3)
+
+    def test_lcsv_missing_naa_what(self):
+        code, output = self.run_import_skos(
+            'whatever',
+            '--format', 'lcsv',
+            '--scheme', 'whatever',
+        )
+        self.assertEqual(code, 1)
+        self.assertIn('command failed: --naa-what option is required', output)
+
+
 if __name__ == '__main__':
     from unittest import main
     main()


More information about the saem-devel mailing list