Esempio n. 1
0
def add_latlon(d, lat, lon):
	lat = bm_extract.coerce_float(lat, otherwise = None)
	lon = bm_extract.coerce_float(lon, otherwise = None)

	if lat == None or lon == None:
		return

	d["georss:point"] = [ lat, lon ]
	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
Esempio n. 3
0
	def as_float(self, path, **ad):
		return	bm_extract.coerce_float(self.get(path), **ad)