Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added csv output format #1766

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SoftLayer/CLI/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}

PROG_NAME = "slcli (SoftLayer Command-line)"
VALID_FORMATS = ['table', 'raw', 'json', 'jsonraw']
VALID_FORMATS = ['table', 'raw', 'json', 'jsonraw', 'csv']
DEFAULT_FORMAT = 'raw'

if sys.stdout.isatty():
Expand Down
30 changes: 30 additions & 0 deletions SoftLayer/CLI/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"""
# pylint: disable=E0202, consider-merging-isinstance, arguments-differ, keyword-arg-before-vararg
import collections
import csv
import json
import os
import tempfile

import click
from rich import box
Expand All @@ -29,6 +31,8 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
return json.dumps(data, indent=4, cls=CLIJSONEncoder)
elif fmt == 'jsonraw':
return json.dumps(data, cls=CLIJSONEncoder)
if fmt == 'csv':
return csv_output_format(data)

if isinstance(data, str) or isinstance(data, rTable):
return data
Expand Down Expand Up @@ -440,3 +444,29 @@ def _format_list_objects(result):
table.add_row(values)

return table


def csv_output_format(data, delimiter=','):
"""Formating a table to csv format and show it."""
data = clean_null_table_rows(data)
with tempfile.TemporaryDirectory() as temp_file:
f_name = os.path.join(temp_file, 'temp_csv_file')
with open(f_name, 'w', encoding='UTF8') as file:
writer = csv.writer(file, delimiter=delimiter)
edsonarios marked this conversation as resolved.
Show resolved Hide resolved
writer.writerow(data.columns)
writer.writerows(data.rows)
with open(f_name, 'r', encoding='UTF8') as file:
csv_file = csv.reader(file, delimiter='\t')
for row in csv_file:
if len(row) != 0:
print(row[0])
return ''


def clean_null_table_rows(data):
"""Delete Null fields by '-', in a table"""
for index_i, row in enumerate(data.rows):
for index_j, value in enumerate(row):
if str(value) == 'NULL':
data.rows[index_i][index_j] = '-'
return data