Commit 5b5b0e6b authored by Seblu's avatar Seblu
Browse files

Check remote functions in local command

Introduce new class RemoteCommand, which help checking needed remote functions are available
before adding a command to list of commands
parent 55c82c55
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -55,18 +55,19 @@ class Cli(object):
            self.printer.debug("Loaded history: %s"%len(self.printer.history))
            # enable completion
            self.printer.completion.set_completer(self._command_completer)
        # load commands
        # connecting
        self._connect()
        # auth
        self._auth()
        # load commands (need to be done after connection)
        self.commands = Commands(self)
        self.printer.debug("Remote functions: %d"%len(self.commands.functions))
        self.printer.debug("Loaded commands: %d"%len(self.commands))
        # load alias
        self.aliases.load(self.settings.get("alias", None))
        self.printer.debug("Loaded aliases: %d"%len(self.aliases))
        # load tagdisplay
        self.tagdisplay.load(self.settings.get("tagdisplay", ""))
        # connecting
        self._connect()
        # auth
        self._auth()
        # parsing
        self._parse()
        # save history
+15 −0
Original line number Diff line number Diff line
@@ -40,6 +40,9 @@ class Command_addaccount(TqlCommand):
        if _pass is not None:
            self.rpccall("passwd", "a=%s"%self.args[0], _pass, _direct=True)

    def remote_functions(self):
        return set(("addaccount", "passwd"))


class Command_delaccount(TqlCommand):
    '''Delete an account'''
@@ -54,6 +57,9 @@ class Command_delaccount(TqlCommand):
            raise cmdBadArgument("<tql>")
        self.rpccall("delaccount", self.args[0])

    def remote_functions(self):
        return set(("delaccount",))


class Command_close(TqlCommand):
    '''Disable accounts'''
@@ -68,6 +74,9 @@ class Command_close(TqlCommand):
            raise cmdBadArgument("<tql>")
        self.rpccall("close", self.args[0])

    def remote_functions(self):
        return set(("close",))


class Command_declose(TqlCommand):
    '''Enable accounts'''
@@ -82,6 +91,9 @@ class Command_declose(TqlCommand):
            raise cmdBadArgument("<tql>")
        self.rpccall("declose", self.args[0])

    def remote_functions(self):
        return set(("declose",))


class Command_passwd(TqlCommand):
    '''Change account password'''
@@ -120,3 +132,6 @@ class Command_passwd(TqlCommand):
            raise cmdError("You don't type twice the same password. Aborted")
        # execute command
        self.rpccall("passwd", _tql, _pass)

    def remote_functions(self):
        return set(("passwd",))
+6 −3
Original line number Diff line number Diff line
@@ -8,13 +8,13 @@ CloudControl jobs command
from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
from cccli.command.command import OptionCommand
from cccli.command.command import RemoteCommand

class Command_cancel(OptionCommand):
class Command_cancel(RemoteCommand):
    '''Cancel a job'''

    def __init__(self, cli, argv0):
        OptionCommand.__init__(self, cli, argv0)
        RemoteCommand.__init__(self, cli, argv0)
        self.set_usage("%prog [options] <job_id> ...")

    def __call__(self, argv):
@@ -32,3 +32,6 @@ class Command_cancel(OptionCommand):
                self.printer.error("Invalid job id: %s"%jid)
            except RpcError as e:
                self.printer.error("%s"%e)

    def remote_functions(self):
        return set(("cancel",))
+44 −34
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ class Command(object):


class OptionCommand(Command):
    '''Add options parser to Command'''
    '''Commands with options handling'''

    class OptionCommandParser(OptionParser):
        '''Parser of Option for OptionCommand'''
@@ -67,12 +67,53 @@ class OptionCommand(Command):
        '''Proxy to OptionParser'''
        self.optionparser.set_usage(*args, **kwargs)

class TqlCommand(OptionCommand):
    '''Add Tql stuff to Command'''
class RemoteCommand(OptionCommand):
    '''Command which needs connection to server'''

    def __init__(self, cli, argv0):
        OptionCommand.__init__(self, cli, argv0)
        self.rpc = cli.rpc

    def remote_functions(self):
        '''Return a set of needed remote functions'''
        raise NotImplementedError

    def print_objects(self, objectlist, ignore=None, index=False):
        '''Trivial objectlist printing of tag'''
        if objectlist is None:
            return
        _order = objectlist.get("order", None)
        for (i,o) in enumerate(objectlist["objects"]):
            if index:
                self.printer.out("[%s] "%i, nl="")
            self.print_tags(o, order=_order, ignore=ignore)

    def print_tags(self, taglist, order=None, ignore=None):
        '''Display a tag with tagdisplay settings'''
        ignore = () if ignore is None else ignore
        order = () if order is None else order
        # copy dict to show
        tl = taglist.copy()
        # remove ignore tags
        for tn in ignore:
            tl.pop(tn, None)
        # list to print
        pls = []
        # print firstly order tags
        for tn in order:
            tv = tl.pop(tn, None)
            if tv is not None:
                pls.append("%s%s:%s%s"%(self.tdtc(tn), tn, self.tdc(tn), self.tdr(tn, tv)))
        # print tags without order, alpha ordered
        for tn in sorted(tl.keys()):
            pls.append("%s%s:%s%s"%(self.tdtc(tn), tn, self.tdc(tn), self.tdr(tn, tl[tn])))
        self.printer.out("%s%s"%(" ".join(pls), color["reset"]))

class TqlCommand(RemoteCommand):
    '''Command which handle TQL stuff'''

    def __init__(self, cli, argv0):
        RemoteCommand.__init__(self, cli, argv0)
        self.set_usage("%prog [options] <tql>")
        # set tql filter stuff
        self.tql_filter = ""
@@ -214,34 +255,3 @@ class TqlCommand(OptionCommand):
                    self.print_objects(d, ["output"], index=False)
            except RpcError as e:
                self.printer.error("RPCError: %s"%str(e))

    def print_objects(self, objectlist, ignore=None, index=False):
        '''Trivial objectlist printing of tag'''
        if objectlist is None:
            return
        _order = objectlist.get("order", None)
        for (i,o) in enumerate(objectlist["objects"]):
            if index:
                self.printer.out("[%s] "%i, nl="")
            self.print_tags(o, order=_order, ignore=ignore)

    def print_tags(self, taglist, order=None, ignore=None):
        '''Display a tag with tagdisplay settings'''
        ignore = () if ignore is None else ignore
        order = () if order is None else order
        # copy dict to show
        tl = taglist.copy()
        # remove ignore tags
        for tn in ignore:
            tl.pop(tn, None)
        # list to print
        pls = []
        # print firstly order tags
        for tn in order:
            tv = tl.pop(tn, None)
            if tv is not None:
                pls.append("%s%s:%s%s"%(self.tdtc(tn), tn, self.tdc(tn), self.tdr(tn, tv)))
        # print tags without order, alpha ordered
        for tn in sorted(tl.keys()):
            pls.append("%s%s:%s%s"%(self.tdtc(tn), tn, self.tdc(tn), self.tdr(tn, tl[tn])))
        self.printer.out("%s%s"%(" ".join(pls), color["reset"]))
+3 −0
Original line number Diff line number Diff line
@@ -30,3 +30,6 @@ class Command_execute(TqlCommand):
            self.printer.out("%sid:%s%s%s output:"%(self.tdtc("id"), self.tdc("id"),
                                                    o["id"], color["reset"]))
            self.printer.out(o.get("output", ""), nl="")

    def remote_functions(self):
        return set(("execute",))
Loading