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

# Coverage for jutil/management/commands/make_bank_const_se.py : 0%

## 84 statements

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,too-many-nested-blocks,too-many-locals

2 import csv

3 import re

4 from copy import copy

5 from typing import Any

6

7 from django.core.management.base import CommandParser

8 from jutil.command import SafeCommand

9 from jutil.bank_const_se import SE_BANK_CLEARING_LIST

10

11

12 def se_iban_load_map(filename: str) -> list:

13 """

14 Loads Swedish monetary institution codes in CSV format.

15 :param filename: CSV file name of the BIC definitions.

16 Columns: Institution Name, Range Begin-Range End (inclusive), Account digits count

17 :return: List of (bank name, clearing code begin, clearing code end, account digits)

18 """

19 out = []

20 name_repl = {

21 'BNP Paribas Fortis SA/NV, Bankfilial Sverige': 'BNP Paribas Fortis SA/NV',

22 'Citibank International Plc, Sweden Branch': 'Citibank',

23 'Santander Consumer Bank AS (deltar endast i Dataclearingen)': 'Santander Consumer Bank AS',

24 'Nordax Bank AB (deltar endast i Dataclearingen)': 'Nordax Bank AB',

25 'Swedbank och fristående Sparbanker, t ex Leksands Sparbank och Roslagsbanken.': 'Swedbank',

26 'Ålandsbanken Abp (Finland),svensk filial': 'Ålandsbanken Abp',

27 'SBAB (deltar endast i Dataclearingen)': 'SBAB Bank AB',

28 'SBAB deltar endast i Dataclearingen': 'SBAB Bank AB',

29 'DNB Bank ASA, filial Sverige': 'Den Norske Bank',

30 'Länsförsäkringar Bank Aktiebolag': 'Länsförsäkringar Bank AB',

31 'MedMera Bank AB': 'Med Mera Bank AB',

32 }

33 with open(filename) as fp:

34 for row in csv.reader(fp):

35 if len(row) == 3:

36 name, series, acc_digits = row

37 # pprint([name, series, acc_digits])

38

39 # clean up name

40 name = re.sub(r'\n.*', '', name)

41 name = name_repl.get(name, name)

42

43 # clean up series

44 ml_acc_digits = acc_digits.split('\n')

45 for i, ser in enumerate(series.split('\n')):

46 begin, end = None, None

47 res = re.match(r'^(\d+)-(\d+).*$', ser)

48 if res:

49 begin, end = res.group(1), res.group(2)

50 if begin is None:

51 res = re.match(r'^(\d{4}).*$', ser)

52 if res:

53 begin = res.group(1)

54 end = begin

55

56 if begin and end:

57 digits: Any = None

58 try:

59 digits = int(acc_digits)

60 except ValueError:

61 pass

62 if digits is None:

63 try:

64 digits = int(ml_acc_digits[i])

65 except ValueError:

66 digits = '?'

67 except IndexError:

68 digits = '?'

69

70 out.append([name.strip(), begin.strip(), end.strip(), digits])

71 # print('OK!')

72 return out

73

74

75 class Command(SafeCommand):

76 help = 'Generates Python file with Swedish bank info as constants'

77

78 def add_arguments(self, parser: CommandParser):

79 parser.add_argument('--filename', type=str)

80 parser.add_argument('--php', action='store_true')

81

82 def do(self, *args, **kw):

83 new_bank_list = se_iban_load_map(kw['filename']) if kw['filename'] else []

84 # pprint(bank_list)

85

86 bank_list = list(copy(SE_BANK_CLEARING_LIST))

87 for name, begin, end, acc_digits in new_bank_list:

88 exists = False

89 #pylint: disable=unused-variable

90 for name0, begin0, end0, acc_digits0 in bank_list:

91 if begin0 == begin and end0 == end:

92 exists = True

93 break

94 #pylint: enable=unused-variable

95 if not exists:

96 bank_list.append((name, begin, end, acc_digits))

97

98 if kw['php']:

99 print('<?php')

100 print('')

101 print('global $SE_BANK_CLEARING_LIST;')

102 print('$SE_BANK_CLEARING_LIST = array(')

103 errors = False

104 for name, begin, end, acc_digits in bank_list:

105 print(" ['{}', '{}', '{}', {}],".format(name, begin, end, acc_digits))

106 if acc_digits == '?':

107 errors = True

108 print(');')

109 if errors:

110 print('')

111 print('// TODO: fix errors from above marked with "?"')

112 print('')

113 else:

114 print('SE_BANK_CLEARING_LIST = ( # ' + str(len(bank_list)))

115 errors = False

116 for name, begin, end, acc_digits in bank_list:

117 print(" ('{}', '{}', '{}', {}),".format(name, begin, end, acc_digits))

118 if acc_digits == '?':

119 errors = True

120 print(')')

121 if errors:

122 print('')

123 print('# TODO: fix errors from above marked with "?"')

124 print('')