[PATCH 3 of 5 saem_ref] Add a regexp to match (unqualified) ARK identifiers

Denis Laxalde denis.laxalde at logilab.fr
Wed Feb 14 17:26:10 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1518449271 -3600
#      Mon Feb 12 16:27:51 2018 +0100
# Node ID f584d40d5f5a3caeded786b72d831f068a1ca00d
# Parent  9f222afb9e9f2e5feb6d54e4b6a2a539806722c1
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r f584d40d5f5a
# EXP-Topic ark/qualifiers
Add a regexp to match (unqualified) ARK identifiers

We only want to match ark:/<NAAN>/<name> identifiers without a
qualifier. This will be useful when building qualified ARK identifiers
from a base identifier parsed using this regexp.

Related to extranet #46880051.

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,6 +15,8 @@
 # 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
@@ -26,6 +28,14 @@ ARK_PREFIX = 'rf'
 ARK_CONTROLCHAR = 'g'
 ARK_NAME_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,
+    ),
+)
+
 
 class ARKIdentifierGenerator(Adapter):
     """Adapter for ARK unique identifier generation"""
diff --git a/test/unittest_entities.py b/test/unittest_entities.py
--- a/test/unittest_entities.py
+++ b/test/unittest_entities.py
@@ -14,9 +14,12 @@
 # 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/>.
 
+from unittest import TestCase
+
 from cubicweb.devtools import PostgresApptestConfiguration
 from cubicweb.devtools.testlib import CubicWebTC
 
+from cubicweb_saem_ref.entities import ARK_RGX
 import testutils
 
 
@@ -24,6 +27,21 @@ def setUpModule():
     testutils.startpgcluster(__file__)
 
 
+class ArkRgxTC(TestCase):
+
+    def test(self):
+        match = ARK_RGX.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))
+        self.assertTrue(match)
+        self.assertEqual(match.group('naan'), '12345')
+        self.assertEqual(match.group('name'), 'rfqqqqqqqg')
+
+
 class ArkGeneratorTC(CubicWebTC):
 
     configcls = PostgresApptestConfiguration



More information about the saem-devel mailing list