Coverage for mt940/parser.py: 0%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2025-02-23 05:07 +0000

1# vim: fileencoding=utf-8: 

2''' 

3 

4Format 

5--------------------- 

6 

7Sources: 

8 

9.. _Swift for corporates: http://www.sepaforcorporates.com/\ 

10 swift-for-corporates/account-statement-mt940-file-format-overview/ 

11.. _Rabobank MT940: https://www.rabobank.nl/images/\ 

12 formaatbeschrijving_swift_bt940s_1_0_nl_rib_29539296.pdf 

13 

14 - `Swift for corporates`_ 

15 - `Rabobank MT940`_ 

16 

17:: 

18 

19 [] = optional 

20 ! = fixed length 

21 a = Text 

22 x = Alphanumeric, seems more like text actually. Can include special 

23 characters (slashes) and whitespace as well as letters and numbers 

24 d = Numeric separated by decimal (usually comma) 

25 c = Code list value 

26 n = Numeric 

27''' 

28 

29import os 

30 

31import mt940 

32 

33 

34def parse(src, encoding=None, processors=None, tags=None): 

35 ''' 

36 Parses mt940 data and returns transactions object 

37 

38 :param src: file handler to read, filename to read or raw data as string 

39 :return: Collection of transactions 

40 :rtype: Transactions 

41 ''' 

42 

43 def safe_is_file(filename): 

44 try: 

45 return os.path.isfile(src) 

46 except ValueError: # pragma: no cover 

47 return False 

48 

49 if hasattr(src, 'read'): # pragma: no branch 

50 data = src.read() 

51 elif safe_is_file(src): 

52 with open(src, 'rb') as fh: 

53 data = fh.read() 

54 else: # pragma: no cover 

55 data = src 

56 

57 if hasattr(data, 'decode'): # pragma: no branch 

58 exception = None 

59 encodings = [encoding, 'utf-8', 'cp852', 'iso8859-15', 'latin1'] 

60 

61 for encoding in encodings: # pragma: no cover 

62 if not encoding: 

63 continue 

64 

65 try: 

66 data = data.decode(encoding) 

67 break 

68 except UnicodeDecodeError as e: 

69 exception = e 

70 except UnicodeEncodeError: 

71 break 

72 else: 

73 raise exception # pragma: no cover 

74 

75 transactions = mt940.models.Transactions(processors, tags) 

76 transactions.parse(data) 

77 

78 return transactions