Beispiel #1
0
    def initialize(self, parsed_args):
        IOLoop.configurable_default().initialize(self)
        self.install()

        self.args = parsed_args
        self.t0 = time.time()  # Keep track of when we started

        self.devices = {}  # Map device names to AV_Device objects

        self.cmd_handlers = {}  # Map commands to list of handlers
Beispiel #2
0
    def initialize(self, parsed_args):
        IOLoop.configurable_default().initialize(self)
        self.install()

        self.args = parsed_args
        self.t0 = time.time()  # Keep track of when we started

        self.devices = {}  # Map device names to AV_Device objects

        self.cmd_handlers = {}  # Map commands to list of handlers
Beispiel #3
0
def patch_io_loop():
    """Create an IOLoop sub-class

    We have out own io loop subclass that handles unhandled exceptions by
    re-raising them and quitting.  This is useful for io loops used in contexts
    that are not multi-user... for example dynochemy uses its own ioloops for
    parallelizing io operations, in which case an exception would be better off
    just raised out of ioloop.start()

    This is a function that generates a class because newwer version of tornado
    has some dynamic IOLoop sub-class selection mechanism based on platform. So
    we can just statically subclass 'IOLoop'.
    """
    if hasattr(IOLoop, 'configurable_default'):
        base_cls = IOLoop.configurable_default()
    else:
        base_cls = IOLoop

    class StrictExceptionIOLoop(base_cls):
        def handle_callback_exception(self, callback):
            raise

    return StrictExceptionIOLoop
Beispiel #4
0
class AV_Loop(IOLoop.configurable_default()):
    def initialize(self, parsed_args):
        IOLoop.configurable_default().initialize(self)
        self.install()

        self.args = parsed_args
        self.t0 = time.time()  # Keep track of when we started

        self.devices = {}  # Map device names to AV_Device objects

        self.cmd_handlers = {}  # Map commands to list of handlers

    def add_device(self, name, dev):
        assert name not in self.devices
        self.devices[name] = dev

    def add_cmd_handler(self, cmd, handler):
        """Registers the given handler to receive the given A/V command.

        A/V commands are strings made up of whitespace-separated words.
        By registering a handler for a given string "$word1 $word2...",
        that handler will be invoked for any command that starts with
        "$word1 $word2..." (unless a more specific matching handler is
        also registered). E.g. given the following cmd_handlers map: {
          "foo bar": foo_bar,
          "foo": foo,
          "": catch_all,
        }, the following describes which commands will invoke which
        functions:
          - "foo bar baz" -> foo_bar("foo bar", "baz")
          - "foo bar" -> foo_bar("foo bar", "")
          - "foo baz" -> foo("foo", "baz")
          - "foo" -> foo("foo", "")
          - "bar" -> catch_all("", "bar")
          - "foo barf" -> foo("foo", "barf")

        The given handler will be invoked with two arguments, the first
        is the cmd which matched its registration, and the second is
        the remainder of the command that followed the match.
        """
        if cmd in self.cmd_handlers:
            self.cmd_handlers[cmd].append(handler)
        else:
            self.cmd_handlers[cmd] = [handler]

    def remove_cmd_handler(self, cmd, handler):
        """Remove the given handler for the given A/V command."""
        try:
            self.cmd_handlers[cmd].remove(handler)
            if not self.cmd_handlers[cmd]:
                del self.cmd_handlers[cmd]
        except:
            pass

    def submit_cmd(self, cmd):
        """Forward the given A/V command to the appropriate handler(s).

        See the documentation of add_cmd_handler() to see how commands
        are mapped to handlers.
        """
        pre_words = cmd.strip().split()
        post_words = []

        def _invoke(cmd, rest):
            for handler in self.cmd_handlers[cmd]:
                handler(cmd, rest)

        while pre_words:
            pre_cmd = " ".join(pre_words)
            if pre_cmd in self.cmd_handlers:
                return _invoke(pre_cmd, " ".join(post_words))
            post_words.insert(0, pre_words.pop())
        return _invoke("", " ".join(post_words))

    def get_ts(self):
        return time.time() - self.t0

    def run(self):
        """Run the I/O loop until aborted."""
        try:
            self.start()
        except KeyboardInterrupt:
            print("Aborted by Ctrl-C")

        self.close()
        return 0