You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
9.6 KiB

  1. # -*- coding: utf-8 -*-
  2. """Class to parse camt files."""
  3. # © 2013-2016 Therp BV <http://therp.nl>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. import re
  6. from lxml import etree
  7. class CamtParser(object):
  8. """Parser for camt bank statement import files."""
  9. def parse_amount(self, ns, node):
  10. """Parse element that contains Amount and CreditDebitIndicator."""
  11. if node is None:
  12. return 0.0
  13. sign = 1
  14. amount = 0.0
  15. sign_node = node.xpath('ns:CdtDbtInd', namespaces={'ns': ns})
  16. if sign_node and sign_node[0].text == 'DBIT':
  17. sign = -1
  18. amount_node = node.xpath('ns:Amt', namespaces={'ns': ns})
  19. if amount_node:
  20. amount = sign * float(amount_node[0].text)
  21. return amount
  22. def add_value_from_node(
  23. self, ns, node, xpath_str, obj, attr_name, join_str=None):
  24. """Add value to object from first or all nodes found with xpath.
  25. If xpath_str is a list (or iterable), it will be seen as a series
  26. of search path's in order of preference. The first item that results
  27. in a found node will be used to set a value."""
  28. if not isinstance(xpath_str, (list, tuple)):
  29. xpath_str = [xpath_str]
  30. for search_str in xpath_str:
  31. found_node = node.xpath(search_str, namespaces={'ns': ns})
  32. if found_node:
  33. if join_str is None:
  34. attr_value = found_node[0].text
  35. else:
  36. attr_value = join_str.join([x.text for x in found_node])
  37. obj[attr_name] = attr_value
  38. break
  39. def parse_transaction_details(self, ns, node, transaction):
  40. """Parse transaction details (message, party, account...)."""
  41. # message
  42. self.add_value_from_node(
  43. ns, node, [
  44. './ns:RmtInf/ns:Ustrd',
  45. './ns:AddtlNtryInf',
  46. './ns:Refs/ns:InstrId',
  47. ], transaction, 'note', join_str='\n')
  48. # name
  49. self.add_value_from_node(
  50. ns, node, [
  51. './ns:AddtlTxInf',
  52. ], transaction, 'name', join_str='\n')
  53. # eref
  54. self.add_value_from_node(
  55. ns, node, [
  56. './ns:RmtInf/ns:Strd/ns:CdtrRefInf/ns:Ref',
  57. './ns:Refs/ns:EndToEndId',
  58. ],
  59. transaction, 'ref'
  60. )
  61. # remote party values
  62. party_type = 'Dbtr'
  63. party_type_node = node.xpath(
  64. '../../ns:CdtDbtInd', namespaces={'ns': ns})
  65. if party_type_node and party_type_node[0].text != 'CRDT':
  66. party_type = 'Cdtr'
  67. party_node = node.xpath(
  68. './ns:RltdPties/ns:%s' % party_type, namespaces={'ns': ns})
  69. if party_node:
  70. self.add_value_from_node(
  71. ns, party_node[0], './ns:Nm', transaction, 'partner_name')
  72. self.add_value_from_node(
  73. ns, party_node[0], './ns:PstlAdr/ns:Ctry', transaction,
  74. 'partner_country'
  75. )
  76. address_node = party_node[0].xpath(
  77. './ns:PstlAdr/ns:AdrLine', namespaces={'ns': ns})
  78. if address_node:
  79. transaction['partner_address'] = [address_node[0].text]
  80. # Get remote_account from iban or from domestic account:
  81. account_node = node.xpath(
  82. './ns:RltdPties/ns:%sAcct/ns:Id' % party_type,
  83. namespaces={'ns': ns}
  84. )
  85. if account_node:
  86. iban_node = account_node[0].xpath(
  87. './ns:IBAN', namespaces={'ns': ns})
  88. if iban_node:
  89. transaction['account_number'] = iban_node[0].text
  90. bic_node = node.xpath(
  91. './ns:RltdAgts/ns:%sAgt/ns:FinInstnId/ns:BIC' % party_type,
  92. namespaces={'ns': ns}
  93. )
  94. if bic_node:
  95. transaction['account_bic'] = bic_node[0].text
  96. else:
  97. self.add_value_from_node(
  98. ns, account_node[0], './ns:Othr/ns:Id', transaction,
  99. 'account_number'
  100. )
  101. def parse_transaction(self, ns, node):
  102. """Parse transaction (entry) node."""
  103. transaction = {}
  104. self.add_value_from_node(
  105. ns, node, './ns:BkTxCd/ns:Prtry/ns:Cd', transaction,
  106. 'transfer_type'
  107. )
  108. self.add_value_from_node(
  109. ns, node, './ns:BookgDt/ns:Dt', transaction, 'date')
  110. self.add_value_from_node(
  111. ns, node, './ns:BookgDt/ns:Dt', transaction, 'execution_date')
  112. self.add_value_from_node(
  113. ns, node, './ns:ValDt/ns:Dt', transaction, 'value_date')
  114. transaction['amount'] = self.parse_amount(ns, node)
  115. details_node = node.xpath(
  116. './ns:NtryDtls/ns:TxDtls', namespaces={'ns': ns})
  117. if details_node:
  118. self.parse_transaction_details(ns, details_node[0], transaction)
  119. if not transaction.get('name'):
  120. self.add_value_from_node(
  121. ns, node, './ns:AddtlNtryInf', transaction, 'name')
  122. if not transaction.get('name'):
  123. transaction['name'] = '/'
  124. if not transaction.get('ref'):
  125. self.add_value_from_node(
  126. ns, node, [
  127. './ns:NtryDtls/ns:Btch/ns:PmtInfId',
  128. ],
  129. transaction, 'ref'
  130. )
  131. transaction['data'] = etree.tostring(node)
  132. return transaction
  133. def get_balance_amounts(self, ns, node):
  134. """Return opening and closing balance.
  135. Depending on kind of balance and statement, the balance might be in a
  136. different kind of node:
  137. OPBD = OpeningBalance
  138. PRCD = PreviousClosingBalance
  139. ITBD = InterimBalance (first ITBD is start-, second is end-balance)
  140. CLBD = ClosingBalance
  141. """
  142. start_balance_node = None
  143. end_balance_node = None
  144. for node_name in ['OPBD', 'PRCD', 'CLBD', 'ITBD']:
  145. code_expr = (
  146. './ns:Bal/ns:Tp/ns:CdOrPrtry/ns:Cd[text()="%s"]/../../..' %
  147. node_name
  148. )
  149. balance_node = node.xpath(code_expr, namespaces={'ns': ns})
  150. if balance_node:
  151. if node_name in ['OPBD', 'PRCD']:
  152. start_balance_node = balance_node[0]
  153. elif node_name == 'CLBD':
  154. end_balance_node = balance_node[0]
  155. else:
  156. if not start_balance_node:
  157. start_balance_node = balance_node[0]
  158. if not end_balance_node:
  159. end_balance_node = balance_node[-1]
  160. return (
  161. self.parse_amount(ns, start_balance_node),
  162. self.parse_amount(ns, end_balance_node)
  163. )
  164. def parse_statement(self, ns, node):
  165. """Parse a single Stmt node."""
  166. result = {}
  167. self.add_value_from_node(
  168. ns, node, [
  169. './ns:Acct/ns:Id/ns:IBAN',
  170. './ns:Acct/ns:Id/ns:Othr/ns:Id',
  171. ], result, 'account_number'
  172. )
  173. self.add_value_from_node(
  174. ns, node, './ns:Id', result, 'name')
  175. self.add_value_from_node(
  176. ns, node, './ns:Dt', result, 'date')
  177. self.add_value_from_node(
  178. ns, node, './ns:Acct/ns:Ccy', result, 'currency')
  179. result['balance_start'], result['balance_end_real'] = (
  180. self.get_balance_amounts(ns, node))
  181. transaction_nodes = node.xpath('./ns:Ntry', namespaces={'ns': ns})
  182. result['transactions'] = []
  183. for entry_node in transaction_nodes:
  184. transaction = self.parse_transaction(ns, entry_node)
  185. if transaction:
  186. result['transactions'].append(transaction)
  187. return result
  188. def check_version(self, ns, root):
  189. """Validate validity of camt file."""
  190. # Check wether it is camt at all:
  191. re_camt = re.compile(
  192. r'(^urn:iso:std:iso:20022:tech:xsd:camt.'
  193. r'|^ISO:camt.)'
  194. )
  195. if not re_camt.search(ns):
  196. raise ValueError('no camt: ' + ns)
  197. # Check wether version 052 or 053:
  198. re_camt_version = re.compile(
  199. r'(^urn:iso:std:iso:20022:tech:xsd:camt.053.'
  200. r'|^urn:iso:std:iso:20022:tech:xsd:camt.052.'
  201. r'|^ISO:camt.053.'
  202. r'|^ISO:camt.052.)'
  203. )
  204. if not re_camt_version.search(ns):
  205. raise ValueError('no camt 052 or 053: ' + ns)
  206. # Check GrpHdr element:
  207. root_0_0 = root[0][0].tag[len(ns) + 2:] # strip namespace
  208. if root_0_0 != 'GrpHdr':
  209. raise ValueError('expected GrpHdr, got: ' + root_0_0)
  210. def parse(self, data):
  211. """Parse a camt.052 or camt.053 file."""
  212. try:
  213. root = etree.fromstring(
  214. data, parser=etree.XMLParser(recover=True))
  215. except etree.XMLSyntaxError:
  216. # ABNAmro is known to mix up encodings
  217. root = etree.fromstring(
  218. data.decode('iso-8859-15').encode('utf-8'))
  219. if root is None:
  220. raise ValueError(
  221. 'Not a valid xml file, or not an xml file at all.')
  222. ns = root.tag[1:root.tag.index("}")]
  223. self.check_version(ns, root)
  224. statements = []
  225. currency = None
  226. account_number = None
  227. for node in root[0][1:]:
  228. statement = self.parse_statement(ns, node)
  229. if len(statement['transactions']):
  230. if 'currency' in statement:
  231. currency = statement.pop('currency')
  232. if 'account_number' in statement:
  233. account_number = statement.pop('account_number')
  234. statements.append(statement)
  235. return currency, account_number, statements