Exemplo n.º 1
0
Arquivo: rpc.py Projeto: 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)
Exemplo n.º 2
0
	def run(self,ctx,**k):
		event = self.params(ctx)
		if not event:
			for m in all_collect("del"):
				print >>self.ctx.out, " ".join(m.name)
			print >>self.ctx.out, "."
			return
		c = get_collect(event)
		if c is None:
			raise SyntaxError(u"Usage: del ‹type› ‹name…›")
		if not hasattr(c,"delete"):
			raise SyntaxError(u"You cannot delete those items.")
		return c.delete(ctx)
Exemplo n.º 3
0
	def run(self,ctx,**k):

		def getter(out,q):
			while True:
				res = q.get()
				if res is None:
					return
				p,t = res
				if isinstance(t,datetime):
					if TESTING and t.year != 2003:
						t = "%s" % (humandelta(t-now(t.year != 2003)),)
					else:
						t = "%s (%s)" % (humandelta(t-now(t.year != 2003)),t)
					if TESTING:
						lim = 3
					else:
						lim = 4
					ti = t.rfind('.')
					if ti>0 and len(t)-ti>lim and len(t)-ti<lim+6: # limit to msec
						t = t[:ti+lim]+")"

				print >>out,p+u": "+unicode(t)

		event = self.params(ctx)
		c = get_collect(event, allow_collection=True)

		try:
			def out_one(c):
				q = Queue(3)
				try:
					job = spawn(getter,self.ctx.out,q)
					flatten(q,(c,))
				finally:
#					with log_wait("list "+str(event)):
					q.put(None)
					job.join()

			if c is None:
				for m in all_collect(skip=False):
					print >>self.ctx.out, " ".join(m.name)
			elif isinstance(c,Collection):
				if event[-1] == "*":
					for m in c.iteritems():
						print >>self.ctx.out,"* %s :: %s" % (n,m)
						out_one(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 isinstance(n,Name):
						n = u" ".join(unicode(x) for x in n)
					if m is not None:
						print >>self.ctx.out,u"%s :: %s" % (n,m)
					else:
						print >>self.ctx.out,u"%s" % (n,)
			else:
				out_one(c)

		except Exception as e:
			fix_exception(e)
			print >>self.ctx.out, "* ERROR *",repr(e)
			print_exception(e,file=self.ctx.out)
		finally:
			print >>self.ctx.out, "."