forked from sassoftware/pyviyatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportstudioflowcode.py
executable file
·135 lines (98 loc) · 3.65 KB
/
exportstudioflowcode.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# exportstudioflowcode.py
# Feb 2022
#
# create a code from SAS Studio flows
#
# Change History
#
#
import argparse, os, sys, json
from sharedfunctions import callrestapi, getfolderid
# write code function
def writecode():
if debug: print(data)
reqval="/studioDevelopment/code"
reqtype="post"
flow_json=callrestapi(reqval,reqtype,data=data,stoponerror=0,noprint=1)
# if code is returned then output to file
if flow_json !=None:
code=flow_json['code']
if os.path.exists(basedir):
# remove / from path
filename=flow_name.split('.')[0].replace("/","_")
file=os.path.join(basedir,filename+'.sas')
print("NOTE: SAS Code output to "+file)
with open(file, "w") as outfile:
outfile.write(code)
else:
print("ERROR: output directory does not exist.")
sys.exit()
else:
print("WARNING: code cannot be generated for "+flow_name)
# get python version
version=int(str(sys.version_info[0]))
# get arguements
parser = argparse.ArgumentParser(description="Create code from a SAS Studio flow or from all flows in folder.")
parser.add_argument("-t","--type", help="Enter 'Flow' for a single flow or 'Folder' for all flows in a folder.",required=True,choices=['Flow','Folder'])
parser.add_argument("-n","--name", help="Name of Flow or Folder Path.",required=True)
parser.add_argument("-d","--directory", help="Directory to store generated code",required='True')
parser.add_argument("--includeinitcode", help="Include init code (default False)",action='store_true')
parser.add_argument("--includewrappercode", help="Include wrapper code (default False)",action='store_true')
parser.add_argument("--debug", help="Include wrapper code (default False)",action='store_true')
args = parser.parse_args()
basedir=args.directory
type=args.type
debug=args.debug
# user passed in a single flow
if type=="Flow":
flow_name=args.name
# build the json parameters
data = {}
reference={}
data["reference"]=reference
reference["type"]="content"
reference['path']=flow_name
reference['mediaType']="application/vnd.sas.dataflow"
data['initCode'] = args.includeinitcode
data['wrapperCode']= args.includewrappercode
# run the request and get the code
writecode()
else:
# user passed in a folder, loop and output code for the flows in the folder
folder_path=args.name
print("NOTE: creating code for flows in folder "+folder_path)
targets=getfolderid(folder_path)
# if the folder is found
if targets[0] is not None: uri=targets[1]
else:
print("ERROR: Folder does not exist.")
sys.exit()
reqval=uri+"/members?&limit=100000"
reqtype='get'
folder_members=callrestapi(reqval,reqtype)
items=folder_members["items"]
if len(items)==0:
print("NOTE: folder is empty.")
sys.exit()
count=0
# loop folder members
for itemlist in items:
ctype=itemlist["contentType"]
flow_name=folder_path+"/"+itemlist["name"]
if ctype=="dataFlow":
# write out code from each flow
data = {}
reference={}
data["reference"]=reference
reference["type"]="content"
reference['path']=flow_name
reference['mediaType']="application/vnd.sas.dataflow"
data['initCode'] = args.includeinitcode
data['wrapperCode']= args.includewrappercode
writecode()
else:
count=count+1
if count > 0: print("NOTE: No flows in folder.")