Skip to content

Commit

Permalink
Add typehints in book_providers.py and app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
yivgen committed Aug 23, 2024
1 parent bf4bc9d commit 3d4f8c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
3 changes: 2 additions & 1 deletion openlibrary/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from infogami.utils import app as _app
from infogami.utils.view import render, public
from infogami.utils.macro import macro
from web.template import TemplateResult


class view(_app.page):
Expand Down Expand Up @@ -63,7 +64,7 @@ class work_identifiers(delegate.view):

@macro
@public
def render_template(name, *a, **kw):
def render_template(name: str, *a, **kw) -> TemplateResult:
if "." in name:
name = name.rsplit(".", 1)[0]
return render[name](*a, **kw)
29 changes: 17 additions & 12 deletions openlibrary/book_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import web
from web import uniq
from web.template import TemplateResult

from openlibrary.app import render_template
from openlibrary.plugins.upstream.models import Edition
Expand Down Expand Up @@ -147,7 +148,7 @@ class AbstractBookProvider(Generic[TProviderMetadata]):
"""
identifier_key: str | None

def get_olids(self, identifier):
def get_olids(self, identifier: str) -> list[str]:
return web.ctx.site.things(
{"type": "/type/edition", self.db_selector: identifier}
)
Expand All @@ -157,7 +158,7 @@ def editions_query(self):
return {f"{self.db_selector}~": "*"}

@property
def db_selector(self):
def db_selector(self) -> str:
return f"identifiers.{self.identifier_key}"

@property
Expand Down Expand Up @@ -190,14 +191,16 @@ def get_template_path(self, typ: Literal['read_button', 'download_options']) ->

def render_read_button(
self, ed_or_solr: Edition | dict, analytics_attr: Callable[[str], str]
):
) -> TemplateResult:
return render_template(
self.get_template_path('read_button'),
self.get_best_identifier(ed_or_solr),
analytics_attr,
)

def render_download_options(self, edition: Edition, extra_args: list | None = None):
def render_download_options(
self, edition: Edition, extra_args: list | None = None
) -> TemplateResult:
return render_template(
self.get_template_path('download_options'),
self.get_best_identifier(edition),
Expand Down Expand Up @@ -234,11 +237,11 @@ class InternetArchiveProvider(AbstractBookProvider[IALiteMetadata]):
identifier_key = 'ocaid'

@property
def db_selector(self):
def db_selector(self) -> str:
return self.identifier_key

@property
def solr_key(self):
def solr_key(self) -> str:
return "ia"

def get_identifiers(self, ed_or_solr: Edition | dict) -> list[str]:
Expand All @@ -258,7 +261,9 @@ def get_identifiers(self, ed_or_solr: Edition | dict) -> list[str]:
def is_own_ocaid(self, ocaid: str) -> bool:
return True

def render_download_options(self, edition: Edition, extra_args: list | None = None):
def render_download_options(
self, edition: Edition, extra_args: list | None = None
) -> TemplateResult | str:
if edition.is_access_restricted():
return ''

Expand Down Expand Up @@ -430,11 +435,11 @@ class DirectProvider(AbstractBookProvider):
identifier_key = None

@property
def db_selector(self):
def db_selector(self) -> str:
return "providers.url"

@property
def solr_key(self):
def solr_key(self) -> None:
# TODO: Not implemented yet
return None

Expand Down Expand Up @@ -469,7 +474,7 @@ def get_identifiers(self, ed_or_solr: Edition | dict) -> list[str]:

def render_read_button(
self, ed_or_solr: Edition | dict, analytics_attr: Callable[[str], str]
):
) -> TemplateResult | str:
acq_sorted = sorted(
(
p
Expand Down Expand Up @@ -595,7 +600,7 @@ def get_book_provider_by_name(short_name: str) -> AbstractBookProvider | None:
prefer_ia_provider_order = uniq([ia_provider, *PROVIDER_ORDER])


def get_provider_order(prefer_ia=False) -> list[AbstractBookProvider]:
def get_provider_order(prefer_ia: bool = False) -> list[AbstractBookProvider]:
default_order = prefer_ia_provider_order if prefer_ia else PROVIDER_ORDER

provider_order = default_order
Expand Down Expand Up @@ -679,7 +684,7 @@ def get_best_edition(
return best if best else (None, None)


def get_solr_keys():
def get_solr_keys() -> list[str]:
return [p.solr_key for p in PROVIDER_ORDER if p.solr_key]


Expand Down

0 comments on commit 3d4f8c9

Please sign in to comment.