def on_feed_filter(self, feed, config): entry_actions = { 'accept': feed.accept, 'reject': feed.reject, 'fail': feed.fail} for entry in feed.entries: # Make entry fields and other utilities available in the eval namespace # We need our namespace to be an Entry instance for lazy loading to work eval_locals = copy(entry) eval_locals.update({'has_field': lambda f: f in entry, 'timedelta': datetime.timedelta, 'now': datetime.datetime.now()}) for item in config: requirement, action = item.items()[0] try: # Restrict eval namespace to have no globals and locals only from eval_locals passed = safer_eval(requirement, eval_locals) except NameError, e: # Extract the name that did not exist missing_field = e.message.split('\'')[1] log.debug('%s does not contain the field %s' % (entry['title'], missing_field)) except Exception, e: log.error('Error occurred in if statement: %r' % e) else: if passed: log.debug('%s matched requirement %s' % (entry['title'], requirement)) if isinstance(action, basestring): # Simple entry action (accept, reject or fail) was specified as a string entry_actions[action](entry, 'Matched requirement: %s' % requirement) else: # Other filters were specified to run on this entry fake_feed = Feed(feed.manager, feed.name, feed.config) fake_feed.session = feed.session fake_feed.entries = [entry] try: for filter_name, filter_config in action.iteritems(): filter = get_plugin_by_name(filter_name) method = filter.phase_handlers['filter'] method(fake_feed, filter_config) except Exception: raise else: # Populate changes from the fake feed to the real one for e in fake_feed.accepted: feed.accept(e, e.get('reason')) for e in fake_feed.rejected: feed.reject(e, e.get('reason')) for e in fake_feed.failed: feed.fail(e, e.get('reason'))
def handle_phase(feed, config): if feed.name not in self.feed_phases: log.debug('No config dict was generated for this feed.') return entry_actions = { 'accept': feed.accept, 'reject': feed.reject, 'fail': feed.fail} for item in self.feed_phases[feed.name][phase]: requirement, action = item.items()[0] passed_entries = [e for e in feed.entries if self.check_condition(requirement, e)] if passed_entries: if isinstance(action, basestring): # Simple entry action (accept, reject or fail) was specified as a string for entry in passed_entries: entry_actions[action](entry, 'Matched requirement: %s' % requirement) else: # Other plugins were specified to run on this entry fake_feed = Feed(feed.manager, feed.name, feed.config) fake_feed.session = feed.session fake_feed.entries = passed_entries fake_feed.accepted = [e for e in feed.accepted if self.check_condition(requirement, e)] fake_feed.rejected = [e for e in feed.rejected if self.check_condition(requirement, e)] try: for plugin_name, plugin_config in action.iteritems(): plugin = get_plugin_by_name(plugin_name) method = plugin.phase_handlers[phase] method(fake_feed, plugin_config) except Exception: raise else: # Populate changes from the fake feed to the real one for e in fake_feed.accepted: feed.accept(e, e.get('reason')) for e in fake_feed.rejected: feed.reject(e, e.get('reason')) for e in fake_feed.failed: feed.fail(e, e.get('reason')) feed.purge()