[PATCH 2 of 2 saem_ref] [ark] Move ark utilities into their own module

Denis Laxalde denis.laxalde at logilab.fr
Fri Feb 16 10:07:51 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1518771123 -3600
#      Fri Feb 16 09:52:03 2018 +0100
# Node ID 968869971c49d8bdd9cc923581a05862d1335815
# Parent  ae9b438859eb66cd5a0edd29b5b98552c041f047
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 968869971c49
# EXP-Topic ark/naan
[ark] Move ark utilities into their own module

Along the way, we now expose a 'match' function in this module that is
used instead of ARK_RGX directly.

diff --git a/cubicweb_saem_ref/ark.py b/cubicweb_saem_ref/ark.py
new file mode 100644
--- /dev/null
+++ b/cubicweb_saem_ref/ark.py
@@ -0,0 +1,63 @@
+# copyright 2018 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr -- mailto:contact at logilab.fr
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# 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-saem-ref utilities for ARK identifiers"""
+
+import re
+
+
+ARK_PREFIX = 'rf'
+ARK_CONTROLCHAR = 'g'
+ARK_NAME_LENGTH = 10
+ARK_QUALIFIER_LENGTH = 10
+
+ARK_RGX = re.compile(
+    r'^(ark:/)?(?P<naan>\d+)/(?P<name>{prefix}\w{{{length}}}{controlchar})$'.format(
+        prefix=ARK_PREFIX,
+        length=ARK_NAME_LENGTH - len(ARK_PREFIX) - len(ARK_CONTROLCHAR),
+        controlchar=ARK_CONTROLCHAR,
+    ),
+)
+
+
+match = ARK_RGX.match
+
+
+def generate_ark(cnx, naan):
+    """Insert a record in "ark" table with "naan" value, a "name" generated
+    and an empty qualifier.
+
+    Return the generated name.
+    """
+    cu = cnx.system_sql(
+        'select * from gen_ark(%s, %s, %s, %s);',
+        (naan, ARK_NAME_LENGTH, ARK_PREFIX, ARK_CONTROLCHAR),
+    )
+    name, = cu.fetchone()
+    return name
+
+
+def generate_qualified_ark(cnx, naan, name):
+    """Insert a record in "ark" table with "naan" and "name" values and a
+    "qualifier" generated.
+
+    Return the generated qualifier.
+    """
+    cu = cnx.system_sql(
+        'select * from gen_qualified_ark(%s, %s, %s);',
+        (naan, name, ARK_QUALIFIER_LENGTH),
+    )
+    qualifier, = cu.fetchone()
+    return qualifier
diff --git a/cubicweb_saem_ref/entities/__init__.py b/cubicweb_saem_ref/entities/__init__.py
--- a/cubicweb_saem_ref/entities/__init__.py
+++ b/cubicweb_saem_ref/entities/__init__.py
@@ -15,27 +15,13 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-saem-ref entity's classes"""
 
-import re
-
 from logilab.common.decorators import monkeypatch
 
 from cubicweb.view import Adapter, EntityAdapter
 from cubicweb.predicates import match_kwargs, relation_possible, is_instance
 from cubicweb.entities import AnyEntity, fetch_config, authobjs, lib
 
-
-ARK_PREFIX = 'rf'
-ARK_CONTROLCHAR = 'g'
-ARK_NAME_LENGTH = 10
-ARK_QUALIFIER_LENGTH = 10
-
-ARK_RGX = re.compile(
-    r'^(ark:/)?(?P<naan>\d+)/(?P<name>{prefix}\w{{{length}}}{controlchar})$'.format(
-        prefix=ARK_PREFIX,
-        length=ARK_NAME_LENGTH - len(ARK_PREFIX) - len(ARK_CONTROLCHAR),
-        controlchar=ARK_CONTROLCHAR,
-    ),
-)
+from .. import ark
 
 
 class ARKIdentifierGeneratorMixin(object):
