Browse Source

Pull request 1 (#119)

* getobjectdetails added and getfolderid updated in sharedfunctions

getobjectdetails added and getfolderid updated in sharedfunctions

* Initial commit

New program (getpathsplus.py) to retrieve path and other details from ObjectURIs in bulk.

* Add CreatedBy to the fields returned

Add CreatedBy to the fields returned by getfolderid.py when 'simple' output style is set.
master
Tom Starr 3 years ago
committed by GitHub
parent
commit
ca6eb6b553
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      getfolderid.py
  2. 146
      getpathsplus.py
  3. 39
      sharedfunctions.py

8
getfolderid.py

@ -11,6 +11,8 @@
# #
# 27JAN2017 Comments added # 27JAN2017 Comments added
# 08FEB2020 Added the option to return full json # 08FEB2020 Added the option to return full json
# 04OCT2022 Added the CreatedBy to results returned.
# 'getfolderid' from sharedfunctions.py has been updated to enable this.
# #
# #
# Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
@ -44,6 +46,9 @@ output_style=args.output
# call the get folderid function and pass it the entered path # call the get folderid function and pass it the entered path
targets=getfolderid(path_to_folder) targets=getfolderid(path_to_folder)
# uncomment the following line for debug
#print("targets = "+str(targets))
# default simple output style prints with original print method # default simple output style prints with original print method
# but can also choose json or csv # but can also choose json or csv
if output_style=='simple': if output_style=='simple':
@ -53,4 +58,5 @@ if output_style=='simple':
print("Id = "+targets[0]) print("Id = "+targets[0])
print("URI = "+targets[1]) print("URI = "+targets[1])
print("Path = "+targets[2]) print("Path = "+targets[2])
else: printresult(targets[3],output_style) print("CreatedBy = "+targets[3])
else: printresult(targets[4],output_style)

146
getpathsplus.py

@ -0,0 +1,146 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# getpathsplus.py
# October 2022 - Initial commit
# Conceptually based on getpathsplus.py, getpathsplus.ph enables mass objectURI --> path
# conversion as well as options to return additional details.
#
# Change History
# DDMMMYYY -
#
# Usage:
# getpathsplus.py [-o] [-m "all", "name", "createdby"] -u objectURI [-d]
#
# Examples:
#
# 1. Return path of folder identified by objectURI
# ./getpathsplus.py -u /folders/folders/060c0ea4-07ee-43ea-aa79-a1696b7f8f52
#
# 2. Return path of report identified by objectURI
# ./getpathsplus.py -u /reports/reports/43de1f98-d7ef-4490-bb46-cc177f995052
#
# 3. Return path of report and folder from their objectURIs
# ./getpathsplus.py -u /folders/folders/060c0ea4-07ee-43ea-aa79-a1696b7f8f52 /reports/reports/43de1f98-d7ef-4490-bb46-cc177f995052
#
# 4. Return path of of folder identified by objectURI including the folder's name
# ./getpathsplus.py -m name -u /folders/folders/060c0ea4-07ee-43ea-aa79-a1696b7f8f52
#
# 5. Return path of of folder identified by objectURI including the createdBy field
# ./getpathsplus.py -m createdby -u /folders/folders/060c0ea4-07ee-43ea-aa79-a1696b7f8f52
#
# 6. Return path of of folder identified by objectURI including the folder's name and the createdBy field AND prefixing the output with the Object's URI
# ./getpathsplus.py -o -m all -u /folders/folders/060c0ea4-07ee-43ea-aa79-a1696b7f8f52
#
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Import Python modules
import argparse
import sys
import os
from sharedfunctions import getpath, getapplicationproperties, getobjectdetails
# get python version
version=int(str(sys.version_info[0]))
# get cli location from properties
propertylist=getapplicationproperties()
clidir=propertylist["sascli.location"]
cliexe=propertylist["sascli.executable"]
clicommand=os.path.join(clidir,cliexe)
debug=False
# Define exception handler so that we only output trace info from errors when in debug mode
def exception_handler(exception_type, exception, traceback, debug_hook=sys.excepthook):
if debug:
debug_hook(exception_type, exception, traceback)
else:
print (exception_type.__name__, exception)
sys.excepthook = exception_handler
parser = argparse.ArgumentParser()
parser.add_argument("-m","--more", action='store', const='all', nargs='?', choices=['all', 'name', 'createdby'], help="Returns additional details for each Object (default: all)")
parser.add_argument("-o", action='store_const', const="yes", help="Prepends ObjectURIs to each row output")
parser.add_argument("-u","--objecturi", action='store', dest='urilist', type=str, nargs='*', default=['objecturi1', 'objecturi2', 'objecturi3'], help="Object URI of folder or other object contained within a folder. Lists should be used WITHOUT quotes and delimited with a space. E.g. getpathsplus.py -u /files/files/1234 /files/files/abcd ", required=True)
parser.add_argument("-d","--debug", action='store_true', help="Debug")
args = parser.parse_args()
urilist=args.urilist
more=args.more
oid=args.o
debug=args.debug
# checks for user input of at least ONE objecturi
if len(urilist) == 0:
print("ERROR: At least one ObjectURI input is required.")
exit()
else:
print("Running...".format(args.urilist))
# checks for use of 'more' flag
if more is None:
# loop to retrieve all path values
i = 0
while i < len(urilist):
path=getpath(urilist[i])
# checks for 'o' flag and includes objecturis in output if it's set
# output from: -o -u <objecturi>
if oid =="yes":
print(urilist[i]+","+path)
i=i+1
# output from: -u <objecturi>
else:
print(path)
i=i+1
else:
# loop to retrieve all objecturi values entered + object name
i = 0
while i < len(urilist):
path=getpath(urilist[i])
name=getobjectdetails(urilist[i])
creator=getobjectdetails(urilist[i])
# checks for 'o' flag and includes objecturis in output if it's set
if oid == "yes":
# output from: -o -m name -u <objecturi>
if more == "name":
print(urilist[i]+","+path+name[0])
i=i+1
# output from: -o -m createdby -u <objecturi>
elif more == "createdby":
print(urilist[i]+","+path+","+creator[1])
i=i+1
# output from: -o -m all|<blank> -u <objecturi>
else:
print(urilist[i]+","+path+name[0]+","+creator[1])
i=i+1
else:
# output from: -m name -u <objecturi>
if more == "name":
print(path+name[0])
i=i+1
# output from: -m createdby -u <objecturi>
elif more == "createdby":
print(path+","+creator[1])
i=i+1
# output from: -m all|<blank> -u <objecturi>
else:
print(path+name[0]+","+creator[1])
i=i+1

39
sharedfunctions.py

@ -39,6 +39,7 @@
# 28Feb2022 Added functionality to callrestapi optionally pass in etags, and to request they be returned, for API endpoints that use them # 28Feb2022 Added functionality to callrestapi optionally pass in etags, and to request they be returned, for API endpoints that use them
# 08Sep2022 Catch Unicode error in get_valid_filename and remove string function if it happens # 08Sep2022 Catch Unicode error in get_valid_filename and remove string function if it happens
# 12OCT2022 Build date filter function # 12OCT2022 Build date filter function
# 14OCT2022 Added getobjectdetails and updated the array returned by getfolderid
# #
# Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# #
@ -181,6 +182,7 @@ def callrestapi(reqval, reqtype, acceptType='application/json', contentType='app
# change history # change history
# 01dec2017 initial development # 01dec2017 initial development
# 08Feb2020 return full json as 4 item in list that is returned # 08Feb2020 return full json as 4 item in list that is returned
# 14OCT2022 added 'createdBy' to return array
def getfolderid(path): def getfolderid(path):
@ -189,19 +191,22 @@ def getfolderid(path):
reqtype='get' reqtype='get'
callrestapi(reqval,reqtype) callrestapi(reqval,reqtype)
if result==None: if result==None:
print("NOTE: Folder'"+path+"' not found.") print("NOTE: Folder'"+path+"' not found.")
targetid=None targetid=None
targetname=None targetname=None
targeturi=None targeturi=None
targetcreatedBy=None
else: else:
targetid=result['id'] targetid=result['id']
targetname=result['name'] targetname=result['name']
targeturi="/folders/folders/"+targetid targeturi="/folders/folders/"+targetid
targetcreatedBy=result['createdBy']
return [targetid,targeturi,targetname,result] return [targetid,targeturi,targetname,targetcreatedBy,result]
# getbaseurl # getbaseurl
# from the default profile return the baseurl of the Viya server # from the default profile return the baseurl of the Viya server
@ -655,6 +660,34 @@ def getpath(objecturi):
return path return path
# getobjectdetails
# Viya objectURI is input, assorted fields are returned
# change history
# 14OCT2022 initial development
def getobjectdetails(objecturi):
# build the request parameters
reqval=objecturi
reqtype='get'
callrestapi(reqval,reqtype)
# verfiyc objectURI input found and return attributes
if result==None:
print("NOTE: Object with ObjectURI:'"+objecturi+"' not found.")
else:
targetname=result['name']
targetcreator=result['createdBy']
targetid=result['id']
targeturi=objecturi
return [targetname,targetcreator,targetid,targeturi,result]
# getidsanduris # getidsanduris
# given a result json structure, return a dictionary with a list of id's and uri's # given a result json structure, return a dictionary with a list of id's and uri's
# change history # change history

Loading…
Cancel
Save