Coverage for jutil/management/commands/make_bank_const_fi.py : 0%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# pylint: disable=too-many-branches
2from django.core.exceptions import ValidationError
3from django.core.management.base import CommandParser
4from jutil.command import SafeCommand
5from jutil.dict import sorted_dict
6from jutil.bank_const_fi import FI_BIC_BY_ACCOUNT_NUMBER, FI_BANK_NAME_BY_BIC
9def fi_iban_load_map(filename: str) -> dict:
10 """
11 Loads Finnish monetary institution codes and BICs in CSV format.
12 Map which is based on 3 digits as in FIXX<3 digits>.
13 Can be used to map Finnish IBAN number to bank information.
14 Format: dict('<3 digits': (BIC, name), ...)
15 :param filename: CSV file name of the BIC definitions. Columns: National ID, BIC Code, Institution Name
16 """
17 out = {}
18 with open(filename, 'rt') as fp:
19 lines = [line.strip().split(',') for line in fp.readlines()]
20 lines.pop(0) # ver
21 head = lines.pop(0)
22 if head != ['National ID', 'BIC Code', 'Financial Institution Name']:
23 raise ValidationError('Incompatible file content in {}'.format(filename))
24 for line in lines:
25 if len(line) == 3 and line[0]:
26 nat_id = str(line[0]).strip()
27 bic_code = line[1].strip()
28 name = line[2].strip()
29 out[nat_id] = (bic_code, name)
30 return out
33class Command(SafeCommand):
34 help = 'Generates Python file with Finnish bank info as constants'
36 def add_arguments(self, parser: CommandParser):
37 parser.add_argument('--filename', type=str)
38 parser.add_argument('--php', action='store_true')
40 def do(self, *args, **kw):
41 iban_map = fi_iban_load_map(kw['filename']) if kw['filename'] else {}
42 bic_by_acc = FI_BIC_BY_ACCOUNT_NUMBER
43 for acc, bank in iban_map.items():
44 bic_by_acc[acc] = bank[0]
45 bic_map = FI_BANK_NAME_BY_BIC
46 for acc, bank in iban_map.items():
47 if bank[0] not in bic_map:
48 bic_map[bank[0]] = bank[1]
49 bic_by_acc = sorted_dict(bic_by_acc)
50 bic_map = sorted_dict(bic_map)
52 if kw['php']:
53 print('<?php')
54 print('')
55 print('global $FI_BIC_BY_ACCOUNT_NUMBER;')
56 print('$FI_BIC_BY_ACCOUNT_NUMBER = array(')
57 for acc, bank in iban_map.items():
58 print(" '{}' => '{}',".format(acc, bank[0]))
59 print(');')
60 print('')
61 print('global $FI_BANK_NAME_BY_BIC;')
62 print('$FI_BANK_NAME_BY_BIC = array(')
63 for bic, bank in bic_map.items():
64 print(" '{}' => '{}',".format(bic, bank))
65 print(');')
66 print('')
67 else:
68 print('FI_BIC_BY_ACCOUNT_NUMBER = { # ' + str(len(bic_by_acc.items())))
69 for acc, bic in bic_by_acc.items():
70 print(" '{}': '{}',".format(acc, bic))
71 print('}\n')
72 print('FI_BANK_NAME_BY_BIC = { # ' + str(len(bic_map.items())))
73 for bic, name in bic_map.items():
74 print(" '{}': '{}',".format(bic, name))
75 print('}')
76 print('')