def check(expected, string, delimiters=None): def expand(ref, index): return "<%d: %s>" % (index, ref) if delimiters != None: actual = utils.expand_delimited_tokens(string, expand, delimiters) else: actual = utils.expand_delimited_tokens(string, expand) self.assertEquals(expected, actual)
def eval(self, context, **kwargs): # Using the configuration to pass the eval globals dictionary to here, # since there isn't any easy way to do this more elegantly globals_and_locals = {} if hasattr(context, '_eval_expression_globals_dict'): globals_and_locals = context._eval_expression_globals_dict str_to_eval = self._str_to_eval def expand_feature_ref(ref, index): var_name = "__fea_%05d" % index globals_and_locals[ var_name] = context.configuration.get_default_view( ).get_feature(ref) return var_name def expand_value_ref(ref, index): var_name = "__feaval_%05d" % index globals_and_locals[ var_name] = context.configuration.get_default_view( ).get_feature(ref).get_value() return var_name str_to_eval = utils.expand_delimited_tokens(str_to_eval, expand_feature_ref, delimiters=('@{', '}')) str_to_eval = utils.expand_delimited_tokens(str_to_eval, expand_value_ref, delimiters=('${', '}')) # Strip leading and trailing whitespace to avoid indentation problems str_to_eval = str_to_eval.strip() ret = None try: ret = eval(str_to_eval, globals_and_locals) return ret except SyntaxError, e: logging.getLogger('cone.ruleml').warning( "Invalid syntax in eval: %s" % (str_to_eval)) self.ast.add_error( self.expression, { 'error_string': 'Invalid syntax in eval', 'str_to_eval': str_to_eval, 'rule': self.ast.expression })
def _solve_condition(self, condition_str, context): """ Internal function to handle condition """ if condition_str != "": #Expanding ConfML information modstr = utils.expand_delimited_tokens( condition_str, lambda ref, index: repr(context.configuration.get_default_view( ).get_feature(ref).get_value())) return eval(modstr) else: #Empty condition is true always. return True
def get_input(self): """ Get the confml ref value from configuration if the outputpath is actually a ref """ if self._path and self.configuration is not None: dview = self.configuration.get_default_view() def expand(ref, index): value = dview.get_feature(ref).get_original_value() if value is None: return '' else: return value return utils.expand_delimited_tokens(self._path, expand) else: return self._path
def _replace_eval_blocks(cls, code): return utils.expand_delimited_tokens( string = code, expander_func = lambda ref, index: '__eval__ %r' % ref, delimiters =('{%', '%}'))