Skip to content

Commit

Permalink
Update code/tests to use the new tag methods.
Browse files Browse the repository at this point in the history
1092 warnins -> 0 warnings.

For #266.
  • Loading branch information
lemon24 committed Jan 17, 2022
1 parent 939cb71 commit 22a0464
Showing 8 changed files with 133 additions and 131 deletions.
20 changes: 10 additions & 10 deletions src/reader/_app/__init__.py
Original file line number Diff line number Diff line change
@@ -234,7 +234,7 @@ def entries():
feed = reader.get_feed(feed_url, None)
if not feed:
abort(404)
feed_tags = list(reader.get_feed_tags(feed))
feed_tags = list(reader.get_tag_keys(feed))

args = request.args.copy()
query = args.pop('q', None)
@@ -438,7 +438,7 @@ def feeds():
feed_data = (
(
feed,
list(reader.get_feed_tags(feed)),
list(reader.get_tag_keys(feed)),
reader.get_entry_counts(feed=feed) if with_counts else None,
)
for feed in feeds
@@ -468,7 +468,7 @@ def metadata():
if not feed:
abort(404)

metadata = reader.get_feed_metadata(feed_url)
metadata = reader.get_tags(feed_url)

# Ensure flashed messages get removed from the session.
# https://github.com/lemon24/reader/issues/81
@@ -504,7 +504,7 @@ def tags():
with_counts = {None: None, 'no': False, 'yes': True}[with_counts]

def iter_tags():
for tag in itertools.chain([None, True, False], reader.get_feed_tags()):
for tag in itertools.chain([None, True, False], reader.get_tag_keys()):
feed_counts = None
entry_counts = None

@@ -628,7 +628,7 @@ def update_feed_title(data):
def add_metadata(data):
feed_url = data['feed-url']
key = data['key']
get_reader().set_feed_metadata_item(feed_url, key, None)
get_reader().set_tag(feed_url, key, None)


@form_api
@@ -640,15 +640,15 @@ def update_metadata(data):
value = yaml.safe_load(data['value'])
except yaml.YAMLError as e:
raise APIError("invalid JSON: {}".format(e), (feed_url, key)) from e
get_reader().set_feed_metadata_item(feed_url, key, value)
get_reader().set_tag(feed_url, key, value)


@form_api(really=True)
@readererror_to_apierror()
def delete_metadata(data):
feed_url = data['feed-url']
key = data['key']
get_reader().delete_feed_metadata_item(feed_url, key)
get_reader().delete_tag(feed_url, key)


@form_api
@@ -658,12 +658,12 @@ def update_feed_tags(data):
feed_tags = set(data['feed-tags'].split())

reader = get_reader()
tags = set(reader.get_feed_tags(feed_url))
tags = set(reader.get_tag_keys(feed_url))

for tag in tags - feed_tags:
reader.remove_feed_tag(feed_url, tag)
reader.delete_tag(feed_url, tag, True)
for tag in feed_tags - tags:
reader.add_feed_tag(feed_url, tag)
reader.set_tag(feed_url, tag)


@form_api(really=True)
10 changes: 5 additions & 5 deletions src/reader/_plugins/global_metadata.py
Original file line number Diff line number Diff line change
@@ -54,19 +54,19 @@ def init_reader(reader):
pass
else:
reader.disable_feed_updates(url)
reader.add_feed_tag(url, tag)
reader.set_tag(url, tag)

def get_global_metadata(*args):
return reader.get_feed_metadata(url, *args)
return reader.get_tags(url, *args)

def get_global_metadata_item(*args):
return reader.get_feed_metadata_item(url, *args)
return reader.get_tag(url, *args)

def set_global_metadata_item(*args):
reader.set_feed_metadata_item(url, *args)
reader.set_tag(url, *args)

def delete_global_metadata_item(*args):
reader.delete_feed_metadata_item(url, *args)
reader.delete_tag(url, *args)

reader.get_global_metadata = get_global_metadata
reader.get_global_metadata_item = get_global_metadata_item
18 changes: 11 additions & 7 deletions src/reader/core.py
Original file line number Diff line number Diff line change
@@ -1535,7 +1535,7 @@ def get_feed_metadata(
self,
feed: FeedInput,
key: Optional[str] = None,
) -> Iterable[Tuple[str, JSONType]]:
) -> Iterable[Tuple[str, JSONType]]: # pragma: no cover
"""Get all or some of the metadata for a feed as ``(key, value)`` pairs.
Args:
@@ -1576,7 +1576,7 @@ def get_feed_metadata_item(
@deprecated('get_tag', '2.8', '3.0')
def get_feed_metadata_item(
self, feed: FeedInput, key: str, default: Union[MissingType, _T] = MISSING
) -> Union[JSONType, _T]:
) -> Union[JSONType, _T]: # pragma: no cover
"""Get metadata for a feed.
Like ``next(iter(reader.get_feed_metadata(feed, key=key)), (None, default))[1]``,
@@ -1610,7 +1610,7 @@ def get_feed_metadata_item(
@deprecated('set_tag', '2.8', '3.0')
def set_feed_metadata_item(
self, feed: FeedInput, key: str, value: JSONType
) -> None:
) -> None: # pragma: no cover
"""Set metadata for a feed.
Args:
@@ -1630,7 +1630,9 @@ def set_feed_metadata_item(
self.set_tag(feed, key, value)

@deprecated('delete_tag', '2.8', '3.0')
def delete_feed_metadata_item(self, feed: FeedInput, key: str) -> None:
def delete_feed_metadata_item(
self, feed: FeedInput, key: str
) -> None: # pragma: no cover
"""Delete metadata for a feed.
Args:
@@ -1883,7 +1885,7 @@ def search_entry_counts(
return self._search.search_entry_counts(query, now, filter_options)

@deprecated('set_tag', '2.8', '3.0')
def add_feed_tag(self, feed: FeedInput, tag: str) -> None:
def add_feed_tag(self, feed: FeedInput, tag: str) -> None: # pragma: no cover
"""Add a tag to a feed.
Adding a tag that the feed already has is a no-op.
@@ -1902,7 +1904,7 @@ def add_feed_tag(self, feed: FeedInput, tag: str) -> None:
self.set_tag(feed, tag)

@deprecated('delete_tag', '2.8', '3.0')
def remove_feed_tag(self, feed: FeedInput, tag: str) -> None:
def remove_feed_tag(self, feed: FeedInput, tag: str) -> None: # pragma: no cover
"""Remove a tag from a feed.
Removing a tag that the feed does not have is a no-op.
@@ -1920,7 +1922,9 @@ def remove_feed_tag(self, feed: FeedInput, tag: str) -> None:
self.delete_tag(feed, tag, True)

@deprecated('get_tag_keys', '2.8', '3.0')
def get_feed_tags(self, feed: Optional[FeedInput] = None) -> Iterable[str]:
def get_feed_tags(
self, feed: Optional[FeedInput] = None
) -> Iterable[str]: # pragma: no cover
"""Get all or some of the feed tags.
Args:
6 changes: 3 additions & 3 deletions src/reader/plugins/entry_dedupe.py
Original file line number Diff line number Diff line change
@@ -269,7 +269,7 @@ def _get_same_group_entries(reader, entry):


def _after_feed_update(reader, feed, *, dry_run=False):
all_tags = set(reader.get_feed_tags(feed))
all_tags = set(reader.get_tag_keys(feed))

dedupe_tags = []
for suffix in _IS_DUPLICATE_BY_TAG_SUFFIX:
@@ -291,7 +291,7 @@ def _after_feed_update(reader, feed, *, dry_run=False):
_dedupe_entries(reader, entry, duplicates, dry_run=dry_run)

for tag, _ in dedupe_tags:
reader.remove_feed_tag(feed, tag)
reader.delete_tag(feed, tag)


_MAX_GROUP_SIZE = 16
@@ -409,7 +409,7 @@ def init_reader(reader):

for feed in feeds:
# if 'n-gate' not in feed.url: continue
reader.add_feed_tag(feed, reader.make_reader_reserved_name('dedupe.once'))
reader.set_tag(feed, reader.make_reader_reserved_name('dedupe.once'))
_after_feed_update(reader, feed.url)

import resource
38 changes: 18 additions & 20 deletions src/reader/plugins/mark_as_read.py
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
To configure, set the ``make_reader_reserved_name('mark-as-read')``
(by default, ``.reader.mark-as-read``)
feed metadata to something like::
tag to something like::
{
"title": ["first-regex", "second-regex"]
@@ -41,63 +41,61 @@
import logging
import re

from reader.exceptions import MetadataNotFoundError
from reader.types import EntryUpdateStatus

# avoid circular imports

log = logging.getLogger(__name__)


def _get_config(reader, feed_url, metadata_key, patterns_key):
try:
metadata = reader.get_feed_metadata_item(feed_url, metadata_key)
except MetadataNotFoundError:
def _get_config(reader, feed_url, key, patterns_key):
value = reader.get_tag(feed_url, key, None)
if value is None:
return None

if isinstance(metadata, dict):
patterns = metadata.get(patterns_key, [])
if isinstance(value, dict):
patterns = value.get(patterns_key, [])
if isinstance(patterns, list):
if all(isinstance(p, str) for p in patterns):
return patterns

# TODO: there should be a hook to allow plugins to validate metadata
log.warning("%s: invalid mark_as_read config metadata: %s", feed_url, metadata_key)
# TODO: there should be a hook to allow plugins to validate tags
log.warning("%s: invalid mark_as_read config: %s", feed_url, key)
return []


_METADATA_KEY = 'mark-as-read'
_CONFIG_TAG = 'mark-as-read'


def _mark_as_read(reader, entry, status):
if status is EntryUpdateStatus.MODIFIED:
return

metadata_key = reader.make_reader_reserved_name(_METADATA_KEY)
patterns = _get_config(reader, entry.feed_url, metadata_key, 'title')
key = reader.make_reader_reserved_name(_CONFIG_TAG)
patterns = _get_config(reader, entry.feed_url, key, 'title')

for pattern in patterns or ():
if re.search(pattern, entry.title):
reader._mark_entry_as_dont_care(entry)
return


_OLD_METADATA_KEY = 'mark_as_read'
_OLD_CONFIG_TAG = 'mark_as_read'


def _migrate_pre_2_7_metadata(reader, feed):
old_key = reader.make_reader_reserved_name(_OLD_METADATA_KEY)
old_value = reader.get_feed_metadata_item(feed, old_key, None)
old_key = reader.make_reader_reserved_name(_OLD_CONFIG_TAG)
old_value = reader.get_tag(feed, old_key, None)
if not old_value:
return

key = reader.make_reader_reserved_name(_METADATA_KEY)
value = reader.get_feed_metadata_item(feed, key, None)
key = reader.make_reader_reserved_name(_CONFIG_TAG)
value = reader.get_tag(feed, key, None)
if value: # pragma: no cover
return

reader.set_feed_metadata_item(feed, key, old_value)
reader.delete_feed_metadata_item(feed, old_key)
reader.set_tag(feed, key, old_value)
reader.delete_tag(feed, old_key)


def init_reader(reader):
8 changes: 4 additions & 4 deletions tests/test_plugins_entry_dedupe.py
Original file line number Diff line number Diff line change
@@ -278,11 +278,11 @@ def test_plugin_once(make_reader, db_path, monkeypatch, tags):
}

for tag in tags:
reader.add_feed_tag(feed, tag)
reader.add_feed_tag(feed, 'unrelated')
reader.set_tag(feed, tag)
reader.set_tag(feed, 'unrelated')
reader.update_feeds()

assert set(reader.get_feed_tags(feed)) == {'unrelated'}
assert set(reader.get_tag_keys(feed)) == {'unrelated'}

# if both 'once' and 'once.title' are in tags,
# 'once' (the strictest) has priority
@@ -543,7 +543,7 @@ def _test_modified_copying(

reader = make_reader(db_path, plugins=['reader.entry_dedupe'])
reader._parser = parser
reader.add_feed_tag(feed, '.reader.dedupe.once')
reader.set_tag(feed, '.reader.dedupe.once')
reader.update_feeds()

actual = sorted(
12 changes: 6 additions & 6 deletions tests/test_plugins_mark_as_read.py
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ def get_entry_data(**kwargs):

reader.update_feeds()

reader.set_feed_metadata_item(one, key, value)
reader.set_tag(one, key, value)

one = parser.feed(1, datetime(2010, 1, 2))
match_new = parser.entry(1, 2, datetime(2010, 1, 2), title='match new')
@@ -69,7 +69,7 @@ def test_regex_mark_as_read_bad_metadata(make_reader, value):
parser.entry(1, 1, datetime(2010, 1, 1), title='match')

reader.add_feed(one)
reader.set_feed_metadata_item(one, '.reader.mark-as-read', value)
reader.set_tag(one, '.reader.mark-as-read', value)

reader.update_feeds()

@@ -88,12 +88,12 @@ def test_regex_mark_as_read_pre_2_7_metadata(make_reader, with_entry):
parser.entry(1, 1, datetime(2010, 1, 1), title='match old')

reader.add_feed(one)
reader.set_feed_metadata_item(one, '.reader.mark_as_read', {'title': ['^match']})
reader.set_tag(one, '.reader.mark_as_read', {'title': ['^match']})

reader.update_feeds()

assert all(e.read for e in reader.get_entries())

metadata = dict(reader.get_feed_metadata(one))
assert '.reader.mark_as_read' not in metadata
assert metadata['.reader.mark-as-read'] == {'title': ['^match']}
tags = dict(reader.get_tags(one))
assert '.reader.mark_as_read' not in tags
assert tags['.reader.mark-as-read'] == {'title': ['^match']}
Loading

0 comments on commit 22a0464

Please sign in to comment.