Esempio n. 1
0
	def simple_statement(self,args):
		fn = self.lookup(args)
		try:
			fn.run(self.ctx)
		except Exception as ex:
			fix_exception(ex)
			self.error(self,ex)
Esempio n. 2
0
	def _run(self,syn):
		syn.get()
		try:
			while True:
				# allow tasks which kill this one to run
				gevent.sleep(0)
				l = self.input.readline()
				if not l:
					self.add_line(".")
					break
				self.add_line(l)
		
		except BaseException as e:
			fix_exception(e)
			return e
		finally:
			job = gevent.spawn(self.endConnection,kill=False)
			def dead(e):
				fix_exception(e)
				process_failure(e)
			job.link_exception(dead)
			if not hasattr(self.input,"fileno") or self.input.fileno() > 2:
				self.input.close()
			self.input = None
		return "Bla"
Esempio n. 3
0
	def process(self, event=None, **k):
		super(EventCallback,self).process(**k)

		# This is an event monitor. Failures will not be tolerated.
		try:
			msg = getattr(event.ctx,'raw',None)
			if msg is None:
				d = dict((x,y) for x,y in event.ctx if isinstance(y,(int,str,unicode,long,bool,float)))
				try:
					msg = json.dumps(dict(event=list(event), **d))
				except (TypeError,UnicodeDecodeError):
					msg = json.dumps(dict(data=repr(event)+"|"+repr(d)))
			elif isinstance(msg,(int,long,float)):
				msg = str(msg)
			elif isinstance(msg,unicode):
				msg = msg.encode("utf-8")
			global _mseq
			_mseq += 1
			msg = amqp.Message(body=msg, content_type='application/json', message_id=base_mseq+str(_mseq))
			self.channel.basic_publish(msg=msg, exchange=self.exchange, routing_key=".".join(str(x) for x in self.prefix+tuple(event)[self.strip:]))
		except Exception as ex:
			fix_exception(ex)
			process_failure(ex)
			try:
				self.cancel()
			except Exception as ex:
				fix_exception(ex)
				process_failure(ex)
		raise TrySomethingElse
Esempio n. 4
0
	def _do_measure(self):
		log("monitor",TRACE,"Start run",self.name)
		try:
			self.running.clear()
			self.started_at = now()
			self._monitor()
			if self.send_check_event:
				simple_event(self.ectx, "monitor","checked",*self.name)
			if self.new_value is not None:
				self.last_value = self.value
				self.value = self.new_value
				if hasattr(self,"delta"):
					if self.last_value is not None:
						val = self.value-self.last_value
						self._ectx.value_delta = val
						if val >= 0 or self.delta == 0:
							simple_event(self.ectx,"monitor","update",*self.name)
				else:
					simple_event(self.ectx,"monitor","update",*self.name)
		except Exception as e:
			fix_exception(e)
			process_failure(e)
		finally:
			log("monitor",TRACE,"Stop run",self.name)
			self.running.set()
			self._schedule()
Esempio n. 5
0
	def __init__(self, name, host,port, socket=None):
		self.socket = socket
		self.host = host
		self.port = port
		self.name = name
		storage2 = getattr(self,"storage2",{})
		assert (host,port) not in storage2, "already known host/port tuple"
		super(NetCommonConnector,self).__init__()
		storage2[(host,port)] = self
		external = (self.socket is not None)
		if self.socket is None:
			try:
				self._connect()
			except Exception as ex:
				fix_exception(ex)
				if isinstance(ex,EnvironmentError) and ex.errno in (errno.ECONNREFUSED,errno.EHOSTDOWN,errno.EHOSTUNREACH,errno.ENETDOWN,errno.ENETUNREACH,errno.ETIMEDOUT):
					ex.no_backtrace = True
				try:
					del storage2[(host,port)]
				except KeyError:
					pass
				else:
					super(NetCommonConnector,self).delete()
				try:
					self.not_up_event()
				except Exception as ex2:
					fix_exception(ex2)
					process_failure(ex2)
				reraise(ex)
			self.handshake(False)
		else:
			self.handshake(True)

		self.start_job("job",self._reader)
Esempio n. 6
0
	def _connect(self):
		e = None
		for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
			af, socktype, proto, canonname, sa = res
			try:
				s = socket.socket(af, socktype, proto)
			except socket.error as err:
				fix_exception(err)
				if e is None:
					e = err
				s = None
				continue
			try:
				s.connect(sa)
			except socket.error as err:
				fix_exception(err)
				if e is None:
					e = err
				s.close()
				s = None
				continue
			break
		if s is None:
			reraise(e)
		self.socket = s
