diff --git a/bin/cc-cli b/bin/cc-cli index 4d09a8168b27604be8366da238bd7d18478c7980..04766a5a1a651aa133aa6a97855a878858ad26c2 100755 --- a/bin/cc-cli +++ b/bin/cc-cli @@ -24,7 +24,7 @@ settings = { "timeout": "30", "hsize": "100", "alias": "%s/alias"%BaseDirectory.save_config_path(cccli.canonical_name), - "tag": "%s/tag"%BaseDirectory.save_config_path(cccli.canonical_name), + "tagdisplay": "%s/tagdisplay"%BaseDirectory.save_config_path(cccli.canonical_name), "history": "%s/history"%BaseDirectory.save_data_path(cccli.canonical_name), "expert": "%s/expert"%BaseDirectory.save_data_path(cccli.canonical_name), } diff --git a/cccli/cli.py b/cccli/cli.py index 8afa740141dca4308d53d9b0da5ec803ca7a34dd..912399601013679a41aa997cbc9ffdf3956d5b25 100644 --- a/cccli/cli.py +++ b/cccli/cli.py @@ -62,7 +62,7 @@ class Cli(object): self.alias.load(self.settings.get("alias", "")) self.printer.debug("Loaded aliases: %d"%len(self.alias)) # load tagdisplay - self.tagdisplay.load(self.settings.get("tag", "")) + self.tagdisplay.load(self.settings.get("tagdisplay", "")) # connecting self._connect() # auth diff --git a/cccli/command/__init__.py b/cccli/command/__init__.py index a2f447f90a2ca0d2c70a4b46acf0ff2f417b9a76..58782b2d3906c2c806c056b8190c8b32c0467cec 100644 --- a/cccli/command/__init__.py +++ b/cccli/command/__init__.py @@ -19,3 +19,4 @@ from cccli.command.vm import * from cccli.command.list import Command_list from cccli.command.expert import Command_expert from cccli.command.server import Command_server +from cccli.command.tagdisplay import Command_tagdisplay diff --git a/cccli/command/command.py b/cccli/command/command.py index 5b4aa9d75492c45a5c0ac47f413247e14643920a..92549d8161f365938c3d37380806ba1e74c095f0 100644 --- a/cccli/command/command.py +++ b/cccli/command/command.py @@ -14,6 +14,9 @@ class Command(object): self.printer = self.cli.printer self.name = argv0 + def __call__(self, argv): + raise NotImplementedError + def usage(self): return "Usage: %s"%self.name @@ -27,6 +30,9 @@ class OptionCommand(Command): 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() diff --git a/cccli/command/server.py b/cccli/command/server.py index 1a0eedbd4e4c75c6c856f70874ddc63cb1594f50..3ce28ad0f7a8ae5e43b0107ff86b7c3a8ecea7f7 100644 --- a/cccli/command/server.py +++ b/cccli/command/server.py @@ -25,7 +25,6 @@ class Command_server(OptionCommand): (options, args) = self.option.parse_args(argv[1:]) except SystemExit: return - print dir(options) if len(args) > 0 or (not options.cache and not options.commands): self.printer.out(self.usage()) return diff --git a/cccli/command/tagdisplay.py b/cccli/command/tagdisplay.py new file mode 100644 index 0000000000000000000000000000000000000000..e0608e07c5c977702c396e7a70c750e62c204e7b --- /dev/null +++ b/cccli/command/tagdisplay.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +#coding=utf8 + +''' +CloudControl tagdisplay command +''' +from cccli.exception import * +from sjrpc.core.exceptions import * +from cccli.printer import Printer, color +from cccli.command.command import OptionCommand + +class Command_tagdisplay(OptionCommand): + '''Tagdisplay tool''' + + def __init__(self, cli, argv0): + OptionCommand.__init__(self, cli, argv0) + self.option.set_usage("%prog [options] [tag] ...") + self.option.add_option("-c", action="store", dest="setcolor", + help="Set color SETCOLOR to tags") + self.option.add_option("-t", action="store", dest="settype", + help="Set type SETTYPE to tags") + self.option.add_option("-C", action="store_true", dest="delcolor", + help="Remove custom tags color") + self.option.add_option("-T", action="store_true", dest="deltype", + help="Remove custom tags type") + self.option.add_option("--list-colors", action="store_true", dest="listcolor", + help="List allowed color") + self.option.add_option("--list-types", action="store_true", dest="listtype", + help="List allowed types") + + def __call__(self, argv): + try: + (options, args) = self.option.parse_args(argv[1:]) + except SystemExit: + return + if options.listcolor: + self.list("color") + elif options.listtype: + self.list("type") + elif len(args) > 0 and options.setcolor: + self.set("color", args, options.setcolor) + elif len(args) > 0 and options.delcolor: + self.set("color", args, None) + elif len(args) > 0 and options.settype: + self.set("type", args, options.settype) + elif len(args) > 0 and options.deltype: + self.set("type", args, None) + elif len(args) == 0: + self.list("my") + else: + self.printer.out(self.usage()) + + def list(self, what): + '''List displaytag information''' + if what == "color": + self.printer.out("%sTag allowed colors:%s"%(color["lblue"], color["reset"])) + for c in color.keys(): + if c.isalpha(): + self.printer.out(c) + elif what == "type": + self.printer.out("%sTag allowed types:%s"%(color["lblue"], color["reset"])) + for t in self.cli.tagdisplay.types: + self.printer.out(t) + elif what == "my": + self.printer.out("%sMy tagdisplay colors:%s"%(color["lblue"], color["reset"])) + for (u,v) in self.cli.tagdisplay.tagcolor.items(): + self.printer.out("%s: %s"%(u,v)) + self.printer.out("%sMy tagdisplay types:%s"%(color["lblue"], color["reset"])) + for (u,v) in self.cli.tagdisplay.tagtype.items(): + self.printer.out("%s: %s"%(u,v)) + else: + raise AttributeError + + def set(self, what, tags, value): + '''Set displaytag info''' + # crazy gard + if not len(tags) > 0: + return + # set appropriate database + if what == "color": + db = self.cli.tagdisplay.tagcolor + vdb = [ c for c in color.keys() if c.isalpha() ] + elif what == "type": + db = self.cli.tagdisplay.tagtype + vdb = self.cli.tagdisplay.types + else: + raise AttributeError + # check attribute + if value is not None and value not in vdb: + self.printer.error("Invalid value: %s"%value) + return + # update db + for tagname in tags: + if value is None: + if tagname in db: + del db[tagname] + else: + db[tagname] = value + self.cli.tagdisplay.save(self.cli.settings.get("tagdisplay", ""))