[PATCH 1 of 3 saem_ref] Determine ARK NAA from concept scheme in LCSV command-line import

Denis Laxalde denis.laxalde at logilab.fr
Tue Feb 6 11:34:15 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1517861875 -3600
#      Mon Feb 05 21:17:55 2018 +0100
# Node ID 8e7459b4a29c4218c382ce17fda0dc2d493b979e
# Parent  efb428324c9ccd7658beefa40db97ffa3fe33568
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 8e7459b4a29c
# EXP-Topic lcsv-import
Determine ARK NAA from concept scheme in LCSV command-line import

Following up on 311c3c0222ce, we can actually determine the
ArkNameAssigningAuthority to use for generating ARK from the concept
scheme to import concepts in. This is certainly more convenient to the
end-user as this is one less command line parameter required.

We need a draft changeset from cubicweb-skos to have the "get_cnx"
method in ImportSkosData.

Related to extranet #44146518.

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
@@ -43,15 +43,6 @@ 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.
 
@@ -127,7 +118,29 @@ def run(self, args):
 
 @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)
+    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')
+            sys.exit(1)
+        scheme_uri = scheme_uri.strip().decode('utf-8')
+        appid = args[0]
+        with self.get_cnx(appid) as cnx:
+            rset = cnx.find('ConceptScheme', cwuri=scheme_uri)
+            if not rset:
+                print(u'command failed: no concept scheme found matching "%s"'
+                      % scheme_uri)
+                sys.exit(1)
+            scheme = rset.one()
+            # cubicweb-skos's command need a cwuri for --scheme.
+            setattr(self.config, 'scheme', scheme.cwuri)
+            # Per schema, ark_naa is optional for ConceptScheme.
+            if not scheme.ark_naa:
+                print(u'command failed: specified concept scheme "%s" must have an ARK NAA set'
+                      % scheme_uri)
+                sys.exit(1)
+            naa_what = scheme.ark_naa[0].what
+        # Set a "config" attribute which will be retrieved from metadata
+        # generator defined in __init__.py.
+        setattr(self.config, 'naa_what', naa_what)
     return _orig_run(self, args)
diff --git a/dev-requirements.txt b/dev-requirements.txt
--- a/dev-requirements.txt
+++ b/dev-requirements.txt
@@ -1,3 +1,4 @@
 mock
 pytest
 webtest
+http://hg.logilab.org/review/cubes/skos/archive/f98a95bcd195.tar.bz2#egg=cubicweb-skos
diff --git a/test/test_ccplugin.py b/test/test_ccplugin.py
--- a/test/test_ccplugin.py
+++ b/test/test_ccplugin.py
@@ -119,16 +119,17 @@ class ImportSkosDataCommandTC(CCPluginTC
 
     def test_lcsv(self):
         with self.admin_access.cnx() as cnx:
+            naa = testutils.naa(cnx)
             scheme = cnx.create_entity('ConceptScheme', title=u'lcsv',
-                                       ark=u'123/345')
-            naa_what = testutils.naa(cnx).what
+                                       ark_naa=naa)
             cnx.commit()
             scheme_uri = scheme.cwuri
+            assert scheme.ark_naa
+            assert scheme.ark
         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:
@@ -136,13 +137,19 @@ class ImportSkosDataCommandTC(CCPluginTC
             self.assertEqual(len(scheme.reverse_in_scheme), 3)
 
     def test_lcsv_missing_naa_what(self):
+        with self.admin_access.cnx() as cnx:
+            scheme = cnx.create_entity('ConceptScheme', title=u'lcsv',
+                                       ark=u'000/123', cwuri=u'123')
+            cnx.commit()
+            scheme_uri = scheme.cwuri
+            assert not scheme.ark_naa
         code, output = self.run_import_skos(
-            'whatever',
+            self.datapath('lcsv_example_shortened.csv'),
             '--format', 'lcsv',
-            '--scheme', 'whatever',
+            '--scheme', scheme_uri,
         )
-        self.assertEqual(code, 1)
-        self.assertIn('command failed: --naa-what option is required', output)
+        self.assertEqual(code, 1, output)
+        self.assertIn('command failed: specified concept scheme', output)
 
 
 if __name__ == '__main__':



More information about the saem-devel mailing list