Merge branch 'feature-load_extension' of https://github.com/jvanasco/python-feedgen into custom-extensions
This commit is contained in:
commit
1a2c032654
2 changed files with 110 additions and 4 deletions
|
@ -56,6 +56,7 @@ class FeedEntry(object):
|
||||||
|
|
||||||
# Extension list:
|
# Extension list:
|
||||||
self.__extensions = {}
|
self.__extensions = {}
|
||||||
|
self.__extensions_register = {}
|
||||||
|
|
||||||
|
|
||||||
def atom_entry(self, extensions=True):
|
def atom_entry(self, extensions=True):
|
||||||
|
@ -654,3 +655,43 @@ class FeedEntry(object):
|
||||||
extinst = ext()
|
extinst = ext()
|
||||||
setattr(self, name, extinst)
|
setattr(self, name, extinst)
|
||||||
self.__extensions[name] = {'inst':extinst,'atom':atom,'rss':rss}
|
self.__extensions[name] = {'inst':extinst,'atom':atom,'rss':rss}
|
||||||
|
|
||||||
|
def register_extension(
|
||||||
|
self,
|
||||||
|
namespace,
|
||||||
|
extension_class_feed=None,
|
||||||
|
extension_class_entry=None,
|
||||||
|
atom=True,
|
||||||
|
rss=True
|
||||||
|
):
|
||||||
|
'''Register a specific extension by classes to a namespace.
|
||||||
|
|
||||||
|
:param namespace: namespace for the extension
|
||||||
|
:param extension_class_feed: Class of the feed extension to load.
|
||||||
|
:param extension_class_entry: Class of the entry extension to load.
|
||||||
|
:param atom: If the extension should be used for ATOM feeds.
|
||||||
|
:param rss: If the extension should be used for RSS feeds.
|
||||||
|
'''
|
||||||
|
# Check loaded extensions
|
||||||
|
# `load_extension` ignores the "Extension" suffix.
|
||||||
|
if not isinstance(self.__extensions, dict):
|
||||||
|
self.__extensions = {}
|
||||||
|
if namespace in self.__extensions.keys():
|
||||||
|
raise ImportError('Extension already loaded')
|
||||||
|
|
||||||
|
extinst = extension_class_entry()
|
||||||
|
setattr(self, namespace, extinst)
|
||||||
|
|
||||||
|
# `load_extension` registry
|
||||||
|
self.__extensions[namespace] = {'inst': extinst,
|
||||||
|
'atom': atom,
|
||||||
|
'rss': rss
|
||||||
|
}
|
||||||
|
|
||||||
|
# `register_extension` registry
|
||||||
|
self.__extensions_register[namespace] = {
|
||||||
|
'extension_class_feed': extension_class_feed,
|
||||||
|
'extension_class_entry': extension_class_entry,
|
||||||
|
'atom': atom,
|
||||||
|
'rss': rss,
|
||||||
|
}
|
|
@ -29,7 +29,6 @@ class FeedGenerator(object):
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__extensions = {}
|
|
||||||
self.__feed_entries = []
|
self.__feed_entries = []
|
||||||
|
|
||||||
## ATOM
|
## ATOM
|
||||||
|
@ -82,7 +81,8 @@ class FeedGenerator(object):
|
||||||
self.__rss_webMaster = None
|
self.__rss_webMaster = None
|
||||||
|
|
||||||
# Extension list:
|
# Extension list:
|
||||||
__extensions = {}
|
self.__extensions = {}
|
||||||
|
self.__extensions_register = {}
|
||||||
|
|
||||||
|
|
||||||
def _create_atom(self, extensions=True):
|
def _create_atom(self, extensions=True):
|
||||||
|
@ -1024,7 +1024,14 @@ class FeedGenerator(object):
|
||||||
# Try to load extensions:
|
# Try to load extensions:
|
||||||
for extname,ext in items:
|
for extname,ext in items:
|
||||||
try:
|
try:
|
||||||
feedEntry.load_extension( extname, ext['atom'], ext['rss'] )
|
if extname in self.__extensions_register:
|
||||||
|
ext_reg = self.__extensions_register[extname]
|
||||||
|
feedEntry.register_extension(extname,
|
||||||
|
ext_reg['extension_class_feed'],
|
||||||
|
ext_reg['extension_class_entry'],
|
||||||
|
ext_reg['atom'], ext_reg['rss'] )
|
||||||
|
else:
|
||||||
|
feedEntry.load_extension( extname, ext['atom'], ext['rss'] )
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -1066,7 +1073,14 @@ class FeedGenerator(object):
|
||||||
for e in entry:
|
for e in entry:
|
||||||
for extname,ext in items:
|
for extname,ext in items:
|
||||||
try:
|
try:
|
||||||
e.load_extension( extname, ext['atom'], ext['rss'] )
|
if extname in self.__extensions_register:
|
||||||
|
ext_reg = self.__extensions_register[extname]
|
||||||
|
e.register_extension(extname,
|
||||||
|
ext_reg['extension_class_feed'],
|
||||||
|
ext_reg['extension_class_entry'],
|
||||||
|
ext_reg['atom'], ext_reg['rss'] )
|
||||||
|
else:
|
||||||
|
e.load_extension( extname, ext['atom'], ext['rss'] )
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -1127,3 +1141,54 @@ class FeedGenerator(object):
|
||||||
entry.load_extension( name, atom, rss )
|
entry.load_extension( name, atom, rss )
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def register_extension(
|
||||||
|
self,
|
||||||
|
namespace,
|
||||||
|
extension_class_feed = None,
|
||||||
|
extension_class_entry = None,
|
||||||
|
atom=True,
|
||||||
|
rss=True
|
||||||
|
):
|
||||||
|
'''Registers an extension by class.
|
||||||
|
|
||||||
|
:param namespace: namespace for the extension
|
||||||
|
:param extension_class_feed: Class of the feed extension to load.
|
||||||
|
:param extension_class_entry: Class of the entry extension to load
|
||||||
|
:param atom: If the extension should be used for ATOM feeds.
|
||||||
|
:param rss: If the extension should be used for RSS feeds.
|
||||||
|
'''
|
||||||
|
# Check loaded extensions
|
||||||
|
# `load_extension` ignores the "Extension" suffix.
|
||||||
|
if not isinstance(self.__extensions, dict):
|
||||||
|
self.__extensions = {}
|
||||||
|
if namespace in self.__extensions.keys():
|
||||||
|
raise ImportError('Extension already loaded')
|
||||||
|
|
||||||
|
# Load extension
|
||||||
|
extinst = extension_class_entry()
|
||||||
|
setattr(self, namespace, extinst)
|
||||||
|
|
||||||
|
# `load_extension` registry
|
||||||
|
self.__extensions[namespace] = {'inst':extinst,
|
||||||
|
'atom':atom,
|
||||||
|
'rss':rss
|
||||||
|
}
|
||||||
|
|
||||||
|
# `register_extension` registry
|
||||||
|
self.__extensions_register[namespace] = {
|
||||||
|
'extension_class_feed': extension_class_feed,
|
||||||
|
'extension_class_entry': extension_class_entry,
|
||||||
|
'atom': atom,
|
||||||
|
'rss': rss,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Try to load the extension for already existing entries:
|
||||||
|
for entry in self.__feed_entries:
|
||||||
|
try:
|
||||||
|
entry.register_extension(namespace,
|
||||||
|
extension_class_entry,
|
||||||
|
extension_class_feed,
|
||||||
|
atom, rss)
|
||||||
|
except ImportError:
|
||||||
|
raise
|
Loading…
Reference in a new issue