1
1
import argparse
2
2
import getpass
3
3
import logging
4
-
5
4
import tableauserverclient as TSC
5
+ from collections import defaultdict
6
6
7
7
8
8
def main ():
@@ -21,8 +21,8 @@ def main():
21
21
parser .add_argument ('--type' , '-t' , required = False , choices = ['site' , 'workbook' , 'project_name' ,
22
22
'project_id' , 'project_path' ],
23
23
help = 'type of content you want to update materialized views settings on' )
24
+ parser .add_argument ('--file-path' , '-fp' , required = False , help = 'path to a list of workbooks' )
24
25
parser .add_argument ('--project-name' , '-pn' , required = False , help = 'name of the project' )
25
- parser .add_argument ('--project-id' , '-pi' , required = False , help = "id of the project" )
26
26
parser .add_argument ('--project-path' , '-pp' , required = False , help = "path of the project" )
27
27
28
28
args = parser .parse_args ()
@@ -52,9 +52,6 @@ def main():
52
52
elif args .type == 'project_name' :
53
53
update_project_by_name (args , enable_materialized_views , password , site_content_url )
54
54
55
- elif args .type == 'project_id' :
56
- update_project_by_id (args , enable_materialized_views , password , site_content_url )
57
-
58
55
elif args .type == 'project_path' :
59
56
update_project_by_path (args , enable_materialized_views , password , site_content_url )
60
57
@@ -67,7 +64,7 @@ def find_project_path(project, all_projects, path):
67
64
if project .parent_id is None :
68
65
return path
69
66
else :
70
- find_project_path (all_projects [project .parent_id ], all_projects , path )
67
+ return find_project_path (all_projects [project .parent_id ], all_projects , path )
71
68
72
69
73
70
def get_project_paths (server , projects ):
@@ -76,7 +73,7 @@ def get_project_paths(server, projects):
76
73
77
74
result = dict ()
78
75
for project in projects :
79
- result [find_project_path (project , all_projects , "" )] = project
76
+ result [find_project_path (project , all_projects , "" )[: - 1 ] ] = project
80
77
return result
81
78
82
79
@@ -99,7 +96,7 @@ def show_materialized_views_status(args, password, site_content_url):
99
96
print "Site name:" , site .name
100
97
print '\n '
101
98
102
- print ( "Materialized views is enabled on workbooks:" )
99
+ print "Materialized views is enabled on workbooks:"
103
100
# Individual workbooks can be enabled only when the sites they belong to are enabled too
104
101
for site in enabled_sites :
105
102
site_auth = TSC .TableauAuth (args .username , password , site .content_url )
@@ -115,30 +112,17 @@ def update_project_by_path(args, enable_materialized_views, password, site_conte
115
112
return
116
113
tableau_auth = TSC .TableauAuth (args .username , password , site_content_url )
117
114
server = TSC .Server (args .server , use_server_version = True )
118
- project_name = args .project_path .split ('/' )
115
+ project_name = args .project_path .split ('/' )[ - 1 ]
119
116
with server .auth .sign_in (tableau_auth ):
120
117
projects = [project for project in TSC .Pager (server .projects ) if project .name == project_name ]
121
118
122
119
if len (projects ) > 1 :
123
- possible_paths = get_project_paths (server . projects )
120
+ possible_paths = get_project_paths (server , projects )
124
121
update_project (possible_paths [args .project_path ], server , enable_materialized_views )
125
122
else :
126
123
update_project (projects [0 ], server , enable_materialized_views )
127
124
128
125
129
- def update_project_by_id (args , enable_materialized_views , password , site_content_url ):
130
- if args .project_id is None :
131
- print "Use --project-id <project id> to specify the id of the project"
132
- return
133
- tableau_auth = TSC .TableauAuth (args .username , password , site_content_url )
134
- server = TSC .Server (args .server , use_server_version = True )
135
- with server .auth .sign_in (tableau_auth ):
136
- for project in TSC .Pager (server .projects ):
137
- if project .id == args .project_id :
138
- update_project (project , server , enable_materialized_views )
139
- break
140
-
141
-
142
126
def update_project_by_name (args , enable_materialized_views , password , site_content_url ):
143
127
if args .project_name is None :
144
128
print "Use --project-name <project name> to specify the name of the project"
@@ -151,7 +135,7 @@ def update_project_by_name(args, enable_materialized_views, password, site_conte
151
135
152
136
if len (projects ) > 1 :
153
137
possible_paths = get_project_paths (server , projects )
154
- print "Project name is not unique, use '--project_path <path>' or '--project-id <project id>' "
138
+ print "Project name is not unique
57AE
, use '--project_path <path>'"
155
139
print "Possible project paths:"
156
140
print_paths (possible_paths )
157
141
print '\n '
@@ -169,21 +153,51 @@ def update_project(project, server, enable_materialized_views):
169
153
print '\n '
170
154
171
155
156
+ def parse_workbook_path (file_path ):
157
+ workbook_paths = open (file_path , 'r' )
158
+ workbook_path_mapping = defaultdict (list )
159
+ for workbook_path in workbook_paths :
160
+ workbook_project = workbook_path .rstrip ().split ('/' )
161
+ workbook_path_mapping [workbook_project [- 1 ]].append ('/' .join (workbook_project [:- 1 ]))
162
+ return workbook_path_mapping
163
+
164
+
172
165
def update_workbook (args , enable_materialized_views , password , site_content_url ):
166
+ if args .file_path is None :
167
+ print "Use '--file-path <file path>' to specify the path of a list of workbooks"
168
+ print "In the file, each line is a path to a workbook, for example:"
169
+ print "project1/project2/workbook_name_1"
170
+ print "project3/workbook_name_2"
171
+ print '\n '
172
+ return
173
+
173
174
tableau_auth = TSC .TableauAuth (args .username , password , site_id = site_content_url )
174
175
server = TSC .Server (args .server , use_server_version = True )
175
- # Now it updates all the workbooks in the site
176
- # To update selected ones please use filter:
177
- # https://github.com/tableau/server-client-python/blob/master/docs/docs/filter-sort.md
178
- # This only updates the workbooks in the site you are signing into
179
176
with server .auth .sign_in (tableau_auth ):
180
- for workbook in TSC .Pager (server .workbooks ):
181
- workbook .materialized_views_enabled = enable_materialized_views
182
-
183
- server .workbooks .update (workbook )
184
- site = server .sites .get_by_content_url (site_content_url )
185
- print "Updated materialized views settings for workbook:" , workbook .name , "from site:" , site .name
186
- print '\n '
177
+ workbook_path_mapping = parse_workbook_path (args .file_path )
178
+ all_projects = {project .id : project for project in TSC .Pager (server .projects )}
179
+ update_workbooks_by_paths (all_projects , enable_materialized_views , server , workbook_path_mapping )
180
+
181
+
182
+ def update_workbooks_by_paths (all_projects , enable_materialized_views , server , workbook_path_mapping ):
183
+ for workbook_name , workbook_paths in workbook_path_mapping .items ():
184
+ req_option = TSC .RequestOptions ()
185
+ req_option .filter .add (TSC .Filter (TSC .RequestOptions .Field .Name ,
186
+ TSC .RequestOptions .Operator .Equals ,
187
+ workbook_name ))
188
+ workbooks = list (TSC .Pager (server .workbooks , req_option ))
189
+ if len (workbooks ) == 1 :
190
+ workbooks [0 ].materialized_views_enabled = enable_materialized_views
191
+ server .workbooks .update (workbooks [0 ])
192
+ print "Updated materialized views settings for workbook:" , workbooks [0 ].name
193
+ else :
194
+ for workbook in workbooks :
195
+ path = find_project_path (all_projects [workbook .project_id ], all_projects , "" )[:- 1 ]
196
+ if path in workbook_paths :
197
+ workbook .materialized_views_enabled = enable_materialized_views
198
+ server .workbooks .update (workbook )
199
+ print "Updated materialized views settings for workbook:" , path + '/' + workbook .name
200
+ print '\n '
187
201
188
202
189
203
def update_site (args , enable_materialized_views , password , site_content_url ):
0 commit comments