From b561d59a0a456d59b824fd3ca22f8868f0955a84 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Sun, 19 Feb 2012 09:55:51 +0000 Subject: add a VAT (European Union VAT number) module git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@155 9dea7c4f-944c-4273-ac1a-574ede026edc --- stdnum/__init__.py | 1 + stdnum/eu/__init__.py | 21 +++++++++++++ stdnum/eu/vat.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 stdnum/eu/__init__.py create mode 100644 stdnum/eu/vat.py (limited to 'stdnum') diff --git a/stdnum/__init__.py b/stdnum/__init__.py index c1b0570..beac0e5 100644 --- a/stdnum/__init__.py +++ b/stdnum/__init__.py @@ -74,6 +74,7 @@ Currently this package supports the following formats: * VAT (Moms, Mervärdesskatt, Swedish VAT number) * VAT (United Kingdom (and Isle of Man) VAT registration number) * VAT (Идентификационен номер по ДДС, Bulgarian VAT number) + * VAT (European Union VAT number) * IMEI (International Mobile Equipment Identity) * IMSI (International Mobile Subscriber Identity) * MEID (Mobile Equipment Identifier) diff --git a/stdnum/eu/__init__.py b/stdnum/eu/__init__.py new file mode 100644 index 0000000..a00e832 --- /dev/null +++ b/stdnum/eu/__init__.py @@ -0,0 +1,21 @@ +# __init__.py - collection of European Union numbers +# coding: utf-8 +# +# Copyright (C) 2012 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 European Union numbers.""" diff --git a/stdnum/eu/vat.py b/stdnum/eu/vat.py new file mode 100644 index 0000000..a408b8f --- /dev/null +++ b/stdnum/eu/vat.py @@ -0,0 +1,85 @@ +# vat.py - functions for handling European VAT numbers +# coding: utf-8 +# +# Copyright (C) 2012 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 + +"""Module for handling various European VAT numbers. + +>>> is_valid('ATU 57194903') +True +>>> is_valid('BE697449992') +True +>>> compact('FR 61 954 506 077') +'FR61954506077' +>>> guess_country('00449544B01') +['nl'] +""" + + +country_codes = set([ + 'at', 'be', 'bg', 'cy', 'cz', 'de', 'dk', 'ee', 'es', 'fi', 'fr', 'gb', + 'gr', 'hu', 'ie', 'it', 'lt', 'lu', 'lv', 'mt', 'nl', 'pl', 'pt', 'ro', + 'se', 'si', 'sk' +]) +"""The collection of country codes that are queried.""" + +_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() + if cc == 'el': + cc = 'gr' + if cc not in country_codes: + return + if cc not in _country_modules: + # do `from stdnum.CC import vat` instead of `import stdnum.CC.vat` + # to handle the case where vat is an alias + _country_modules[cc] = __import__( + 'stdnum.%s' % cc, globals(), locals(), ['vat']).vat + return _country_modules[cc] + + +def compact(number): + """Convert the number to the minimal representation. This strips the + number of any valid separators and removes surrounding whitespace.""" + number = number.upper().strip() + return number[:2] + _get_cc_module(number[:2]).compact(number[2:]) + + +def is_valid(number): + """Checks to see if the number provided is a valid VAT number. This + performs the country-specific check for the number.""" + try: + number = compact(number) + except: + return False + module = _get_cc_module(number[:2]) + return bool(module) and module.is_valid(number[2:]) + + +def guess_country(number): + """Guess the country code based on the provided number. This checks the + provided number against each of the validation routines and returns + the list of countries for which it is valid. This returns lower case + codes and returns gr (instead of el) for Greece.""" + return [cc + for cc in country_codes + if _get_cc_module(cc).is_valid(number)] -- cgit v1.2.3