Clean up itemClass code

To avoid globals()
master
pictuga 2020-10-03 16:25:29 +02:00
parent 03a122c41f
commit 6966e03bef
1 changed files with 13 additions and 15 deletions

View File

@ -647,12 +647,11 @@ class Uniq(object):
class Feed(object): class Feed(object):
itemsClass = 'Item' itemsClass = property(lambda x: Item) # because Item is define below, i.e. afterwards
dic = ('title', 'desc', 'items') dic = ('title', 'desc', 'items')
def wrap_items(self, items): def wrap_items(self, items):
itemsClass = globals()[self.itemsClass] return [self.itemsClass(x, self.rules, self) for x in items]
return [itemsClass(x, self.rules, self) for x in items]
title = property( title = property(
lambda f: f.get('title'), lambda f: f.get('title'),
@ -672,7 +671,7 @@ class Feed(object):
if new is None: if new is None:
return return
for attr in globals()[self.itemsClass].dic: for attr in self.itemsClass.dic:
try: try:
setattr(item, attr, getattr(new, attr)) setattr(item, attr, getattr(new, attr))
@ -732,8 +731,12 @@ class Item(Uniq):
lambda f: f.rmv('item_updated') ) lambda f: f.rmv('item_updated') )
class ItemXML(Item, ParserXML):
pass
class FeedXML(Feed, ParserXML): class FeedXML(Feed, ParserXML):
itemsClass = 'ItemXML' itemsClass = ItemXML
def root_siblings(self): def root_siblings(self):
out = [] out = []
@ -761,20 +764,12 @@ class FeedXML(Feed, ParserXML):
return etree.tostring(self.root.getroottree(), encoding=encoding, method='xml', **k) 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): class ItemHTML(Item, ParserHTML):
pass pass
class FeedJSON(Feed, ParserJSON): class FeedHTML(Feed, ParserHTML):
itemsClass = 'ItemJSON' itemsClass = ItemHTML
class ItemJSON(Item, ParserJSON): class ItemJSON(Item, ParserJSON):
@ -789,6 +784,9 @@ class ItemJSON(Item, ParserJSON):
cur = cur[node] cur = cur[node]
class FeedJSON(Feed, ParserJSON):
itemsClass = ItemJSON
if __name__ == '__main__': if __name__ == '__main__':
from . import crawler from . import crawler