Mercurial > louis > ofxstatement-us-hsbc
diff src/ofxstatement/plugins/us_hsbc/transactions.py @ 7:829eb62755b0
First cut at an HSBC (USA) plugin
author | Louis Opter <kalessin@kalessin.fr> |
---|---|
date | Thu, 17 Nov 2016 16:25:12 -0800 |
parents | |
children | 164da24a2997 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ofxstatement/plugins/us_hsbc/transactions.py Thu Nov 17 16:25:12 2016 -0800 @@ -0,0 +1,85 @@ +# Copyright (c) 2016, Louis Opter <louis@opter.org> +# +# This file is part of ofxstatement-us-hsbc. +# +# ofxstatement-us-hsbc is free software: you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# ofxstatement-us-hsbc is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty +# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import dateutil.parser +import re + +from ofxstatement.statement import StatementLine +from typing import Callable, Dict + +from .record import Record + + +def _enrich_date_user(sl: StatementLine, record: Record, date: str) -> None: + if not date: + return + + dt = dateutil.parser.parse(date, dayfirst=False, yearfirst=False) + sl.date_user = dt.replace(year=min(dt.year, sl.date.year)) + + +def _enrich_check(sl: StatementLine, record: Record) -> None: + sl.check_no = record.original_description.split("-")[-1].strip() + sl.memo = record.user_description + + +def _enrich_cash_withdrawal(sl: StatementLine, record: Record) -> None: + desc = record.original_description.replace("CASH WITHDRAWAL ON", "").strip() + date, sl.memo = [part.strip() for part in desc.split("AT", 1)] + _enrich_date_user(sl, record, date) + + +def _enrich_purchase(sl: StatementLine, record: Record) -> None: + # ORIGINAL_DESCRIPTION has more informations than SIMPLE_DESCRIPTION even + # if it's a bit less readable: + sl.memo = record.original_description + sl.memo = re.sub(r"^PURCHASE (-|(MADE )?ON)", "", sl.memo, re.I).strip() + + +def _enrich_credit(sl: StatementLine, record: Record) -> None: + prefix = "CREDIT RECEIVED ON" + sl.memo = record.original_description.replace(prefix, "").strip() + parts = [part.strip() for part in sl.memo.split("FROM", 1)] + if len(parts) > 1: + date, sl.memo = parts + _enrich_date_user(sl, record, date) + + +def _enrich_deposit(sl: StatementLine, record: Record) -> None: + sl.memo = record.original_description.replace("DEPOSIT FROM", "").strip() + + +def _enrich_payment(sl: StatementLine, record: Record) -> None: + sl.memo = record.original_description + sl.memo = re.sub(r"^(ONLINE )?PAYMENT (TO|-)", "", sl.memo, re.I).strip() + + +def _enrich_generic(sl: StatementLine, record: Record) -> None: + sl.memo = record.original_description + +_TRNTYPE_TO_METHOD: Dict[str, Callable[[StatementLine, Record], None]] = { + "CHECK": _enrich_check, + "CASH": _enrich_cash_withdrawal, + "PURCHASE": _enrich_purchase, + "CREDIT": _enrich_credit, + "DEP": _enrich_deposit, + "PAYMENT": _enrich_payment, +} + + +def enrich(sl: StatementLine, record: Record) -> None: + _TRNTYPE_TO_METHOD.get(sl.trntype, _enrich_generic)(sl, record)