Skip to content

Commit

Permalink
add function 'get_all_feeds' to FeedList, FeedCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
tribela committed Oct 5, 2013
1 parent c4b8f80 commit 7f941d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions libearth/feedlist.py
Expand Up @@ -174,6 +174,16 @@ def __init__(self, title, type=None, text=None, category=None,
self.children = []
self.urls = [] # to avoid duplication of feeds for the same category

def get_all_feeds(self):
res = []
for obj in self.children:
if isinstance(obj, FeedCategory):
res += obj.get_all_feeds()
else:
res.append(obj)

return list(set(res))

def insert(self, index, value):
if not isinstance(value, FeedTree):
raise TypeError('expected an instance of {0.__module__}.'
Expand Down Expand Up @@ -473,6 +483,9 @@ def make_feed(self, type, title, xml_url, html_url=None, text=None,

return feed

def get_all_feeds(self):
return self.feedlist.get_all_feeds()

def convert_from_outline(self, outline_obj):
if outline_obj.children:
title = outline_obj.title or outline_obj.text
Expand Down
27 changes: 27 additions & 0 deletions tests/feedlist_test.py
Expand Up @@ -224,3 +224,30 @@ def test_feedCategory():

#Or valid rfc822 string.
feedCategory = FeedCategory('title', 'Mon, 23 Sep 2013 11:49:28 +0900')


def test_get_all_feeds():
feed1 = Feed('rss2', 'feed1', 'http://xmlurl1.com/rss')
feed2 = Feed('rss2', 'feed2', 'http://xmlurl2.com/rss')
feed3 = Feed('rss2', 'feed3', 'http://xmlurl3.com/rss')

cate1 = FeedCategory('category1')
cate2 = FeedCategory('category2')

#feeds can be placed on multi category
cate1.append(feed1)
cate1.append(feed2)
cate2.append(feed2)
cate2.append(feed3)

feedlist = FeedList()
feedlist.append(cate1)
feedlist.append(cate2)
feedlist.append(feed3)

all_feeds = feedlist.get_all_feeds()

assert feed1 in all_feeds
assert feed2 in all_feeds
assert feed3 in all_feeds
assert len(all_feeds) == 3

0 comments on commit 7f941d2

Please sign in to comment.