lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.3-3-gfeab917

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

python-stdnum branch master updated. 1.3-3-gfeab917



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  feab9177256f77da9413a397016691fad177be79 (commit)
       via  619b097dfe1a549808f2ae6c0072cd6006107052 (commit)
      from  095dcbb11f6fc9f5ddee90c27b4d466c4a75a7ed (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 -----------------------------------------------------------------
http://arthurdejong.org/git/python-stdnum/commit/?id=feab9177256f77da9413a397016691fad177be79

commit feab9177256f77da9413a397016691fad177be79
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sat Apr 9 21:51:47 2016 +0200

    Implement online TC Kimlik check
    
    This refactors out the SOAP client function that was implemented for
    VIES to the stdnum.utils module.

diff --git a/stdnum/eu/vat.py b/stdnum/eu/vat.py
index 2c6728e..88d97ef 100644
--- a/stdnum/eu/vat.py
+++ b/stdnum/eu/vat.py
@@ -40,7 +40,7 @@ that country.
 """
 
 from stdnum.exceptions import *
-from stdnum.util import clean, get_vat_module
+from stdnum.util import clean, get_vat_module, get_soap_client
 
 
 country_codes = set([
@@ -118,18 +118,7 @@ def _get_client():  # pragma: no cover (no tests for this 
function)
     # it are not automatically tested
     global _vies_client
     if _vies_client is None:
-        try:
-            from urllib import getproxies
-        except ImportError:
-            from urllib.request import getproxies
-        # try suds first
-        try:
-            from suds.client import Client
-            _vies_client = Client(vies_wsdl, proxy=getproxies()).service
-        except ImportError:
-            # fall back to using pysimplesoap
-            from pysimplesoap.client import SoapClient
-            _vies_client = SoapClient(wsdl=vies_wsdl, proxy=getproxies())
+        _vies_client = get_soap_client(vies_wsdl)
     return _vies_client
 
 
diff --git a/stdnum/tr/tckimlik.py b/stdnum/tr/tckimlik.py
index 940024a..a465cf3 100644
--- a/stdnum/tr/tckimlik.py
+++ b/stdnum/tr/tckimlik.py
@@ -45,7 +45,15 @@ InvalidFormat: ...
 """
 
 from stdnum.exceptions import *
-from stdnum.util import clean
+from stdnum.util import clean, get_soap_client
+
+
+tckimlik_wsdl = 'https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx?WSDL'
+"""The WSDL URL of the T.C. Kimlik validation service."""
+
+
+# a cached version of the SOAP client for Kimlik validation
+_tckimlik_client = None
 
 
 def compact(number):
@@ -83,3 +91,27 @@ def is_valid(number):
         return bool(validate(number))
     except ValidationError:
         return False
+
+
+def _get_client():  # pragma: no cover (no tests for this function)
+    """Get a SOAP client for performing T.C. Kimlik validation."""
+    # this function isn't automatically tested because the functions using
+    # it are not automatically tested
+    global _tckimlik_client
+    if _tckimlik_client is None:
+        _tckimlik_client = get_soap_client(tckimlik_wsdl)
+    return _tckimlik_client
+
+
+def check_kps(number, name, surname, birth_year):  # pragma: no cover
+    """Queries the online T.C. Kimlik validation service run by the
+    Directorate of Population and Citizenship Affairs. This returns a boolean
+    but may raise a SOAP exception for missing or invalid values."""
+    # this function isn't automatically tested because it would require
+    # network access for the tests and unnecessarily load the online service
+    number = compact(number)
+    result = _get_client().TCKimlikNoDogrula(
+        TCKimlikNo=number, Ad=name, Soyad=surname, DogumYili=birth_year)
+    if hasattr(result, 'get'):
+        return result.get('TCKimlikNoDogrulaResult')
+    return result
diff --git a/stdnum/util.py b/stdnum/util.py
index efaa5ca..266b292 100644
--- a/stdnum/util.py
+++ b/stdnum/util.py
@@ -1,7 +1,7 @@
 # util.py - common utility functions
 # coding: utf-8
 #
-# Copyright (C) 2012, 2013, 2015 Arthur de Jong
+# Copyright (C) 2012-2016 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
@@ -178,3 +178,21 @@ def get_vat_module(cc):
     cc = cc.lower()
     cc = _cc_translations.get(cc, cc)
     return __import__('stdnum.%s' % cc, globals(), locals(), ['vat']).vat
+
+
+def get_soap_client(wsdlurl):  # pragma: no cover (no tests for this function)
+    """Get a SOAP client for performing requests."""
+    # this function isn't automatically tested because the functions using
+    # it are not automatically tested
+    try:
+        from urllib import getproxies
+    except ImportError:
+        from urllib.request import getproxies
+    # try suds first
+    try:
+        from suds.client import Client
+        return Client(wsdlurl, proxy=getproxies()).service
+    except ImportError:
+        # fall back to using pysimplesoap
+        from pysimplesoap.client import SoapClient
+        return SoapClient(wsdl=wsdlurl, proxy=getproxies())

http://arthurdejong.org/git/python-stdnum/commit/?id=619b097dfe1a549808f2ae6c0072cd6006107052

commit 619b097dfe1a549808f2ae6c0072cd6006107052
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sat Apr 9 20:14:09 2016 +0200

    Add Turkish personal identification number

diff --git a/stdnum/tr/__init__.py b/stdnum/tr/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/stdnum/tr/tckimlik.py b/stdnum/tr/tckimlik.py
new file mode 100644
index 0000000..940024a
--- /dev/null
+++ b/stdnum/tr/tckimlik.py
@@ -0,0 +1,85 @@
+# tckimlik.py - functions for handling T.C. Kimlik No.
+# coding: utf-8
+#
+# Copyright (C) 2016 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
+
+"""T.C. Kimlik No. (Turkish personal identification number)
+
+The Turkish Identification Number (Türkiye Cumhuriyeti Kimlik Numarası) is a
+unique personal identification number assigned to every citizen of Turkey.
+The number consists of 11 digits and the last two digits are check digits.
+
+More information can be found at:
+  https://en.wikipedia.org/wiki/Turkish_Identification_Number
+  https://tckimlik.nvi.gov.tr/
+
+>>> validate('17291716060')
+'17291716060'
+>>> validate('17291716050')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('1729171606')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> validate('07291716092')  # number must not start with a 0
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+"""
+
+from stdnum.exceptions import *
+from stdnum.util import clean
+
+
+def compact(number):
+    """Convert the number to the minimal representation. This strips the
+    number of any valid separators and removes surrounding whitespace."""
+    return clean(number).strip()
+
+
+def calc_check_digits(number):
+    """Calculate the check digits for the specified number. The number
+    passed should not have the check digit included."""
+    check1 = (10 - sum((3 - 2 * (i % 2)) * int(n)
+              for i, n in enumerate(number[:9]))) % 10
+    check2 = (check1 + sum(int(n) for n in number[:9])) % 10
+    return '%d%d' % (check1, check2)
+
+
+def validate(number):
+    """Checks to see if the number provided is a valid .C. Kimlik No..
+    This checks the length and check digits"""
+    number = compact(number)
+    if not number.isdigit() or number[0] == '0':
+        raise InvalidFormat()
+    if len(number) != 11:
+        raise InvalidLength()
+    if calc_check_digits(number) != number[-2:]:
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid .C. Kimlik No..
+    This checks the length and check digits"""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_tr_tckimlik.doctest b/tests/test_tr_tckimlik.doctest
new file mode 100644
index 0000000..c3cb4a2
--- /dev/null
+++ b/tests/test_tr_tckimlik.doctest
@@ -0,0 +1,234 @@
+test_tr_tckimlik.doctest - more detailed doctests for stdnum.tr.tckimlik
+
+Copyright (C) 2016 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.tr.tckimlik
+module.
+
+>>> from stdnum.tr import tckimlik
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 10010405214
+... 10036951702
+... 10048987300
+... 10211782428
+... 10328097460
+... 10406257588
+... 10634889650
+... 10942702016
+... 10945940248
+... 10964454474
+... 11015620602
+... 11051965132
+... 11272211322
+... 11510943206
+... 11521565534
+... 11629545766
+... 11678091474
+... 11678588622
+... 11856036426
+... 12245687724
+... 12254905536
+... 12281024156
+... 12629254764
+... 12668958814
+... 12691048056
+... 12743195844
+... 12790164022
+... 12797231260
+... 13159065448
+... 13195732540
+... 13267669224
+... 13331822036
+... 13466947000
+... 13481622074
+... 13649294334
+... 13684258322
+... 13700315886
+... 13789534246
+... 13796225410
+... 14693160156
+... 14834750260
+... 14953629638
+... 15038532782
+... 15076154212
+... 16130649606
+... 16168925222
+... 16412902252
+... 16834076296
+... 16910960518
+... 17311901384
+... 17690240920
+... 18164076700
+... 18535119968
+... 18563796476
+... 18737290298
+... 18766481124
+... 18986317036
+... 19261708150
+... 19304490066
+... 19315991200
+... 19594888024
+... 19649559862
+... 19736210324
+... 19946754342
+... 20923453974
+... 21289134606
+... 22508375282
+... 22514314664
+... 22645425550
+... 22850263862
+... 23128444884
+... 23761367960
+... 23776369980
+... 23803533490
+... 23812308782
+... 23893394292
+... 23945549168
+... 24118906764
+... 24721853608
+... 25171067942
+... 25175346104
+... 25262304970
+... 26465047138
+... 26912437970
+... 27085445038
+... 27352676660
+... 27806346354
+... 27977266680
+... 28040246264
+... 28196469500
+... 28423494398
+... 29008505256
+... 29449926498
+... 29458830458
+... 29660083116
+... 29663288082
+... 29717351038
+... 30229834674
+... 30517382986
+... 30547067204
+... 30713063856
+... 30841564102
+... 31177269242
+... 31261062360
+... 31378085252
+... 31666319410
+... 31972018304
+... 32080659878
+... 32191139436
+... 32276435798
+... 32362147840
+... 32383033566
+... 32806023532
+... 32986865670
+... 33163817956
+... 33370005450
+... 33632324534
+... 33869149092
+... 34399824114
+... 34411781920
+... 34837267190
+... 35209878136
+... 35389844082
+... 35467790720
+... 35740345494
+... 35927206914
+... 35944794678
+... 36412601542
+... 36799571254
+... 37474709214
+... 37561226518
+... 37708357112
+... 37987046004
+... 38065982832
+... 38312029198
+... 38491580480
+... 38818946524
+... 39346988482
+... 39433505530
+... 39488099268
+... 39556079970
+... 39872148282
+... 40285129270
+... 40429332580
+... 41116624858
+... 41314615566
+... 41537123152
+... 41740577612
+... 42529353182
+... 42568749892
+... 42679341534
+... 43198351554
+... 43282542684
+... 43609945662
+... 43846057142
+... 43879752656
+... 44815212906
+... 45358790944
+... 45577460604
+... 46162064740
+... 46186184424
+... 46786024366
+... 47248345760
+... 47353662028
+... 48064083294
+... 48637402194
+... 48637792714
+... 48883810224
+... 49432850678
+... 49636373966
+... 49879800274
+... 50035230828
+... 50479247086
+... 51508004946
+... 51532505860
+... 51865395732
+... 53320726198
+... 53728014842
+... 54439040694
+... 54508382038
+... 54541189670
+... 54781681538
+... 55996400266
+... 56698147152
+... 57025178016
+... 57061155224
+... 59020072634
+... 59464150004
+... 59599009130
+... 59605521564
+... 61579176350
+... 61957262828
+... 62077362474
+... 62224350880
+... 62512131214
+... 63352195206
+... 64756048358
+... 68536141048
+... 69844021374
+... 71467095762
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not tckimlik.is_valid(x)]
+[]

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

Summary of changes:
 stdnum/eu/vat.py               |  15 +--
 stdnum/tr/__init__.py          |   0
 stdnum/tr/tckimlik.py          | 117 +++++++++++++++++++++
 stdnum/util.py                 |  20 +++-
 tests/test_tr_tckimlik.doctest | 234 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 372 insertions(+), 14 deletions(-)
 create mode 100644 stdnum/tr/__init__.py
 create mode 100644 stdnum/tr/tckimlik.py
 create mode 100644 tests/test_tr_tckimlik.doctest


hooks/post-receive
-- 
python-stdnum
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/