lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.16-17-g36d723c

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 1.16-17-g36d723c



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  36d723ce54dc0a9e2404e24b55311321367786af (commit)
      from  48bfd84b02a09e014e01090b37fe188dc0ac895b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=36d723ce54dc0a9e2404e24b55311321367786af

commit 36d723ce54dc0a9e2404e24b55311321367786af
Author: Nuno André <mail@nunoand.re>
Date:   Wed Mar 10 10:56:53 2021 +0100

    Add ISRC (International Standard Recording Code)
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/261

diff --git a/stdnum/isrc.py b/stdnum/isrc.py
new file mode 100644
index 0000000..7a2592c
--- /dev/null
+++ b/stdnum/isrc.py
@@ -0,0 +1,93 @@
+# isrc.py - functions for International Standard Recording Codes (ISRC)
+# coding: utf-8
+#
+# Copyright (C) 2021 Nuno André Novo
+# Copyright (C) 2021 Arthur de Jong
+#
+# This library 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 library 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 library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""ISRC (International Standard Recording Code).
+
+The ISRC is an international standard code (ISO 3901) for uniquely
+identifying sound recordings and music video recordings.
+
+More information:
+
+* https://en.wikipedia.org/wiki/International_Standard_Recording_Code
+
+>>> validate('US-SKG-19-12345')
+'USSKG1912345'
+>>> validate('XX-SKG-19-12345')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+"""
+
+import re
+
+from stdnum.exceptions import *
+from stdnum.isin import _iso_3116_1_country_codes
+from stdnum.util import clean
+
+
+# An ISRC is composed of a country code, a registrant code a year of
+# reference and designation code.
+_isrc_re = re.compile(
+    
r'^(?P<country>[A-Z]{2})(?P<registrant>[A-Z0-9]{3})(?P<year>[0-9]{2})(?P<record>[0-9]{5})$')
+
+
+# These special codes are allowed for ISRC
+_country_codes = set(_iso_3116_1_country_codes + [
+    'QM',  # US new registrants due to US codes became exhausted
+    'CP',  # reserved for further overflow
+    'DG',  # reserved for further overflow
+    'ZZ',  # International ISRC Agency codes
+])
+
+
+def compact(number):
+    """Convert the ISRC to the minimal representation. This strips the
+    number of any valid separators and removes surrounding whitespace."""
+    return clean(number, ' -').strip().upper()
+
+
+def validate(number):
+    """Check if the number provided is a valid ISRC. This checks the length,
+    the alphabet, and the country code but does not check if the registrant
+    code is known."""
+    number = compact(number)
+    if len(number) != 12:
+        raise InvalidLength()
+    match = _isrc_re.search(number)
+    if not match:
+        raise InvalidFormat()
+    if match.group('country') not in _country_codes:
+        raise InvalidComponent()
+    return number
+
+
+def is_valid(number):
+    """Check if the number provided is a valid ISRC."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
+
+
+def format(number, separator='-'):
+    """Reformat the number to the standard presentation format."""
+    number = compact(number)
+    return separator.join((number[0:2], number[2:5], number[5:7], number[7:]))
diff --git a/tests/test_isrc.doctest b/tests/test_isrc.doctest
new file mode 100644
index 0000000..bebe0c4
--- /dev/null
+++ b/tests/test_isrc.doctest
@@ -0,0 +1,58 @@
+test_isrc.doctest - more detailed doctests for stdnum.isrc module
+
+Copyright (C) 2021 Nuno André Novo
+Copyright (C) 2021 Arthur de Jong
+
+This library 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 library 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 library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA
+
+
+This file contains more detailed doctests for the stdnum.isrc module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import isrc
+
+
+These are normal variations that should just work.
+
+>>> isrc.validate('US-SKG-19-12345')
+'USSKG1912345'
+>>> isrc.validate('USSKG1912345')
+'USSKG1912345'
+>>> isrc.validate('us-skg1912345')
+'USSKG1912345'
+
+
+Tests for mangling and incorrect country codes.
+
+>>> isrc.validate('US-SKG-19-123456')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> isrc.validate('US-SKG-19-1234*')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> isrc.validate('XX-SKG-19-12345')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+
+
+Formatting tests.
+
+>>> isrc.format('USSKG1912345')
+'US-SKG-19-12345'

-----------------------------------------------------------------------

Summary of changes:
 stdnum/isrc.py                                | 93 +++++++++++++++++++++++++++
 tests/{test_ean.doctest => test_isrc.doctest} | 43 +++++++++----
 2 files changed, 122 insertions(+), 14 deletions(-)
 create mode 100644 stdnum/isrc.py
 copy tests/{test_ean.doctest => test_isrc.doctest} (57%)


hooks/post-receive
-- 
python-stdnum