Esempio n. 7
0
	def dataReceived(self, data):
		self._stop_timer()
		data = self.dbuf+data
		while True:
			xi = len(data)+1
			try: pi = data.index('\r')
			except ValueError: pi = xi
			try: ei = data.index('\n')
			except ValueError: ei = xi
			if pi==xi and ei==xi:
				break
			if pi < ei:
				self.lbuf = data[:pi]
				data = data[pi+1:]
			else:
				msg = data[:ei]
				data = data[ei+1:]
				if msg == "" and self.lbuf is not None:
					msg = self.lbuf
					self.lbuf = None
				try:
					self.lineReceived(msg)
				except Exception as e:
					fix_exception(e)
					process_failure(e)

		self.dbuf = data
		self._start_timer()
Esempio n. 8
0
	def do_timed_switch(self):
		self.timer = None
		try:
			self.do_switch()
		except Exception as e:
			fix_exception(e)
			process_failure(e)
Esempio n. 9
0
	def run(self,ctx,**k):
		event = self.params(ctx)
		if len(event):
			self.displayname = SName(event)

		if self.timespec is None:
			raise SyntaxError(u'Usage: wait [name…]: for|until|next ‹timespec›')
		if self.is_update:
			return Waiters[self.displayname].retime(self.timespec())
		w = Waiter(self, self.displayname, self.force)
		w.init(self.timespec())
		simple_event("wait","start",*w.name, end_time=ixtime(w.end,self.force), loglevel=TRACE)
		try:
			if w.job:
				r = w.job.get()
			else:
				r = True
		except Exception as ex:
			simple_event("wait","error", *w.name, time=tm,loglevel=TRACE)

			fix_exception(ex)
			log_exc(msg=u"Wait %s died:"%(self.name,), err=ex, level=TRACE)
			raise
		else:
			tm = ixtime(now(self.force),self.force)
			if r:
				simple_event("wait","done", *w.name, loglevel=TRACE)
			else:
				simple_event("wait","cancel", *w.name, loglevel=TRACE)
			ctx.wait = tm
			if not r:
				raise DelayCancelled(w)
		finally:
			w.delete()
Esempio n. 10
0
	def update_all(self):
		try:
			simple_event("onewire","scanning",self.name)
			self._update_all()
		except Exception as e:
			fix_exception(e)
			process_failure(e)
Esempio n. 11
0
	def _watcher(self):
		res = []
		while True:
			try:
				self.update_all()
			except Exception as ex:
				fix_exception(ex)
				process_failure(ex)
				resl = len(res)
				while res:
					q = res.pop()
					q.set_exception(ex)
			else:
				resl = len(res)
				while res:
					q = res.pop()
					q.set(None)

			if TESTING:
				if resl: d = 10
				else: d = 30
			else:
				if resl: d = 60
				else: d = 300

			while True:
				try:
					q = self.watch_q.get(timeout=(d if not res else 0))
				except Empty:
					break
				else:
					res.append(q)
Esempio n. 12
0
	def run(self,ctx,**k):
		event = self.params(ctx)
		if len(event):
			self.displayname = SName(event)

		if self.timespec is None:
			raise SyntaxError(u'Usage: wait [name…]: for|until|next ‹timespec›')
		if self.is_update:
			return Waiters[self.displayname].retime(self.timespec())
		w = Waiter(self, self.displayname, self.force)
		w.init(self.timespec())
		process_event(Event(self.ctx(loglevel=TRACE),"wait","start",ixtime(w.end,self.force),*w.name))
		try:
			if w.job:
				r = w.job.get()
			else:
				r = True
		except Exception as ex:
			fix_exception(ex)
			log_exc(msg=u"Wait %s died:"%(self.name,), err=ex, level=TRACE)
			raise
		else:
			tm = ixtime(now(self.force),self.force)
			if r: # don't log 'done' if canceled
				process_event(Event(self.ctx(loglevel=TRACE),"wait","done",tm, *w.name))
			ctx.wait = tm
			if not r:
				raise DelayCancelled(w)
Esempio n. 13
0
	def update_all(self):
		try:
			process_event(Event(Context(),"onewire","scanning",self.name))
			self._update_all()
		except Exception as e:
			fix_exception(e)
			process_failure(e)
