-
Notifications
You must be signed in to change notification settings - Fork 31
/
listmemberswithpath.py
executable file
·102 lines (90 loc) · 3.57 KB
/
listmemberswithpath.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# listmemberswithpath.py
# January 2019
#
# Usage:
# listmemberswithpath.py -u objectURI [-r] [-d]
#
# Examples:
#
# 1. Return list of members of a folder identified by objectURI
# ./listmemberswithpath.py -u /folders/folders/060c0ea4-07ee-43ea-aa79-a1696b7f8f52
#
# 2. Return list of all members of a folder identified by objectURI, recursively searching subfolders
# ./listmemberswithpath.py -u /folders/folders/060c0ea4-07ee-43ea-aa79-a1696b7f8f52 -r
#
# Change History
#
# 09MAR2022 Python3 compatibility fix
#
#
# 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
from __future__ import unicode_literals
import argparse
import sys
from sharedfunctions import callrestapi,getpath
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("-u","--objecturi", help="Object URI of folder or other object that can be contained within a folder.", required=True)
parser.add_argument("-r","--recursive", action='store_true', help="Debug")
parser.add_argument("-d","--debug", action='store_true', help="Debug")
args = parser.parse_args()
objecturi=args.objecturi
recursive=args.recursive
debug=args.debug
#We expect a URI, but if the objectURI does not begin with a /, assume it is a folder id. This may be nonsense, but it's much more likely to just fail than return data for the wrong object.
if not objecturi.startswith("/"):
objecturi='/folders/folders/'+objecturi
#Exit if the objecURI is not a folder
if not objecturi.startswith("/folders/folders/"):
raise Exception('ObjectURI must be a folder, and should begin with /folders/folders/.')
#First, use the /folders/{folderId}/members endpoint to ask for a list of objects which are in the folder passed in by objecturi
#See Folders API documentation in swagger at http://swagger.na.sas.com/apis/folders/v1/apidoc.html#op:getAncestors
endpoint=objecturi+'/members'
if recursive:
endpoint=endpoint+'?recursive=true&limit=10000'
else:
endpoint=endpoint+'?limit=10000'
method='get'
#make the rest call
members_result_json=callrestapi(endpoint,method)
if debug:
print(members_result_json)
#print('members_result_json is a '+type(members_result_json).__name__+' object') #members_result_json is a dict object
members = members_result_json['items']
for member in members:
outstr=''
path=getpath(member['uri'])
outstr=outstr+path+','+member['id']+','+member['name']+','+member['type']
if 'description' in member:
outstr=outstr+','+member['description']
else:
outstr=outstr+','
outstr=outstr+','+member['uri']
try:
print(outstr)
except UnicodeEncodeError:
print(outstr.encode('ascii','replace'))