Newer
Older
#!/usr/bin/env python
#coding=utf8
'''
CloudControl list command
'''
from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
def __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.parse_args(argv)
if len(self.args) == 0:
self.args.append("")
objs = self.rpccall("list", str.join("", self.args))
if self.options.align:
elif self.options.table:
self._trivial_list(objs)
def _trivial_list(self, objs):
'''Trivial listing of tag'''
id = self.tdr("id", o.pop("id"))
tags = " ".join([ "%s%s:%s%s"%(self.tdtc(t),
for (t,v) in o.items() ])
self.printer.out("%sid=%s%s %s%s"%(self.tdtc("id"), self.tdc("id"), id, tags, color["reset"]))
'''Listing line aligned'''
# get max size by tag
tags = dict()
for o in objs:
for t,v in o.items():
# extract id size
idsize = tags.pop("id")
# dislay each object by line
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()):
self.tdc(tagname),
self.tdr(tagname, o.get(tagname, u"")).ljust(tags[tagname] + 1))
self.printer.out("%s%s"%(line, color["reset"]))
'''Listing table style'''
# get max size by tag
tags = dict()
for o in objs:
for t,v in o.items():
# print others titles
self.printer.out(t.ljust(v), nl=" ")
self.printer.out(color["reset"])
# print obj
for obj in objs:
self.printer.out(self.tdc("id"), nl="")
self.printer.out(self.tdr("id", obj.pop("id")).ljust(idsize+1), nl=" ")
# print others tags
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"])