From bea530ffe7281c80780990886856eb0996992bcf Mon Sep 17 00:00:00 2001 From: Seblu Date: Thu, 10 Feb 2011 16:11:32 +0100 Subject: [PATCH] new TqlCommand class which handle command with tql server must send tags with string format --- cccli/command/account.py | 20 +++++++++++++------- cccli/command/command.py | 40 +++++++++++++++++++++++++++++++++++----- cccli/command/list.py | 2 +- cccli/tagdisplay.py | 4 +--- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/cccli/command/account.py b/cccli/command/account.py index 52b5467..95ae16c 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 92549d8..c78dc6d 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 3464b2b..f70f717 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 1b418d7..151378d 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) -- GitLab