Skip to content
Snippets Groups Projects
list.py 2.58 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")
        self.remove_option("--quiet")
        self.remove_option("--direct")
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), _status=False, _direct=True)
Seblu's avatar
Seblu committed
        if len(objs) == 0:
            return
            self.list_align(objs)
            self.list_table(objs)
Seblu's avatar
Seblu committed
        else:
            self.print_objects(objs)
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:
Seblu's avatar
Seblu committed
                tags[t] = max(len(self.tdr(t, v)), tags.get(t, len(t)))
        # dislay each object by line
Seblu's avatar
Seblu committed
        for o in objs:
            line = str()
            for (tagname,tagvalue) in o:
Seblu's avatar
Seblu committed
                line += "%s%s:%s%s"%(self.tdtc(tagname),
                                     tagname,
                                     self.tdc(tagname),
                                     self.tdr(tagname, tagvalue).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:
Seblu's avatar
Seblu committed
                tags[t] = max(len(self.tdr(t, v)), tags.get(t, len(t)))
        # print title
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:
            obj = dict(obj)
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"])