Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parse_string function to Qif class #109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions quiffen/core/qif.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ def parse(
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 data string is empty.")

accounts: Dict[str, Account] = {}
last_account = None
categories: Dict[str, Category] = {}
Expand Down
25 changes: 23 additions & 2 deletions tests/test_qif.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading