cli.py: add option to print out resource as JSON

This commit is contained in:
Przemyslaw Kaminski 2015-05-28 13:06:04 +02:00
parent 07e5d9845a
commit bd03d4c6ad
2 changed files with 21 additions and 11 deletions

15
cli.py
View File

@ -66,7 +66,15 @@ def init_cli_resource():
@click.argument('path')
@click.option('--all/--one', default=False)
@click.option('--tag', default=None)
def show(tag, all, path):
@click.option('--use-json/--no-use-json', default=False)
def show(use_json, tag, all, path):
import json
import six
printer = lambda r: six.print_(r)
if use_json:
printer = lambda r: six.print_(json.dumps(r.to_dict()))
if all or tag:
for name, resource in xr.load_all(path).items():
show = True
@ -75,10 +83,9 @@ def init_cli_resource():
show = False
if show:
print resource
print
printer(resource)
else:
print xr.load(path)
printer(xr.load(path))
resource.add_command(show)

View File

@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
import copy
import json
import os
import shutil
from copy import deepcopy
@ -45,11 +43,16 @@ class Resource(object):
self.tags = tags or []
def __repr__(self):
return ("Resource(name='{0}', metadata={1}, args={2}, "
"tags={3})").format(self.name,
json.dumps(self.metadata),
json.dumps(self.args_show()),
self.tags)
return ("Resource(name='{name}', metadata={metadata}, args={args}, "
"tags={tags})").format(**self.to_dict)
def to_dict(self):
return {
'name': self.name,
'metadata': self.metadata,
'args': self.args_show(),
'tags': self.tags,
}
def args_show(self):
def formatter(v):