[PATCH 2 of 3 saem_ref] Let --scheme argument in LCSV command import be an ARK identifier

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


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1517913158 -3600
#      Tue Feb 06 11:32:38 2018 +0100
# Node ID ec84ee304378e6296f522f1e5718f2e8c000b1be
# Parent  8e7459b4a29c4218c382ce17fda0dc2d493b979e
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r ec84ee304378
# EXP-Topic lcsv-import
Let --scheme argument in LCSV command import be an ARK identifier

This is more meaningful in SAEM where everything has an ARK.

We thus change the help of the option in cubicweb-skos's command and
tweak a bit more the monkeypatch.

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
@@ -66,6 +66,26 @@ def _skos_drop_rql_store():
 del _skos_drop_rql_store
 
 
+def _skos_reword_scheme_option():
+    """Change "--scheme" option help of skos-import command to match SAEM
+    semantics (which relies on ARK instead of bare URI).
+    """
+    for name, value in skos.ImportSkosData.options:
+        if name == 'scheme':
+            value['help'] = (
+                'ARK identifier of an existing concept scheme to import concepts in '
+                '(only relevant for LCSV import format)'
+            )
+            break
+    else:
+        raise AssertionError(
+            'Could not find "scheme" option in ImportSkosData ccplugin command')
+
+
+_skos_reword_scheme_option()
+del _skos_reword_scheme_option
+
+
 def _enc(string):
     if PY2 and not isinstance(string, str):
         string = string.encode('utf-8')
@@ -119,17 +139,19 @@ def run(self, args):
 @monkeypatch(skos.ImportSkosData)  # noqa: F811
 def run(self, args):
     if self.get('format') == 'lcsv':
-        scheme_uri = self.get('scheme')
-        if not scheme_uri:
+        scheme_ark = self.get('scheme')
+        if not scheme_ark:
             print(u'command failed: --scheme option is required for LCSV import')
             sys.exit(1)
-        scheme_uri = scheme_uri.strip().decode('utf-8')
+        scheme_ark = scheme_ark.strip().decode('utf-8')
         appid = args[0]
         with self.get_cnx(appid) as cnx:
-            rset = cnx.find('ConceptScheme', cwuri=scheme_uri)
+            if scheme_ark.startswith(u'ark:/'):
+                scheme_ark = scheme_ark[len(u'ark:/'):]
+            rset = cnx.find('ConceptScheme', ark=scheme_ark)
             if not rset:
                 print(u'command failed: no concept scheme found matching "%s"'
-                      % scheme_uri)
+                      % scheme_ark)
                 sys.exit(1)
             scheme = rset.one()
             # cubicweb-skos's command need a cwuri for --scheme.
@@ -137,7 +159,7 @@ def run(self, args):
             # 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)
+                      % scheme_ark)
                 sys.exit(1)
             naa_what = scheme.ark_naa[0].what
         # Set a "config" attribute which will be retrieved from metadata
diff --git a/test/test_ccplugin.py b/test/test_ccplugin.py
--- a/test/test_ccplugin.py
+++ b/test/test_ccplugin.py
@@ -129,7 +129,7 @@ class ImportSkosDataCommandTC(CCPluginTC
         code, output = self.run_import_skos(
             self.datapath('lcsv_example_shortened.csv'),
             '--format', 'lcsv',
-            '--scheme', scheme_uri,
+            '--scheme', scheme.ark,
         )
         self.assertEqual(code, 0, output)
         with self.admin_access.cnx() as cnx:
@@ -141,12 +141,11 @@ class ImportSkosDataCommandTC(CCPluginTC
             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(
             self.datapath('lcsv_example_shortened.csv'),
             '--format', 'lcsv',
-            '--scheme', scheme_uri,
+            '--scheme', '000/123',
         )
         self.assertEqual(code, 1, output)
         self.assertIn('command failed: specified concept scheme', output)



More information about the saem-devel mailing list