Browse Source

Gn simplejson (#44)

* added simplejson print

* changes to add simplejson to output
master
Gerry Nelson 6 years ago
committed by GitHub
parent
commit
31c05141a4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      EXAMPLES.md
  2. BIN
      __pycache__/sharedfunctions.cpython-36.pyc
  3. 2
      getauditrecords.py
  4. 2
      getconfigurationproperties.py
  5. 2
      getfolderid.py
  6. 9
      getruleid.py
  7. 4
      listcaslibs_example.py
  8. 2
      listfiles.py
  9. 4
      listrules.py
  10. 36
      sharedfunctions.py
  11. 15
      snapshotreports.py

14
EXAMPLES.md

@ -55,6 +55,12 @@ The following examples are Linux.
./getfolderid.py -f /gelcontent ./getfolderid.py -f /gelcontent
``` ```
**getruleid.py**
```bash
getruleid.py -u /SASVisualAnalytics/** -p "authenticatedUsers"
```
**deletefolder.py** **deletefolder.py**
```bash ```bash
@ -239,3 +245,11 @@ importpackages.py -d /tmp/mypackage -q
# read log files from job execution and add archive them to an os directory # read log files from job execution and add archive them to an os directory
./archivefiles.py -n log -d 6 -p /job -fp /tmp ./archivefiles.py -n log -d 6 -p /job -fp /tmp
``` ```
**archivefiles.py**
```bash
#his tool will export all the reports in your viya system to there own
# individual json file in a directory
./snapshotreports.py -c 10 -d ~/snapshot
```

BIN
__pycache__/sharedfunctions.cpython-36.pyc

Binary file not shown.

2
getauditrecords.py

@ -44,7 +44,7 @@ parser.add_argument("-u","--user", help="Filter by Username",default=None)
parser.add_argument("-A","--after", help="Filter entries that are created after the specified timestamp. For example: 2020-01-03 or 2020-01-03T18:15Z",default=None) parser.add_argument("-A","--after", help="Filter entries that are created after the specified timestamp. For example: 2020-01-03 or 2020-01-03T18:15Z",default=None)
parser.add_argument("-B","--before", help="Filter entries that are created before the specified timestamp. For example: 2020-01-03 or 2020-01-03T18:15Z",default=None) parser.add_argument("-B","--before", help="Filter entries that are created before the specified timestamp. For example: 2020-01-03 or 2020-01-03T18:15Z",default=None)
parser.add_argument("-S","--sortby", help="Sort the output ascending by this field",default='timeStamp') parser.add_argument("-S","--sortby", help="Sort the output ascending by this field",default='timeStamp')
parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple'],default='csv') parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple','simplejson'],default='csv')
args = parser.parse_args() args = parser.parse_args()
appname=args.application appname=args.application

2
getconfigurationproperties.py

@ -38,7 +38,7 @@ from sharedfunctions import callrestapi, printresult
parser = argparse.ArgumentParser(description="Return a set of configuration properties") parser = argparse.ArgumentParser(description="Return a set of configuration properties")
parser.add_argument("-c","--configuration", help="Enter the configuration definition.",required='True') parser.add_argument("-c","--configuration", help="Enter the configuration definition.",required='True')
parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple'],default='json') parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple','simplejson'],default='json')
args = parser.parse_args() args = parser.parse_args()
configurationdef=args.configuration configurationdef=args.configuration

2
getfolderid.py

@ -35,7 +35,7 @@ from sharedfunctions import getfolderid, printresult
# setup command-line arguements # setup command-line arguements
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-f","--folderpath", help="Enter the path to the viya folder.",required='True') parser.add_argument("-f","--folderpath", help="Enter the path to the viya folder.",required='True')
parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple'],default='simple') parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple','simplejson'],default='csv')
args = parser.parse_args() args = parser.parse_args()
path_to_folder=args.folderpath path_to_folder=args.folderpath

9
getruleid.py

@ -5,11 +5,14 @@
# December 2017 # December 2017
# #
# getruleid pass in a uri and identity and return the rule id # getruleid pass in a uri and identity and return the rule id
# for example
# getruleid.py -u /SASVisualAnalytics/** -p "authenticatedUsers"
# #
# Change History # Change History
# #
# 27JAN2017 Comments added # 27JAN2017 Comments added
# 18JUN2018 Output JSON # 18JUN2018 Output JSON
# 20Feb2020 make identity a required parameter
# #
# Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# #
@ -34,8 +37,8 @@ from sharedfunctions import callrestapi, printresult
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-u","--objecturi", help="Enter the objecturi.",required='True') parser.add_argument("-u","--objecturi", help="Enter the objecturi.",required='True')
parser.add_argument("-p","--principal", help="Enter the identity name or authenticatedUsers, everyone or guest") parser.add_argument("-p","--principal", help="Enter the identity name or authenticatedUsers, everyone or guest",required='True')
parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple'],default='json') parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple','simplejson'],default='json')
args = parser.parse_args() args = parser.parse_args()
objuri=args.objecturi objuri=args.objecturi
@ -53,7 +56,5 @@ reqtype='get'
result=callrestapi(reqval,reqtype) result=callrestapi(reqval,reqtype)
#print("ruleid= "+result['items'][0]['id'])
# print rest call results # print rest call results
printresult(result,output_style) printresult(result,output_style)

4
listcaslibs_example.py

