From 3267f34ff94bc2f7b95390f311f4c6c9604a2a08 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 1 Sep 2014 15:48:21 -0700 Subject: [PATCH] feeds: Raise an exception when adding a feed with a duplicate name Avoid: $ r2e add example http://example.com/feed1 $ r2e add example http://example.com/feed2 $ r2e list 1: [*] example (http://example.com/feed2 -> lkmorlan) 2: [*] example (http://example.com/feed2 -> lkmorlan) Instead, the second addition now prints: duplicate feed name 'example' to stderr and exits with status 1. Reported-by: Liam K Morland Signed-off-by: W. Trevor King --- CHANGELOG | 1 + rss2email/error.py | 12 ++++++++++-- rss2email/feeds.py | 9 ++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 382e626..dbcc6dd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ v3.9 (unreleased) + * Catch and error out if a user adds a feed with a duplicate name. * Split sender into both sendmail's -F and -f. * Fix an error with SMTPConnectionError inhertence order ("does not take keyword arguments"). * Add a new `smtp-ssl-protocol` setting, to select acceptable protocols for SMTP connections. diff --git a/rss2email/error.py b/rss2email/error.py index 49a1c93..9567b32 100644 --- a/rss2email/error.py +++ b/rss2email/error.py @@ -153,12 +153,20 @@ class InvalidFeedConfig (FeedError): class InvalidFeedName (InvalidFeedConfig): - def __init__(self, name, **kwargs): - message = "invalid feed name '{}'".format(name) + def __init__(self, name, message=None, **kwargs): + if not message: + message = 'invalid feed name {!r}'.format(name) super(InvalidFeedName, self).__init__( setting='name', message=message, **kwargs) +class DuplicateFeedName (InvalidFeedName): + def __init__(self, name, **kwargs): + message = 'duplicate feed name {!r}'.format(name) + super(DuplicateFeedName, self).__init__( + name=name, message=message, **kwargs) + + class ProcessingError (FeedError): def __init__(self, parsed, feed, message=None, **kwargs): if message is None: diff --git a/rss2email/feeds.py b/rss2email/feeds.py index edd5f66..5b5a34c 100644 --- a/rss2email/feeds.py +++ b/rss2email/feeds.py @@ -388,15 +388,22 @@ class Feeds (list): feed-0 (None -> a@b.com) >>> print(feeds.new_feed()) feed-1 (None -> a@b.com) + >>> print(feeds.new_feed(name='feed-1')) + Traceback (most recent call last): + ... + rss2email.error.DuplicateFeedName: duplicate feed name 'feed-1' """ + feed_names = [feed.name for feed in self] if name is None: i = 0 while True: name = '{}{}'.format(prefix, i) - feed_names = [feed.name for feed in self] if name not in feed_names: break i += 1 + elif name in feed_names: + feed = self[name] + raise _error.DuplicateFeedName(name=feed.name, feed=feed) feed = _feed.Feed(name=name, **kwargs) self.append(feed) return feed -- 2.26.2