diff options
author | Eddy Pedroni <epedroni@pm.me> | 2024-09-25 16:12:53 +0200 |
---|---|---|
committer | Eddy Pedroni <epedroni@pm.me> | 2024-09-25 16:12:53 +0200 |
commit | 28afae563e3e32187b389db4680bcc08b9fc55a9 (patch) | |
tree | 0cb940560297401924316f3fc7226bd7983eb2ad /src/parser.py | |
parent | fe36d1261dc96004e4d4e692a65e1bc53137726b (diff) |
General improvements, CLI initial implementation
Diffstat (limited to 'src/parser.py')
-rw-r--r-- | src/parser.py | 20 |
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 + |