Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
fshowalter committed Nov 23, 2023
1 parent 50e1dbf commit ba619a1
Show file tree
Hide file tree
Showing 69 changed files with 1,222 additions and 1,371 deletions.
4 changes: 2 additions & 2 deletions booklog/cli/add_author.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from prompt_toolkit.shortcuts import confirm

from booklog.cli import ask
from booklog.data import api as data_api
from booklog.repository import api as repository_api

Option = Tuple[Optional[str], AnyFormattedText]

Expand All @@ -17,7 +17,7 @@ def prompt() -> None:
if not name:
return

data_api.create_author(name)
repository_api.create_author(name)


def ask_for_name() -> Optional[str]:
Expand Down
18 changes: 10 additions & 8 deletions booklog/cli/add_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from prompt_toolkit.validation import Validator

from booklog.cli import ask, radio_list, select_work
from booklog.data import api as data_api
from booklog.repository import api as repository_api

Option = Tuple[Optional[str], AnyFormattedText]

WorkOption = Tuple[Optional[data_api.Work], AnyFormattedText]

Stages = Literal[
"ask_for_work",
Expand All @@ -30,8 +29,8 @@
@dataclass(kw_only=True)
class State(object):
stage: Stages = "ask_for_work"
work: Optional[data_api.Work] = None
timeline: list[data_api.TimelineEntry] = field(default_factory=list)
work: Optional[repository_api.Work] = None
timeline: list[repository_api.TimelineEntry] = field(default_factory=list)
edition: Optional[str] = None
grade: Optional[str] = None

Expand All @@ -57,11 +56,14 @@ def persist_reading(state: State) -> State:
assert state.timeline
assert state.grade

data_api.create_reading(
repository_api.create_reading(
work=state.work,
edition=state.edition,
timeline=state.timeline,
grade=state.grade,
)

repository_api.create_or_update_review(
work=state.work, date=state.timeline[-1].date, grade=state.grade
)

if confirm("Add another reading?"):
Expand Down Expand Up @@ -170,7 +172,7 @@ def ask_for_timeline(state: State) -> State: # noqa: WPS231
continue

state.timeline.append(
data_api.TimelineEntry(date=timeline_date, progress=progress)
repository_api.TimelineEntry(date=timeline_date, progress=progress)
)

if progress in {"Finished", "Abandoned"}:
Expand Down Expand Up @@ -207,7 +209,7 @@ def ask_for_edition(state: State) -> State:


def build_edition_options() -> List[Option]:
editions = data_api.all_editions()
editions = repository_api.reading_editions()

options: list[Option] = [
(edition, "<cyan>{0}</cyan>".format(html.escape(edition)))
Expand Down
28 changes: 10 additions & 18 deletions booklog/cli/add_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import html
from dataclasses import dataclass, field
from typing import Callable, Literal, Optional, Tuple
from typing import Callable, Literal, Optional

from prompt_toolkit.formatted_text import AnyFormattedText
from prompt_toolkit.shortcuts import confirm

from booklog.cli import ask, radio_list, select_author, select_work
from booklog.cli.utils.array_to_sentence import array_to_sentence
from booklog.data import api as data_api

AuthorOption = Tuple[Optional[data_api.AuthorWithWorks], AnyFormattedText]

Option = Tuple[Optional[str], AnyFormattedText]
from booklog.repository import api as repository_api

Stages = Literal[
"ask_for_authors",
Expand All @@ -30,9 +25,9 @@
@dataclass(kw_only=True)
class State(object):
stage: Stages = "ask_for_authors"
kind: Optional[str] = None
kind: Optional[repository_api.WORK_KIND_TYPE] = None
title: Optional[str] = None
work_authors: list[data_api.WorkAuthor] = field(default_factory=list)
work_authors: list[repository_api.WorkAuthor] = field(default_factory=list)
subtitle: Optional[str] = None
year_published: Optional[str] = None
included_works: list[str] = field(default_factory=list)
Expand Down Expand Up @@ -61,7 +56,7 @@ def persist_work(state: State) -> State:
assert state.kind
assert state.work_authors

data_api.create_work(
repository_api.create_work(
title=state.title,
work_authors=state.work_authors,
subtitle=state.subtitle,
Expand All @@ -70,7 +65,9 @@ def persist_work(state: State) -> State:
included_work_slugs=state.included_works,
)

author_names = array_to_sentence([author.name for author in state.work_authors])
author_names = array_to_sentence(
[author.author().name for author in state.work_authors]
)

if confirm("Add more works by {0}?".format(author_names)):
state.stage = "ask_for_kind"
Expand Down Expand Up @@ -113,12 +110,7 @@ def ask_for_authors(state: State) -> State:
author_notes = None

state.work_authors.append(
data_api.WorkAuthor(
slug=author.slug,
notes=author_notes,
name=author.name,
sort_name=author.sort_name,
)
repository_api.WorkAuthor(notes=author_notes, author_slug=author.slug)
)

if not confirm("Add more Authors?"):
Expand Down Expand Up @@ -192,7 +184,7 @@ def ask_for_kind(state: State) -> State:
title="Select kind:",
options=[
(kind, "<cyan>{0}</cyan>".format(html.escape(kind)))
for kind in sorted(data_api.WORK_KINDS)
for kind in sorted(repository_api.WORK_KINDS)
],
)

Expand Down
6 changes: 3 additions & 3 deletions booklog/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from booklog.cli import add_author, add_reading, add_work, radio_list
from booklog.data import api as data_api
from booklog.logger import logger
from booklog.exports import api as exports_api
from booklog.utils.logging import logger


@logger.catch
Expand All @@ -22,4 +22,4 @@ def prompt() -> None:


def export() -> None:
data_api.export_data()
exports_api.export_data()
26 changes: 17 additions & 9 deletions booklog/cli/select_author.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
from __future__ import annotations

import html
from typing import List, Optional, Tuple
import itertools
from typing import Iterable, Optional, Tuple

from prompt_toolkit.formatted_text import AnyFormattedText

from booklog.cli import ask, radio_list
from booklog.data import api as data_api
from booklog.repository import api as repository_api

AuthorOption = Tuple[Optional[data_api.AuthorWithWorks], AnyFormattedText]
AuthorOption = Tuple[Optional[repository_api.Author], AnyFormattedText]


def prompt() -> Optional[data_api.AuthorWithWorks]:
def prompt() -> Optional[repository_api.Author]:
while True:
name = ask.prompt("Author: ")

if not name:
return None

authors = data_api.search_authors(name)
authors = search_authors(name)

options: list[AuthorOption] = build_author_options(authors)

Expand All @@ -33,15 +34,22 @@ def prompt() -> Optional[data_api.AuthorWithWorks]:
return selected_author


def format_author_works(author: data_api.AuthorWithWorks) -> str:
first_three_author_works = author.works[:3]
def search_authors(query: str) -> Iterable[repository_api.Author]:
return filter(
lambda author: query.lower() in author.name.lower(),
repository_api.authors(),
)


def format_author_works(author: repository_api.Author) -> str:
first_three_author_works = itertools.islice(author.works(), 3)

return ", ".join(html.escape(work.title) for work in first_three_author_works)


def build_author_options(
authors: list[data_api.AuthorWithWorks],
) -> List[AuthorOption]:
authors: Iterable[repository_api.Author],
) -> list[AuthorOption]:
if not authors:
return [(None, "Search Again")]

Expand Down
28 changes: 19 additions & 9 deletions booklog/cli/select_work.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
from __future__ import annotations

import html
from typing import List, Optional, Tuple
from typing import Iterable, Optional, Tuple

from prompt_toolkit.formatted_text import AnyFormattedText
from prompt_toolkit.shortcuts import confirm

from booklog.cli import ask, radio_list
from booklog.cli.utils.array_to_sentence import array_to_sentence
from booklog.data import api as data_api
from booklog.repository import api as repository_api

WorkOption = Tuple[Optional[data_api.Work], AnyFormattedText]
WorkOption = Tuple[Optional[repository_api.Work], AnyFormattedText]


def prompt() -> Optional[data_api.Work]:
def prompt() -> Optional[repository_api.Work]:
while True:
title = ask.prompt("Title: ")

if title is None:
return None

works = data_api.search_works(title)

works = search_works(title)
options = build_work_options(works)

selected_work = radio_list.prompt(
Expand All @@ -36,9 +35,17 @@ def prompt() -> Optional[data_api.Work]:
return selected_work


def search_works(query: str) -> Iterable[repository_api.Work]:
return filter(
lambda work: query.lower()
in "{0}: {1}".format(work.title, work.subtitle).lower(),
repository_api.works(),
)


def build_work_options(
works: list[data_api.Work],
) -> List[WorkOption]:
works: Iterable[repository_api.Work],
) -> list[WorkOption]:
if not works:
return [(None, "Search Again")]

Expand All @@ -48,7 +55,10 @@ def build_work_options(
"<cyan>{0}</cyan> by {1}".format(
html.escape(work.title),
array_to_sentence(
[html.escape(author.name) for author in work.authors]
[
html.escape(work_author.author().name)
for work_author in work.work_authors
]
),
),
)
Expand Down
53 changes: 0 additions & 53 deletions booklog/data/api.py

This file was deleted.

23 changes: 0 additions & 23 deletions booklog/data/core/api.py

This file was deleted.

Loading

0 comments on commit ba619a1

Please sign in to comment.