lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.13-44-gff188bd

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

python-stdnum branch master updated. 1.13-44-gff188bd



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  ff188bd5f100c3033b3fc53f186af6c52815a770 (commit)
      from  b6e43cdd6c137acf57193b749d5ed8950eaf2103 (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=ff188bd5f100c3033b3fc53f186af6c52815a770

commit ff188bd5f100c3033b3fc53f186af6c52815a770
Author: Leandro Regueiro <leandro.regueiro@gmail.com>
Date:   Sun Mar 8 17:06:25 2020 +0100

    Add module to check any VAT number
    
    This effectively mimics vatnumber's `check_vat` function, so people can
    easily replace the outdated vatnumber library with stdnum.
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/199

diff --git a/stdnum/vatin.py b/stdnum/vatin.py
new file mode 100644
index 0000000..50f587c
--- /dev/null
+++ b/stdnum/vatin.py
@@ -0,0 +1,94 @@
+# vatin.py - function to validate any given VATIN.
+#
+# Copyright (C) 2020 Leandro Regueiro
+#
+# 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
+
+"""VATIN (International value added tax identification number)
+
+The number VAT identification number (VATIN) is an identifier used in many
+countries. It starts with an ISO 3166-1 alpha-2 (2 letters) country code
+(except for Greece, which uses EL, instead of GR) and is followed by the
+country-specific the identifier.
+
+This module supports all VAT numbers that are supported in python-stdnum.
+
+More information:
+
+* https://en.wikipedia.org/wiki/VAT_identification_number
+
+>>> validate('FR 40 303 265 045')
+'FR40303265045'
+>>> validate('DE136,695 976')
+'DE136695976'
+>>> validate('BR16.727.230/0001-97')
+'BR16727230000197'
+>>> validate('FR 40 303')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> validate('XX')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+"""
+
+import re
+
+from stdnum.exceptions import *
+from stdnum.util import clean, get_cc_module
+
+
+# Cache of country code modules
+_country_modules = dict()
+
+
+def _get_cc_module(cc):
+    """Get the VAT number module based on the country code."""
+    # Greece uses a "wrong" country code
+    cc = cc.lower().replace('el', 'gr')
+    if not re.match(r'^[a-z]{2}$', cc):
+        raise InvalidFormat()
+    if cc not in _country_modules:
+        _country_modules[cc] = get_cc_module(cc, 'vat')
+    if not _country_modules[cc]:
+        raise InvalidComponent()  # unknown/unsupported country code
+    return _country_modules[cc]
+
+
+def compact(number):
+    """Convert the number to the minimal representation."""
+    number = clean(number).strip()
+    module = _get_cc_module(number[:2])
+    return number[:2] + module.compact(number[2:])
+
+
+def validate(number):
+    """Check if the number is a valid VAT number.
+
+    This performs the country-specific check for the number.
+    """
+    number = clean(number, '').strip()
+    module = _get_cc_module(number[:2])
+    return number[:2].upper() + module.validate(number[2:])
+
+
+def is_valid(number):
+    """Check if the number is a valid VAT number."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_vatin.doctest b/tests/test_vatin.doctest
new file mode 100644
index 0000000..f0c5f92
--- /dev/null
+++ b/tests/test_vatin.doctest
@@ -0,0 +1,75 @@
+test_vatin.doctest - more detailed doctests for stdnum.vatin module
+
+Copyright (C) 2020 Leandro Regueiro
+
+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.vatin module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import vatin
+
+
+Check valid VAT numbers for several countries with existing validation:
+
+>>> vatin.validate('FR 40 303 265 045')
+'FR40303265045'
+>>> vatin.validate('DE136,695 976')
+'DE136695976'
+>>> vatin.validate('BR16.727.230/0001-97')
+'BR16727230000197'
+>>> vatin.validate('el-082857563')
+'EL082857563'
+
+
+Try validating invalid VAT numbers for country with validation:
+
+>>> vatin.compact('FR 40 303')
+'FR40303'
+>>> vatin.validate('FR 40 303')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> vatin.validate('FR')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+
+
+Try validating not specifying a country:
+
+>>> vatin.validate('')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> vatin.validate('00')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+
+
+Try to validate for countries with no VAT validation:
+
+>>> vatin.validate('XX')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> vatin.validate('US')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...

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

Summary of changes:
 stdnum/vatin.py          | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_vatin.doctest | 75 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 169 insertions(+)
 create mode 100644 stdnum/vatin.py
 create mode 100644 tests/test_vatin.doctest


hooks/post-receive
-- 
python-stdnum