def Filter(self, name, argument, value): """ Returns True if the value is divisible by the argument. For example: {{ value|divisibleby:"3" }} If value is 21, the output would be True. """ value = bm_extract.coerce_int(value, otherwise = "") argument = bm_extract.coerce_int(argument, otherwise = "") return value % argument == 0 and True or False
def Filter(self, name, argument, value): """ Returns a plural suffix if the value is not 1. By default, this suffix is 's'. Example: You have {{ num_messages }} message{{ num_messages|pluralize }}. For words that require a suffix other than 's', you can provide an alternate suffix as a parameter to the filter. Example: You have {{ num_walruses }} walrus{{ num_walrus|pluralize:"es" }}. For words that don't pluralize by simple suffix, you can specify both a singular and plural suffix, separated by a comma. Example: You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}. """ ivalue = bm_extract.coerce_int(value) plurals = filter(None, bm_extract.coerce_string(argument, otherwise = "").split(",", 1)) if len(plurals) == 0: plurals = [ "", "s" ] elif len(plurals) == 1: plurals = [ "", plurals[0] ] if ivalue == 1: return plurals[0] else: return plurals[1]
def Filter(self, name, argument, value): """ Example: {{ value|atindex:1 }} If value is the list ['a', 'b', 'c'], the output will be 'b'. If called without an argument, we assume 0. """ value = bm_extract.coerce_list(value, separator = None) index = bm_extract.coerce_int(argument) if index < len(value): return value[index]
def Filter(self, name, argument, value): """ Returns True if the value's length is the argument, or False otherwise. For example: {{ value|length_is:"4" }} If value is ['a', 'b', 'c', 'd'], the output will be True. """ try: lvalue = len(value) except TypeError: lvalue = 0 argument = bm_extract.coerce_int(argument, otherwise = 0) return lvalue == argument
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 as_int(self, path, **ad): return bm_extract.coerce_int(self.get(path), **ad)
def Filter(self, name, argument, value): value = bm_extract.coerce_int(value, otherwise = 0) argument = bm_extract.coerce_int(argument, otherwise = 0) return "%d" % ( value + argument )