-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaamva_fields.py
More file actions
679 lines (433 loc) · 15.8 KB
/
aamva_fields.py
File metadata and controls
679 lines (433 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
"""
Implementation of AAMVA Data Elements. See Section D.12.5 'Data Elements', starting at page 52.
"""
import datetime
# locals
import ansi_d20
EMPTY_KEY = "NONE"
# ----- HELPER METHODS -----
def parse_date(mmddyyyy_date) -> datetime.datetime:
month, day, year = mmddyyyy_date[0:2], mmddyyyy_date[2:4], mmddyyyy_date[4:8]
return datetime.datetime(year=int(year), month=int(month), day=int(day))
def parse_truncation(truncation_flag) -> str:
if truncation_flag == "T":
return "Truncated"
elif truncation_flag == "N":
return "Not truncated"
elif truncation_flag == "U":
return "Unknown if truncated"
else:
return ""
# --------------------
class DataElement:
code = ""
min_length = 0
max_length = 0
key_name = ""
value = ""
# Given a specific code and length, this creates a simple regular expression
# to search for that code in the text. e.g. if the code is 'ABC' and the length
# is between 2 and 4, this regex will match 2-4 characters after 'ABC' in a string
def get_regex(self):
return fr"{self.code}(.{{{self.min_length},{self.max_length}}})"
def parse(self):
return self.value
class JurisdictionSpecificVehicleClass(DataElement):
def __init__(self):
self.code = "DCA"
self.key_name = "Jurisdiction-specific vehicle class"
self.min_length = 1
self.max_length = 6
def parse(self):
# TODO: How does this work for commercial licenses?
if self.value == "A":
return "[CA] Travel trailer/fifth wheel (noncommercial)"
elif self.value == "B":
return "[CA] Housecar/motorhome (noncommercial)"
elif self.value == "C":
return "[CA-only] Standard vehicle (GVWR < 26000 lbs)"
elif self.value == "D":
return "[Other states besides CA] Standard vehicle"
elif self.value == "M1":
return "Motorcycle license"
elif self.value == "M2":
return "Limited motorcycle license (moped/scooter only)"
else:
raise ValueError(
f"'{self.value}' is not a recognized license type")
class JurisdictionSpecificRestrictionCodes(DataElement):
def __init__(self):
self.code = "DCB"
self.key_name = "Jurisdiction-specific restriction codes"
self.min_length = 4
self.max_length = 12
class JurisdictionSpecificEndorsementCodes(DataElement):
def __init__(self):
self.code = "DCD"
self.key_name = "Jurisdiction-specific endorsement codes"
self.min_length = 4
self.max_length = 5
class DocumentExpirationDate(DataElement):
def __init__(self):
self.code = "DBA"
self.key_name = "Document Expiration Date"
self.min_length = 8
self.max_length = self.min_length
def parse(self):
def get_expired_or_expiration_string(timestamp, current):
difference_timestamp = timestamp - current
if (timestamp > current):
return f"Expires in {difference_timestamp.days} days"
elif (current > timestamp):
return f"Expired {abs(difference_timestamp.days)} days ago"
else:
raise ValueError(
"Timestamp of expiration matches current timestamp -- verify data was correctly parsed")
timestamp = parse_date(self.value)
now = datetime.datetime.now()
return f"{timestamp.isoformat()} | {get_expired_or_expiration_string(timestamp, now)}"
class CustomerFamilyName(DataElement):
def __init__(self):
self.code = "DCS"
self.key_name = "Customer Family Name"
self.min_length = 3
self.max_length = 40
class CustomerFirstName(DataElement):
def __init__(self):
self.code = "DAC"
self.key_name = "Customer First Name"
self.min_length = 3
self.max_length = 40
class CustomerMiddleName(DataElement):
def __init__(self):
self.code = "DAD"
self.key_name = "Customer Middle Name(s)"
self.min_length = 3
self.max_length = 40
def parse(self):
if self.value == EMPTY_KEY:
return ""
else:
return self.value
class DocumentIssueDate(DataElement):
def __init__(self):
self.code = "DBD"
self.key_name = "Document Issue Date"
self.min_length = 8
self.max_length = self.min_length
def parse(self):
timestamp = parse_date(self.value)
return timestamp.isoformat()
class DateOfBirth(DataElement):
def __init__(self):
self.code = "DBB"
self.key_name = "Date of Birth"
self.min_length = 8
self.max_length = self.min_length
def parse(self):
timestamp = parse_date(self.value)
now = datetime.datetime.now()
return f"{timestamp.isoformat()} | Approx age: {now.year - timestamp.year}"
class PhysicalDescriptionSex(DataElement):
def __init__(self):
self.code = "DBC"
self.key_name = "Physical Description - Sex"
self.min_length = 1
self.max_length = self.min_length
def parse(self):
if str(self.value) == "1":
return "Male"
elif str(self.value) == "2":
return "Female"
elif str(self.value) == "9":
return "Not specified"
else:
raise ValueError(f"'{self.value}' is not a valid value")
class PhysicalDescriptionEyeColor(DataElement):
def __init__(self):
self.code = "DAY"
self.key_name = "Physical Description - Eye Color"
self.min_length = 3
self.max_length = self.min_length
def parse(self):
return ansi_d20.colors.get(self.value)
class PhysicalDescriptionHeight(DataElement):
def __init__(self):
self.code = "DAU"
self.key_name = "Physical Description - Height"
self.min_length = 6
self.max_length = self.min_length
def parse(self):
"""
This value can be in both centimeters or inches. e.g.
'073 IN' or '181 CM'
Start first by separating the units from the value themselves
"""
value, units = self.value.split(" ")
if units.lower() == "in":
# Convert the # of inches to a height in feet and inches
# eg 73 inches is 6'1
inches = int(value)
inches_to_feet = 12
return f"{inches // inches_to_feet}'{inches % inches_to_feet} ({inches} inches)"
elif units.lower() == "cm":
# metric, no conversion needed :)
return f"{int(value)} centimeters"
class AddressStreet1(DataElement):
def __init__(self):
self.code = "DAG"
self.key_name = "Address - Street 1"
self.min_length = 8
self.max_length = 35
class AddressCity(DataElement):
def __init__(self):
self.code = "DAI"
self.key_name = "Address - City"
self.min_length = 4
self.max_length = 20
class AddressJurisdictionCode(DataElement):
def __init__(self):
self.code = "DAJ"
self.key_name = "Address - Jurisdiction Code"
self.min_length = 2
self.max_length = 2
class AddressPostalCode(DataElement):
def __init__(self):
self.code = "DAK"
self.key_name = "Address - Postal Code"
self.min_length = 5
self.max_length = 11
class CustomerIDNumber(DataElement):
def __init__(self):
self.code = "DAQ"
self.key_name = "Customer ID Number"
self.min_length = 8
self.max_length = self.min_length
class DocumentDiscriminator(DataElement):
def __init__(self):
self.code = "DCF"
self.key_name = "Document Discriminator"
self.min_length = 25
self.max_length = self.min_length
class CountryIdentification(DataElement):
def __init__(self):
self.code = "DCG"
self.key_name = "Country Identification"
self.min_length = 3
self.max_length = self.min_length
class FamilyNameTruncation(DataElement):
def __init__(self):
self.code = "DDE"
self.key_name = "Family name truncation"
self.min_length = 1
self.max_length = self.min_length
def parse(self):
return parse_truncation(self.value)
class FirstNameTruncation(DataElement):
def __init__(self):
self.code = "DDF"
self.key_name = "First name truncation"
self.min_length = 1
self.max_length = self.min_length
def parse(self):
return parse_truncation(self.value)
class MiddleNameTruncation(DataElement):
def __init__(self):
self.code = "DDG"
self.key_name = "Middle name truncation"
self.min_length = 1
self.max_length = self.min_length
def parse(self):
return parse_truncation(self.value)
### OPTIONAL ELEMENTS ###
class AddressStreet2(DataElement):
def __init__(self):
self.code = "DAH"
self.min_length = 2
self.max_length = self.min_length
self.key_name = "Address - Street 2"
class HairColor(DataElement):
def __init__(self):
self.code = "DAZ"
self.key_name = "Hair color"
self.min_length = 3
self.max_length = 12
def parse(self):
return ansi_d20.colors.get(self.value)
class PlaceOfBirth(DataElement):
def __init__(self):
self.code = "DCI"
self.key_name = "Place of birth"
self.min_length = 5
self.max_length = 33
class AuditInformation(DataElement):
def __init__(self):
self.code = "DCJ"
self.key_name = "Audit Information"
self.min_length = 3
self.max_length = 25
class InventoryControlNumber(DataElement):
def __init__(self):
self.code = "DCK"
self.key_name = "Inventory control number"
self.min_length = 10
self.max_length = 25
class AliasFamilyName(DataElement):
def __init__(self):
self.code = "DBN"
self.key_name = "Alias / AKA Family Name"
self.min_length = 2
self.max_length = 10
class AliasGivenName(DataElement):
def __init__(self):
self.code = "DBG"
self.key_name = "Alias / AKA Given Name"
self.min_length = 2
self.max_length = 15
class AliasSuffixName(DataElement):
def __init__(self):
self.code = "DBS"
self.key_name = "Alias / AKA Suffix Name"
self.min_length = 1
self.max_length = 5
class NameSuffix(DataElement):
def __init__(self):
self.code = "DCU"
self.key_name = "Name Suffix"
self.min_length = 1
self.max_length = 5
class PhysicalDescriptionWeightRange(DataElement):
def __init__(self):
self.code = "DCE"
self.key_name = "Physical Description - Weight Range"
self.min_length = 1
self.max_length = self.min_length
def parse(self):
weight_ranges = dict({'0': "up to 31 kg (up to 70 lbs)", '1': "32 - 45 kg (71 - 100 lbs)", '2': "46 - 59 kg (101 – 130 lbs)", '3': "60 - 70 kg (131 – 160 lbs)", '4': "71 - 86 kg (161 – 190 lbs) ",
'5': "87 - 100 kg (191 – 220 lbs) ", '6': "101 - 113 kg (221 – 250 lbs) ", '7': "114 - 127 kg (251 – 280 lbs)", '8': " 128 – 145 kg (281 – 320 lbs)", '9': "146+ kg (321+ lbs)"})
return weight_ranges.get(str(self.value))
class RaceEthnicity(DataElement):
def __init__(self):
self.code = "DCL"
self.key_name = "Race / ethnicity"
self.min_length = 3
self.max_length = self.min_length
def parse(self):
return ansi_d20.races_and_ethnicities.get(self.value)
class StandardVehicleClassification(DataElement):
def __init__(self):
self.code = "DCM"
self.key_name = "Standard vehicle classification"
self.min_length = 4
self.max_length = self.min_length
class StandardEndorsementCode(DataElement):
def __init__(self):
self.code = "DCN"
self.key_name = "Standard endorsement code"
self.min_length = 5
self.max_length = self.min_length
class StandardRestrictionCode(DataElement):
def __init__(self):
self.code = "DCO"
self.key_name = "Standard restriction code"
self.min_length = 10
self.max_length = 12
class JurisdictionSpecificVehicleClassificationDescription(DataElement):
def __init__(self):
self.code = "DCP"
self.key_name = "Jurisdiction-specific vehicle classification description"
self.min_length = 10
self.max_length = 50
class JurisdictionSpecificEndorsementCodeDescription(DataElement):
def __init__(self):
self.code = "DCQ"
self.key_name = "Jurisdiction-specific endorsement code description"
self.min_length = 10
self.max_length = 50
class JurisdictionSpecificRestrictionCodeDescription(DataElement):
def __init__(self):
self.code = "DCR"
self.key_name = "Jurisdiction-specific restriction code description"
self.min_length = 10
self.max_length = 50
class ComplianceType(DataElement):
def __init__(self):
self.code = "DDA"
self.key_name = "Compliance Type (REAL ID)"
self.min_length = 1
self.max_length = self.min_length
def parse(self):
if self.value == "F":
return "Compliant ✅"
elif self.value == "N":
return "Non-compliant"
else:
raise ValueError("Can't determine compliance type")
class CardRevisionDate(DataElement):
def __init__(self):
self.code = "DDB"
self.key_name = "Card Revision Date"
self.min_length = 8
self.max_length = self.min_length
def parse(self):
return parse_date(self.value).isoformat()
class HAZMAT_Endorsement_Expiration_Date(DataElement):
def __init__(self):
self.code = "DDC"
self.key_name = "HAZMAT Endorsement Expiration Date"
self.min_length = 8
self.max_length = self.min_length
def parse(self):
return parse_date(self.value).isoformat()
class LimitedDurationDocumentIndicator(DataElement):
def __init__(self):
self.code = "DDD"
self.key_name = "Limited Duration Document Indicator"
self.min_length = 1
self.max_length = self.min_length
class WeightPounds(DataElement):
def __init__(self):
self.code = "DAW"
self.key_name = "Weight (pounds)"
self.min_length = 3
self.max_length = self.min_length
class WeightKilograms(DataElement):
def __init__(self):
self.code = "DAX"
self.key_name = "Weight (kilograms)"
self.min_length = 3
self.max_length = 3
class Under18Until(DataElement):
def __init__(self):
self.code = "DDH"
self.key_name = "Under 18 Until"
self.min_length = 8
self.max_length = self.min_length
class Under19Until(DataElement):
def __init__(self):
self.code = "DDI"
self.key_name = "Under 19 Until"
self.min_length = 8
self.max_length = self.min_length
class Under21Until(DataElement):
def __init__(self):
self.code = "DDJ"
self.key_name = "Under 21 Until"
self.min_length = 8
self.max_length = self.min_length
class OrganDonorIndicator(DataElement):
def __init__(self):
self.code = "DDK"
self.key_name = "Organ Donor Indicator"
self.min_length = 1
self.max_length = self.min_length
def parse(self):
return "Organ Donor" if self.value == "1" else "Not an organ donor"
class VeteranIndicator(DataElement):
def __init__(self):
self.code = "DDL"
self.key_name = "Veteran Indicator"
self.min_length = 1
self.max_length = self.min_length
def parse(self):
return "Veteran" if self.value == "1" else "Not a veteran"