lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.13-15-g60139a8

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

python-stdnum branch master updated. 1.13-15-g60139a8



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  60139a8f862f8a40bed2ed746c4402366f238aff (commit)
      from  ebe7e1012ddc1286d61de5c5a565aff9cd4faedf (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=60139a8f862f8a40bed2ed746c4402366f238aff

commit 60139a8f862f8a40bed2ed746c4402366f238aff
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sat Mar 7 23:07:25 2020 +0100

    Add Bellarus УНП number
    
    This also adds a function to do an online lookup of the number.
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/196

diff --git a/stdnum/by/__init__.py b/stdnum/by/__init__.py
new file mode 100644
index 0000000..893f386
--- /dev/null
+++ b/stdnum/by/__init__.py
@@ -0,0 +1,24 @@
+# __init__.py - collection of Belarusian numbers
+# coding: utf-8
+#
+# Copyright (C) 2020 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
+
+"""Collection of Belarusian numbers."""
+
+# provide aliases
+from stdnum.by import unp as vat  # noqa: F401
diff --git a/stdnum/by/portal.nalog.gov.by.crt 
b/stdnum/by/portal.nalog.gov.by.crt
new file mode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u/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq
+OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b
+xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw
+7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD
+aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV
+HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG
+SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69
+ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr
+AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz
+R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5
+JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo
+Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ
+-----END CERTIFICATE-----
diff --git a/stdnum/by/unp.py b/stdnum/by/unp.py
new file mode 100644
index 0000000..f7f144a
--- /dev/null
+++ b/stdnum/by/unp.py
@@ -0,0 +1,129 @@
+# unp.py - functions for handling Belarusian UNP numbers
+# coding: utf-8
+#
+# Copyright (C) 2020 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
+
+"""УНП, UNP (Учетный номер плательщика, the Bellarus VAT number).
+
+The УНП (UNP) or Учетный номер плательщика (Uchetniy nomer platel'shika,
+Payer account number) is issued to organisations and individuals for tax
+purposes. The number consists of 9 digits (numeric for organisations,
+alphanumeric for individuals) and contains a region identifier, a serial per
+region and a check digit.
+
+More information:
+
+* https://be.wikipedia.org/wiki/Уліковы_нумар_плацельшчыка
+* http://pravo.levonevsky.org/bazaby09/sbor37/text37892/index3.htm
+
+>>> validate('200988541')
+'200988541'
+>>> validate('УНП MA1953684')
+'MA1953684'
+>>> validate('200988542')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+"""
+
+from stdnum.exceptions import *
+from stdnum.util import clean, isdigits, to_unicode
+
+
+# Mapping of Cyrillic letters to Latin letters
+_cyrillic_to_latin = dict(zip(
+    u'АВЕКМНОРСТ',
+    u'ABEKMHOPCT',
+))
+
+
+def compact(number):
+    """Convert the number to the minimal representation. This strips the
+    number of any valid separators and removes surrounding whitespace."""
+    number = clean(number, ' ').upper().strip()
+    for prefix in ('УНП', u'УНП', 'UNP', u'UNP'):
+        if type(number) == type(prefix) and number.startswith(prefix):
+            number = number[len(prefix):]
+    # Replace Cyrillic letters with Latin letters
+    cleaned = ''.join(_cyrillic_to_latin.get(x, x) for x in to_unicode(number))
+    if type(cleaned) != type(number):  # pragma: no cover (Python2 only)
+        cleaned = cleaned.encode('utf-8')
+    return cleaned
+
+
+def calc_check_digit(number):
+    """Calculate the check digit for the number."""
+    number = compact(number)
+    alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+    weights = (29, 23, 19, 17, 13, 7, 5, 3)
+    if not isdigits(number):
+        number = number[0] + str('ABCEHKMOPT'.index(number[1])) + number[2:]
+    c = sum(w * alphabet.index(n) for w, n in zip(weights, number)) % 11
+    if c > 9:
+        raise InvalidChecksum()
+    return str(c)
+
+
+def validate(number):
+    """Check if the number is a valid number. This checks the length,
+    formatting and check digit."""
+    number = compact(number)
+    if len(number) != 9:
+        raise InvalidLength()
+    if not isdigits(number[2:]):
+        raise InvalidFormat()
+    if not isdigits(number[:2]) and not all(x in 'ABCEHKMOPT' for x in 
number[:2]):
+        raise InvalidFormat()
+    if number[0] not in '1234567ABCEHKM':
+        raise InvalidComponent()
+    if number[-1] != calc_check_digit(number):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid number."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
+
+
+def check_nalog(number, timeout=30):  # pragma: no cover (not part of normal 
test suite)
+    """Retrieve registration information from the portal.nalog.gov.by web site.
+
+    This basically returns the JSON response from the web service as a dict.
+    Will return ``None`` if the number is invalid or unknown.
+    """
+    # this function isn't automatically tested because it would require
+    # network access for the tests and unnecessarily load the web service
+    import requests
+    from pkg_resources import resource_filename
+    # Since the nalog.gov.by web site currently provides an incomplete
+    # certificate chain, we provide our own.
+    certificate = resource_filename(__name__, 'portal.nalog.gov.by.crt')
+    response = requests.get(
+        'https://www.portal.nalog.gov.by/grp/getData',
+        params={
+            'unp': compact(number),
+            'charset': 'UTF-8',
+            'type': 'json'},
+        timeout=timeout,
+        verify=certificate)
+    if response.ok:
+        return response.json()['ROW']
diff --git a/tests/test_by_unp.doctest b/tests/test_by_unp.doctest
new file mode 100644
index 0000000..6c2b8a4
--- /dev/null
+++ b/tests/test_by_unp.doctest
@@ -0,0 +1,97 @@
+test_by_unp.doctest - more detailed doctests for stdnum.by.unp module
+
+Copyright (C) 2020 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.by.unp. It
+tries to cover more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum.by import unp
+>>> from stdnum.exceptions import *
+
+
+Tests for some corner cases.
+
+>>> unp.validate('591705582')
+'591705582'
+>>> unp.validate('УНП 591705582')
+'591705582'
+>>> str(unp.validate(u'\u0423\u041d\u041f 591705582'))
+'591705582'
+>>> unp.validate('UNP 591705582')
+'591705582'
+>>> unp.validate('5917DDD82')  # letters in wrong places
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> unp.validate('991705588')  # first digit is unknown region
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> unp.validate('M01953684')  # if first digit is letter, so should the second
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> unp.validate('7A1953689')  # if second digit is letter, so should the first
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> unp.validate('МА1953684')  # Cyrillic letters will be translated to Latin
+'MA1953684'
+
+
+The check digit cannot be 10:
+
+>>> unp.calc_check_digit('711953681')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 100217336
+... 100693551
+... 100864035
+... 101288529
+... 101541947
+... 190658169
+... 190960352
+... 191453533
+... 191682495
+... 191767445
+... 191827058
+... 192035780
+... 192345153
+... 192988109
+... 200019773
+... 290380347
+... 400051902
+... 490127567
+... 500037201
+... 591705582
+... 690314863
+... 691631805
+... 791022114
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not unp.is_valid(x)]
+[]
diff --git a/tests/test_by_unp.py b/tests/test_by_unp.py
new file mode 100644
index 0000000..229b034
--- /dev/null
+++ b/tests/test_by_unp.py
@@ -0,0 +1,63 @@
+# test_by_unp.py - online validation tests
+# coding: utf-8
+#
+# Copyright (C) 2020 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 is a separate test file because it should not be run regularly
+# because it could negatively impact the online service.
+
+"""Extra tests for the stdnum.by.unp module."""
+
+import os
+import unittest
+
+from stdnum.by import unp
+
+
+@unittest.skipIf(
+    not os.environ.get('ONLINE_TESTS'),
+    'Do not overload online services')
+class TestNalog(unittest.TestCase):
+    """Test the web services provided by the portal.nalog.gov.by web site."""
+
+    def test_check_nalog(self):
+        """Test stdnum.by.unp.check_nalog()"""
+        # Test a normal valid number
+        result = unp.check_nalog('191682495')
+        self.assertDictEqual(
+            result,
+            {
+                'CKODSOST': '1',
+                'DLIKV': None,
+                'DREG': '08.07.2011',
+                'NMNS': '104',
+                'VKODS': 'Действующий',
+                'VLIKV': None,
+                'VMNS': 'Инспекция МНС по Московскому району г.Минска ',
+                'VNAIMK': 'Частное предприятие "КРИОС ГРУПП"',
+                'VNAIMP': 'Частное производственное унитарное предприятие 
"КРИОС ГРУПП"',
+                'VPADRES': 'г. Минск,ул. Уманская, д.54, пом. 152',
+                'VUNP': '191682495',
+            })
+        # Check that result has at least these keys
+        keys = ['VUNP', 'VNAIMP', 'VNAIMK', 'DREG', 'CKODSOST', 'VKODS']
+        self.assertEqual([key for key in keys if key in result], keys)
+        self.assertEqual(result['VUNP'], '191682495')
+        # Test invalid number
+        result = unp.check_nalog('771681495')
+        self.assertIsNone(result)

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

Summary of changes:
 stdnum/{ve => by}/__init__.py     |   8 +--
 stdnum/by/portal.nalog.gov.by.crt |  47 ++++++++++++++
 stdnum/by/unp.py                  | 129 ++++++++++++++++++++++++++++++++++++++
 tests/test_by_unp.doctest         |  97 ++++++++++++++++++++++++++++
 tests/test_by_unp.py              |  63 +++++++++++++++++++
 5 files changed, 340 insertions(+), 4 deletions(-)
 copy stdnum/{ve => by}/__init__.py (81%)
 create mode 100644 stdnum/by/portal.nalog.gov.by.crt
 create mode 100644 stdnum/by/unp.py
 create mode 100644 tests/test_by_unp.doctest
 create mode 100644 tests/test_by_unp.py


hooks/post-receive
-- 
python-stdnum