def __init__(self, widget_name, arguments): """ Arguments look like this {% widget <widget-name> a:"value" b:value c:'value' d %} This returns ( <widget-name>, { "a" : "value", "b" : "value", "c" : "value", d : None } ) """ parts = arguments.split(" ", 1) if not parts: raise djolt_base.DjoltSyntaxError, "too few arguments for '%s'" % widget_name self.component_name = parts[0] if len(parts) == 2: for m in a_rex.finditer(parts[1]): key = m.group('var') value = m.group('a1') or m.group('a2') or m.group('a3') or None if key == "render": value = bm_extract.coerce_bool(value, otherwise = True) key_var = standard_argumentd.get(key) if not key_var: raise DjoltUnknownArgumentError, key setattr(self, key_var, value)
def Filter(self, name, argument, value): """ Same as default, without the empty string rule """ bvalue = bm_extract.coerce_bool(value, otherwise = False) if bvalue: return value else: return bm_extract.coerce_string(argument, otherwise = "")
def Filter(self, name, argument, value): """ If value evaluates to False, use given default. Otherwise, use the value. For example: {{ value|default:"nothing" }} If value is "" (the empty string), the output will be nothing. """ if value == "": return "" bvalue = bm_extract.coerce_bool(value, otherwise = False) if bvalue: return value else: return bm_extract.coerce_string(argument, otherwise = "")
def Execute(self, context): import bm_extract id = self.id if id == ID_NAME: ## Log("HERE:B", value = self.value, result = context.get(self.value)) return context.get(self.value) elif id == ID_LITERAL_STRING: return self.value elif id == ID_LITERAL_NUMBER: if self.value.find('.') > -1: return bm_extract.coerce_float(self.value) else: return bm_extract.coerce_int(self.value) elif id == ID_LITERAL_TRUE: return True elif id == ID_LITERAL_FALSE: return False elif id == ID_LITERAL_NONE: return False elif id == '==': return self.first.Execute(context) == self.second.Execute(context) elif id == '!=': return self.first.Execute(context) != self.second.Execute(context) elif id == '<': ## Log("HERE:XXX", ## first = self.first.Execute(context), ## second = self.second.Execute(context), ## ) return self.first.Execute(context) < self.second.Execute(context) elif id == '<=': return self.first.Execute(context) <= self.second.Execute(context) elif id == '>': return self.first.Execute(context) > self.second.Execute(context) elif id == '>=': return self.first.Execute(context) >= self.second.Execute(context) elif id == '+': if self.second == None: return self.first.Execute(context) return self.first.Execute(context) + self.second.Execute(context) elif id == '-': if self.second == None: return -self.first.Execute(context) return self.first.Execute(context) - self.second.Execute(context) elif id == '.': ## key = "%s.%s" % ( self.first.Literal(context), self.second.Literal(context), ) ## value = context.get(key) ## print "<%s>/<%s>" % ( key, value ) return context.get("%s.%s" % ( self.first.Literal(context), self.second.Literal(context), )) elif id == '[': ## key = "%s[%s]" % ( self.first.Literal(context), self.second.Literal(context), ) ## value = context.get(key) ## print >> sys.stderr, "!!! <%s>/<%s>" % ( key, value ) return context.get("%s[%s]" % ( self.first.Literal(context), self.second.Literal(context), )) elif id == 'and': a = bm_extract.coerce_bool(self.first.Execute(context)) b = bm_extract.coerce_bool(self.second.Execute(context)) return a and b elif id == 'or': a = bm_extract.coerce_bool(self.first.Execute(context)) b = bm_extract.coerce_bool(self.second.Execute(context)) return a or b elif id == 'not': a = bm_extract.coerce_bool(self.first.Execute(context)) return not a elif id == '(': if len(self.second) != 1: raise NotImplementedError, self.id + ": functions take exactly one argument" if self.first.value == "int": b = bm_extract.coerce_int(self.second[0].Execute(context)) ## Log("HERE:A", b = b, first = self.first, second = self.second[0], valuewas = self.second[0].Execute(context)) elif self.first.value == "string": b = bm_extract.coerce_string(self.second[0].Execute(context)) elif self.first.value == "bool": b = bm_extract.coerce_bool(self.second[0].Execute(context)) elif self.first.value == "float": b = bm_extract.coerce_float(self.second[0].Execute(context)) else: raise NotImplementedError, self.id + ": function can only be int|string|bool|float" return b else: print self.id, self.first, self.second, self.third raise NotImplementedError, self.id
def Test(self, context): import djolt_expression return bm_extract.coerce_bool(djolt_expression.parse(self.arguments).Execute(context))
def as_bool(self, path, **ad): return bm_extract.coerce_bool(self.get(path), **ad)