Commit 2e54ffd0 authored by Sébastien Luttringer's avatar Sébastien Luttringer
Browse files

Merge tagdisplay and alias into shell module

parent c09af9a2
Loading
Loading
Loading
Loading
+0 −83
Original line number Original line Diff line number Diff line
#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 <http://www.gnu.org/licenses/>.

'''
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)
+166 −1
Original line number Original line Diff line number Diff line
@@ -21,7 +21,9 @@ CloudControl shells related commands


from cloudcontrol.cli.exception import *
from cloudcontrol.cli.exception import *
from cloudcontrol.cli.printer import Printer, color
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):
class Command_quit(Command):
    '''Quit application with respect'''
    '''Quit application with respect'''
@@ -138,3 +140,166 @@ class Command_source(Command):


    def usage(self):
    def usage(self):
        return "Usage: source <file>"
        return "Usage: source <file>"


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)
+0 −127
Original line number Original line Diff line number Diff line
#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 <http://www.gnu.org/licenses/>.

'''
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)