diff --git a/cccli/cli.py b/cccli/cli.py index 912399601013679a41aa997cbc9ffdf3956d5b25..0329d6a9c094e887b4fee90e5e4fd0aac1f9f45d 100644 --- a/cccli/cli.py +++ b/cccli/cli.py @@ -145,6 +145,8 @@ class Cli(object): # execute command self.printer.debug("argv: %s"%argv) self.commands(argv) + except cmdExit: + pass except cmdBadArgument as e: if str(e): self.printer.error("Bad argument: %s."%str(e)) diff --git a/cccli/command/account.py b/cccli/command/account.py index 95ae16cba229276f0ead2541b616f7b9722d17fb..619b653caea3c0fa5d3594ad5f39243bc00ec528 100644 --- a/cccli/command/account.py +++ b/cccli/command/account.py @@ -28,53 +28,31 @@ class Command_addaccount(Command): class Command_delaccount(TqlCommand): '''Delete an account''' - def __init__(self, cli, argv0): - TqlCommand.__init__(self, cli, argv0) - def __call__(self, argv): - try: - (options, args) = self.option.parse_args(argv[1:]) - except SystemExit: - return - if len(args) != 1: - raise cmdBadArgument() - try: - 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)) + self.parse_args(argv) + if len(self.args) != 1: + raise cmdBadArgument("") + self.rpccall("delaccount", self.args[0]) -class Command_close(Command): +class Command_close(TqlCommand): '''Disable accounts''' def __call__(self, argv): - if len(argv) != 2: - raise cmdBadArgument() - try: - self.cli.rpc.call("close", argv[1]) - except RpcError as e: - raise cmdError("RPCError: %s"%str(e)) - - def usage(self): - return "Usage: close " + self.parse_args(argv) + if len(self.args) != 1: + raise cmdBadArgument("") + self.rpccall("close", self.args[0]) -class Command_declose(Command): +class Command_declose(TqlCommand): '''Enable accounts''' def __call__(self, argv): - - if len(argv) != 2: - raise cmdBadArgument() - try: - self.cli.rpc.call("declose", argv[1]) - except RpcError as e: - raise cmdError("RPCError: %s"%str(e)) - - def usage(self): - return "Usage: declose " + self.parse_args(argv) + if len(self.args) != 1: + raise cmdBadArgument("") + self.rpccall("declose", self.args[0]) class Command_passwd(Command): diff --git a/cccli/command/command.py b/cccli/command/command.py index c78dc6d23c341f0ccdb69ee8b9aeab6f180972c4..13cc1fcfb39bca2fa08a4134558081a4458aaa7c 100644 --- a/cccli/command/command.py +++ b/cccli/command/command.py @@ -5,6 +5,8 @@ CloudControl CLI command module ''' +from cccli.exception import * +from sjrpc.core.exceptions import * from cccli.printer import Printer, color from optparse import OptionParser @@ -29,13 +31,40 @@ class Command(object): class OptionCommand(Command): '''Add options parser to Command''' + class OptionCommandParser(OptionParser): + '''Parser of Option for OptionCommand''' + + def error(self, e): + raise cmdBadArgument(e) + + def exit(self): + raise cmdExit() + def __init__(self, cli, argv0): Command.__init__(self, cli, argv0) - self.option = OptionParser(prog=argv0) + self.optionparser = OptionCommand.OptionCommandParser(prog=argv0) + self.options = None + self.args = list() def usage(self): - return self.option.format_help().strip() + '''Return usage string''' + return self.optionparser.format_help().strip() + + def parse_args(self, argv): + '''Wrapper to parse_args''' + (self.options, self.args) = self.optionparser.parse_args(argv[1:]) + + def add_option(self, *args, **kwargs): + '''Proxy to OptionParser''' + self.optionparser.add_option(*args, **kwargs) + def remove_option(self, *args, **kwargs): + '''Proxy to OptionParser''' + self.optionparser.remove_option(*args, **kwargs) + + def set_usage(self, *args, **kwargs): + '''Proxy to OptionParser''' + self.optionparser.set_usage(*args, **kwargs) class TqlCommand(OptionCommand): '''Add Tql stuff to Command''' @@ -43,28 +72,47 @@ class TqlCommand(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) self.rpc = cli.rpc - self.option.set_usage("%prog [options] ") + self.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") + self.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") + self.tdtc = self.cli.tagdisplay.titlecolor + self.add_option("-n", "--no-tagdisplay", action="callback", dest="tagdisplay", + callback=self._cb_notagdisplay, + help="No tagdisplay custom display") + - def opt_notagdisplay(self, option, opt, value, parser): + def _cb_notagdisplay(self, option, opt, value, parser): '''Callback for option --no-tagdisplay''' self.tdr = lambda tagname, tagvalue: tagvalue - self.tdc = lambda tagname: color["reset"] + self.tdc = self.cli.tagdisplay.default_color + self.tdtc = self.cli.tagdisplay.default_titlecolor + + def rpccall(self, *args, **kwargs): + '''Call a RPC method an show tql return''' + try: + d = self.rpc.call(*args, **kwargs) + if self.options.status: + self.show_status(d) + return d + except RpcError as e: + raise cmdError("RPCError: %s"%str(e)) + 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) + try: + s = "" + for o in ans: + s += "%sid: %s%s %sstatus: %s%s %smessage:%s%s%s"%( + self.tdtc("id"), self.tdc("id"), self.tdr("id", o), + self.tdtc("status"), self.tdc("status"), ans[o][0], + self.tdtc("message"), self.tdc("message"), ans[o][1], + color["reset"]) + if s: + self.printer.out(s) + except Exception: + pass diff --git a/cccli/command/host.py b/cccli/command/host.py index c5bf1b9e818485097a4010c980fce02995e1d307..6cd6d6749458abec82c34ef8c78468b2635cceb6 100644 --- a/cccli/command/host.py +++ b/cccli/command/host.py @@ -21,27 +21,26 @@ class Command_exec(Command): def usage(self): return "Usage: exec " + class Command_shutdown(OptionCommand): '''Shutdown a physical host''' def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) - self.option.set_usage("%prog [options] ") - self.option.add_option("-r", "--reboot", action="store_true", dest="reboot", - help="Reboot after shutdown (default)") - self.option.add_option("-H", "--halt", action="store_false", dest="reboot", - help="Halt after shutdown") - self.option.add_option("-n", action="store_false", dest="graceful", default=True, - help="do not go through init but go down real fast") + self.set_usage("%prog [options] ") + self.add_option("-r", "--reboot", action="store_true", dest="reboot", + help="Reboot after shutdown (default)") + self.add_option("-H", "--halt", action="store_false", dest="reboot", + help="Halt after shutdown") + self.add_option("-n", action="store_false", dest="graceful", default=True, + help="do not go through init but go down real fast") def __call__(self, argv): - try: - (options, args) = self.option.parse_args(argv[1:]) - except SystemExit: - return - if len(args) != 1: + self.parse_args(argv) + if len(self.args) != 1: raise cmdBadArgument() try: - self.cli.rpc.call("shutdown", args[0], options.reboot, options.graceful) + self.cli.rpc.call("shutdown", self.args[0], self.options.reboot, + self.options.graceful) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) diff --git a/cccli/command/list.py b/cccli/command/list.py index f70f7178e7dd62294562c117b7c22027f2a6d6d3..5a3d74852f216b20e8e2bdb1b3dae07deb21e559 100644 --- a/cccli/command/list.py +++ b/cccli/command/list.py @@ -15,36 +15,33 @@ class Command_list(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) - self.option.set_usage("%prog [options] [tql]") - self.option.add_option("-t", action="store_true", dest="table", - help="column aligment display") - self.option.add_option("-l", action="store_true", dest="align", - help="line aligment display") - self.option.add_option("-n", "--no-tagdisplay", action="store_false", dest="tagdisplay", default=True, - help="No tag display system") + self.set_usage("%prog [options] [tql]") + self.add_option("-t", action="store_true", dest="table", + help="column aligment display") + self.add_option("-l", action="store_true", dest="align", + help="line aligment display") + self.add_option("-n", "--no-tagdisplay", action="store_false", dest="tagdisplay", default=True, + help="No tag display system") def __call__(self, argv): - try: - (options, args) = self.option.parse_args(argv[1:]) - except SystemExit: - return - if options.tagdisplay: + self.parse_args(argv) + if self.options.tagdisplay: self.td = self.cli.tagdisplay.resolve self.tc = self.cli.tagdisplay.color else: self.td = lambda tagname, tagvalue: unicode(tagvalue) self.tc = lambda tagname: color["reset"] - if len(args) == 0: - args.append("") + if len(self.args) == 0: + self.args.append("") try: - objs = self.cli.rpc.call("list", str.join("", args)) + objs = self.cli.rpc.call("list", str.join("", self.args)) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) if len(objs) == 0: return - if options.align: + if self.options.align: self._list_align(objs) - elif options.table: + elif self.options.table: self._list_table(objs) else: self._trivial_list(objs) diff --git a/cccli/command/right.py b/cccli/command/right.py index 23d5c15475df72f00577f1267b690a40bf556b0a..b849ff626dc7bf078c2cb346f0f7a6722f7abcd4 100644 --- a/cccli/command/right.py +++ b/cccli/command/right.py @@ -8,36 +8,30 @@ CloudControl right releated commands from cccli.exception import * from sjrpc.core.exceptions import * from cccli.printer import Printer, color -from cccli.command.command import Command, OptionCommand +from cccli.command.command import Command, TqlCommand -class Command_rights(OptionCommand): +class Command_rights(TqlCommand): '''List account rights''' def __init__(self, cli, argv0): - OptionCommand.__init__(self, cli, argv0) - self.option.set_usage("%prog [options] [tql]") - self.option.add_option("--raw", action="store_true", dest="raw", - help="Don't append filter on request") + TqlCommand.__init__(self, cli, argv0) + self.set_usage("%prog [options] [tql]") + self.add_option("--raw", action="store_true", dest="raw", + help="Don't append filter on request") def __call__(self, argv): # Parse argline - try: - (options, args) = self.option.parse_args(argv[1:]) - except SystemExit: - return + self.parse_args(argv) # append current login if nothing asked - if len(args) == 0: + if len(self.args) == 0: tql = "a=%s"%self.cli.settings["login"] else: - tql = "".join(args) + tql = "".join(self.args) # update tql if mode - if not options.raw: + if not self.options.raw: tql += "&a" # ask server - try: - al = self.cli.rpc.call("rights", tql) - except RpcError as e: - raise cmdError("RPCError: %s"%str(e)) + al = self.rpccall("rights", tql) # display answer for (a, rl) in al.items(): self.printer.out("%srights of %s%s"%(color["lblue"], a, color["reset"])) @@ -46,46 +40,52 @@ class Command_rights(OptionCommand): self.printer.out("[%s] %s"%(i,tags)) -class Command_addright(Command): +class Command_addright(TqlCommand): '''Add or edit account right''' + def __init__(self, cli, argv0): + TqlCommand.__init__(self, cli, argv0) + self.set_usage('''%prog [options] [index] + + is the name of the rpc command to allow + can be allow or deny''') + def __call__(self, argv): - if len(argv) == 5: - argv.append(None) - elif len(argv) == 6: - argv[5] = int(argv[5]) + # argv check + self.parse_args(argv) + if len(self.args) == 4: + self.args.append(None) + elif len(self.args) == 5: + self.args[5] = int(self.args[5]) else: raise cmdBadArgument() - try: - self.cli.rpc.call("addright", argv[1], argv[2], argv[3], argv[4], argv[5]) - except RpcError as e: - raise cmdError("RPCError: %s"%str(e)) - - def usage(self): - return '''Usage: addright [index] + # rpc call + self.rpccall("addright", + self.args[0], + self.args[1], + self.args[2], + self.args[3], + self.args[4]) - is the name of the rpc command to allow - can be allow or deny''' +class Command_delright(TqlCommand): + '''Delete account right''' + def __init__(self, cli, argv0): + TqlCommand.__init__(self, cli, argv0) + self.set_usage('''%prog [options] -class Command_delright(Command): - '''Delete account right''' + * means all''') def __call__(self, argv): - if len(argv) < 3: + # argv check + self.parse_args(argv) + if len(self.args) != 2: raise cmdBadArgument() - l = argv[2:] + # building list of index + l = self.args[1:] + # if all is detected if "*" in l: - try: - self.cli.rpc.call("delright", argv[1], None) - except RpcError as e: - raise cmdError("RPCError: %s"%str(e)) + self.rpccall("delright", self.args[0], None) else: for index in l: - try: - self.cli.rpc.call("delright", argv[1], int(index)) - except RpcError as e: - raise cmdError("RPCError: %s"%unicode(e)) - - def usage(self): - return "Usage: delright ...\n\nindex * means all" + self.rpccall("delright", self.args[0], index) diff --git a/cccli/command/server.py b/cccli/command/server.py index 94ee3fa940465e66465945c63c73f7a627c7231d..68bcb675d56728f7e1e48519e7b831b7a65abef3 100644 --- a/cccli/command/server.py +++ b/cccli/command/server.py @@ -14,27 +14,23 @@ class Command_server(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) - self.option.set_usage("%prog ") - self.option.add_option("-c", action="store_true", dest="cache", - help="show server cache") - self.option.add_option("-l", action="store_true", dest="commands", - help="list server commands") - self.option.add_option("-v", action="store_true", dest="version", - help="show server version") - + self.set_usage("%prog ") + self.add_option("-c", action="store_true", dest="cache", + help="show server cache") + self.add_option("-l", action="store_true", dest="commands", + help="list server commands") + self.add_option("-v", action="store_true", dest="version", + help="show server version") def __call__(self, argv): - try: - (options, args) = self.option.parse_args(argv[1:]) - except SystemExit: - return - if len(args) > 0: + self.parse_args(argv) + if len(self.args) > 0: self.printer.out(self.usage()) - elif options.cache: + elif self.options.cache: self.show_cache() - elif options.commands: + elif self.options.commands: self.show_commands() - elif options.version: + elif self.options.version: self.show_version() else: self.printer.out(self.usage()) diff --git a/cccli/command/tag.py b/cccli/command/tag.py index fae9c40e58d6fc008fb400fbc457a176ad66a4d8..8558b07a11ec377263a4e3ace872b422f618bf6c 100644 --- a/cccli/command/tag.py +++ b/cccli/command/tag.py @@ -8,7 +8,7 @@ CloudControl tag releated commands from cccli.exception import * from sjrpc.core.exceptions import * from cccli.printer import Printer, color -from cccli.command.command import Command, OptionCommand +from cccli.command.command import OptionCommand, TqlCommand from optparse import OptionParser @@ -17,20 +17,17 @@ class Command_tags(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) - self.option.set_usage("%prog [options] [tql]") - self.option.add_option("--raw", action="store_true", dest="raw", - help="Don't append filter on request") - self.option.add_option("-n", "--no-tagdisplay", action="store_false", dest="tagdisplay", default=True, - help="No tag display system") + self.set_usage("%prog [options] [tql]") + self.add_option("--raw", action="store_true", dest="raw", + help="Don't append filter on request") + self.add_option("-n", "--no-tagdisplay", action="store_false", dest="tagdisplay", default=True, + help="No tag display system") def __call__(self, argv): # Parse argline - try: - (options, args) = self.option.parse_args(argv[1:]) - except SystemExit: - return + self.parse_args(argv) # handle tagdisplay - if options.tagdisplay: + if self.options.tagdisplay: self.td = self.cli.tagdisplay.resolve self.tc = self.cli.tagdisplay.color else: @@ -42,7 +39,7 @@ class Command_tags(OptionCommand): else: tql = "".join(args) # update tql if mode - if not options.raw: + if not self.options.raw: tql += "&a" # ask server try: @@ -60,31 +57,33 @@ class Command_tags(OptionCommand): self.printer.out("%sid:%s%s %s%s"%(color["reset"], color["lblue"], id, tags, color["reset"])) -class Command_addtag(Command): +class Command_addtag(TqlCommand): '''Add/Modify a static tag on an account''' + def __init__(self, cli, argv0): + TqlCommand.__init__(self, cli, argv0) + self.set_usage("%prog [options] ") + def __call__(self, argv): - if len(argv) != 4: + # argv check + self.parse_args(argv) + if len(self.args) != 3: raise cmdBadArgument() - try: - self.cli.rpc.call("addtag", argv[1], argv[2], argv[3]) - except RpcError as e: - raise cmdError("RPCError: %s"%str(e)) - - def usage(self): - return "Usage: addtag " + # rpc call + self.rpccall("addtag", self.args[0], self.args[1], self.args[2]) -class Command_deltag(Command): +class Command_deltag(TqlCommand): '''Delete a static tag from an account''' + def __init__(self, cli, argv0): + TqlCommand.__init__(self, cli, argv0) + self.set_usage("%prog [options] ") + def __call__(self, argv): - if len(argv) != 3: + # argv check + self.parse_args(argv) + if len(self.args) != 2: raise cmdBadArgument() - try: - self.cli.rpc.call("deltag", argv[1], argv[2]) - except RpcError as e: - raise cmdError("RPCError: %s"%str(e)) - - def usage(self): - return "Usage: deltag " + # rpc call + self.rpccall("deltag", self.args[0], self.args[1]) diff --git a/cccli/command/tagdisplay.py b/cccli/command/tagdisplay.py index af5e8725aaf064f47b78c10101fa7f717e96bd55..3d60d890de90c32c9ae14cdbe7ffb32a6f583b73 100644 --- a/cccli/command/tagdisplay.py +++ b/cccli/command/tagdisplay.py @@ -14,38 +14,39 @@ class Command_tagdisplay(OptionCommand): 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") + self.set_usage("%prog [options] [tag] ...") + self.add_option("-c", action="store", dest="setcolor", + help="Set custom color on [tag] ") + self.add_option("-C", action="store_true", dest="delcolor", + help="Remove custom color on [tag]") + self.add_option("-t", action="store", dest="settype", + help="Set custom type on [tag]") + self.add_option("-T", action="store_true", dest="deltype", + help="Remove custom type on [tag]") + self.add_option("-f", action="store", dest="settitlecolor", + help="Set custom title color on [tag]") + self.add_option("-F", action="store_true", dest="deltitlecolor", + help="Remove custom title color on [tag]") + self.add_option("--list-colors", action="store_true", dest="listcolor", + help="List allowed color") + self.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.parse_args(argv) + if self.options.listcolor: self.list("color") - elif options.listtype: + elif self.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: + elif len(self.args) > 0 and self.options.setcolor: + self.set("color", self.args, self.options.setcolor) + elif len(self.args) > 0 and self.options.delcolor: + self.set("color", self.args, None) + elif len(self.args) > 0 and self.options.settype: + self.set("type", self.args, self.options.settype) + elif len(self.args) > 0 and self.options.deltype: + self.set("type", self.args, None) + elif len(self.args) == 0: self.list("my") else: self.printer.out(self.usage()) diff --git a/cccli/command/vm.py b/cccli/command/vm.py index 49b4babda798c8456e0fa6ea53fb69e4df59b2e9..8aa7f097e8a03df4f6fc119d56acc9f8843385d9 100644 --- a/cccli/command/vm.py +++ b/cccli/command/vm.py @@ -13,26 +13,23 @@ class VmCommand(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) - self.option.set_usage("%prog [options] ") - self.option.add_option("--raw", action="store_true", dest="raw", - help="Don't append filter on request") - self.option.add_option("--direct", action="store_true", dest="direct", - help="Directly send tql to server (don't list before)") - self.option.add_option("--tg", action="store_true", dest="noask", - help="Don't ask confirmation (Dangerous)") + self.set_usage("%prog [options] ") + self.add_option("--raw", action="store_true", dest="raw", + help="Don't append filter on request") + self.add_option("--direct", action="store_true", dest="direct", + help="Directly send tql to server (don't list before)") + self.add_option("--tg", action="store_true", dest="noask", + help="Don't ask confirmation (Dangerous)") def _vm_action(self, argv, filters=None): - try: - (options, args) = self.option.parse_args(argv[1:]) - except SystemExit: - return + self.parse_args() if len(args) == 0: raise cmdBadArgument() tql = str.join("", args) # append securty options by command name - if filters is not None and not options.raw: + if filters is not None and not self.options.raw: tql += filters - if options.direct: + if self.options.direct: try: objs = self.cli.rpc.call(argv[0], tql) except RpcError as e: @@ -51,7 +48,7 @@ class VmCommand(OptionCommand): self.printer.out("%sid:%s%s%s"%(color["green"],color["yellow"],obj["id"],color["reset"])) self.printer.out("%sCount: %s%s"%(color["green"],color["reset"], len(objs))) # be sure boby want do that - if not options.noask: + if not self.options.noask: self.printer.out("%sProceed?%s"%(color["lred"], color["reset"])) if self.printer.ask("Answer (yes/NO): ") != "yes": raise cmdWarning("Aborted") diff --git a/cccli/exception.py b/cccli/exception.py index 78048a3d4ae06adc806ce2e00929b3cba787928f..cde414517174b3c1b5b8c43f75b68ffc4ff5b2cc 100644 --- a/cccli/exception.py +++ b/cccli/exception.py @@ -25,6 +25,9 @@ class cmdBadName(cmdException): class cmdBadArgument(cmdException): pass +class cmdExit(cmdException): + pass + class cmdWarning(cmdException): pass diff --git a/cccli/tagdisplay.py b/cccli/tagdisplay.py index 151378ded44a9aa6b102f4b7f4c00e5cbc4f4f68..c87aee498a01dc6776bb443cdbb776ebdbfccf18 100644 --- a/cccli/tagdisplay.py +++ b/cccli/tagdisplay.py @@ -20,7 +20,8 @@ class TagDisplay(object): def __init__(self): self.option = dict() self.tagtype = dict() - self.tagcolor = { "id": "lblue" } + self.tagcolor = dict() + self.tagtitlecolor = dict() self.types = [ x[5:] for x in dir(self) if x.startswith("type_") ] def load(self, filename): @@ -35,13 +36,16 @@ class TagDisplay(object): self.tagtype.update(fparser.items("type")) if fparser.has_section("color"): self.tagcolor.update(fparser.items("color")) + if fparser.has_section("titlecolor"): + self.tagtitlecolor.update(fparser.items("titlecolor")) def save(self, filename): '''save tagdisplay settings on file''' if os.access(filename, os.R_OK or os.W_OK): fparser = ConfigParser.RawConfigParser() fparser.read(filename) - for n,d in (("type", self.tagtype), ("color", self.tagcolor), ("option", self.option)): + for n,d in (("type", self.tagtype), ("color", self.tagcolor), + ("option", self.option), ("titlecolor", self.tagtitlecolor)): fparser.remove_section(n) fparser.add_section(n) for k,v in d.items(): @@ -57,12 +61,29 @@ class TagDisplay(object): tm = max(l) if self.tagcolor[tm] in color: return color[self.tagcolor[tm]] - return color["yellow"] + return self.default_color(tagname) + + def default_color(self, tagname): + '''Return default color''' + return color["yellow"] if tagname != "id" else color["lblue"] + + def titlecolor(self, tagname): + # build list of matching pattern with tagname + l = [ x for x in self.tagtitlecolor if fnmatch.fnmatch(tagname, x) ] + if len(l) > 0: + # select longest match + tm = max(l) + if self.tagtitlecolor[tm] in color: + return color[self.tagtitlecolor[tm]] + return self.default_titlecolor(tagname) + + def default_titlecolor(self, tagname): + '''Return default title color''' + return color["reset"] def resolve(self, tagname, tagvalue): '''Transform a tagvalue respecting custom display settings''' # 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) @@ -124,3 +145,4 @@ class TagDisplay(object): else: return "%dd%dh%dm%ds"%(v/86400, v/3600%24, v/60%60, v%60) return value +