Example #1
0
        def cb_call(text):
          """
          Interpret the text, execute the service, and insert the 
          output into the command window.

          """
          try:
            hlp = text.split()
            if len(hlp) == 2 and hlp[0] == "help":
              output = self.server.serviceUsage(hlp[1]) or "No help available"
            else:
              registrar = self.server.registrar
              cmd, namespace, args = registrar.find_best_service(text)
              if cmd is None:
                output = "Could not parse input."
              else:
                output = registrar.request_service(cmd, namespace,
                   args) or "No output."
          except exceptions.ServiceNotProvidedError as err:
            output = str(err)
          except UnsuccessfulExecution as err:
            output = "Execution Unsuccessful: " + str(err)
          except Exception as err:
            output = "Exception encountered: " + str(err)
          output = output.strip()
          if len(output) > 0:
            with locked(self.command_log_lock):
              buf = textview.get_buffer()
              end = buf.get_end_iter()
              if buf.get_char_count() > 0:
                buf.insert(end, '\n')
              buf.insert(end, output)
              textview.scroll_mark_onscreen(buf.get_insert())
          return False
Example #2
0
  def unregister_service(self, svc_name, namespace=None):
    """Remove the registered service with the provided name and
    (optional) namespace.

    """
    svc_name = svc_name.lower()
    svc = self.services.get(svc_name, namespace)
    if svc:
      with locked(svc.lock):
        self.services.remove(svc_name, namespace)
Example #3
0
    def unregister_service(self, svc_name, namespace=None):
        """Remove the registered service with the provided name and
    (optional) namespace.

    """
        svc_name = svc_name.lower()
        svc = self.services.get(svc_name, namespace)
        if svc:
            with locked(svc.lock):
                self.services.remove(svc_name, namespace)
Example #4
0
  def register_object(self, obj_name, obj):
    """Create a shallow copy of the provided object and register it.
    WARNING: Anything beyond simple objects may be changed without warning
    by other plugins.

    """
    obj_name = obj_name.lower()
    with locked(self.__obj_lock):
      if obj_name not in self.objects:
        self.objects[obj_name] = self._RepoObj(_copy(obj))
      else:
        self.objects[obj_name].obj = obj

      # Prevents the handler from needing a call to get_object
      self.objects[obj_name].change_event.fire(obj)
Example #5
0
    def register_object(self, obj_name, obj):
        """Create a shallow copy of the provided object and register it.
    WARNING: Anything beyond simple objects may be changed without warning
    by other plugins.

    """
        obj_name = obj_name.lower()
        with locked(self.__obj_lock):
            if obj_name not in self.objects:
                self.objects[obj_name] = self._RepoObj(_copy(obj))
            else:
                self.objects[obj_name].obj = obj

            # Prevents the handler from needing a call to get_object
            self.objects[obj_name].change_event.fire(obj)
Example #6
0
  def request_service(self, svc_name, namespace=None, argdict=None, **kwargs):
    """Call the service registered to svc_name with the specified (optional)
    namespace and given kwargs.

    """
    svc_name = svc_name.lower()
    if not self.services.get(svc_name, namespace):
      raise ServiceDoesNotExist("Service does not exist.")
    service = self.services.get(svc_name, namespace)
    with locked(service.lock):
      if argdict:
        try:
          kwargs.update(argdict)
        except AttributeError:
          pass
      return service.svc(**kwargs)
Example #7
0
    def request_service(self,
                        svc_name,
                        namespace=None,
                        argdict=None,
                        **kwargs):
        """Call the service registered to svc_name with the specified (optional)
    namespace and given kwargs.

    """
        svc_name = svc_name.lower()
        if not self.services.get(svc_name, namespace):
            raise ServiceDoesNotExist("Service does not exist.")
        service = self.services.get(svc_name, namespace)
        with locked(service.lock):
            if argdict:
                try:
                    kwargs.update(argdict)
                except AttributeError:
                    pass
            return service.svc(**kwargs)
