Skip to content

Instantly share code, notes, and snippets.

@gazpachoking
Created October 10, 2011 03:32
Show Gist options
  • Save gazpachoking/1274582 to your computer and use it in GitHub Desktop.
Save gazpachoking/1274582 to your computer and use it in GitHub Desktop.
Generic combining plugin (now added to repo as 'sequence')
import logging
from flexget.plugin import register_plugin, get_plugin_by_name, get_plugin_keywords, get_phases_by_plugin, phase_methods
log = logging.getLogger('combine')
class PluginCombine(object):
""" Allows the same plugin to be configured multiple times in a feed.
Example:
combine:
- rss: http://feeda.com
- rss: http://feedb.com
"""
def validator(self):
from flexget import validator
root = validator.factory()
valid_plugins = root.accept('list').accept('dict')
for plugin_name in get_plugin_keywords():
plugin = get_plugin_by_name(plugin_name)
if plugin.api_ver < 2 or not hasattr(plugin.instance, 'validator'):
continue
valid_plugins.accept(plugin.instance.validator, key=plugin_name)
return root
def __getattr__(self, item):
"""Returns a function for all on_feed_* and on_process_* events, that runs all the configured plugins."""
for phase, method in phase_methods.iteritems():
# TODO: Deal with entry phases
if item == method and phase not in ['accept', 'reject', 'fail']:
break
else:
raise AttributeError(item)
def handle_phase(feed, config):
"""Function that runs all of the configured plugins which act on the current phase."""
# Keep a list of all results, for input plugin combining
results = []
for item in config:
for plugin_name, plugin_config in item.iteritems():
if phase in get_phases_by_plugin(plugin_name):
method = get_plugin_by_name(plugin_name).phase_handlers[phase]
log.debug('Running plugin %s' % plugin_name)
result = method(feed, plugin_config)
if isinstance(result, list):
results.extend(result)
return results
return handle_phase
register_plugin(PluginCombine, 'combine', api_ver=2)
# Allows configs such as this totally pointless example.
# Plugins which share a phase will be run in the order listed. (can be dangerous, for example limit_new will become ineffective if listed before the other filter plugins)
feeds:
test:
combine:
- mock:
- title: aoeuaoeu
- mock:
- title: hhh
- regexp:
accept:
- aoeuaoeu
- regexp:
accept:
- hhh
- limit_new: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment