Ejemplo n.º 1
0
    def get(self, state, ext=None, handler=None):
        simple_event("fs20", "state", *self.name, state=state, ext=ext)

        ctx = Context(value=state)
        if ext is not None:
            ctx.ext = ext
        simple_event(ctx, "input", "fs20", *self.name)
Ejemplo n.º 2
0
	def get(self, state, ext=None, handler=None):
		simple_event("fs20","state", *self.name, state=state, ext=ext)

		ctx = Context(value=state)
		if ext is not None:
			ctx.ext = ext
		simple_event(ctx, "input","fs20", *self.name)
Ejemplo n.º 3
0
 def _main():
     global ht
     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()
             ht = None
Ejemplo n.º 4
0
def simple_event(*args, **data):
    """\
		A shortcut for triggering a "simple" background event
		"""
    from moat.event import Event
    _direct = data.pop('_direct', False)

    if isinstance(args[0], Context):
        if data:
            ctx = Context(args[0], **data)
        else:
            ctx = args[0]
        args = args[1:]
    else:
        ctx = Context(**data)
    run_event(Event(ctx, *args), _direct=_direct)
Ejemplo n.º 5
0
 def __init__(self, event):
     if not hasattr(event, "ctx"):
         super(LogEndEvent, self).__init__(Context(), "END",
                                           event.__class__.__name__)
     else:
         super(LogEndEvent, self).__init__(event.ctx, "END", *event.name)
         self.id = event.id
Ejemplo n.º 6
0
    def __init__(self, input, interpreter, ctx=None):
        """Parse an input stream and pass the commands to the processor @proc."""
        global _npars
        _npars += 1
        super(Parser, self).__init__("n" + str(_npars))

        self.ending = False

        if ctx is None:
            self.ctx = Context()
        else:
            self.ctx = ctx

        if "filename" not in self.ctx:
            self.ctx.filename = u"‹stdin?›"
        self.input = input
        self.proc = interpreter
        self.do_prompt = interpreter.do_prompt
Ejemplo n.º 7
0
 def on_connect(self):
     global conn_seq
     conn_seq += 1
     self.name = self.dest + ("n" + str(conn_seq), )
     self.ctx = Context()
     self.ctx.out = Reporter(self)
     self.ctx.words = global_words(self.ctx)
     self.workers = set()
     simple_event("rpc", "connect", *self.name)
     Collected.__init__(self)
Ejemplo n.º 8
0
 def _init(self, bus, short_id=None, id=None, path=()):
     log("onewire", DEBUG, "NEW", bus, short_id, id, path)
     self.bus_id = id
     if short_id:
         self.id = short_id.lower()
     self.bus = bus
     assert path is not None
     self.path = path
     self.is_up = None
     self.ctx = Context()
Ejemplo n.º 9
0
    def _command(self, args=(), sub=(), **kwargs):
        ctx = Context(**kwargs)
        ctx.out = Reporter()
        ctx.words = global_words()  # self.ctx)

        try:
            if sub:
                cmd = CommandProcessor(parent=self, ctx=ctx)
                proc = cmd.complex_statement(args)
                for s in sub:
                    proc.simple_statement(s)
                proc.done()
                cmd.run()
            else:
                CommandProcessor(parent=self, ctx=ctx).simple_statement(args)
        except Exception as e:
            fix_exception(e)
            process_failure(e)
            reraise(e)
        return ctx.out.data
Ejemplo n.º 10
0
	def _command(self, args=(), sub=(),**kwargs):
		ctx = Context(**kwargs)
		ctx.out = Reporter()
		ctx.words = global_words() # self.ctx)

		try:
			if sub:
				cmd = CommandProcessor(parent=self,ctx=ctx)
				proc = cmd.complex_statement(args)
				for s in sub:
					proc.simple_statement(s)
				proc.done()
				cmd.run()
			else:
				CommandProcessor(parent=self,ctx=ctx).simple_statement(args)
		except Exception as e:
			fix_exception(e)
			process_failure(e)
			reraise(e)
		return ctx.out.data
Ejemplo n.º 11
0
def ready():
    c = Context()
    for f in sys.argv[1:]:
        parse(f, ctx=c)

    #c.logger=parse_logger
    if os.isatty(0):
        i = InteractiveInterpreter
    else:
        i = Interpreter
    print("""Ready. Type «help» if you don't know what to do.""")
    try:
        parse(sys.stdin, interpreter=i, ctx=c)
    except Exception as e:
        fix_exception(e)
        reporter(e)
    shut_down()
Ejemplo n.º 12
0
    def __init__(self, event, worker):
        super(WorkSequence, self).__init__()
        self.work = []
        self.event = event
        self.worker = worker
        #		if hasattr(event,"id"):
        #			self.id = self.event.id
        if hasattr(event, "ctx"):
            self.ctx = event.ctx()
        else:
            self.ctx = Context()

        if isinstance(self.event, Event):
            en = SName(self.event)
        else:
            en = six.text_type(self.event)
        self.info = u"Worker %d for ‹%s›" % (self.id, en)
Ejemplo n.º 13
0
	def __init__(self, input, interpreter, ctx=None):
		"""Parse an input stream and pass the commands to the processor @proc."""
		global _npars
		_npars += 1
		super(Parser,self).__init__("n"+str(_npars))

		self.ending = False

		if ctx is None:
			self.ctx = Context()
		else:
			self.ctx = ctx

		if "filename" not in self.ctx:
			self.ctx.filename=u"‹stdin?›"
		self.input = input
		self.proc = interpreter
		self.do_prompt = interpreter.do_prompt
Ejemplo n.º 14
0
    def __init__(self, *name):
        if not name:
            name = self.name
        if not name:
            raise RuntimeError("Unnamed object of '%s'" %
                               (self.__class__.__name__, ))

        if self.storage is None:
            raise RuntimeError("You didn't declare a storage for '%s'" %
                               (self.__class__.__name__, ))

        self.name = name = SName(name)
        if name in self.storage:
            self.dup_error(name)

        super(Collected, self).__init__()
        self.storage[name] = self
        self._ectx = Context()
Ejemplo n.º 15
0
    def __init__(self, ctx, *name):
        """\
			Events have a context and at least one name. For example:
				Event(ctx, "startup")
				Event(ctx, "switch","toggle","sw12")
				Event(ctx, "switch","dim","livingroom","lamp12")
				Event(ctx, "timer","timeout","t123")
			"""
        self._name_check(name)
        #print("E_INIT",name,"with",ctx)
        self.name = Name(name)
        self.ctx = ctx if ctx is not None else Context()
        if "loglevel" in self.ctx:
            self.loglevel = ctx.loglevel
        self.timestamp = now()

        global event_id
        event_id += 1
        self.id = event_id
Ejemplo n.º 16
0
class Parser(Collected, Jobber):
    """The input parser object. It is a consumer of lines."""
    storage = Parsers.storage
    line = None
    p_gen = None
    do_prompt = False
    last_pos = None
    job = None

    def __init__(self, input, interpreter, ctx=None):
        """Parse an input stream and pass the commands to the processor @proc."""
        global _npars
        _npars += 1
        super(Parser, self).__init__("n" + str(_npars))

        self.ending = False

        if ctx is None:
            self.ctx = Context()
        else:
            self.ctx = ctx

        if "filename" not in self.ctx:
            self.ctx.filename = u"‹stdin?›"
        self.input = input
        self.proc = interpreter
        self.do_prompt = interpreter.do_prompt

    def list(self):
        """Yield a couple of (left,right) tuples, for enumeration."""
        yield super(Parser, self)
        yield ("input", str(self.input))
        if self.last_pos is not None:
            yield ("line", str(self.last_pos[0]))
            yield ("pos", str(self.last_pos[1]))

    def info(self):
        return str(self.input)

    def stop_client(self):
        """Helper. See TwistedParser.stop_client()."""
        pass

    def lineReceived(self, data):
        self.add_line(data)

    def run(self):
        self.init_state()
        self.prompt()
        if self.input is None:
            self.p_gen = tokizer(self._do_parse, self.job, self.stop_client)
            return
        syn = AsyncResult()
        self.start_job("job", self._run, syn)
        self.p_gen = tokizer(self._do_parse, self.job)
        syn.set(None)

        try:
            e = self.job.get()
            if isinstance(e, BaseException):
                reraise(e)
        except StopParsing:
            pass

        self.p_gen.exit()

    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)
            self.input.close()
            self.input = None
        return "Bla"

    def endConnection(self, res=None, kill=True):
        """Called to stop"""
        if self.job:
            if kill:
                self.p_gen.exit()
                self.stop_job("job")
            else:
                self.p_gen.feed(None)

    def delete(self, ctx=None):
        self.endConnection()
        super(Parser, self).delete()

    def add_line(self, data):
        """Standard LineReceiver method"""
        if not isinstance(data, six.string_types):
            data = data.decode("utf-8")
        self.p_gen.feed(data)

    def init_state(self):
        self.p_state = 0
        self.p_pop_after = False
        self.p_stack = []
        self.p_args = []
        if self.p_gen:
            self.p_gen.init()

    def prompt(self):
        if not self.do_prompt:
            return
        if self.p_state == 0 and not self.p_stack:
            self.ctx.out.write(">> ")
        else:
            self.ctx.out.write(".. ")
        getattr(self.ctx.out, "flush", lambda: None)()

    def _do_parse(self, t, txt, beg, end, line):
        # States: 0 newline, 1 after first word, 2 OK to extend word
        #         3+4 need newline+indent after sub-level start, 5 extending word
        #log("parser",TRACE,"PARSE",t,repr(txt))

        try:
            self._parseStep(t, txt, beg, end, line)

        except StopIteration:
            return

        except Exception as ex:
            fix_exception(ex)
            if self.p_stack:
                self.proc = self.p_stack[0]

            self.proc.error(self, ex)
            self.prompt()

    def _parseStep(self, t, txt, beg, end, line):
        from token import NUMBER,NAME,DEDENT,INDENT,OP,NEWLINE,ENDMARKER, \
         STRING
        from moat.tokize import COMMENT, NL
        self.last_pos = beg

        if "logger" in self.ctx:
            self.ctx.logger("T", self.p_state, t, repr(txt), beg, end,
                            repr(line))
        if t == COMMENT:
            return
        if self.p_state == 0:  # begin of statement
            if t == NAME:
                self.p_args = [txt]
                self.p_state = 1
                return
            elif t == DEDENT:
                self.proc.done()
                if self.p_stack:
                    self.proc = self.p_stack.pop()
                    return
                else:
                    raise StopIteration
            elif t == ENDMARKER:
                self.proc.done()
                while self.p_stack:
                    self.proc = self.p_stack.pop()
                    self.proc.done()
                raise StopIteration
            elif t in (NL, NEWLINE):
                self.prompt()
                return
            elif t == OP and txt == ".":
                return  # "I am done"
        elif self.p_state in (1, 2):  # after first word
            if t == NAME:
                self.p_args.append(txt)
                self.p_state = 2
                return
            elif t == OP and txt in ("*", "+", "-"):
                self.p_args.append(txt)
                self.p_state = 1
                return
            elif t == NUMBER:
                self.p_args.append(eval(txt, {}, {}))
                self.p_state = 1
                return
            elif t == STRING:
                self.p_args.append(eval(txt, {}, {}))
                self.p_state = 1
                return
            elif t == OP and txt[0] in ("$", "*"):
                self.p_args.append(txt)
                self.p_state = 1
                return
            elif t == OP and txt == "." and self.p_state == 2:
                self.p_state = 5
                return
            elif t == OP and txt == ":":
                log("parser", TRACE, "RUN2")
                log("parser", TRACE, self.proc.complex_statement, self.p_args)
                self.p_state = 3
                _ = self.proc.complex_statement(self.p_args)

                self.p_stack.append(self.proc)
                self.proc = _
                return
            elif t == NEWLINE:
                log("parser", TRACE, "RUN3")
                log("parser", TRACE, self.proc.simple_statement, self.p_args)
                # defer setting state to zero when pop_after is set
                # because that would break one-line compound statements
                # ("wait :for 2").
                # On the other hand, setting it later anyway breaks
                # statements which terminate the parser ("exit")
                if not self.p_pop_after:
                    self.p_state = 0
                self.proc.simple_statement(self.p_args)

                if self.p_pop_after:
                    self.proc.done()
                    self.proc = self.p_stack.pop()
                    self.p_pop_after = False
                    self.p_state = 0
                self.prompt()
                return
        elif self.p_state == 3:
            if t == NEWLINE:
                self.p_state = 4
                self.prompt()
                return
            elif t == NAME:
                self.p_args = [txt]
                self.p_state = 1
                self.p_pop_after = True
                return
            else:
                self.proc = self.p_stack.pop()
        elif self.p_state == 4:
            if t == INDENT:
                self.p_state = 0
                return
            elif t == NEWLINE:
                # ignore
                return
            else:
                self.proc = self.p_stack.pop()
        elif self.p_state == 5:
            if t == NAME:
                self.p_args[-1] += "." + txt
                self.p_state = 2
                return

        if self.p_pop_after:
            self.proc = self.p_stack.pop()
            self.p_pop_after = False

        raise SyntaxError("Unknown token %s (%s, state %d) in %s:%d" %
                          (repr(txt), tok_name[t] if t in tok_name else t,
                           self.p_state, self.ctx.filename, beg[0]))
Ejemplo n.º 17
0
 wait_for_all_threads
from moat.collect import Collection, Collected

import gevent
import aiogevent
import asyncio
import qbroker
qbroker.setup(gevent=True)
from qbroker.util. async import Main as _Main

__all__ = ("start_up", "shut_down", "startup_event", "shutdown_event",
           "ShutdownHandler", "mainloop", "Events")

Main = None

startup_event = Event(Context(startup=True), "startup")
shutdown_event = Event(Context(shutdown=True), "shutdown")

active_q_id = 0
running = False
stopping = False


class Events(Collection):
    name = "event"


Events = Events()


class Shutdown_Worker(Worker):
Ejemplo n.º 18
0
	def __init__(self, parent=None, ctx=None):
		self.ctx = ctx or Context()
		self.parent = parent
Ejemplo n.º 19
0
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License (included; see the file LICENSE)
##  for more details.
##
##  This header is auto-generated and may self-destruct at any time,
##  courtesy of "make update". The original is in ‘scripts/_boilerplate.py’.
##  Thus, do not remove the next line, or insert any blank lines above.
##BP

from moat.context import Context, default_context

c = Context()
assert not c._store
assert not c
c.foo = "bar"
assert c
assert c.foo == "bar"

d = c(bla="fasel")
assert d.bla == "fasel"
assert d.foo == "bar"
try:
    c.bla
except AttributeError:
    pass
else:
    assert False
Ejemplo n.º 20
0
	def __init__(self,parent=None, ctx=None):
		self.name = SName(self.name)
		self.parent = parent
		self.ctx = ctx or Context()
		self.args = None
Ejemplo n.º 21
0
class Parser(Collected,Jobber):
	"""The input parser object. It is a consumer of lines."""
	storage = Parsers.storage
	line = None
	p_gen = None
	do_prompt = False
	last_pos = None
	job = None

	def __init__(self, input, interpreter, ctx=None):
		"""Parse an input stream and pass the commands to the processor @proc."""
		global _npars
		_npars += 1
		super(Parser,self).__init__("n"+str(_npars))

		self.ending = False

		if ctx is None:
			self.ctx = Context()
		else:
			self.ctx = ctx

		if "filename" not in self.ctx:
			self.ctx.filename=u"‹stdin?›"
		self.input = input
		self.proc = interpreter
		self.do_prompt = interpreter.do_prompt

	def list(self):
		"""Yield a couple of (left,right) tuples, for enumeration."""
		yield super(Parser,self)
		yield ("input",str(self.input))
		if self.last_pos is not None:
			yield ("line",str(self.last_pos[0]))
			yield ("pos",str(self.last_pos[1]))

	def info(self):
		return str(self.input)

	def stop_client(self):
		"""Helper. See TwistedParser.stop_client()."""
		pass

	def lineReceived(self, data):
		self.add_line(data)

	def run(self):
		self.init_state()
		self.prompt()
		if self.input is None:
			self.p_gen = tokizer(self._do_parse,self.job,self.stop_client)
			return
		syn = AsyncResult()
		self.start_job("job",self._run,syn)
		self.p_gen = tokizer(self._do_parse,self.job)
		syn.set(None)

		try:
			e = self.job.get()
			if isinstance(e,BaseException):
				reraise(e)
		except StopParsing:
			pass

		self.p_gen.exit()
		
	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)
			self.input.close()
			self.input = None
		return "Bla"
	

	def endConnection(self, res=None, kill=True):
		"""Called to stop"""
		if self.job:
			if kill:
				self.p_gen.exit()
				self.stop_job("job")
			else:
				self.p_gen.feed(None)

	def delete(self,ctx=None):
		self.endConnection()
		super(Parser,self).delete()

	def add_line(self, data):
		"""Standard LineReceiver method"""
		if not isinstance(data,six.string_types):
			data = data.decode("utf-8")
		self.p_gen.feed(data)

	def init_state(self):
		self.p_state=0
		self.p_pop_after=False
		self.p_stack = []
		self.p_args = []
		if self.p_gen:
			self.p_gen.init()

	def prompt(self):
		if not self.do_prompt:
			return
		if self.p_state == 0 and not self.p_stack:
			self.ctx.out.write(">> ")
		else:
			self.ctx.out.write(".. ")
		getattr(self.ctx.out,"flush",lambda :None)()

	def _do_parse(self, t,txt,beg,end,line):
		# States: 0 newline, 1 after first word, 2 OK to extend word
		#         3+4 need newline+indent after sub-level start, 5 extending word
		#log("parser",TRACE,"PARSE",t,repr(txt))

		try:
			self._parseStep(t,txt,beg,end,line)

		except StopIteration:
			return

		except Exception as ex:
			fix_exception(ex)
			if self.p_stack:
				self.proc = self.p_stack[0]

			self.proc.error(self,ex)
			self.prompt()

	def _parseStep(self, t,txt,beg,end,line):
		from token import NUMBER,NAME,DEDENT,INDENT,OP,NEWLINE,ENDMARKER, \
			STRING
		from moat.tokize import COMMENT,NL
		self.last_pos = beg

		if "logger" in self.ctx:
			self.ctx.logger("T",self.p_state,t,repr(txt),beg,end,repr(line))
		if t == COMMENT:
			return
		if self.p_state == 0: # begin of statement
			if t == NAME:
				self.p_args = [txt]
				self.p_state=1
				return
			elif t == DEDENT:
				self.proc.done()
				if self.p_stack:
					self.proc = self.p_stack.pop()
					return
				else:
					raise StopIteration
			elif t == ENDMARKER:
				self.proc.done()
				while self.p_stack:
					self.proc = self.p_stack.pop()
					self.proc.done()
				raise StopIteration
			elif t in(NL,NEWLINE):
				self.prompt()
				return
			elif t == OP and txt == ".":
				return # "I am done"
		elif self.p_state in (1,2): # after first word
			if t == NAME:
				self.p_args.append(txt)
				self.p_state = 2
				return
			elif t == OP and txt in ("*","+","-"):
				self.p_args.append(txt)
				self.p_state = 1
				return
			elif t == NUMBER:
				self.p_args.append(eval(txt,{},{}))
				self.p_state = 1
				return
			elif t == STRING:
				self.p_args.append(eval(txt,{},{}))
				self.p_state = 1
				return
			elif t == OP and txt[0] in ("$","*"):
				self.p_args.append(txt)
				self.p_state = 1
				return
			elif t == OP and txt == "." and self.p_state == 2:
				self.p_state = 5
				return
			elif t == OP and txt == ":":
				log("parser",TRACE,"RUN2")
				log("parser",TRACE,self.proc.complex_statement,self.p_args)
				self.p_state = 3
				_ = self.proc.complex_statement(self.p_args)

				self.p_stack.append(self.proc)
				self.proc = _
				return
			elif t == NEWLINE:
				log("parser",TRACE,"RUN3")
				log("parser",TRACE,self.proc.simple_statement,self.p_args)
				# defer setting state to zero when pop_after is set
				# because that would break one-line compound statements
				# ("wait :for 2").
				# On the other hand, setting it later anyway breaks
				# statements which terminate the parser ("exit")
				if not self.p_pop_after:
					self.p_state=0
				self.proc.simple_statement(self.p_args)
					
				if self.p_pop_after:
					self.proc.done()
					self.proc = self.p_stack.pop()
					self.p_pop_after=False
					self.p_state=0
				self.prompt()
				return
		elif self.p_state == 3:
			if t == NEWLINE:
				self.p_state = 4
				self.prompt()
				return
			elif t == NAME:
				self.p_args = [txt]
				self.p_state = 1
				self.p_pop_after = True
				return
			else:
				self.proc = self.p_stack.pop()
		elif self.p_state == 4:
			if t == INDENT:
				self.p_state = 0
				return
			elif t == NEWLINE:
				# ignore
				return
			else:
				self.proc = self.p_stack.pop()
		elif self.p_state == 5:
			if t == NAME:
				self.p_args[-1] += "."+txt
				self.p_state = 2
				return

		if self.p_pop_after:
			self.proc = self.p_stack.pop()
			self.p_pop_after = False

		raise SyntaxError("Unknown token %s (%s, state %d) in %s:%d" % (repr(txt),tok_name[t] if t in tok_name else t,self.p_state,self.ctx.filename,beg[0]))
Ejemplo n.º 22
0
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License (included; see the file LICENSE)
##  for more details.
##
##  This header is auto-generated and may self-destruct at any time,
##  courtesy of "make update". The original is in ‘scripts/_boilerplate.py’.
##  Thus, do not remove the next line, or insert any blank lines above.
##BP

from moat.context import Context, default_context

c = Context()
assert not c._store
assert not c
c.foo = "bar"
assert c
assert c.foo == "bar"

d = c(bla="fasel")
assert d.bla == "fasel"
assert d.foo == "bar"
try:
    c.bla
except AttributeError:
    pass
else:
    assert False
Ejemplo n.º 23
0
 def __init__(self):
     self.ctx = Context()