Example #8
0
                def cb_call(text):
                    """
          Interpret the text, execute the service, and insert the 
          output into the command window.

          """
                    try:
                        hlp = text.split()
                        if len(hlp) == 2 and hlp[0] == "help":
                            output = self.server.serviceUsage(
                                hlp[1]) or "No help available"
                        else:
                            registrar = self.server.registrar
                            cmd, namespace, args = registrar.find_best_service(
                                text)
                            if cmd is None:
                                output = "Could not parse input."
                            else:
                                output = registrar.request_service(
                                    cmd, namespace, args) or "No output."
                    except exceptions.ServiceNotProvidedError as err:
                        output = str(err)
                    except UnsuccessfulExecution as err:
                        output = "Execution Unsuccessful: " + str(err)
                    except Exception as err:
                        output = "Exception encountered: " + str(err)
                    output = output.strip()
                    if len(output) > 0:
                        with locked(self.command_log_lock):
                            buf = textview.get_buffer()
                            end = buf.get_end_iter()
                            if buf.get_char_count() > 0:
                                buf.insert(end, '\n')
                            buf.insert(end, output)
                            textview.scroll_mark_onscreen(buf.get_insert())
                    return False
Example #9
0
 def remove_object(self, obj_name):
   """Remove a registered object from the internal cache."""
   obj_name = obj_name.lower()
   with locked(self.__obj_lock):
     del self.objects[obj_name]
Example #10
0
 def remove_object(self, obj_name):
     """Remove a registered object from the internal cache."""
     obj_name = obj_name.lower()
     with locked(self.__obj_lock):
         del self.objects[obj_name]
Example #11
0
  def interpret(self, clientid, raw):
    """Use the interpreter to translate the raw (arbitrary) text into
    a command:arguments pair that is then executed like normal


    Arguments:
    clientid -- a unique id assigned to the client when registering
    raw -- the raw text to interpret
    
    Raises:
    ClientNotRegisteredError
    ServiceNotRegisteredError
    UnknownIntentError

    """
    client = self.get_client(clientid, touch_client=False)
    with utils.locked(client.lock):
      # TODO put conversation_timeout in settings
      conversation_timeout = 40  # seconds
      if client.last_contact + conversation_timeout < time.time():
        client.current_conversation = None
      client.last_contact = time.time()
      # Continue a previous conversation
      if client.current_conversation is not None:
        try:
          kwargs = {"_raw": raw.lower()}
          output = client.current_conversation.send(kwargs)
        except (StopIteration, AttributeError) as e:
          client.current_conversation = None
      # Start a new conversation
      if client.current_conversation is None:
        matches = self.registrar.find_services(raw)
        
        # remove all unregistered services
        matches = [match for match in matches if 
          match[0] in client.plugins]

        if len(matches) == 0:
          raise exceptions.UnknownIntentError(
            "Execution failed: could not find a registered command.")

        output = None
        # TODO move limit to settings file
        limit = 1  # How many results to try before quitting
        try:
          # Zip terminates with shortest sequence, so stops after
          # matches end or `limit` iterations
          for (ix, (command, namespace, args)) in zip(xrange(limit), matches):
            conversation = self.registrar.request_service(
              svc_name=command, namespace=namespace, argdict=args)
            if conversation:  # Service returns None if it can't handle input
              try:
                output = conversation.next()
                client.current_conversation = conversation
              except AttributeError as e:  # Not a generator, get string output
                output = str(conversation)
                client.history.append(
                  clientmanager.QueryHistory(raw, command, namespace, args))
                client.history.append(
                  clientmanager.ResponseHistory(output))
              break  # We got some good output, don't continue to the next
        except UnsuccessfulExecution as e:
          output = "Execution failed: " + str(e)

      if not output:
        output = "No output."
    
    return output
Example #12
0
 def testfunc(lock, stream):
   with utils.locked(lock):
     for x in xrange(output_length):
       stream.write(unicode(x))
Example #13
0
 def testfunc(lock, stream):
     with utils.locked(lock):
         for x in xrange(output_length):
             stream.write(unicode(x))
Example #14
0
 def clear_log(button):  # pylint: disable-msg=W0613
   """Clears the command history log display."""
   with locked(self.command_log_lock):
     self.command_log.delete(*self.command_log.get_bounds())
Example #15
0
 def clear_log(button):  # pylint: disable-msg=W0613
     """Clears the command history log display."""
     with locked(self.command_log_lock):
         self.command_log.delete(*self.command_log.get_bounds())