diff --git a/cccli/command/account.py b/cccli/command/account.py index 52b5467d408b5774974b724db3ab3916d59c379e..95ae16cba229276f0ead2541b616f7b9722d17fb 100644 --- a/cccli/command/account.py +++ b/cccli/command/account.py @@ -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 " -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 " - class Command_close(Command): '''Disable accounts''' diff --git a/cccli/command/command.py b/cccli/command/command.py index 92549d8161f365938c3d37380806ba1e74c095f0..c78dc6d23c341f0ccdb69ee8b9aeab6f180972c4 100644 --- a/cccli/command/command.py +++ b/cccli/command/command.py @@ -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] ") + # 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) diff --git a/cccli/command/list.py b/cccli/command/list.py index 3464b2bb4ead9e9455199355496fca9e8d5ef8bf..f70f7178e7dd62294562c117b7c22027f2a6d6d3 100644 --- a/cccli/command/list.py +++ b/cccli/command/list.py @@ -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''' diff --git a/cccli/tagdisplay.py b/cccli/tagdisplay.py index 1b418d7f770bc522b41fa4c246c86dca5a17a448..151378ded44a9aa6b102f4b7f4c00e5cbc4f4f68 100644 --- a/cccli/tagdisplay.py +++ b/cccli/tagdisplay.py @@ -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)