From 828df9b2dcce75c24c5785beacda019c412fcafe Mon Sep 17 00:00:00 2001 From: David George Date: Sun, 26 Jan 2025 18:34:35 +0000 Subject: [PATCH 1/3] Add parse_string function --- quiffen/core/qif.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/quiffen/core/qif.py b/quiffen/core/qif.py index 53796ad..ffc9895 100644 --- a/quiffen/core/qif.py +++ b/quiffen/core/qif.py @@ -134,6 +134,32 @@ def parse( data = path.read_text(encoding=encoding).strip().strip("\n") + if not data: + raise ParserException("The file is empty.") + + return cls.parse_string(data, separator=separator, day_first=day_first) + + @classmethod + def parse_string( + cls, data: str, separator: str = "\n", day_first: bool = False + ) -> Qif: + """Return a class instance from a string containing QIF data. + + Parameters + ---------- + data : str + The string of QIF data + separator : str, default='\n' + The line separator for the QIF file. This probably won't need + changing. + day_first : bool, default=False + Whether the day or month comes first in the date. + + Returns + ------- + Qif + A Qif object containing all the data in the QIF file. + """ if not data: raise ParserException("The file is empty.") From 1f9d7cb816a7066a2e8955cb49ddf600dadd4880 Mon Sep 17 00:00:00 2001 From: David George Date: Sun, 26 Jan 2025 18:35:20 +0000 Subject: [PATCH 2/3] Add tests for parse_string --- tests/test_qif.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/test_qif.py b/tests/test_qif.py index 6e8fbb1..80bfe7a 100644 --- a/tests/test_qif.py +++ b/tests/test_qif.py @@ -217,12 +217,33 @@ def test_from_list(): qif.from_list([]) -def test_parse_works(qif_file): +def test_parse_string_works(): + """Test parsing a QIF file""" + qif_string = ( + "!Type:Class\n" + "NTest class\n" + "DThis is just a class I added here for test purposes\n" + "^\n" + "!Type:Class\n" + "NTest class 2\n" + "DThis is just a class I added here for test purposes\n" + "^\n" + ) + Qif.parse_string(qif_string) + + +def test_parse_empty_string(): + """Test parsing an empty QIF file""" + with pytest.raises(ParserException): + Qif.parse("") + + +def test_parse_file_works(qif_file): """Test parsing a QIF file""" Qif.parse(qif_file) -def test_parse_works_with_oth_a_account(qif_file_with_oth_a_account): +def test_parse_file_works_with_oth_a_account(qif_file_with_oth_a_account): """Test parsing a QIF file with an OTH account""" Qif.parse(qif_file_with_oth_a_account) From 6c9b1e53482119571bd27e93de81002423aceaa3 Mon Sep 17 00:00:00 2001 From: David George Date: Mon, 27 Jan 2025 21:49:46 +0000 Subject: [PATCH 3/3] Update error message in parse_string --- quiffen/core/qif.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quiffen/core/qif.py b/quiffen/core/qif.py index ffc9895..6d260bb 100644 --- a/quiffen/core/qif.py +++ b/quiffen/core/qif.py @@ -161,7 +161,7 @@ def parse_string( A Qif object containing all the data in the QIF file. """ if not data: - raise ParserException("The file is empty.") + raise ParserException("The data string is empty.") accounts: Dict[str, Account] = {} last_account = None