Skip to content

Commit

Permalink
Add exist_ok argument to add_feed().
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Jan 20, 2022
1 parent aea57df commit 8cd566a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Unreleased

* Add the ``missing_ok`` argument to :meth:`~Reader.delete_feed`
and :meth:`~Reader.delete_entry`.
* Add the ``exist_ok`` argument to :meth:`~Reader.add_feed`.

* In the web application, show maxrss when debug is enabled. (:issue:`269`)
* In the web application, decrease memory usage of the entries page
when there are a lot of entries
Expand Down
7 changes: 2 additions & 5 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,14 @@ Adding and updating feeds

Create a ``podcast.py`` file::

from reader import make_reader, FeedExistsError
from reader import make_reader

feed_url = "http://www.hellointernet.fm/podcast?format=rss"

reader = make_reader("db.sqlite")

def add_and_update_feed():
try:
reader.add_feed(feed_url)
except FeedExistsError:
pass
reader.add_feed(feed_url, exist_ok=True)
reader.update_feeds()

add_and_update_feed()
Expand Down
7 changes: 2 additions & 5 deletions examples/podcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import requests

from reader import make_reader, FeedExistsError
from reader import make_reader


feed_url = "http://www.hellointernet.fm/podcast?format=rss"
Expand All @@ -22,10 +22,7 @@


def add_and_update_feed():
try:
reader.add_feed(feed_url)
except FeedExistsError:
pass
reader.add_feed(feed_url, exist_ok=True)
reader.update_feeds()


Expand Down
32 changes: 29 additions & 3 deletions src/reader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from ._utils import MapType
from ._utils import zero_or_one
from .exceptions import EntryNotFoundError
from .exceptions import FeedExistsError
from .exceptions import FeedMetadataNotFoundError
from .exceptions import FeedNotFoundError
from .exceptions import InvalidPluginError
Expand Down Expand Up @@ -408,7 +409,13 @@ def close(self) -> None:
"""
self._storage.close()

def add_feed(self, feed: FeedInput, *, allow_invalid_url: bool = False) -> None:
def add_feed(
self,
feed: FeedInput,
exist_ok: bool = False,
*,
allow_invalid_url: bool = False,
) -> None:
"""Add a new feed.
Feed updates are enabled by default.
Expand All @@ -419,9 +426,12 @@ def add_feed(self, feed: FeedInput, *, allow_invalid_url: bool = False) -> None:
Add feed even if the current Reader configuration
does not know how to handle the feed URL
(and updates for it would fail).
exist_ok (bool):
If true, don't raise :exc:`FeedExistsError`
if the feed already exists.
Raises:
FeedExistsError
FeedExistsError: If the feed already exists, and `exist_ok` is false.
StorageError
InvalidFeedURLError: If ``feed`` is invalid and ``allow_invalid_url`` is false.
Expand All @@ -433,18 +443,28 @@ def add_feed(self, feed: FeedInput, *, allow_invalid_url: bool = False) -> None:
To get the previous behavior (no validation),
use ``allow_invalid_url=True``.
.. versionadded:: 2.8
The ``exist_ok`` argument.
"""
url = _feed_argument(feed)
if not allow_invalid_url:
self._parser.validate_url(url)
now = self._now()
self._storage.add_feed(url, now)
try:
self._storage.add_feed(url, now)
except FeedExistsError:
if not exist_ok:
raise

def delete_feed(self, feed: FeedInput, missing_ok: bool = False) -> None:
"""Delete a feed and all of its entries, metadata, and tags.
Args:
feed (str or Feed): The feed URL.
missing_ok (bool):
If true, don't raise :exc:`FeedNotFoundError`
if the feed does not exist.
Raises:
FeedNotFoundError: If the feed does not exist, and `missing_ok` is false.
Expand Down Expand Up @@ -1526,6 +1546,9 @@ def delete_entry(self, entry: EntryInput, missing_ok: bool = False) -> None:
Args:
entry (tuple(str, str) or Entry): (feed URL, entry id) tuple.
missing_ok (bool):
If true, don't raise :exc:`EntryNotFoundError`
if the entry does not exist.
Raises:
EntryNotFoundError: If the entry does not exist, and `missing_ok` is false.
Expand Down Expand Up @@ -2094,6 +2117,9 @@ def delete_tag(
Args:
resource (str or Feed): The resource.
key (str): The key of the tag to delete.
missing_ok (bool):
If true, don't raise :exc:`TagNotFoundError`
if the tag does not exist.
Raises:
TagNotFoundError: If the tag does not exist, and `missing_ok` is false.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,9 @@ def test_add_remove_get_feeds(reader, feed_arg):
assert excinfo.value.url == one.url
assert 'feed exists' in excinfo.value.message

# no exception
reader.add_feed(feed_arg(one), True)

reader._now = lambda: naive_datetime(2010, 1, 2)
reader.update_feeds()

Expand Down

0 comments on commit 8cd566a

Please sign in to comment.