Hide keyboard shortcuts

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 

2import csv 

3from copy import copy 

4from django.core.management.base import CommandParser 

5from jutil.command import SafeCommand 

6from jutil.bank_const_dk import DK_BANK_CLEARING_MAP 

7 

8 

9def is_int(x) -> bool: 

10 try: 

11 int(x) 

12 return True 

13 except Exception: 

14 return False 

15 

16 

17def dk_iban_load_map(filename: str) -> list: 

18 """ 

19 Loads Denmark monetary institution codes in CSV format. 

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

21 Columns: 4-digit code, bank name, ... 

22 :return: list of (code, name) 

23 """ 

24 data_list = [] 

25 with open(filename) as fp: 

26 for row in csv.reader(fp): 

27 if len(row) >= 2 and is_int(row[0]) and row[1]: 

28 data_list.append((row[0], row[1])) 

29 return data_list 

30 

31 

32class Command(SafeCommand): 

33 help = "Generates Python file with Denmark bank info as constants" 

34 

35 def add_arguments(self, parser: CommandParser): 

36 parser.add_argument("--filename", type=str) 

37 parser.add_argument("--php", action="store_true") 

38 

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

40 new_bank_list = dk_iban_load_map(kw["filename"]) if kw["filename"] else [] 

41 

42 bank_data = dict(copy(DK_BANK_CLEARING_MAP)) 

43 for code, name in new_bank_list: 

44 if code not in bank_data: 

45 bank_data[code] = name 

46 

47 if kw["php"]: 

48 print("<?php") 

49 print("") 

50 print("global $DK_BANK_CLEARING_MAP;") 

51 print("$DK_BANK_CLEARING_MAP = array(") 

52 errors = False 

53 for code, name in bank_data.items(): 

54 print(" '{}' => '{}',".format(code, name)) 

55 print(");") 

56 if errors: 

57 print("") 

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

59 print("") 

60 else: 

61 print("DK_BANK_CLEARING_MAP = { # " + str(len(bank_data))) 

62 errors = False 

63 for code, name in bank_data.items(): 

64 print(" '{}': '{}',".format(code, name)) 

65 print("}") 

66 if errors: 

67 print("") 

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

69 print("")