Esempio n. 14
0
	def _do_measure(self):
		log("monitor",TRACE,"Start run",self.name)
		try:
			self.running.clear()
			self.started_at = now()
			self._monitor()
			if self.send_check_event:
				process_event(Event(self.ctx, "monitor","checked",*self.name))
			if self.new_value is not None:
				if hasattr(self,"delta"):
					if self.value is not None:
						val = self.new_value-self.value
						if val >= 0 or self.delta == 0:
							process_event(Event(Context(),"monitor","value",self.new_value-self.value,*self.name))
				else:
					process_event(Event(Context(),"monitor","value",self.new_value,*self.name))
			if self.new_value is not None:
				self.value = self.new_value
		except Exception as e:
			fix_exception(e)
			process_failure(e)
		finally:
			log("monitor",TRACE,"Stop run",self.name)
			self.running.set()
			self._schedule()
Esempio n. 15
0
	def sendMsg(self,conn, typ,data, rlen=0):
		# messages are not tagged, so process received messages in strict order
		self.prio = PRIO_STANDARD
		try:
			conn.sendMsg(typ,data,rlen)
		except Exception as ex:
			fix_exception(ex)
			self.error(ex)
Esempio n. 16
0
	def _run(self,a,k):
		try:
			super(Async,self).run(*a,**k)
		except HaltSequence:
			pass
		except Exception as err:
			fix_exception(err)
			process_failure(err)
Esempio n. 17
0
File: rpc.py Progetto: pombreda/MoaT
	def exposed_monitor(self,callback,*args):
		try:
			w = EventCallback(self,callback,*args)
			self.workers.add(w)
			register_worker(w)
			return w
		except Exception as ex:
			fix_exception(ex)
			process_failure(ex)
Esempio n. 18
0
	def _run(self,ctx,**k):
		try:
			super(TryStatement,self).run(ctx,**k)
		except Exception as err:
			fix_exception(err)
			if self.catch_do:
				self.catch_do.run(ctx(error_=err), **k)
			else:
				process_failure(err)
Esempio n. 19
0
	def _timeout(self):
		self._timer = None
		try:
			self.parent.write(self.val)
		except Exception as ex:
			fix_exception(ex)
			self.q.set(ex)
		else:
			self.q.set(None)
Esempio n. 20
0
def load_module(*m):
	md = dict()
	mod = None
	p = None
	for d in ModuleDirs:
		p = os.path.join(d,m[-1])+".py"
		try:
			c = compile(open(p,"r").read(), p, "exec",0,True)
		except (OSError,IOError):
			md = None
			continue
		else:
			eval(c,md)
			break

	try:
		if not md:
			if TESTING:
				if os.path.isdir("modules"):
					p = "modules"
				else:
					p = os.path.join(os.pardir,"modules")
				p = os.path.join(p,m[-1])+".py"
				c = compile(open(p,"r").read(), p, "exec",0,True)
			else:
				from pkg_resources import resource_string
				p = "homevent.modules."+m[-1]+".py"
				c = compile(resource_string("homevent.modules", m[-1]+".py"), os.path.join('homevent','modules',m[-1]+".py"), "exec",0,True)
			eval(c,md)
	
		mod = md["init"]
		if callable(mod):
			mod = mod(*m)
		elif len(event) > 1:
			raise RuntimeError("You cannot parameterize this module.")
		if not hasattr(mod,"load"):
			mod.load = md["load"]
			mod.unload = md["unload"]
	
		try:
			mod.load()
		except BaseException as e:
			fix_exception(e)
			try:
				mod.unload()
			except Exception:
				reraise(e)
			else:
				reraise(e)
			# do not use finally: here
		else:
			mod.path = p
	except BaseException:
		if mod is not None and hasattr(mod,"name") and mod.name in Modules:
			del Modules[mod.name]
		raise
	return mod
Esempio n. 21
0
def _readcf():
	c = Context()
	try:
		for f in args:
			parse(f,ctx=c)
	except Exception as e:
		fix_exception(e)
		process_failure(e)
		shut_down()