@@ -53,12 +39,7 @@ class ARKIdentifierGenerator(ARKIdentifi
     def assign_name(self):
         """Assign and return a new name part of the ARK identifier"""
         naan = int(self.cw_extra_kwargs['naa_what'])
-        cu = self._cw.system_sql(
-            'select * from gen_ark(%s, %s, %s, %s);',
-            (naan, ARK_NAME_LENGTH, ARK_PREFIX, ARK_CONTROLCHAR),
-        )
-        ark_name, = cu.fetchone()
-        return ark_name
+        return ark.generate_ark(self._cw, naan)
 
 
 class QualifiedARKIdentifierGenerator(ARKIdentifierGeneratorMixin, Adapter):
@@ -72,7 +53,7 @@ class QualifiedARKIdentifierGenerator(AR
         raise NotImplementedError
 
     def assign_name(self):
-        match = ARK_RGX.match(self.parent_entity.ark)
+        match = ark.match(self.parent_entity.ark)
         if not match:
             raise ValueError(
                 "ARK identifier for parent entity #%d looks malformattted: %s"
@@ -88,12 +69,8 @@ class QualifiedARKIdentifierGenerator(AR
                 % (self.parent_entity.eid, self.cw_extra_kwargs['naa_what'])
             )
         name = match.group('name')
-        cu = self._cw.system_sql(
-            'select * from gen_qualified_ark(%s, %s, %s);',
-            (naan, name, ARK_QUALIFIER_LENGTH),
-        )
-        ark_qualifier, = cu.fetchone()
-        return u'/'.join([name, ark_qualifier])
+        qualifier = ark.generate_qualified_ark(self._cw, naan, name)
+        return u'/'.join([name, qualifier])
 
 
 class ConceptARKIdentifierGenerator(QualifiedARKIdentifierGenerator):
diff --git a/test/testutils.py b/test/testutils.py
--- a/test/testutils.py
+++ b/test/testutils.py
@@ -32,11 +32,8 @@ from cubicweb import NoResultError, devt
 from cubicweb.devtools import testlib
 
 from cubicweb_seda import testutils as seda_testutils
-from cubicweb_saem_ref.entities import (
-    ARK_PREFIX,
-    ARK_CONTROLCHAR,
-    ARK_NAME_LENGTH,
-)
+from cubicweb_saem_ref import ark
+
 
 # make some names them appear in this module namespace to ease test API
 assertValidationError = seda_testutils.assertValidationError
@@ -124,25 +121,25 @@ def authority_with_naa(cnx, name=u'Defau
 
 ARK_URI_RGX = re.compile(
     r'^ark:/0/%s.{%d}%s$'
-    % (ARK_PREFIX,
-       ARK_NAME_LENGTH - len(ARK_PREFIX) - len(ARK_CONTROLCHAR),
-       ARK_CONTROLCHAR),
+    % (ark.ARK_PREFIX,
+       ark.ARK_NAME_LENGTH - len(ark.ARK_PREFIX) - len(ark.ARK_CONTROLCHAR),
+       ark.ARK_CONTROLCHAR),
 )
 
 
 ARK_RGX = re.compile(
     r'^0/%s.{%d}%s$'
-    % (ARK_PREFIX,
-       ARK_NAME_LENGTH - len(ARK_PREFIX) - len(ARK_CONTROLCHAR),
-       ARK_CONTROLCHAR),
+    % (ark.ARK_PREFIX,
+       ark.ARK_NAME_LENGTH - len(ark.ARK_PREFIX) - len(ark.ARK_CONTROLCHAR),
+       ark.ARK_CONTROLCHAR),
 )
 
 
 ARK_NAME_RGX = re.compile(
     r'^%s.{%d}%s$'
-    % (ARK_PREFIX,
-       ARK_NAME_LENGTH - len(ARK_PREFIX) - len(ARK_CONTROLCHAR),
-       ARK_CONTROLCHAR),
+    % (ark.ARK_PREFIX,
+       ark.ARK_NAME_LENGTH - len(ark.ARK_PREFIX) - len(ark.ARK_CONTROLCHAR),
+       ark.ARK_CONTROLCHAR),
 )
 
 
diff --git a/test/unittest_entities.py b/test/unittest_entities.py
--- a/test/unittest_entities.py
+++ b/test/unittest_entities.py
@@ -19,9 +19,9 @@ from unittest import TestCase
 from cubicweb.devtools import PostgresApptestConfiguration
 from cubicweb.devtools.testlib import CubicWebTC
 
-from cubicweb_saem_ref.entities import (
-    ARK_RGX,
+from cubicweb_saem_ref.ark import (
     ARK_QUALIFIER_LENGTH,
+    match as ark_match,
 )
 import testutils
 
@@ -33,13 +33,13 @@ def setUpModule():
 class ArkRgxTC(TestCase):
 
     def test(self):
-        match = ARK_RGX.match('ark:/12345/rf%sg' % ('q' * 7))
+        match = ark_match('ark:/12345/rf%sg' % ('q' * 7))
         self.assertTrue(match)
         self.assertEqual(match.group('naan'), '12345')
         self.assertEqual(match.group('name'), 'rfqqqqqqqg')
 
     def test_no_scheme(self):
-        match = ARK_RGX.match('12345/rf%sg' % ('q' * 7))
+        match = ark_match('12345/rf%sg' % ('q' * 7))
         self.assertTrue(match)
         self.assertEqual(match.group('naan'), '12345')
         self.assertEqual(match.group('name'), 'rfqqqqqqqg')



More information about the saem-devel mailing list