Added PrettyPrint for action: Info

Change-Id: Ia2fd5d2a9ec2ab93fe710384704f009e29339596
This commit is contained in:
Saad Zaher 2017-02-27 10:52:17 +00:00
parent 1a58dd23f5
commit baa7942549
4 changed files with 32 additions and 13 deletions

View File

@ -87,7 +87,19 @@ class InfoJob(Job):
pass
def execute(self):
self.storage.info()
info = self.storage.info()
if not info:
return
fields = ["Container", "Size", "Object Count"]
data = []
for container in info:
values = [
container.get('container_name'),
container.get('size'),
container.get('objects_count')
]
data.append(values)
return [fields, data]
class BackupJob(Job):

View File

@ -138,7 +138,7 @@ def run_job(conf, storage):
else:
with open(conf.metadata_out, 'w') as outfile:
outfile.write(json.dumps(response))
elif response:
elif response and isinstance(response, dict):
pp = prettytable.PrettyTable(["Property", "Value"])
for k, v in response.items():
k = k.replace("_", " ")
@ -146,6 +146,12 @@ def run_job(conf, storage):
sys.stdout.writelines(pp.get_string())
sys.stdout.write('\n')
sys.stdout.flush()
elif response and isinstance(response, list):
pp = prettytable.PrettyTable()
pp.field_names = response[0]
for i in response[1]:
pp.add_row(i)
print (pp)
else:
return

View File

@ -15,7 +15,6 @@ limitations under the License.
"""
import json
import os
import time
@ -134,19 +133,21 @@ class SwiftStorage(physical.PhysicalStorage):
self.swift().put_container(self.storage_path)
def info(self):
ordered_container = {}
containers = self.swift().get_account()[1]
ordered_containers = []
for container in containers:
print(container)
ordered_container = {}
ordered_container['container_name'] = container['name']
size = '{0}'.format((int(container['bytes']) / 1024) / 1024)
if size == '0':
size = '1'
ordered_container['size'] = '{0}MB'.format(size)
size = (int(container['bytes']) / 1024) / 1024
if size == 0:
size = 1
ordered_container['size'] = '{0} MB'.format(size)
if size >= 1024:
size /= 1024
ordered_container['size'] = '{0} GB'.format(size)
ordered_container['objects_count'] = container['count']
print(json.dumps(
ordered_container, indent=4,
separators=(',', ': '), sort_keys=True))
ordered_containers.append(ordered_container)
return ordered_containers
def get_file(self, from_path, to_path):
split = from_path.split('/', 1)

View File

@ -30,7 +30,7 @@ class TestJob(commons.FreezerBaseTestCase):
def test_execute(self):
opt = commons.BackupOpt1()
job = jobs.InfoJob(opt, opt.storage)
assert job.execute() is None
assert job.execute() is not None
class TestInfoJob(TestJob):