Skip to content

Commit

Permalink
Allow to customize match logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Feb 4, 2025
1 parent 4e33fde commit 9b51d9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ CONFIG = [
- `destination_account`:在匹配时,对账目使用的目标账户
- `additional_tags/metadata`:在匹配时,在账目上添加的额外标签和元数据
- `priority`:默认为 0,值越大则优先级越高
- `match_logic`:默认为 `"OR"`,即交易描述或交易对手任意一个匹配即可;可以设置为 `"AND"`,即交易描述和交易对手都需要匹配

## 可用 Importer

Expand Down
18 changes: 16 additions & 2 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class BillDetailMapping(typing.NamedTuple):
additional_metadata: typing.Optional[dict[str, object]] = None
# priority (larger means higher priority, 0 means lowest)
priority: int = 0
# match logic ("OR" or "AND")
match_logic: str = "OR"

def canonicalize(self):
tags = set(self.additional_tags) if self.additional_tags else set()
Expand All @@ -45,12 +47,18 @@ def canonicalize(self):
def match(
self, desc: str, payee: str
) -> tuple[typing.Optional[str], dict[str, object], set[str], int]:
assert self.match_logic == "OR" or self.match_logic == "AND"

# match narration first
narration_match = False
if desc is not None and self.narration_keywords is not None:
for keyword in self.narration_keywords:
if keyword in desc:
return self.canonicalize()
narration_match = True
break

# then try payee
payee_match = False
if payee is not None and self.payee_keywords is not None:
keywords = (
self.narration_keywords
Expand All @@ -59,7 +67,13 @@ def match(
)
for keyword in keywords:
if keyword in payee:
return self.canonicalize()
payee_match = True
break

if self.match_logic == "OR" and (narration_match or payee_match):
return self.canonicalize()
elif self.match_logic == "AND" and narration_match and payee_match:
return self.canonicalize()
return None, {}, set(), 0


Expand Down

0 comments on commit 9b51d9f

Please sign in to comment.