From 2e54ffd0d9e86f464ab70f95cac70fad30fd71bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Thu, 30 Jul 2015 18:23:35 +0200 Subject: [PATCH] Merge tagdisplay and alias into shell module --- cloudcontrol/cli/commands/alias.py | 83 ------------ cloudcontrol/cli/commands/shell.py | 167 +++++++++++++++++++++++- cloudcontrol/cli/commands/tagdisplay.py | 127 ------------------ 3 files changed, 166 insertions(+), 211 deletions(-) delete mode 100644 cloudcontrol/cli/commands/alias.py delete mode 100644 cloudcontrol/cli/commands/tagdisplay.py diff --git a/cloudcontrol/cli/commands/alias.py b/cloudcontrol/cli/commands/alias.py deleted file mode 100644 index 41154ff..0000000 --- a/cloudcontrol/cli/commands/alias.py +++ /dev/null @@ -1,83 +0,0 @@ -#coding=utf8 - -# This file is part of CloudControl. -# -# CloudControl is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# CloudControl is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with CloudControl. If not, see . - -''' -CloudControl alias related command -''' - -from cloudcontrol.cli.exception import * -from cloudcontrol.cli.command import OptionCommand - -import re - -class Command_alias(OptionCommand): - '''Show or create alias''' - - def __init__(self, cli, argv0): - OptionCommand.__init__(self, cli, argv0) - self.set_usage("%prog [options] [name] [value]") - - def __call__(self, argv): - # args parse - self.parse_args(argv) - _value = None - if len(self.args) == 0: - _alias = sorted(self.cli.aliases.keys()) - elif len(self.args) == 1: - _alias = [ self.args[0] ] - elif len(self.args) == 2: - _alias = self.args[0] - _value = self.args[1] - else: - raise cmdBadArgument() - # printing - if _value is None: - for a in _alias: - if a in self.cli.aliases: - self.printer.out("%s \"%s\""%(a, re.sub("\"", "\\\"", self.cli.aliases[a]))) - else: - self.printer.warn("No alias %s"%a) - # editing - else: - self.cli.aliases[_alias] = _value - try: - self.cli.aliases.save(self.cli.settings.get("alias", None)) - except Exception as e: - raise cmdError(e) - - -class Command_unalias(OptionCommand): - '''Remove an alias''' - - def __init__(self, cli, argv0): - OptionCommand.__init__(self, cli, argv0) - self.set_usage("%prog [options] [name]") - - def __call__(self, argv): - # parse args - self.parse_args(argv) - if len(self.args) != 1: - raise cmdBadArgument() - # check alias existance - if self.args[0] not in self.cli.aliases: - raise cmdBadArgument("%s: No such alias"%self.args[0]) - # deleting aliases - del self.cli.aliases[self.args[0]] - try: - self.cli.aliases.save(self.cli.settings.get("alias", None)) - except Exception as e: - raise cmdError(e) diff --git a/cloudcontrol/cli/commands/shell.py b/cloudcontrol/cli/commands/shell.py index c5e9501..4f0fa4d 100644 --- a/cloudcontrol/cli/commands/shell.py +++ b/cloudcontrol/cli/commands/shell.py @@ -21,7 +21,9 @@ CloudControl shells related commands from cloudcontrol.cli.exception import * from cloudcontrol.cli.printer import Printer, color -from cloudcontrol.cli.command import Command +from cloudcontrol.cli.command import Command, OptionCommand + +import re class Command_quit(Command): '''Quit application with respect''' @@ -138,3 +140,166 @@ class Command_source(Command): def usage(self): return "Usage: source " + + +class Command_alias(OptionCommand): + '''Show or create alias''' + + def __init__(self, cli, argv0): + OptionCommand.__init__(self, cli, argv0) + self.set_usage("%prog [options] [name] [value]") + + def __call__(self, argv): + # args parse + self.parse_args(argv) + _value = None + if len(self.args) == 0: + _alias = sorted(self.cli.aliases.keys()) + elif len(self.args) == 1: + _alias = [ self.args[0] ] + elif len(self.args) == 2: + _alias = self.args[0] + _value = self.args[1] + else: + raise cmdBadArgument() + # printing + if _value is None: + for a in _alias: + if a in self.cli.aliases: + self.printer.out("%s \"%s\""%(a, re.sub("\"", "\\\"", self.cli.aliases[a]))) + else: + self.printer.warn("No alias %s"%a) + # editing + else: + self.cli.aliases[_alias] = _value + try: + self.cli.aliases.save(self.cli.settings.get("alias", None)) + except Exception as e: + raise cmdError(e) + + +class Command_unalias(OptionCommand): + '''Remove an alias''' + + def __init__(self, cli, argv0): + OptionCommand.__init__(self, cli, argv0) + self.set_usage("%prog [options] [name]") + + def __call__(self, argv): + # parse args + self.parse_args(argv) + if len(self.args) != 1: + raise cmdBadArgument() + # check alias existance + if self.args[0] not in self.cli.aliases: + raise cmdBadArgument("%s: No such alias"%self.args[0]) + # deleting aliases + del self.cli.aliases[self.args[0]] + try: + self.cli.aliases.save(self.cli.settings.get("alias", None)) + except Exception as e: + raise cmdError(e) + + +class Command_tagdisplay(OptionCommand): + '''Tagdisplay tool''' + + def __init__(self, cli, argv0): + OptionCommand.__init__(self, cli, argv0) + 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): + self.parse_args(argv) + if self.options.listcolor: + self.list("color") + elif self.options.listtype: + self.list("type") + 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.settitlecolor: + self.set("titlecolor", self.args, self.options.settitlecolor) + elif len(self.args) > 0 and self.options.deltitlecolor: + self.set("titlecolor", 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()) + + 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 tag 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 tag tiltecolors:%s"%(color["lblue"], color["reset"])) + for (u,v) in self.cli.tagdisplay.tagtitlecolor.items(): + self.printer.out("%s: %s"%(u,v)) + self.printer.out("%sMy tag 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 == "titlecolor": + db = self.cli.tagdisplay.tagtitlecolor + 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 + try: + self.cli.tagdisplay.save(self.cli.settings.get("tagdisplay", None)) + except Exception as e: + raise cmdError(e) diff --git a/cloudcontrol/cli/commands/tagdisplay.py b/cloudcontrol/cli/commands/tagdisplay.py deleted file mode 100644 index 81d5810..0000000 --- a/cloudcontrol/cli/commands/tagdisplay.py +++ /dev/null @@ -1,127 +0,0 @@ -#coding=utf8 - -# This file is part of CloudControl. -# -# CloudControl is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# CloudControl is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with CloudControl. If not, see . - -''' -CloudControl tagdisplay command -''' -from cloudcontrol.cli.exception import * -from sjrpc.core.exceptions import * -from cloudcontrol.cli.printer import Printer, color -from cloudcontrol.cli.command import OptionCommand - -class Command_tagdisplay(OptionCommand): - '''Tagdisplay tool''' - - def __init__(self, cli, argv0): - OptionCommand.__init__(self, cli, argv0) - 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): - self.parse_args(argv) - if self.options.listcolor: - self.list("color") - elif self.options.listtype: - self.list("type") - 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.settitlecolor: - self.set("titlecolor", self.args, self.options.settitlecolor) - elif len(self.args) > 0 and self.options.deltitlecolor: - self.set("titlecolor", 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()) - - 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 tag 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 tag tiltecolors:%s"%(color["lblue"], color["reset"])) - for (u,v) in self.cli.tagdisplay.tagtitlecolor.items(): - self.printer.out("%s: %s"%(u,v)) - self.printer.out("%sMy tag 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 == "titlecolor": - db = self.cli.tagdisplay.tagtitlecolor - 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 - try: - self.cli.tagdisplay.save(self.cli.settings.get("tagdisplay", None)) - except Exception as e: - raise cmdError(e) -- GitLab