@ -32,7 +32,7 @@ from sharedfunctions import callrestapi,printresult
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-s","--server", help="The CAS SERVER.",required='True',default="cas-shared-default") parser.add_argument("-s","--server", help="The CAS SERVER.",required='True',default="cas-shared-default")
parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple'],default='json') parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple','simplejson'],default='csv')
args = parser.parse_args() args = parser.parse_args()
casserver=args.server casserver=args.server
output_style=args.output output_style=args.output
@ -41,7 +41,7 @@ output_style=args.output
reqtype='get' reqtype='get'
# set the endpoint to call # set the endpoint to call
reqval='/dataSources/providers/cas/sources/'+casserver+'/children' reqval='/dataSources/providers/cas/sources/'+casserver+'/children?&limit=100000'
#make the rest call using the callrestapi function. You can have one or many calls #make the rest call using the callrestapi function. You can have one or many calls
caslib_result_json=callrestapi(reqval,reqtype) caslib_result_json=callrestapi(reqval,reqtype)

2
listfiles.py

@ -37,7 +37,7 @@ parser.add_argument("-pf","--parentfolder", help="Parent Folder Name.",default=N
parser.add_argument("-d","--days", help="List files older than this number of days",default='-1') parser.add_argument("-d","--days", help="List files older than this number of days",default='-1')
parser.add_argument("-m","--modifiedby", help="Last modified id equals",default=None) parser.add_argument("-m","--modifiedby", help="Last modified id equals",default=None)
parser.add_argument("-s","--sortby", help="Sort the output descending by this field",default='modifiedTimeStamp') parser.add_argument("-s","--sortby", help="Sort the output descending by this field",default='modifiedTimeStamp')
parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple'],default='json') parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple','simplejson'],default='json')
args = parser.parse_args() args = parser.parse_args()
output_style=args.output output_style=args.output

4
listrules.py

@ -4,7 +4,7 @@
# listrules.py # listrules.py
# August 2018 # August 2018
# #
# listrulesforidentity # listrules
# #
# Change History # Change History
# December 2018 - Added custom CSV output code, which writes out consistent columns in a specific order for the result rules JSON # December 2018 - Added custom CSV output code, which writes out consistent columns in a specific order for the result rules JSON
@ -34,7 +34,7 @@ parser = argparse.ArgumentParser(description="List rules for a principal and/or
parser.add_argument("-u","--uri", help="Enter a string that the objecturi contains.",default="none") parser.add_argument("-u","--uri", help="Enter a string that the objecturi contains.",default="none")
parser.add_argument("-p","--principal", help="Enter the identity name or authenticatedUsers, everyone or guest",default='none') parser.add_argument("-p","--principal", help="Enter the identity name or authenticatedUsers, everyone or guest",default='none')
parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple'],default='json') parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple','simplejson'],default='json')
args = parser.parse_args() args = parser.parse_args()
objuri=args.uri objuri=args.uri

36
sharedfunctions.py

@ -475,6 +475,7 @@ def file_accessible(filepath, mode):
# 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 # 22dec2018 add csv columns only relevent for csv output, defaults provided but can be overriden when called
# 20feb2020 add simplejson output style
def printresult(result,output_style,colsforcsv=["id","name","type","description","creationTimeStamp","modifiedTimeStamp"]): def printresult(result,output_style,colsforcsv=["id","name","type","description","creationTimeStamp","modifiedTimeStamp"]):
@ -484,6 +485,8 @@ def printresult(result,output_style,colsforcsv=["id","name","type","description"
if output_style=='simple': if output_style=='simple':
simpleresults(result) simpleresults(result)
elif output_style=='simplejson':
simplejsonresults(result)
elif output_style=='csv': elif output_style=='csv':
csvresults(result,columns=colsforcsv) csvresults(result,columns=colsforcsv)
else: else:
@ -631,3 +634,36 @@ def getidsanduris(resultdata):
resultdict['uris'].append(resultdata['items'][i]['uri']) resultdict['uris'].append(resultdata['items'][i]['uri'])
return resultdict return resultdict
# simplejsonresults
# given a result json structure, remove all the "links" items
# this will return a more readable json output
# change history
# 20feb2020 initial development
def simplejsonresults(resultdata):
if 'items' in resultdata: # list of items returned by rest call
for key in list(resultdata):
if key == 'links': del resultdata[key]
total_items=resultdata['count']
returned_items=len(resultdata['items'])
if total_items == 0: print("Note: No items returned.")
for i in range(0,returned_items):
for key in list(resultdata['items'][i]):
if key=='links':
del resultdata['items'][i][key]
print(json.dumps(resultdata,indent=2))
elif 'id' in resultdata: #one item returned by rest call
del resultdata['links']
print(json.dumps(resultdata,indent=2))

15
snapshotreports.py

@ -4,6 +4,18 @@
# snapshotreports.py # snapshotreports.py
# April 2019 # April 2019
# #
# this tool will export all the reports in your viya system to there own
# individual json file in a directory.
#
# The purpose of the tools is to be able to have a granular backup of reports
# so that you could restore an individual report to a system. Something that
# is not currently supported by Viya backup or promotion
#
# example
#
# save each to their own package all reports that have changed in the last 10 days
# snapshotreports.py -c 10 -d ~/snapshot
#
# Change History # Change History
# #
# #
@ -119,8 +131,7 @@ if areyousure.upper() =='Y':
json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i) json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i)
json_name=json_name.replace("(","_") json_name=json_name.replace("(","_")
json_name=json_name.replace(")","_") json_name=json_name.replace(")","_")
command=clidir+'sas-admin transfer export -u /reports/reports/'+id+' --name "'+package_name+'"' command=clidir+'sas-admin transfer export -u /reports/reports/'+id+' --name "'+package_name+'"'
print(command) print(command)

Loading…
Cancel
Save