Skip to content

Commit e53f2c6

Browse files
committed
generate state of library from log
1 parent 6b67f0b commit e53f2c6

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

cli.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
import typer
44
from devtools import debug
55

6-
from library.parse import parse_log
6+
import library
77

88
app = typer.Typer()
99

1010

1111
@app.command()
12-
def cmd_parse_log():
12+
def parse_log():
1313
log_path = Path("./log.toml")
14-
debug(parse_log(log_path))
14+
debug(library.parse.parse_log(log_path))
15+
16+
17+
@app.command()
18+
def state_from_log():
19+
log_path = Path("./log.toml")
20+
log = library.parse.parse_log(log_path)
21+
debug(library.parse.state_from_log(log))
1522

1623

1724
if __name__ == "__main__":

library/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from library.parse import *
2+
from library.models import *

library/models.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,19 @@ class Action(BaseModel):
4343

4444

4545
class Log(BaseModel):
46-
actions: List[Action]
46+
actions: List[Action] = []
47+
48+
49+
class StateUser(User):
50+
owned_books: List[Book] = []
51+
held_books: List[Book] = []
52+
53+
54+
class StateBook(Book):
55+
owned_by: User
56+
held_by: User
57+
58+
59+
class State(BaseModel):
60+
users: List[StateUser] = []
61+
books: List[StateBook] = []

library/parse.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
from pathlib import Path
2+
from typing import List
23

34
import toml
45

5-
from library.models import Log
6+
from library.models import (
7+
Book,
8+
Log,
9+
AddBook,
10+
AddUser,
11+
LendBook,
12+
State,
13+
StateBook,
14+
StateUser,
15+
User,
16+
)
617

718

819
def parse_log(log_path: Path) -> Log:
920
return Log(**toml.loads(log_path.read_text()))
21+
22+
23+
def find_user(users: List[StateUser], name: str):
24+
return next(u for u in users if u.name == name)
25+
26+
27+
def find_book(books: List[StateBook], id: str):
28+
return next(b for b in books if b.id == id)
29+
30+
31+
def state_from_log(log: Log) -> State:
32+
state = State()
33+
34+
for action in [a.__root__ for a in log.actions]:
35+
if isinstance(action, AddUser):
36+
state.users.append(StateUser(**action.dict()))
37+
elif isinstance(action, AddBook):
38+
state_owner = find_user(state.users, action.user)
39+
state_owner.owned_books.append(action.book)
40+
state_owner.held_books.append(action.book)
41+
owner = User(**state_owner.dict())
42+
book = StateBook(**action.book.dict(), owned_by=owner, held_by=owner)
43+
state.books.append(book)
44+
elif isinstance(action, LendBook):
45+
from_user = find_user(state.users, action.from_user)
46+
to_user = find_user(state.users, action.to_user)
47+
state_book = find_book(state.books, action.book_id)
48+
state_book.held_by = User(**to_user.dict())
49+
book = Book(**state_book.dict())
50+
from_user.held_books.remove(book)
51+
to_user.held_books.append(book)
52+
53+
return state

0 commit comments

Comments
 (0)