Commit bea530ff authored by Seblu's avatar Seblu
Browse files

new TqlCommand class which handle command with tql

server must send tags with string format
parent c899533e
Loading
Loading
Loading
Loading
+13 −7
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ CloudControl accounts related commands
from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
from cccli.command.command import Command
from cccli.command.command import Command, TqlCommand

class Command_addaccount(Command):
    '''Create an account'''
@@ -25,20 +25,26 @@ class Command_addaccount(Command):
        return "Usage: addaccount <account name> <role>"


class Command_delaccount(Command):
class Command_delaccount(TqlCommand):
    '''Delete an account'''

    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)

    def __call__(self, argv):
        if len(argv) != 2:
        try:
            (options, args) = self.option.parse_args(argv[1:])
        except SystemExit:
            return
        if len(args) != 1:
            raise cmdBadArgument()
        try:
            self.cli.rpc.call("delaccount", argv[1])
            d = self.cli.rpc.call("delaccount", args[0])
            if options.status:
                self.show_status(d)
        except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))

    def usage(self):
        return "Usage: delaccount <tql>"


class Command_close(Command):
    '''Disable accounts'''
+35 −5
Original line number Diff line number Diff line
@@ -5,9 +5,11 @@
CloudControl CLI command module
'''

from cccli.printer import Printer, color
from optparse import OptionParser

class Command(object):
    '''Base of all command class'''

    def __init__(self, cli, argv0):
        self.cli = cli
@@ -25,16 +27,44 @@ class Command(object):


class OptionCommand(Command):
    '''Add options parser to Command'''

    def __init__(self, cli, argv0):
        Command.__init__(self, cli, argv0)
        self.option = OptionParser(prog=argv0)

    def __call__(self, argv):
        raise NotImplementedError

    def usage(self):
        return self.option.format_help().strip()

    def help(self):
        return self.__doc__

class TqlCommand(OptionCommand):
    '''Add Tql stuff to Command'''

    def __init__(self, cli, argv0):
        OptionCommand.__init__(self, cli, argv0)
        self.rpc = cli.rpc
        self.option.set_usage("%prog [options] <tql>")
        # set tql status stuff
        self.option.add_option("-s", "--status", action="store_true", dest="status",
                               help="Show status of each tql object")
        # set tagdisplay stuff
        self.tdr = self.cli.tagdisplay.resolve
        self.tdc = self.cli.tagdisplay.color
        self.option.add_option("-n", "--no-tagdisplay", action="callback", dest="tagdisplay",
                               callback=self.opt_notagdisplay,
                               help="No tagdisplay system")

    def opt_notagdisplay(self, option, opt, value, parser):
        '''Callback for option --no-tagdisplay'''
        self.tdr = lambda tagname, tagvalue: tagvalue
        self.tdc = lambda tagname: color["reset"]

    def show_status(self, ans):
        '''Show status of an Tql request'''
        for o in ans:
            s = "%sid: %s%s %sstatus: %s%s %smessage:%s%s%s"%(
                color["reset"], self.tdc("id"), self.tdr("id", o),
                color["reset"], self.tdc(""), ans[o][0],
                color["reset"], self.tdc(""), ans[o][1],
                color["reset"])
            self.printer.out(s)
+1 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ class Command_list(OptionCommand):
                                           self.tc(t),
                                           self.td(t, v))
                              for (t,v) in o.items() ])
            self.printer.out("%sid:%s%s %s%s"%(color["reset"], color["lblue"], id, tags, color["reset"]))
            self.printer.out("%sid:%s%s %s%s"%(color["reset"], self.tc("id"), id, tags, color["reset"]))

    def _list_align(self, objs):
        '''Listing line aligned'''
+1 −3
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ class TagDisplay(object):

    def color(self, tagname):
        '''Return the current tag color'''
        tagname = unicode(tagname)
        # build list of matching pattern with tagname
        l = [ x for x in self.tagcolor if fnmatch.fnmatch(tagname, x) ]
        if len(l) > 0:
@@ -62,9 +61,8 @@ class TagDisplay(object):

    def resolve(self, tagname, tagvalue):
        '''Transform a tagvalue respecting custom display settings'''
        tagname = unicode(tagname)
        tagvalue = unicode(tagvalue)
        # check general options
        print tagvalue
        if bool(self.option.get("quotespace", False)):
            if re.search("\s", tagvalue) is not None:
                tagvalue = "'%s'"%re.sub("'", "\'", tagvalue)