Esempio n. 22
0
File: rpc.py Progetto: pombreda/MoaT
	def exposed_cmd_list(self,*args):
		# don't call this 'exposed_list'!
		c = get_collect(args, allow_collection=True)
		try:
			if c is None:
				for m in all_collect(skip=False):
					yield m.name,
			elif isinstance(c,Collection):
				if args[-1] == "*":
					for n,m in c.iteritems():
						yield n,m
					return
				for n,m in c.iteritems():
					try:
						m = m.info
					except AttributeError:
						m = m.name
					else:
						if callable(m):
							m = m()
						if isinstance(m,basestring):
							m = m.split("\n")[0].strip()

					if m is not None:
						yield (n,m)
					else:
						yield n,
			else:
				q = Queue(3)
				job = spawn(flatten,q,(c,))
				job.link(lambda _:q.put(None))

				while True:
					res = q.get()
					if res is None:
						return
					p,t = res
					if isinstance(t,datetime):
						if TESTING:
							if t.year != 2003:
								t = "%s" % (humandelta(t-now(t.year != 2003)),)
							else: 
								t = "%s (%s)" % (humandelta(t-now(t.year != 2003)),t)
							ti = t.rfind('.')
							if ti>0 and len(t)-ti > 3 and len(t)-ti<9: # limit to msec
								t= t[:ti+3]+")"
						# otherwise transmit the datetime as-is
					elif not isinstance(t,(date,time,timedelta)):
						t = unicode(t)

					yield p,t

		except Exception as e:
				fix_exception(e)
				yield "* ERROR *",repr(e)
				process_failure(e)
Esempio n. 23
0
def _shut_down():
	"""\
		Code to be called last. The main loop is running and will
		be stopped when all events have progressed.
		"""
	try:
		process_event(shutdown_event)
	except Exception as e:
		fix_exception(e)
		process_failure(e)
Esempio n. 24
0
	def set(self,key,val):
		if not self.bus:
			raise DisconnectedDeviceError(self.id)

		msg = ATTRsetmsg(self.path+(self.bus_id,key),val)
		msg.queue(self.bus)
		try:
			return msg.result.get()
		except Exception as ex:
			fix_exception(ex)
			self.go_down(ex)
Esempio n. 25
0
	def _get_typ(self):
		try:
			t = self.get("type")
			self._setattr(t,"typ")
		except Exception as ex:
			del self.typ
			del devices[self.id]
			fix_exception(ex)
			process_failure(ex)
		else:
			self.go_up()
Esempio n. 26
0
	def _main():
		parse_ctx = Context(filename=name)
		#parse_ctx.logger=parse_logger
		try:
			parse(input, interpreter(Context(out=logwrite(logger))),parse_ctx)
		except Exception as e:
			fix_exception(e)
			print_exception(e,file=sys.stderr)
		finally:
			shut_down()
			if ht is not None:
				ht.try_exit()
Esempio n. 27
0
	def delete(self,ctx=None):
		if self.timer:
			self.timer.cancel()
			self.timer = None
		try:
			if self.state:
				process_event(Event(self.ctx,"pcm","set",self.names[0],*self.name))
		except Exception as ex:
			fix_exception(ex)
			process_failure(ex)
		finally:
			super(CommonPM,self).delete()
Esempio n. 28
0
def start_up():
	"""\
		Code to be called first. The main loop is NOT running.
		"""
	register_worker(Shutdown_Worker("shutdown handler"))

	global running
	if not running:
		running = True
		try:
			process_event(startup_event)
		except Exception as e:
			fix_exception(e)
			process_failure(e)
Esempio n. 29
0
File: rrd.py Progetto: pombreda/MoaT
	def run(self,ctx,**k):
		event = self.params(ctx)
		if len(event) < 2:
			raise SyntaxError(u'Usage: set rrd ‹value› ‹name…›')
		s = RRDs[Name(*event[1:])]
		# Using "N:" may run into a RRD bug
		# if we're really close to the next minute
		try:
			rrdtool.update(s.upath, "-t",s.udataset, now().strftime("%s")+":"+unicode(event[0]).encode("utf-8"))
		except Exception as e:
			fix_exception(e)
			if "minimum one second step" in str(e):
				pass
			else:
				raise
Esempio n. 30
0
	def process(self, event, **k):
		from homevent.collect import collections
		super(Shutdown_Collections,self).process(**k)

		def byprio(a,b):
			return cmp(a.prio,b.prio)
		for w in sorted(collections.itervalues(),cmp=byprio):
			if not w.can_do("del"):
				continue
			for d in w.values():
				try:
					d.delete(event.ctx)
				except Exception as ex:
					fix_exception(ex)
					print_exception(ex)