Begin nomie parser creation
This commit is contained in:
parent
a825287642
commit
bb4b85851e
4 changed files with 44 additions and 14 deletions
10
src/habitmove/nomie_parser.py
Normal file
10
src/habitmove/nomie_parser.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from __future__ import annotations
|
||||
from json import loads as jsonloads
|
||||
|
||||
from habitmove.parser import Parser
|
||||
|
||||
|
||||
class NomieParser(Parser):
|
||||
def __init__(self, data="{}") -> None:
|
||||
"""Load a data set and prepare parser data"""
|
||||
self.data = jsonloads(data)
|
|
@ -1,11 +1,22 @@
|
|||
from __future__ import annotations
|
||||
from pathlib import Path
|
||||
|
||||
from habitmove.nomiedata import Event, ImportData, Tracker
|
||||
|
||||
|
||||
class Parser:
|
||||
def parse(self, path: str, filename: str) -> ImportData:
|
||||
def __init__(self, data="") -> None:
|
||||
"""Load a data set and prepare parser data"""
|
||||
self.data = data
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, path: str) -> Parser:
|
||||
"""Load in a data set"""
|
||||
txt = Path(path).read_text()
|
||||
return cls(data=txt)
|
||||
|
||||
def parse(self) -> ImportData:
|
||||
"""Extract all data from a data set"""
|
||||
raise NotImplementedError
|
||||
|
||||
def extract_version(self) -> str:
|
||||
|
@ -19,7 +30,3 @@ class Parser:
|
|||
def extract_events(self) -> list[Event]:
|
||||
"""Extract events from the data set"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class NomieParser(Parser):
|
||||
pass
|
||||
|
|
18
tests/test_nomie_parser.py
Normal file
18
tests/test_nomie_parser.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from habitmove.nomie_parser import NomieParser
|
||||
import json
|
||||
import pytest
|
||||
|
||||
|
||||
def test_nomie_parser_exists():
|
||||
sut = NomieParser()
|
||||
assert type(sut) == NomieParser
|
||||
|
||||
|
||||
def test_nomie_Parser_errors_on_invalid_data():
|
||||
with pytest.raises(json.decoder.JSONDecodeError):
|
||||
NomieParser(data="invalid_test_data")
|
||||
|
||||
|
||||
def test_nomie_Parser_saves_data():
|
||||
sut = NomieParser(data='{"test": "entry"}')
|
||||
assert sut.data == {"test": "entry"}
|
|
@ -1,4 +1,4 @@
|
|||
from habitmove.parser import Parser, NomieParser
|
||||
from habitmove.parser import Parser
|
||||
from inspect import signature
|
||||
|
||||
|
||||
|
@ -10,14 +10,14 @@ def test_parser_interface_exists():
|
|||
def test_parser_interface_contains_methods():
|
||||
sut = Parser()
|
||||
assert sut.__getattribute__("parse") != None
|
||||
assert sut.__getattribute__("load_file") != None
|
||||
assert sut.__getattribute__("from_file") != None
|
||||
assert sut.__getattribute__("extract_version") != None
|
||||
assert sut.__getattribute__("extract_trackers") != None
|
||||
assert sut.__getattribute__("extract_events") != None
|
||||
|
||||
|
||||
def test_parser_load_file_returns_parser():
|
||||
sut = Parser().load_file
|
||||
def test_parser_from_file_returns_parser():
|
||||
sut = Parser().from_file
|
||||
assert signature(sut).return_annotation == "Parser"
|
||||
|
||||
|
||||
|
@ -39,8 +39,3 @@ def test_parser_extract_trackers_returns_tracker_list():
|
|||
def test_parser_extract_events_returns_event_list():
|
||||
sut = Parser().extract_events
|
||||
assert signature(sut).return_annotation == "list[Event]"
|
||||
|
||||
|
||||
def test_nomie_parser_exists():
|
||||
sut = NomieParser()
|
||||
assert type(sut) == NomieParser
|
||||
|
|
Loading…
Reference in a new issue