From 5bb7b8430481da92ee3e38ef7f62e98ff57df2b7 Mon Sep 17 00:00:00 2001 From: Adyanth Hosavalike Date: Fri, 22 Aug 2025 20:06:23 -0600 Subject: [PATCH] Use custom field separator --- README.md | 1 + main.py | 3 ++- tests/test_main.py | 17 +++++++++++------ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2521aeb..278418b 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Set these variables either in the environment or a `.env` file along with the sc 7. `FIREFLY_DRY_RUN`: Set this to any value to dry run and skip the firefly API call. 8. `SPLITWISE_DAYS=1` 9. `SW_BALANCE_ACCOUNT=Splitwise balance`: Set this to the name of the virtual Splitwise balance asset account on Firefly to enable the debt tracking feature. +10. `SPLITWISE_FIELD_SEPARATOR=/`: Set this to something else to use a custom separator in case any of the field values contain a `/`. ## Debt tracking feature When enabled, tracks Splitwise payable and receivable debts in an account defined by `SW_BALANCE_ACCOUNT`. diff --git a/main.py b/main.py index 1fc1f63..9d28e6d 100644 --- a/main.py +++ b/main.py @@ -39,6 +39,7 @@ def load_config() -> Config: "FOREIGN_CURRENCY_TOFIX_TAG": os.getenv("FOREIGN_CURRENCY_TOFIX_TAG"), "SW_BALANCE_ACCOUNT": os.getenv("SW_BALANCE_ACCOUNT", False), "SW_BALANCE_DEFAULT_DESCRIPTION": os.getenv("SW_BALANCE_DEFAULT_DESCRIPTION", "Splitwise balance"), + "SPLITWISE_FIELD_SEPARATOR": os.getenv("SPLITWISE_FIELD_SEPARATOR", "/") } time_now = datetime.now().astimezone() @@ -156,7 +157,7 @@ def processText(text: str) -> list[str]: """ if not text: return [] - split = text.split("/") + split = text.split(conf["SPLITWISE_FIELD_SEPARATOR"]) if split[0].strip().lower() == "firefly": return split[1:] or [True] return [] diff --git a/tests/test_main.py b/tests/test_main.py index 6d388b8..eb7dd82 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -77,14 +77,19 @@ def test_getDate(): assert result.month == 9 assert result.day == 10 -@pytest.mark.parametrize("text,expected", [ - ("firefly/category/description", ["category", "description"]), - ("firefly", [True]), - ("normal text", []), +@pytest.mark.parametrize("text,expected,separator", [ + ("firefly/category/description", ["category", "description"], None), + ("firefly", [True], None), + ("firefly|custom|sep", ["custom", "sep"], "|"), + ("firefly,custom/with/slash,sep", ["custom/with/slash", "sep"], ","), ]) -def test_processText(text, expected): - processText = load_main().processText +def test_processText(text, expected, separator): + main = load_main() + processText = main.processText + if separator: + main.conf["SPLITWISE_FIELD_SEPARATOR"] = separator assert processText(text) == expected + main.conf["SPLITWISE_FIELD_SEPARATOR"] = "/" @patch('requests.request') def test_callApi(mock_request):