diff --git a/cli.py b/cli.py index b65f60f3..ccc30c8b 100644 --- a/cli.py +++ b/cli.py @@ -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) diff --git a/solar/solar/core/resource.py b/solar/solar/core/resource.py index 59941f07..a4602b61 100644 --- a/solar/solar/core/resource.py +++ b/solar/solar/core/resource.py @@ -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):