Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
fshowalter committed Nov 23, 2023
1 parent ba619a1 commit 281831c
Show file tree
Hide file tree
Showing 19 changed files with 497 additions and 221 deletions.
10 changes: 4 additions & 6 deletions booklog/cli/add_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@ def ask_for_edition(state: State) -> State:
state.stage = "ask_for_timeline"
return state

if confirm("{0}?".format(selected_edition)):
state.edition = selected_edition
state.stage = "ask_for_grade"
state.edition = selected_edition
state.stage = "ask_for_grade"

return state

Expand Down Expand Up @@ -244,8 +243,7 @@ def ask_for_grade(state: State) -> State:
state.stage = "ask_for_edition"
return state

if confirm(review_grade): # noqa: WPS323
state.grade = review_grade
state.stage = "persist_reading"
state.grade = review_grade
state.stage = "persist_reading"

return state
2 changes: 1 addition & 1 deletion booklog/cli/add_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@dataclass(kw_only=True)
class State(object):
stage: Stages = "ask_for_authors"
kind: Optional[repository_api.WORK_KIND_TYPE] = None
kind: Optional[repository_api.Kind] = None
title: Optional[str] = None
work_authors: list[repository_api.WorkAuthor] = field(default_factory=list)
subtitle: Optional[str] = None
Expand Down
12 changes: 7 additions & 5 deletions booklog/cli/select_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def prompt() -> Optional[repository_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 search_works(query: str) -> list[repository_api.Work]:
return list(
filter(
lambda work: query.lower()
in "{0}: {1}".format(work.title, work.subtitle).lower(),
repository_api.works(),
)
)


Expand Down
32 changes: 13 additions & 19 deletions booklog/exports/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,24 @@
from booklog.exports import (
authors,
reading_stats,
reading_timeline_entries,
reviewed_works,
timeline_entries,
unreviewed_works,
)
from booklog.exports.repository_data import RepositoryData
from booklog.repository import api as repository_api


def export_data() -> None:
all_authors = list(repository_api.authors())
all_works = list(repository_api.works())
all_reviews = list(repository_api.reviews())
all_readings = list(repository_api.readings())

authors.export(
all_authors=all_authors, all_works=all_works, all_reviews=all_reviews
)
reviewed_works.export(
all_works=all_works,
all_authors=all_authors,
all_reviews=all_reviews,
all_readings=all_readings,
)
reading_timeline_entries.export(readings=repository_api.readings())
unreviewed_works.export(works=repository_api.works())
reading_stats.export(
readings=repository_api.readings(), reviews=repository_api.reviews()
repository_data = RepositoryData(
authors=list(repository_api.authors()),
works=list(repository_api.works()),
reviews=list(repository_api.reviews()),
readings=list(repository_api.readings()),
)

authors.export(repository_data)
reviewed_works.export(repository_data)
timeline_entries.export(repository_data)
unreviewed_works.export(repository_data)
reading_stats.export(repository_data)
40 changes: 18 additions & 22 deletions booklog/exports/authors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional, TypedDict

from booklog.exports import exporter, json_work_author
from booklog.exports.repository_data import RepositoryData
from booklog.repository import api as repository_api
from booklog.utils.logging import logger

Expand Down Expand Up @@ -36,8 +37,7 @@
def build_json_author_work(
work: repository_api.Work,
review: Optional[repository_api.Review],
all_works: list[repository_api.Work],
all_authors: list[repository_api.Author],
repository_data: RepositoryData,
) -> JsonAuthorWork:
return JsonAuthorWork(
title=work.title,
Expand All @@ -50,23 +50,26 @@ def build_json_author_work(
gradeValue=review.grade_value if review else None,
authors=[
json_work_author.build_json_work_author(
work_author=work_author, all_authors=all_authors
work_author=work_author, all_authors=repository_data.authors
)
for work_author in work.work_authors
],
includedInSlugs=[work.slug for work in work.included_in_works(all_works)],
includedInSlugs=[
work.slug for work in work.included_in_works(repository_data.works)
],
)


def build_json_author(
author: repository_api.Author,
all_works: list[repository_api.Work],
all_reviews: list[repository_api.Review],
all_authors: list[repository_api.Author],
author: repository_api.Author, repository_data: RepositoryData
) -> JsonAuthor:
author_works = list(author.works(all_works))
author_works = list(author.works(repository_data.works))
reviewed_work_count = len(
[author_work for author_work in author_works if author_work.review(all_reviews)]
[
author_work
for author_work in author_works
if author_work.review(repository_data.reviews)
]
)

return JsonAuthor(
Expand All @@ -76,9 +79,8 @@ def build_json_author(
works=[
build_json_author_work(
work=work,
review=work.review(all_reviews),
all_works=all_works,
all_authors=all_authors,
review=work.review(repository_data.reviews),
repository_data=repository_data,
)
for work in author_works
],
Expand All @@ -87,21 +89,15 @@ def build_json_author(
)


def export(
all_authors: list[repository_api.Author],
all_works: list[repository_api.Work],
all_reviews: list[repository_api.Review],
) -> None:
def export(repository_data: RepositoryData) -> None:
logger.log("==== Begin exporting {}...", "authors")

json_authors = [
build_json_author(
author=author,
all_works=all_works,
all_reviews=all_reviews,
all_authors=all_authors,
repository_data=repository_data,
)
for author in all_authors
for author in repository_data.authors
]

exporter.serialize_dicts_to_folder(
Expand Down
98 changes: 57 additions & 41 deletions booklog/exports/reading_stats.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from collections import defaultdict
from datetime import date
from typing import Callable, Iterable, TypedDict, TypeVar
from typing import Callable, TypedDict, TypeVar

from booklog.exports import exporter, list_tools
from booklog.repository.api import Reading, Review, Work
from booklog.exports.repository_data import RepositoryData
from booklog.repository import api as repository_api
from booklog.utils.logging import logger

JsonMostReadAuthorReading = TypedDict(
Expand Down Expand Up @@ -59,7 +60,7 @@


def build_json_distributions(
distribution_items: Iterable[ListType], key: Callable[[ListType], str]
distribution_items: list[ListType], key: Callable[[ListType], str]
) -> list[JsonDistribution]:
distribution = list_tools.group_list_by_key(distribution_items, key)

Expand All @@ -69,7 +70,7 @@ def build_json_distributions(
]


def date_finished_or_abandoned(reading: Reading) -> date:
def date_finished_or_abandoned(reading: repository_api.Reading) -> date:
return next(
timeline_entry.date
for timeline_entry in reading.timeline
Expand All @@ -78,19 +79,21 @@ def date_finished_or_abandoned(reading: Reading) -> date:


def group_readings_by_author(
readings: list[Reading],
) -> dict[str, list[Reading]]:
readings_by_author: dict[str, list[Reading]] = defaultdict(list)
readings: list[repository_api.Reading], repository_data: RepositoryData
) -> dict[str, list[repository_api.Reading]]:
readings_by_author: dict[str, list[repository_api.Reading]] = defaultdict(list)

for reading in readings:
for work_author in reading.work().work_authors:
for work_author in reading.work(repository_data.works).work_authors:
readings_by_author[work_author.author_slug].append(reading)

return readings_by_author


def build_json_most_read_author_reading(reading: Reading) -> JsonMostReadAuthorReading:
work = reading.work()
def build_json_most_read_author_reading(
reading: repository_api.Reading, repository_data: RepositoryData
) -> JsonMostReadAuthorReading:
work = reading.work(repository_data.works)

return JsonMostReadAuthorReading(
sequence=reading.sequence,
Expand All @@ -101,26 +104,32 @@ def build_json_most_read_author_reading(reading: Reading) -> JsonMostReadAuthorR
title=work.title,
yearPublished=work.year,
includedInSlugs=[
included_in_work.slug for included_in_work in work.included_in_works()
included_in_work.slug
for included_in_work in work.included_in_works(repository_data.works)
],
)


def build_most_read_authors(readings: list[Reading]) -> list[JsonMostReadAuthor]:
readings_by_author = group_readings_by_author(readings=readings)
def build_most_read_authors(
readings: list[repository_api.Reading], repository_data: RepositoryData
) -> list[JsonMostReadAuthor]:
readings_by_author = group_readings_by_author(
readings=readings, repository_data=repository_data
)

return [
JsonMostReadAuthor(
name=next(
reading_work_author.author().name
for reading in readings
for reading_work_author in reading.work().work_authors
if reading_work_author.author_slug == author_slug
author.name
for author in repository_data.authors
if author.slug == author_slug
),
count=len(readings),
slug=author_slug,
readings=[
build_json_most_read_author_reading(reading=reading)
build_json_most_read_author_reading(
reading=reading, repository_data=repository_data
)
for reading in readings
],
)
Expand All @@ -129,43 +138,50 @@ def build_most_read_authors(readings: list[Reading]) -> list[JsonMostReadAuthor]
]


def build_grade_distribution(reviews: list[Review]) -> list[JsonDistribution]:
def build_grade_distribution(
reviews: list[repository_api.Review],
) -> list[JsonDistribution]:
return build_json_distributions(reviews, lambda review: review.grade)


def build_kind_distribution(works: list[Work]) -> list[JsonDistribution]:
def build_kind_distribution(works: list[repository_api.Work]) -> list[JsonDistribution]:
return build_json_distributions(works, lambda work: work.kind)


def build_edition_distribution(
readings: list[Reading],
readings: list[repository_api.Reading],
) -> list[JsonDistribution]:
return build_json_distributions(readings, lambda reading: reading.edition)


def build_decade_distribution(works: list[Work]) -> list[JsonDistribution]:
def build_decade_distribution(
works: list[repository_api.Work],
) -> list[JsonDistribution]:
return build_json_distributions(works, lambda work: "{0}0s".format(work.year[:3]))


def book_count(readings: list[Reading]) -> int:
works = [reading.work() for reading in readings]
def book_count(
readings: list[repository_api.Reading], repository_data: RepositoryData
) -> int:
works = [reading.work(repository_data.works) for reading in readings]

return len([work for work in works if work.kind not in {"Short Story", "Novella"}])


def build_json_reading_stats(
span: str,
readings: list[Reading],
reviews: list[Review],
readings: list[repository_api.Reading],
reviews: list[repository_api.Review],
most_read_authors: list[JsonMostReadAuthor],
repository_data: RepositoryData,
) -> JsonReadingStats:
works = [reading.work() for reading in readings]
works = [reading.work(repository_data.works) for reading in readings]

return JsonReadingStats(
span=span,
reviews=len(reviews),
readWorks=len(readings),
books=book_count(readings),
books=book_count(readings, repository_data=repository_data),
gradeDistribution=build_grade_distribution(reviews),
kindDistribution=build_kind_distribution(works),
editionDistribution=build_edition_distribution(readings),
Expand All @@ -174,30 +190,27 @@ def build_json_reading_stats(
)


def export(
readings: Iterable[Reading],
reviews: Iterable[Review],
) -> None:
def export(repository_data: RepositoryData) -> None:
logger.log("==== Begin exporting {}...", "reading_stats")

all_readings = list(readings)
all_reviews = list(reviews)

json_reading_stats = [
build_json_reading_stats(
span="all-time",
reviews=all_reviews,
readings=all_readings,
most_read_authors=build_most_read_authors(readings=all_readings),
reviews=repository_data.reviews,
readings=repository_data.readings,
most_read_authors=build_most_read_authors(
readings=repository_data.readings, repository_data=repository_data
),
repository_data=repository_data,
)
]

reviews_by_year = list_tools.group_list_by_key(
all_reviews, lambda review: str(review.date.year)
repository_data.reviews, lambda review: str(review.date.year)
)

readings_by_year = list_tools.group_list_by_key(
all_readings,
repository_data.readings,
lambda reading: str(date_finished_or_abandoned(reading).year),
)

Expand All @@ -207,7 +220,10 @@ def export(
span=year,
reviews=reviews_by_year[year],
readings=readings_for_year,
most_read_authors=build_most_read_authors(readings=readings_for_year),
most_read_authors=build_most_read_authors(
readings=readings_for_year, repository_data=repository_data
),
repository_data=repository_data,
)
)

Expand Down
Loading

0 comments on commit 281831c

Please sign in to comment.