def check_condition(self, condition, entry): """Checks if a given `entry` passes `condition`""" # 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, 'utcnow': datetime.datetime.utcnow(), 'now': datetime.datetime.now(), } ) try: # Restrict eval namespace to have no globals and locals only from eval_locals passed = evaluate_expression(condition, eval_locals) if passed: log.debug('%s matched requirement %s' % (entry['title'], condition)) return passed except UndefinedError as e: # Extract the name that did not exist missing_field = e.args[0].split('\'')[1] log.debug('%s does not contain the field %s' % (entry['title'], missing_field)) except Exception as e: log.error('Error occurred while evaluating statement `%s`. (%s)' % (condition, e))
def check_condition(self, condition, entry): """Checks if a given `entry` passes `condition`""" # 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, 'utcnow': datetime.datetime.utcnow(), 'now': datetime.datetime.now(), }) try: # Restrict eval namespace to have no globals and locals only from eval_locals passed = evaluate_expression(condition, eval_locals) if passed: log.debug('%s matched requirement %s' % (entry['title'], condition)) return passed except UndefinedError as e: # Extract the name that did not exist missing_field = e.args[0].split('\'')[1] log.debug('%s does not contain the field %s' % (entry['title'], missing_field)) except Exception as e: log.error('Error occurred while evaluating statement `%s`. (%s)' % (condition, e))
def sort_key(entry): val = evaluate_expression(field, entry) if isinstance(val, str) and re_articles: val = re.sub(re_articles, '', val, flags=re.IGNORECASE) # Sort None values last no matter whether reversed or not return (val is not None) == reverse, val