Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: add initial implementation for td1 truncation ✂ #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,9 @@ sudo python3 setup.py install
- [x] Added new checks for periods of time in Checker.
- [x] Visas support
- [x] Fields extraction in checker (name, surname, country, sex, etc) (version 0.5.0)

- [x] Automatic name truncation in Generator

###### TODO:
- [ ] Automatic name truncation in Generator
- [ ] Possibility of disabling checks for country code, nationality, type of document and the others fields in Checker.
- [ ] Add logging

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,11 @@ Features:
- [x] Visas support
- [x] Fields extraction in checker (name, surname, country, sex, etc)
(version 0.5.0)
- [x] Automatic name truncation in Generator

TODO:


- [ ] Automatic name truncation in Generator
- [ ] Possibility of disabling checks for country code, nationality,
type of document and the others fields in Checker.
- [ ] Add logging
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- [ ] Automatic name truncation in Generator
- [ ] Possibility of disabling checks for country code, nationality, type of document and the others fields in Checker.
- [ ] Add logging
- [ ] Use `enum.Enum` instead `int` for kinds _report (Checker)
Expand Down
2 changes: 1 addition & 1 deletion mrz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

__author__ = "Iván Rincón"
__license__ = "GPL3"
__version__ = "0.6.2"
__version__ = "0.7.2"
25 changes: 24 additions & 1 deletion mrz/generator/td1.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,31 @@ def __init__(self, surname: str, given_names: str, transliteration: dict):
def identifier(self) -> str:
"""Return identifier (sum of the primary and secondary identifier)

Might be truncated to initials.

"""
return check.field(self.surname + "<<" + self.given_names, 30, "full name", "<")
# truncate only if necessary.
if len(self.surname + self.given_names) <= 28:
return check.field(self.surname + "<<" + self.given_names, 30, "full name", "<")

# truncate identifiers to initials (approach in sec. 4.2.3.1 a)).
# see: https://www.icao.int/publications/Documents/9303_p5_cons_en.pdf
parts_sur = self.surname.split("<")
parts_giv = self.given_names.split("<")
parts = parts_sur + parts_giv

# reserve chars for initials and fillers '<' or '<<'.
chars_to_distribute = 28 - 1 * (2 * len(parts)-2)
parts_trunc = []
for idx, part in enumerate(parts):
chars_of_part = min(chars_to_distribute, len(part)-1)
chars_to_distribute -= chars_of_part
parts_trunc.append(part[0:chars_of_part+1])
# insert '' to separate surname from given names
if idx + 1 == len(parts_sur):
parts_trunc.append("")
return check.field("<".join(parts_trunc), 30, "full name", "<")



class TD1CodeGenerator(_TD1HolderName, _TD1HashGenerator, _FieldsGenerator):
Expand Down
3 changes: 2 additions & 1 deletion tests/all_gnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from tests.td1_gnt_01 import TestCase01
from tests.td1_gnt_02 import TestCase02
from tests.td1_gnt_03 import TestCase10
from tests.td2_gnt_01 import TestCase03
from tests.td3_gnt_01 import TestCase04
from tests.td3_gnt_02 import TestCase05
Expand All @@ -22,7 +23,7 @@ def test_generator(self):
TestCase07().test_mrva_generator()
TestCase08().test_mrvb_generator()
TestCase09().test_mrvb_generator()

TestCase10().test_td1_generator()

if __name__ == '__main__':
unittest.main()
21 changes: 21 additions & 0 deletions tests/td1_gnt_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from mrz.generator.td1 import TD1CodeGenerator, dictionary


class TestCase10(unittest.TestCase):

def test_td1_generator(self):
# test truncation. Example adopted from:
# https://www.icao.int/publications/Documents/9303_p5_cons_en.pdf
td1_generator = TD1CodeGenerator("id", "THA", "955555546", "680229", "f", "130724", "THA",
"NILAVADHANANANDA", "CHAYAPA DEJTHAMRONG KRASUANG", "2902968000000")

result = ("IDTHA95555554612902968000000<<\n"
"6802295F1307245THA<<<<<<<<<<<6\n"
"NILAVADHANANANDA<<CHAYAPA<DE<K")

self.assertEqual(str(td1_generator), result)


if __name__ == '__main__':
unittest.main()