def __init__(self, validation_context=None, **kwargs): if validation_context: if "jexl_cache" not in validation_context: validation_context["jexl_cache"] = defaultdict(dict) self._cache = validation_context["jexl_cache"] else: self._cache = defaultdict(dict) super().__init__(**kwargs) self._structure = None self._form = None context_data = None if validation_context: # cleaned up variant self._form = validation_context.get("form") self._structure = validation_context.get("structure") context_data = { "form": self._form.slug if self._form else None, "info": self._structure, } self.context = Context(context_data) self.add_transform("answer", self.answer_transform)
def __init__(self, validation_context=None, **kwargs): if validation_context: if "jexl_cache" not in validation_context: validation_context["jexl_cache"] = defaultdict(dict) self._cache = validation_context["jexl_cache"] else: self._cache = defaultdict(dict) super().__init__(**kwargs) self._structure = None self._form = None context_data = None if validation_context: # cleaned up variant self._form = validation_context.get("form") self._structure = validation_context.get("structure") context_data = { "form": self._form.slug if self._form else None, "info": self._structure, } self.context = Context(context_data) self.add_transform("answer", self.answer_transform) self.add_transform( "mapby", lambda arr, key: [obj.get(key, None) for obj in arr]) self.add_binary_operator( "intersects", 20, lambda left, right: any(x in right for x in left))
def __init__(self, context=None): self.context = Context(context or {}) self.config = JEXLConfig( transforms={}, unary_operators=default_unary_operators.copy(), binary_operators=default_binary_operators.copy()) self._grammar = None
def __init__(self, validation_context=None, **kwargs): super().__init__(**kwargs) context_data = None if validation_context: context_data = {"info": validation_context} self.context = Context(context_data) self.add_transform("groups", lambda spec: spec)
def __init__(self, answer_by_question={}, form=None, **kwargs): super().__init__(**kwargs) self.context = Context({"form": form}) self.answer_by_question = answer_by_question self.add_transform("answer", self.answer_transform) self.add_transform("mapby", lambda arr, key: [obj[key] for obj in arr]) self.add_binary_operator( "intersects", 20, lambda left, right: any(x in right for x in left))
def test_array_index(): context = Context( {'foo': { 'bar': [{ 'tek': 'tok' }, { 'tek': 'baz' }, { 'tek': 'foz' }] }}) result = DefaultEvaluator().evaluate(tree('foo.bar[1].tek'), context) assert result == 'baz'
class GroupJexl(JEXL): """ Class for evaluating GroupJexl. validation_context is expected to be the following: { "case": { "created_by_group": str, }, "work_item": { "created_by_group": str, }, "prev_work_item": { "addressed_groups": list of str, "controlling_groups": list of str, }, "context": { "addressed_groups: list of str, "controlling_groups: list of str, }, } """ def __init__( self, validation_context=None, task=None, case=None, work_item_created_by_user=None, prev_work_item=None, dynamic_context=None, **kwargs, ): super().__init__(**kwargs) context_data = None if validation_context: context_data = {"info": validation_context} self.context = Context(context_data) self.task = task self.case = case self.work_item_created_by_user = work_item_created_by_user self.prev_work_item = prev_work_item self.dynamic_context = dynamic_context self.add_transform("groups", self.groups_transform)
def test_filter_arrays(): context = Context( {'foo': { 'bar': [{ 'tek': 'hello' }, { 'tek': 'baz' }, { 'tok': 'baz' }] }}) result = DefaultEvaluator().evaluate(tree('foo.bar[.tek == "baz"]'), context) assert result == [{'tek': 'baz'}]
def test_identifier_chain(): context = Context({'foo': {'baz': {'bar': 'tek'}}}) result = DefaultEvaluator().evaluate(tree('foo.baz.bar'), context) assert result == 'tek'
def test_filter_object_properties(): context = Context({'foo': {'baz': {'bar': 'tek'}}}) result = DefaultEvaluator().evaluate(tree('foo["ba" + "z"].bar'), context) assert result == 'tek'
def evaluate(self, expression, context=None): parsed_expression = self.parse(expression) context = Context(context) if context is not None else self.context return Evaluator(self.config).evaluate(parsed_expression, context)