diff --git a/getfolderid.py b/getfolderid.py index af6789a..eebc3df 100755 --- a/getfolderid.py +++ b/getfolderid.py @@ -11,6 +11,8 @@ # # 27JAN2017 Comments added # 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. @@ -44,6 +46,9 @@ output_style=args.output # call the get folderid function and pass it the entered path targets=getfolderid(path_to_folder) +# uncomment the following line for debug +#print("targets = "+str(targets)) + # default simple output style prints with original print method # but can also choose json or csv if output_style=='simple': @@ -53,4 +58,5 @@ if output_style=='simple': print("Id = "+targets[0]) print("URI = "+targets[1]) print("Path = "+targets[2]) -else: printresult(targets[3],output_style) \ No newline at end of file + print("CreatedBy = "+targets[3]) +else: printresult(targets[4],output_style) diff --git a/getpathsplus.py b/getpathsplus.py new file mode 100644 index 0000000..8f55ef6 --- /dev/null +++ b/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 + if oid =="yes": + print(urilist[i]+","+path) + i=i+1 + # output from: -u + 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 + if more == "name": + print(urilist[i]+","+path+name[0]) + i=i+1 + # output from: -o -m createdby -u + elif more == "createdby": + print(urilist[i]+","+path+","+creator[1]) + i=i+1 + # output from: -o -m all| -u + else: + print(urilist[i]+","+path+name[0]+","+creator[1]) + i=i+1 + else: + # output from: -m name -u + if more == "name": + print(path+name[0]) + i=i+1 + # output from: -m createdby -u + elif more == "createdby": + print(path+","+creator[1]) + i=i+1 + # output from: -m all| -u + else: + print(path+name[0]+","+creator[1]) + i=i+1 diff --git a/sharedfunctions.py b/sharedfunctions.py index a78f52e..93f4c5e 100755 --- a/sharedfunctions.py +++ b/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 # 08Sep2022 Catch Unicode error in get_valid_filename and remove string function if it happens # 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. # @@ -181,6 +182,7 @@ def callrestapi(reqval, reqtype, acceptType='application/json', contentType='app # change history # 01dec2017 initial development # 08Feb2020 return full json as 4 item in list that is returned +# 14OCT2022 added 'createdBy' to return array def getfolderid(path): @@ -189,19 +191,22 @@ def getfolderid(path): reqtype='get' callrestapi(reqval,reqtype) - + if result==None: print("NOTE: Folder'"+path+"' not found.") targetid=None targetname=None targeturi=None + targetcreatedBy=None else: targetid=result['id'] targetname=result['name'] targeturi="/folders/folders/"+targetid + targetcreatedBy=result['createdBy'] - return [targetid,targeturi,targetname,result] - + return [targetid,targeturi,targetname,targetcreatedBy,result] + + # getbaseurl # from the default profile return the baseurl of the Viya server @@ -655,6 +660,34 @@ def getpath(objecturi): 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 # given a result json structure, return a dictionary with a list of id's and uri's # change history