Browse Source

Merge pull request #4 from sassoftware/fixcsvresults

Fixed csv result output to output a common set of columns.
master
David Stern 7 years ago
committed by GitHub
parent
commit
51f2ad7b25
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 104
      sharedfunctions.py

104
sharedfunctions.py

@ -30,6 +30,7 @@
# 23oct2018 Added print csv # 23oct2018 Added print csv
# 28oct2018 Added stop on error to be able to override stopping processing when an error occurs # 28oct2018 Added stop on error to be able to override stopping processing when an error occurs
# 20nov2018 Updated so that multiple profiles can be used # 20nov2018 Updated so that multiple profiles can be used
# 20dec2018 Fixed standard csv output
# #
# Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
@ -54,6 +55,7 @@ import sys
import json import json
import pprint import pprint
import os import os
import collections
pp = pprint.PrettyPrinter(indent=4) pp = pprint.PrettyPrinter(indent=4)
@ -298,6 +300,7 @@ def getinputjson(input_file):
# take the complex json and create a simple print of the results # take the complex json and create a simple print of the results
# change history # change history
# 01dec2017 initial development # 01dec2017 initial development
# 20dec2018 simple output now alphibetical order by key
def simpleresults(resultdata): def simpleresults(resultdata):
@ -317,8 +320,14 @@ def simpleresults(resultdata):
print ("=====Item ",i,"=======") print ("=====Item ",i,"=======")
pairs=resultdata['items'][i] origpairs=resultdata['items'][i]
test=origpairs.get('description')
if test==None: origpairs['description']='None'
pairs=collections.OrderedDict(sorted(origpairs.items()))
for key,val in pairs.items(): for key,val in pairs.items():
if key != 'links': if key != 'links':
@ -343,12 +352,12 @@ def simpleresults(resultdata):
# tableresults # tableresults
# take the complex json and create a simple table of the results # take the complex json and create a simple table of the results
# change history # change history
# 01aug2018 initial development # 01aug2018 initial development
# 19dece2018 print csv in column orderwith only common columns
def csvresults(resultdata): def csvresults(resultdata,columns=[]):
#print(resultdata)
if 'items' in resultdata: if 'items' in resultdata:
total_items=resultdata['count'] total_items=resultdata['count']
@ -357,40 +366,85 @@ def csvresults(resultdata):
if total_items == 0: print("Note: No items returned.") if total_items == 0: print("Note: No items returned.")
for i in range(0,returned_items): for i in range(0,returned_items):
pairs=resultdata['items'][i] origpairs=resultdata['items'][i]
#test=pairs.get('description') # create an ordered dictionary
#if test==None: pairs['description']='None' pairs=collections.OrderedDict()
# loop thru the column list and insert to a new dictionary in that order
# this ensures that colums appear in this order in the csv
for keylabel in columns:
# get the value for the current column
curval=origpairs.get(keylabel)
if curval != None:
pairs[keylabel] = curval
else:
pairs[keylabel] = 'None'
numvals=len(columns)
z=0
# print header row of column names
for key,val in pairs.items(): for key,val in pairs.items():
z=z+1
# seperate with comma except last item
if z==numvals: sep=''
else: sep=','
if i==0: print(key,',',end="") if i==0 and key in columns: print(key,sep,end="")
print("\n",end="") print("\n",end="")
z=0
# print rows
for key,val in pairs.items(): for key,val in pairs.items():
# seperate with comma except last item
z=z+1
if z==numvals: sep=''
else: sep=','
if key != 'links' and key in columns: print('"'+str(val)+'"'+sep, end="")
if key != 'links':
print('"',val,'",',end="")
print("\n",end="") print("\n",end="")
elif 'id' in resultdata: #one item returned by rest call elif 'id' in resultdata: #one item returned by rest call
numvals=len(resultdata.items())
z=0
for key,val in resultdata.items(): for key,val in resultdata.items():
if key != 'links': # seperate with comma except last item
print(key,',',end="") z=z+1
if z==numvals: sep=''
else: sep=','
if key != 'links': print(key,sep,end="")
print("\n",end="") print("\n",end="")
z=0
for key,val in resultdata.items(): for key,val in resultdata.items():
if key != 'links': # seperate with comma except last item
print('"',val,'",',end="") z=z+1
if z==numvals: sep=''
else: sep=','
if key != 'links': print('"'+str(val)+'"'+sep,end="")
print("\n",end="") print("\n",end="")
@ -418,8 +472,10 @@ def file_accessible(filepath, mode):
# prints the results in the style requested # prints the results in the style requested
# change history # change history
# 28oct2018 initial development # 28oct2018 initial development
# 22dec2018 add csv columns only relevent for csv output, defaults provided but can be overriden when called
def printresult(result,output_style,colsforcsv=["id","name","type","description","creationTimeStamp","modifiedTimeStamp"]):
def printresult(result,output_style):
# print rest call results # print rest call results
if type(result) is dict: if type(result) is dict:
@ -427,7 +483,7 @@ def printresult(result,output_style):
if output_style=='simple': if output_style=='simple':
simpleresults(result) simpleresults(result)
elif output_style=='csv': elif output_style=='csv':
csvresults(result) csvresults(result,columns=colsforcsv)
else: else:
print(json.dumps(result,indent=2)) print(json.dumps(result,indent=2))
else: print(result) else: print(result)

Loading…
Cancel
Save