Skip to content

Commit 35692dd

Browse files
authored
Merge pull request #162 from j-bennet/j-bennet/yaml-formatter
[WIP] Use yaml to format hierarchical info.
2 parents 82e52ff + 9bf8baf commit 35692dd

File tree

6 files changed

+30
-55
lines changed

6 files changed

+30
-55
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ before_install:
1919
- docker --version
2020

2121
install:
22-
- pip install pip==9.0
22+
- pip install pip==9.0.1
2323
- pip install -r requirements-dev.txt
2424
- pip install codecov
2525
- pip install -e .

changelog.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Upcoming
2+
========
3+
4+
* Use ``ruamel.yaml`` to format hierarchical info.
5+
16
0.10
27
====
38

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
'py-pretty>=0.1',
2424
'configobj>=5.0.6',
2525
'pexpect>=3.3',
26-
'fuzzyfinder>=1.0.0'
26+
'fuzzyfinder>=1.0.0',
27+
'ruamel.yaml>=0.15.72',
2728
],
2829
extras_require={
2930
'testing': [

tests/test_formatter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_dict_data_formatting():
128128
}
129129
}
130130

131-
lines = format_struct(data, spaces=2)
131+
lines = format_struct(data, indent=2)
132132

133133
print('\n')
134134
for line in lines:
@@ -183,7 +183,7 @@ def test_info_data_formatting():
183183
'SystemTime': '2015-04-29T04:58:07.548655766Z'
184184
}
185185

186-
lines = format_struct(data, spaces=2)
186+
lines = format_struct(data, indent=2)
187187

188188
print('\n')
189189
for line in lines:

wharfee/client.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ def version(self, *_):
258258

259259
try:
260260
verdict = self.instance.version()
261-
result = [(k, verdict[k]) for k in sorted(verdict.keys())]
262-
return result
261+
return verdict
263262
except ConnectionError as ex:
264263
raise DockerPermissionException(ex)
265264

@@ -268,10 +267,8 @@ def info(self, *_):
268267
Return the system info. Equivalent of docker info.
269268
:return: list of tuples
270269
"""
271-
272-
rdict = self.instance.info()
273-
result = [(k, rdict[k]) for k in sorted(rdict.keys())]
274-
return result
270+
info_dict = self.instance.info()
271+
return info_dict
275272

276273
def inspect(self, *args, **_):
277274
"""

wharfee/formatter.py

+17-45
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
from pygments import highlight
1313
from pygments.lexers.data import JsonLexer
1414
from pygments.formatters.terminal import TerminalFormatter
15+
from ruamel.yaml import YAML
16+
17+
try:
18+
from StringIO import StringIO
19+
except ImportError:
20+
from io import StringIO
1521

1622
# Python 3 has no 'basestring' or 'long' type we're checking for.
1723
try:
@@ -181,13 +187,12 @@ def format_data(command, data):
181187
if command and command in DATA_FILTERS:
182188
data = DATA_FILTERS[command](data)
183189

190+
if isinstance(data, dict):
191+
return format_struct(data)
184192
if isinstance(data, list) and len(data) > 0:
185193
if isinstance(data[0], tuple):
186-
if is_plain_lists(data):
187-
text = tabulate(data)
188-
return text.split('\n')
189-
else:
190-
return format_struct(data)
194+
text = tabulate(data)
195+
return text.split('\n')
191196
elif isinstance(data[0], dict):
192197
if data[0].keys() == ['Id']:
193198
# Sometimes our 'quiet' output is a list of dicts but
@@ -211,46 +216,13 @@ def format_data(command, data):
211216
return data
212217

213218

214-
def format_struct(data, spaces=4, indent=0, lines=None):
215-
216-
if lines is None:
217-
lines = []
218-
219-
if isinstance(data, dict):
220-
data = [(k, data[k]) for k in sorted(data.keys())]
221-
222-
def item_to_line(current_item, current_line, is_last_item, current_list):
223-
""" Helper to add item to end of line """
224-
if len(current_line) == 0:
225-
current_indent = ' ' * (indent * spaces)
226-
current_line = current_indent
227-
228-
current_line += '{0}'.format(current_item)
229-
230-
if is_last_item:
231-
current_list.append(current_line)
232-
current_line = ''
233-
else:
234-
current_line += ': '
235-
return current_line, current_list
236-
237-
for row in data:
238-
line = ''
239-
l = len(row)
240-
for i in range(l):
241-
if isinstance(row[i], dict):
242-
lines.append(line)
243-
lines = format_struct(row[i], spaces, indent + 1, lines)
244-
elif isinstance(row[i], list):
245-
if is_plain_list(row[i]):
246-
item = flatten_list(row[i])
247-
line, lines = item_to_line(item, line, i == (l - 1), lines)
248-
else:
249-
lines.append(line)
250-
lines = format_struct(row[i], spaces, indent + 1, lines)
251-
else:
252-
line, lines = item_to_line(row[i], line, i == (l - 1), lines)
253-
219+
def format_struct(data, indent=4):
220+
output = StringIO()
221+
yaml = YAML()
222+
yaml.default_flow_style = False
223+
yaml.indent = indent
224+
yaml.dump(data, stream=output)
225+
lines = output.getvalue().split('\n')
254226
return lines
255227

256228

0 commit comments

Comments
 (0)