summaryrefslogtreecommitdiffstats
path: root/src/parser.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.py')
-rw-r--r--src/parser.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/parser.py b/src/parser.py
index d2b4ce7..ba7d247 100644
--- a/src/parser.py
+++ b/src/parser.py
@@ -30,13 +30,13 @@ id: card.Card
from pathlib import Path
from enum import Enum
from typing import TextIO, Iterator
-from card import Card
+from card import Card, getId
def _getCard(front_lines: list[str], back_lines: list[str]) -> tuple[int, Card]:
front_text = "".join(front_lines).strip()
back_text = "".join(back_lines).strip()
card = Card(front_text, back_text)
- id = hash(card)
+ id = getId(card)
return id, card
def _getCards(f: TextIO) -> Iterator[tuple[id, Card]]:
@@ -90,13 +90,19 @@ def _getCards(f: TextIO) -> Iterator[tuple[id, Card]]:
yield _getCard(front_lines, back_lines)
-def parse(path: Path) -> dict[int, Card]:
+def parseFile(path: str) -> dict[int, Card]:
"""
Parse a .fcard file and return a dictionary of Card instances indexed by ID.
"""
- if not path.is_file():
- print(f"[Warning] Not a file: {path}")
- return {}
-
with open(path, "r") as f:
return {id : card for id, card in _getCards(f)}
+
+def parseFiles(paths: list[str]) -> dict[int, Card]:
+ """
+ Parse a list of .fcard files and return a dictionary of Card instances indexed by ID.
+ """
+ cards = {}
+ for p in paths:
+ cards |= parseFile(p)
+ return cards
+