Skip to content
Snippets Groups Projects
list.py 3.61 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
Seblu's avatar
Seblu committed
from cccli.command.command import TqlCommand
Seblu's avatar
Seblu committed

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

    def __init__(self, cli, argv0):
Seblu's avatar
Seblu committed
        TqlCommand.__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")
Seblu's avatar
Seblu committed
        self.remove_option("-s")
Seblu's avatar
Seblu committed
    def __call__(self, argv):
        self.parse_args(argv)
        if len(self.args) == 0:
            self.args.append("")
        objs = self.rpccall("list", str.join("", self.args))
Seblu's avatar
Seblu committed
        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:
Seblu's avatar
Seblu committed
            id = self.tdr("id", o.pop("id"))
            tags = " ".join([ "%s%s:%s%s"%(self.tdtc(t),
Seblu's avatar
Seblu committed
                                           self.tdc(t),
                                           self.tdr(t, v))
                              for (t,v) in o.items() ])
Seblu's avatar
Seblu committed
            self.printer.out("%sid=%s%s %s%s"%(self.tdtc("id"), self.tdc("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.tdr(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"%(self.tdtc("id"),
                                self.tdc("id"),
                                self.tdr("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"%(self.tdtc(tagname),
Seblu's avatar
Seblu committed
                                      self.tdc(tagname),
                                      self.tdr(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.tdr(t, v)), tags.get(t, len(t)))
Seblu's avatar
Seblu committed
        idsize = tags.pop("id")
Seblu's avatar
Seblu committed
        self.printer.out(self.tdtc("id"), 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():
Seblu's avatar
Seblu committed
            self.printer.out(self.tdtc(t), nl="")
Seblu's avatar
Seblu committed
            self.printer.out(t.ljust(v), nl=" ")
        self.printer.out(color["reset"])
        # print obj
        for obj in objs:
Seblu's avatar
Seblu committed
            self.printer.out(self.tdc("id"), nl="")
            self.printer.out(self.tdr("id", obj.pop("id")).ljust(idsize+1), nl=" ")
Seblu's avatar
Seblu committed
            for (t, v) in tags.items():
Seblu's avatar
Seblu committed
                self.printer.out(self.tdc(t), nl="")
                self.printer.out(self.tdr(t, obj.get(t, u"")).ljust(v) ,nl=" ")
            self.printer.out(color["reset"])