Skip to content

Commit

Permalink
"$thing filter options" -> "$thing filter", "$thing sort order" -> "$…
Browse files Browse the repository at this point in the history
…thing sort".

* filter_options -> filter
* FeedFilterOptions -> FeedFilter
* EntryFilterOptions -> EntryFilter
* FeedSortOrder -> FeedSort
* EntrySortOrder -> EntrySort

For #325.
  • Loading branch information
lemon24 committed Oct 28, 2023
1 parent abf1c1b commit 654c83c
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 128 deletions.
22 changes: 11 additions & 11 deletions src/reader/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
from ._sqlite_utils import SQLiteType
from ._sqlite_utils import wrap_exceptions
from ._sqlite_utils import wrap_exceptions_iter
from ._storage import apply_entry_filter_options
from ._storage import apply_entry_filter
from ._storage import apply_random
from ._storage import apply_recent
from ._storage import make_entry_counts_query
from ._storage import Storage
from ._types import EntryFilterOptions
from ._types import EntryFilter
from ._utils import exactly_one
from ._utils import join_paginated_iter
from ._utils import zero_or_one
Expand Down Expand Up @@ -747,7 +747,7 @@ def _insert_into_search_one_chunk(self) -> bool:
def search_entries(
self,
query: str,
filter_options: EntryFilterOptions = EntryFilterOptions(), # noqa: B008
filter: EntryFilter = EntryFilter(), # noqa: B008
sort: SearchSortOrder = 'relevant',
limit: int | None = None,
starting_after: tuple[str, str] | None = None,
Expand All @@ -762,7 +762,7 @@ def search_entries(
last = self.search_entry_last(query, starting_after)

rv = join_paginated_iter(
partial(self.search_entries_page, query, filter_options, sort), # type: ignore[arg-type]
partial(self.search_entries_page, query, filter, sort), # type: ignore[arg-type]
self.chunk_size,
last,
limit or 0,
Expand All @@ -772,7 +772,7 @@ def search_entries(
assert not starting_after
it = self.search_entries_page(
query,
filter_options,
filter,
sort,
min(limit, self.chunk_size or limit) if limit else self.chunk_size,
)
Expand Down Expand Up @@ -808,12 +808,12 @@ def search_entry_last(self, query: str, entry: tuple[str, str]) -> tuple[Any, ..
def search_entries_page(
self,
query: str,
filter_options: EntryFilterOptions = EntryFilterOptions(), # noqa: B008
filter: EntryFilter = EntryFilter(), # noqa: B008
sort: SearchSortOrder = 'relevant',
chunk_size: int | None = None,
last: _T | None = None,
) -> Iterable[tuple[EntrySearchResult, _T | None]]:
sql_query, context = make_search_entries_query(filter_options, sort)
sql_query, context = make_search_entries_query(filter, sort)

random_mark = ''.join(
random.choices(string.ascii_letters + string.digits, k=20)
Expand Down Expand Up @@ -846,7 +846,7 @@ def search_entry_counts(
self,
query: str,
now: datetime,
filter_options: EntryFilterOptions = EntryFilterOptions(), # noqa: B008
filter: EntryFilter = EntryFilter(), # noqa: B008
) -> EntrySearchCounts:
entries_query = (
Query()
Expand All @@ -865,7 +865,7 @@ def search_entry_counts(
.FROM('entries')
.JOIN("search ON (id, feed) = (_id, _feed)")
)
query_context = apply_entry_filter_options(entries_query, filter_options)
query_context = apply_entry_filter(entries_query, filter)

sql_query, new_context = make_entry_counts_query(
now, self.storage.entry_counts_average_periods, entries_query
Expand All @@ -878,7 +878,7 @@ def search_entry_counts(


def make_search_entries_query(
filter_options: EntryFilterOptions, sort: SearchSortOrder
filter: EntryFilter, sort: SearchSortOrder
) -> tuple[Query, dict[str, Any]]:
search = (
Query()
Expand Down Expand Up @@ -915,7 +915,7 @@ def make_search_entries_query(
.LIMIT("-1 OFFSET 0")
)

context = apply_entry_filter_options(search, filter_options)
context = apply_entry_filter(search, filter)

query = (
Query()
Expand Down
76 changes: 37 additions & 39 deletions src/reader/_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from ._sqlite_utils import setup_db as setup_sqlite_db
from ._sqlite_utils import wrap_exceptions
from ._sqlite_utils import wrap_exceptions_iter
from ._types import EntryFilterOptions
from ._types import EntryFilter
from ._types import EntryForUpdate
from ._types import EntryUpdateIntent
from ._types import FeedFilterOptions
from ._types import FeedFilter
from ._types import FeedForUpdate
from ._types import FeedUpdateIntent
from ._types import TagFilter
Expand All @@ -47,11 +47,11 @@
from .types import Enclosure
from .types import Entry
from .types import EntryCounts
from .types import EntrySortOrder
from .types import EntrySort
from .types import ExceptionInfo
from .types import Feed
from .types import FeedCounts
from .types import FeedSortOrder
from .types import FeedSort
from .types import JSONType
from .types import MISSING
from .types import MissingType
Expand Down Expand Up @@ -527,13 +527,13 @@ def change_feed_url(self, old: str, new: str) -> None:

def get_feeds(
self,
filter_options: FeedFilterOptions = FeedFilterOptions(), # noqa: B008
sort: FeedSortOrder = 'title',
filter: FeedFilter = FeedFilter(), # noqa: B008
sort: FeedSort = 'title',
limit: int | None = None,
starting_after: str | None = None,
) -> Iterable[Feed]:
rv = join_paginated_iter(
partial(self.get_feeds_page, filter_options, sort), # type: ignore[arg-type]
partial(self.get_feeds_page, filter, sort), # type: ignore[arg-type]
self.chunk_size,
self.get_feed_last(sort, starting_after) if starting_after else None,
limit or 0,
Expand Down Expand Up @@ -562,20 +562,20 @@ def get_feed_last(self, sort: str, url: str) -> tuple[Any, ...]:
@wrap_exceptions_iter(StorageError)
def get_feeds_page(
self,
filter_options: FeedFilterOptions = FeedFilterOptions(), # noqa: B008
sort: FeedSortOrder = 'title',
filter: FeedFilter = FeedFilter(), # noqa: B008
sort: FeedSort = 'title',
chunk_size: int | None = None,
last: _T | None = None,
) -> Iterable[tuple[Feed, _T | None]]:
query, context = make_get_feeds_query(filter_options, sort)
query, context = make_get_feeds_query(filter, sort)
yield from paginated_query(
self.get_db(), query, context, chunk_size, last, feed_factory
)

@wrap_exceptions(StorageError)
def get_feed_counts(
self,
filter_options: FeedFilterOptions = FeedFilterOptions(), # noqa: B008
filter: FeedFilter = FeedFilter(), # noqa: B008
) -> FeedCounts:
query = (
Query()
Expand All @@ -587,7 +587,7 @@ def get_feed_counts(
.FROM("feeds")
)

context = apply_feed_filter_options(query, filter_options)
context = apply_feed_filter(query, filter)

row = exactly_one(self.get_db().execute(str(query), context))

Expand All @@ -596,7 +596,7 @@ def get_feed_counts(
@wrap_exceptions_iter(StorageError)
def get_feeds_for_update(
self,
filter_options: FeedFilterOptions = FeedFilterOptions(), # noqa: B008
filter: FeedFilter = FeedFilter(), # noqa: B008
) -> Iterable[FeedForUpdate]:
# Reader shouldn't care this is paginated,
# so we don't expose any pagination stuff.
Expand All @@ -621,7 +621,7 @@ def inner(

# TODO: stale and last_exception should be bool, not int

context = apply_feed_filter_options(query, filter_options)
context = apply_feed_filter(query, filter)

query.scrolling_window_order_by("url")

Expand Down Expand Up @@ -1056,23 +1056,23 @@ def delete_entries(

def get_entries(
self,
filter_options: EntryFilterOptions = EntryFilterOptions(), # noqa: B008
sort: EntrySortOrder = 'recent',
filter: EntryFilter = EntryFilter(), # noqa: B008
sort: EntrySort = 'recent',
limit: int | None = None,
starting_after: tuple[str, str] | None = None,
) -> Iterable[Entry]:
# TODO: deduplicate
if sort == 'recent':
rv = join_paginated_iter(
partial(self.get_entries_page, filter_options, sort), # type: ignore[arg-type]
partial(self.get_entries_page, filter, sort), # type: ignore[arg-type]
self.chunk_size,
self.get_entry_last(sort, starting_after) if starting_after else None,
limit or 0,
)
elif sort == 'random':
assert not starting_after
it = self.get_entries_page(
filter_options,
filter,
sort,
min(limit, self.chunk_size or limit) if limit else self.chunk_size,
)
Expand Down Expand Up @@ -1108,12 +1108,12 @@ def get_entry_last(self, sort: str, entry: tuple[str, str]) -> tuple[Any, ...]:
@wrap_exceptions_iter(StorageError)
def get_entries_page(
self,
filter_options: EntryFilterOptions = EntryFilterOptions(), # noqa: B008
sort: EntrySortOrder = 'recent',
filter: EntryFilter = EntryFilter(), # noqa: B008
sort: EntrySort = 'recent',
chunk_size: int | None = None,
last: _T | None = None,
) -> Iterable[tuple[Entry, _T | None]]:
query, context = make_get_entries_query(filter_options, sort)
query, context = make_get_entries_query(filter, sort)
yield from paginated_query(
self.get_db(), query, context, chunk_size, last, entry_factory
)
Expand All @@ -1122,10 +1122,10 @@ def get_entries_page(
def get_entry_counts(
self,
now: datetime,
filter_options: EntryFilterOptions = EntryFilterOptions(), # noqa: B008
filter: EntryFilter = EntryFilter(), # noqa: B008
) -> EntryCounts:
entries_query = Query().SELECT('id', 'feed').FROM('entries')
context = apply_entry_filter_options(entries_query, filter_options)
context = apply_entry_filter(entries_query, filter)

query, new_context = make_entry_counts_query(
now, self.entry_counts_average_periods, entries_query
Expand Down Expand Up @@ -1269,7 +1269,7 @@ def delete_tag(self, resource_id: ResourceId, key: str) -> None:


def make_get_feeds_query(
filter_options: FeedFilterOptions, sort: FeedSortOrder
filter: FeedFilter, sort: FeedSort
) -> tuple[Query, dict[str, Any]]:
query = (
Query()
Expand All @@ -1290,7 +1290,7 @@ def make_get_feeds_query(
.FROM("feeds")
)

context = apply_feed_filter_options(query, filter_options)
context = apply_feed_filter(query, filter)

# NOTE: when changing, ensure none of the values can be null
# to prevent https://github.com/lemon24/reader/issues/203
Expand Down Expand Up @@ -1318,19 +1318,19 @@ def feed_factory(t: tuple[Any, ...]) -> Feed:
)


def apply_feed_filter_options(
def apply_feed_filter(
query: Query,
filter_options: FeedFilterOptions,
filter: FeedFilter,
) -> dict[str, Any]:
url, tags, broken, updates_enabled, new = filter_options
url, tags, broken, updates_enabled, new = filter

context: dict[str, object] = {}

if url:
query.WHERE("url = :url")
context.update(url=url)

context.update(apply_feed_tags_filter_options(query, tags, 'feeds.url'))
context.update(apply_feed_tags_filter(query, tags, 'feeds.url'))

if broken is not None:
query.WHERE(f"last_exception IS {'NOT' if broken else ''} NULL")
Expand All @@ -1343,8 +1343,8 @@ def apply_feed_filter_options(


def make_get_entries_query(
filter_options: EntryFilterOptions,
sort: EntrySortOrder,
filter: EntryFilter,
sort: EntrySort,
) -> tuple[Query, dict[str, Any]]:
query = (
Query()
Expand Down Expand Up @@ -1385,7 +1385,7 @@ def make_get_entries_query(
.JOIN("feeds ON feeds.url = entries.feed")
)

filter_context = apply_entry_filter_options(query, filter_options)
filter_context = apply_entry_filter(query, filter)

if sort == 'recent':
apply_recent(query)
Expand Down Expand Up @@ -1429,11 +1429,11 @@ def entry_factory(t: tuple[Any, ...]) -> Entry:
)


def apply_entry_filter_options(
query: Query, filter_options: EntryFilterOptions, keyword: str = 'WHERE'
def apply_entry_filter(
query: Query, filter: EntryFilter, keyword: str = 'WHERE'
) -> dict[str, Any]:
add = getattr(query, keyword)
feed_url, entry_id, read, important, has_enclosures, feed_tags = filter_options
feed_url, entry_id, read, important, has_enclosures, feed_tags = filter

context = {}

Expand All @@ -1460,15 +1460,13 @@ def apply_entry_filter_options(
)

context.update(
apply_feed_tags_filter_options(
query, feed_tags, 'entries.feed', keyword=keyword
)
apply_feed_tags_filter(query, feed_tags, 'entries.feed', keyword=keyword)
)

return context


def apply_feed_tags_filter_options(
def apply_feed_tags_filter(
query: Query,
tags: TagFilter,
url_column: str,
Expand Down
Loading

0 comments on commit 654c83c

Please sign in to comment.