Exemplo n.º 1
0
    def execute (self, args, namespace):

        if not args:
            return Error ("invalid syntax")       
        
        names = None
        results = []
        
        try:
            names = args.strip().split ()
        except ValueError:
            return Error ("invalid syntax")

        for name in names:

            cmd = [cmd for cmd in namespace if name in cmd.aliases()]

            if not cmd:
                results.append(Error ("'%s' it's not defined." % name))
                continue

            cmd = cmd[0]
            cmd.delAlias(name)

        return results
Exemplo n.º 2
0
    def add (self, args, namespace):

        if not args or len (args) < 2:
            return Error ("Invalid syntax")

        kind, location = args

        if location in namespace:
            return Error ("%s already on the system." % location)

        ret = False

        if kind == "instrument":
            ret = ConsoleController().getManager().addInstrument (location)
        elif kind == "controller":
            ret = ConsoleController().getManager().addController (location)
        elif kind == "driver":
            ret = ConsoleController().getManager().addDriver (location)
        else:
            return Error("Invalid object type '%s'" % kind)

        if not ret:
            return Error("Couldn't add %s '%s'" % (kind, location))

        # get the object and put on the namespace
        obj = ConsoleController().getObject(location)
        namespace.append (ObjectCommand (location, obj))

        return False
Exemplo n.º 3
0
    def _methodHandler(self, method, args):

        func = None

        try:
            func = getattr(self.obj, method)
        except AttributeError, e:
            return Error("Invalid operation on %s" % self.name())
Exemplo n.º 4
0
    def _do(self, args):

        name, args = self.lastcmd.split()[0], args

        for cmd in self.namespace:
            if cmd.name() == name or name in cmd.aliases():
                return self._execute(cmd, args)

        print Error("Invalid command '%s'" % name)
Exemplo n.º 5
0
    def execute (self, args, namespace):

        if not args:
            return Error ("invalid syntax")
        
        name, value = None, None
        
        try:
            name, value = args.split ()
        except ValueError:
            return Error ("invalid syntax")

        cmd = [cmd for cmd in namespace if cmd.name() == value]

        if not cmd:
            return Error ("'%s' it's not defined." % value)
        else:
            cmd = cmd[0]

        if name in cmd.aliases():
            return Error ("'%s' it's already defined. Unset it first ('unalias %s')." % (value, value))
        else:
            cmd.addAlias(name)
            return False
Exemplo n.º 6
0
    def execute (self, args, namespace):

        if not args:
            return False

        try:
            splitted = args.split()

            if len (splitted) > 1:
                cmd, args = splitted[0], splitted[1:]
            else:
                cmd, args = splitted[0], []

            # try to found a handler
            if cmd in self._getCommands ():
                handler = getattr (self, '%s' % cmd)
                return handler (args, namespace)
            else:
                return Error("Invalid option '%s' to '%s'" % (cmd, self.name()))
            
        except ValueError:
            return Error("Invalid option '%s' to '%s'" % (cmd, self.name()))

        return Error("Invalid option '%s' to '%s'" % (cmd, self.name()))
Exemplo n.º 7
0
    def execute(self, args, namespace):

        if not args.strip():
            return str(self.name())

        method = args.split()[0]

        if method in self.specials:
            return self.specials[method](args)

        try:
            args = args[len(method):].strip()
            evaluated = ()

            # if there are arguments, eval them
            if args:
                evaluated = eval("(%s,)" % args)

            return self._methodHandler(method, evaluated)

        except Exception, e:
            print e
            return Error("Invalid arguments '%s' for %s" % (args, method))
Exemplo n.º 8
0
 def default(self, line):
     print Error("invalid command")
     return False
Exemplo n.º 9
0
    def _configHandler(self, args):

        if not hasattr(self.obj, 'config'):
            return Error("Object doesn't support configuration")

        args = args[len("config"):].strip()

        if not args:
            # return all
            return Config(self.obj.config)

        def getConfig(key):
            return self.obj.config[key]

        def setConfig(key, value):
            self.obj.config[key] = value
            return None

        # parse required on config args
        tasks = []
        errors = []
        results = {}

        for item in args.split(","):

            if "=" in item:
                # setter one
                try:
                    key, value = item.split("=")
                    key = key.strip()
                    value = value.strip()

                    try:
                        value = eval(value)
                    except Exception:
                        errors.append("invalid configuration value")

                    tasks.append((setConfig, (key, value)))

                except ValueError:
                    errors.append("invalid syntax")

            else:
                # simple getters
                key = item.strip()

                if not key:
                    continue

                tasks.append((getConfig, (key, )))

        for task in tasks:

            handler, name, args = task[0], task[1][0], task[1]

            try:
                result = handler(*args)

                if result != None:
                    results[name] = result

            except KeyError:
                errors.append("'%s' doesn't exists" % name)
            except OptionConversionException:
                errors.append("invalid configuration value to '%s'" % name)

        errors_list = [Error("%s: %s" % (self.name(), err)) for err in errors]
        results_list = [Config(results)]

        return errors_list + results_list
Exemplo n.º 10
0
    def _methodHandler(self, method, args):

        func = None

        try:
            func = getattr(self.obj, method)
        except AttributeError, e:
            return Error("Invalid operation on %s" % self.name())

        # check arguments number
        arg_count, non_default_args, signature = self._getMethodSignature(func)

        if len(args) < non_default_args:
            return [
                Error("%s takes %d argument(s), %d given." %
                      (method, non_default_args, len(args))),
                Hint("Usage: %s" % signature)
            ]

        elif len(args) > arg_count:
            return [
                Error("%s takes at most %d argument(s), %d given." %
                      (method, non_default_args, len(args))),
                Hint("Usage: %s" % signature)
            ]

        try:
            return func(*args)
        except Exception, e:
            print e
            return Error("Error in %s %s call" % (self.name(), method))