Skip to content

Commit

Permalink
Add Entry.source.
Browse files Browse the repository at this point in the history
For #239.
  • Loading branch information
lemon24 committed Oct 22, 2021
1 parent 8647ed7 commit 5cec50e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/reader/_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def create_entries(db: sqlite3.Connection, name: str = 'entries') -> None:
read_modified TIMESTAMP,
important INTEGER NOT NULL DEFAULT 0,
important_modified TIMESTAMP,
source TEXT NOT NULL,
last_updated TIMESTAMP NOT NULL,
first_updated TIMESTAMP NOT NULL,
first_updated_epoch TIMESTAMP NOT NULL,
Expand Down Expand Up @@ -277,6 +278,7 @@ def update_from_33_to_34(db: sqlite3.Connection) -> None: # pragma: no cover
read_modified,
important,
important_modified,
source,
last_updated,
first_updated,
first_updated_epoch,
Expand All @@ -300,6 +302,7 @@ def update_from_33_to_34(db: sqlite3.Connection) -> None: # pragma: no cover
read_modified,
important,
important_modified,
'feed',
last_updated,
first_updated_epoch,
first_updated_epoch,
Expand Down Expand Up @@ -925,7 +928,8 @@ def make_params() -> Iterable[Mapping[str, Any]]:
feed_order,
original_feed,
data_hash,
data_hash_changed
data_hash_changed,
source
) VALUES (
:id,
:feed_url,
Expand Down Expand Up @@ -961,7 +965,8 @@ def make_params() -> Iterable[Mapping[str, Any]]:
:feed_order,
NULL, -- original_feed
:data_hash,
:data_hash_changed
:data_hash_changed,
:source
);
""",
make_params(),
Expand Down Expand Up @@ -1376,6 +1381,7 @@ def make_get_entries_query(
entries.first_updated
entries.last_updated
entries.original_feed
entries.source
""".split()
)
.FROM("entries")
Expand Down Expand Up @@ -1410,6 +1416,7 @@ def entry_factory(t: Tuple[Any, ...]) -> Entry:
t[25],
t[26],
t[27] or feed.url,
t[28],
feed,
)
return Entry._make(entry)
Expand Down
5 changes: 5 additions & 0 deletions src/reader/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .types import Enclosure
from .types import Entry
from .types import EntryInput
from .types import EntrySource
from .types import ExceptionInfo
from .types import Feed
from .types import FeedInput
Expand Down Expand Up @@ -112,6 +113,7 @@ def as_entry(self, **kwargs: object) -> Entry:
attrs.pop('hash', None)
attrs.update(kwargs)
attrs.setdefault('original_feed_url', feed_url)
attrs.setdefault('source', 'feed')
return Entry(**attrs)

@property
Expand Down Expand Up @@ -223,6 +225,9 @@ class EntryUpdateIntent(NamedTuple):
#: Same as EntryForUpdate.hash_changed.
hash_changed: Optional[int]

#: Same as Entry.source.
source: EntrySource = 'feed'

@property
def new(self) -> bool:
"""Whether the entry is new or not."""
Expand Down
10 changes: 10 additions & 0 deletions src/reader/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ def from_exception(cls: Type[_EI], exc: BaseException) -> _EI:
)


EntrySource = Literal['feed', 'user']


@dataclass(frozen=True)
class Entry(_namedtuple_compat):

Expand Down Expand Up @@ -294,6 +297,13 @@ def feed_url(self) -> str:
# we don't check for it in __post_init__ because it's still useful
# to have it None in tests. The cast is to please mypy.

#: The source of the entry. One of ``'feed'``, ``'user'``.
#:
#: Other values may be added in the future.
#:
#: .. versionadded:: 2.5
source: EntrySource = cast(EntrySource, None)

#: The entry's feed.
feed: Feed = cast(Feed, None)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3363,3 +3363,9 @@ def test_allow_invalid_url(make_reader, feed_root, url):
assert excinfo.value.url == url

reader.change_feed_url(old, url, allow_invalid_url=True)


@rename_argument('reader', 'reader_with_one_feed')
def test_entry_source(reader):
reader.update_feeds()
assert next(reader.get_entries()).source == 'feed'

0 comments on commit 5cec50e

Please sign in to comment.