From 6966e03bef16b4b5200b50867b6bd5147e62d4e8 Mon Sep 17 00:00:00 2001 From: pictuga Date: Sat, 3 Oct 2020 16:25:29 +0200 Subject: [PATCH] Clean up itemClass code To avoid globals() --- morss/feeds.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/morss/feeds.py b/morss/feeds.py index bbe0f81..2a283f1 100644 --- a/morss/feeds.py +++ b/morss/feeds.py @@ -647,12 +647,11 @@ class Uniq(object): class Feed(object): - itemsClass = 'Item' + itemsClass = property(lambda x: Item) # because Item is define below, i.e. afterwards dic = ('title', 'desc', 'items') def wrap_items(self, items): - itemsClass = globals()[self.itemsClass] - return [itemsClass(x, self.rules, self) for x in items] + return [self.itemsClass(x, self.rules, self) for x in items] title = property( lambda f: f.get('title'), @@ -672,7 +671,7 @@ class Feed(object): if new is None: return - for attr in globals()[self.itemsClass].dic: + for attr in self.itemsClass.dic: try: setattr(item, attr, getattr(new, attr)) @@ -732,8 +731,12 @@ class Item(Uniq): lambda f: f.rmv('item_updated') ) +class ItemXML(Item, ParserXML): + pass + + class FeedXML(Feed, ParserXML): - itemsClass = 'ItemXML' + itemsClass = ItemXML def root_siblings(self): out = [] @@ -761,20 +764,12 @@ class FeedXML(Feed, ParserXML): return etree.tostring(self.root.getroottree(), encoding=encoding, method='xml', **k) -class ItemXML(Item, ParserXML): - pass - - -class FeedHTML(Feed, ParserHTML): - itemsClass = 'ItemHTML' - - class ItemHTML(Item, ParserHTML): pass -class FeedJSON(Feed, ParserJSON): - itemsClass = 'ItemJSON' +class FeedHTML(Feed, ParserHTML): + itemsClass = ItemHTML class ItemJSON(Item, ParserJSON): @@ -789,6 +784,9 @@ class ItemJSON(Item, ParserJSON): cur = cur[node] +class FeedJSON(Feed, ParserJSON): + itemsClass = ItemJSON + if __name__ == '__main__': from . import crawler