Skip to content
Snippets Groups Projects
list.py 4.04 KiB
Newer Older
Seblu's avatar
Seblu committed
#!/usr/bin/env python
#coding=utf8

'''
CloudControl list command
'''

from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
from cccli.command.command import OptionCommand
Seblu's avatar
Seblu committed

class Command_list(OptionCommand):
Seblu's avatar
Seblu committed
    '''List objects'''

    def __init__(self, cli, argv0):
        OptionCommand.__init__(self, cli, argv0)
        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")
Seblu's avatar
Seblu committed
    def __call__(self, argv):
        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(self.args) == 0:
            self.args.append("")
Seblu's avatar
Seblu committed
        try:
            objs = self.cli.rpc.call("list", str.join("", self.args))
Seblu's avatar
Seblu committed
        except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))
        if len(objs) == 0:
            return
Seblu's avatar
Seblu committed
            self._list_align(objs)
Seblu's avatar
Seblu committed
            self._list_table(objs)
        else:
            self._trivial_list(objs)
Seblu's avatar
Seblu committed

    def _trivial_list(self, objs):
        '''Trivial listing of tag'''
Seblu's avatar
Seblu committed
        for o in objs:
            id = self.td("id", o.pop("id"))
Seblu's avatar
Seblu committed
            tags = " ".join([ "%s%s:%s%s"%(color["reset"],
                                           t,
                                           self.tc(t),
                                           self.td(t, v))
                              for (t,v) in o.items() ])
            self.printer.out("%sid:%s%s %s%s"%(color["reset"], self.tc("id"), id, tags, color["reset"]))
Seblu's avatar
Seblu committed

    def _list_align(self, objs):
        '''Listing line aligned'''
        # get max size by tag
Seblu's avatar
Seblu committed
        tags = dict()
        for o in objs:
            for t,v in o.items():
Seblu's avatar
Seblu committed
                tags[t] = max(len(self.td(t, v)), tags.get(t, len(t)))
        # extract id size
        idsize = tags.pop("id")
        # dislay each object by line
Seblu's avatar
Seblu committed
        for o in objs:
Seblu's avatar
Seblu committed
            line = "%sid:%s%s"%(color["reset"],
                                  self.tc("id"),
                                  self.td("id", o.pop("id")).ljust(idsize + 2))
            # show others tags
            for tagname in sorted(tags.keys()):
Seblu's avatar
Seblu committed
                line += "%s%s:%s%s"%(color["reset"],
                                      tagname,
                                      self.tc(tagname),
Seblu's avatar
Seblu committed
                                      self.td(tagname, o.get(tagname, u"")).ljust(tags[tagname] + 1))
            self.printer.out("%s%s"%(line, color["reset"]))
Seblu's avatar
Seblu committed

    def _list_table(self, objs):
        '''Listing table style'''
        # get max size by tag
Seblu's avatar
Seblu committed
        tags = dict()
        for o in objs:
            for t,v in o.items():
Seblu's avatar
Seblu committed
                tags[t] = max(len(self.td(t, v)), tags.get(t, len(t)))
Seblu's avatar
Seblu committed
        idsize = tags.pop("id")
Seblu's avatar
Seblu committed
        self.printer.out(color["reset"], nl="")
Seblu's avatar
Seblu committed
        self.printer.out("id".ljust(idsize+1), nl=" ")
        # print others titles
Seblu's avatar
Seblu committed
        for t,v in tags.items():
            self.printer.out(t.ljust(v), nl=" ")
        self.printer.out(color["reset"])
        # print obj
        for obj in objs:
            # print id first
            self.printer.out(self.tc("id"), nl="")
            self.printer.out(self.td("id", obj.pop("id")).ljust(idsize+1), nl=" ")
            # print others tags
Seblu's avatar
Seblu committed
            for (t, v) in tags.items():
                self.printer.out(self.tc(t), nl="")
Seblu's avatar
Seblu committed
                self.printer.out(self.td(t, obj.get(t, u"")).ljust(v) ,nl=" ")
            self.printer.out(color["reset"])