8000 Fixed the bug that materialize_workbooks.py cannot handle empty lines… · prnka11/server-client-python@3efafb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3efafb2

Browse files
baixin137shinchris
authored andcommitted
Fixed the bug that materialize_workbooks.py cannot handle empty lines (tableau#401)
* fixed the bug that materialize_workbooks.py cannot handle empty lines in workbook list, and not user get notified when workbook name/path is not valid * check for invalid file names, when can't find workbook name/path, remind user to use new line separated file as workbook list * notify user when file name is invalid
1 parent ac25514 commit 3efafb2

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

samples/materialize_workbooks.py

Lines changed: 32 additions & 9 deletions
< 8000 td data-grid-cell-id="diff-5d5116e4fbb991453f5f9fdfbaad7c2a5f1b86ed4edaad95066930420190038f-230-234-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">234
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import getpass
33
import logging
4+
import os
45
import tableauserverclient as TSC
56
from collections import defaultdict
67

@@ -124,7 +125,7 @@ def show_materialized_views_status(args, password, site_content_url):
124125
print("Workbook: {} from site: {}".format(workbook.name, site.name))
125126

126127

127-
def update_project_by_path(args, materialized_views_mode, password, site_content_url):
128+
def update_project_by_path(args, materialized_views_config, password, site_content_url):
128129
if args.project_path is None:
129130
print("Use --project_path <project path> to specify the path of the project")
130131
return False
@@ -139,7 +140,7 @@ def update_project_by_path(args, materialized_views_mode, password, site_content
139140
return False
140141

141142
possible_paths = get_project_paths(server, projects)
142-
update_project(possible_paths[args.project_path], server, materialized_views_mode)
143+
update_project(possible_paths[args.project_path], server, materialized_views_config)
143144
return True
144145

145146

@@ -171,7 +172,7 @@ def update_project_by_name(args, materialized_views_config, password, site_conte
171172

172173
def update_project(project, server, materialized_views_config):
173174
all_projects = list(TSC.Pager(server.projects))
174-
project_ids = find_project_ids_to_update(all_projects, project, server)
175+
project_ids = find_project_ids_to_update(all_projects, project)
175176
for workbook in TSC.Pager(server.workbooks):
176177
if workbook.project_id in project_ids:
177178
workbook.materialized_views_config = materialized_views_config
@@ -181,15 +182,16 @@ def update_project(project, server, materialized_views_config):
181182
print('\n')
182183

183184

184-
def find_project_ids_to_update(all_projects, project, server):
185+
def find_project_ids_to_update(all_projects, project):
185186
projects_to_update = []
186-
find_projects_to_update(project, server, all_projects, projects_to_update)
187+
find_projects_to_update(project, all_projects, projects_to_update)
187188
return set([project_to_update.id for project_to_update in projects_to_update])
188189

189190

190191
def parse_workbook_path(file_path):
191192
# parse the list of project path of workbooks
192-
workbook_paths = open(file_path, 'r')
193+
workbook_paths = sanitize_workbook_list(file_path, "path")
194+
193195
workbook_path_mapping = defaultdict(list)
194196
for workbook_path in workbook_paths:
195197
workbook_project = workbook_path.rstrip().split('/')
@@ -223,23 +225,32 @@ def update_workbooks_by_paths(all_projects, materialized_views_config, server, w
223225
TSC.RequestOptions.Operator.Equals,
224226
workbook_name))
225227
workbooks = list(TSC.Pager(server.workbooks, req_option))
228+
all_paths = set(workbook_paths[:])
226229
for workbook in workbooks:
227230
path = find_project_path(all_projects[workbook.project_id], all_projects, "")
228231
if path in workbook_paths:
232+
all_paths.remove(path)
229233
workbook.materialized_views_config = materialized_views_config
230
server.workbooks.update(workbook)
231235
print("Updated materialized views settings for workbook: {}".format(path + '/' + workbook.name))
236+
237+
for path in all_paths:
238+
print("Cannot find workbook path: {}, each line should only contain one workbook path"
239+
.format(path + '/' + workbook_name))
232240
print('\n')
233241

234242

235243
def update_workbooks_by_names(name_list, server, materialized_views_config):
236-
workbook_names = open(name_list, 'r')
244+
workbook_names = sanitize_workbook_list(name_list, "name")
237245
for workbook_name in workbook_names:
238246
req_option = TSC.RequestOptions()
239247
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
240248
TSC.RequestOptions.Operator.Equals,
241249
workbook_name.rstrip()))
242250
workbooks = list(TSC.Pager(server.workbooks, req_option))
251+
if len(workbooks) == 0:
252+
print("Cannot find workbook name: {}, each line should only contain one workbook name"
253+
.format(workbook_name))
243254
for workbook in workbooks:
244255
workbook.materialized_views_config = materialized_views_config
245256
server.workbooks.update(workbook)
@@ -304,15 +315,27 @@ def assert_project_valid(project_name, projects):
304315
return True
305316

306317

307-
def find_projects_to_update(project, server, all_projects, projects_to_update):
318+
def find_projects_to_update(project, all_projects, projects_to_update):
308319
# Use recursion to find all the sub-projects and enable/disable the workbooks in them
309320
projects_to_update.append(project)
310321
children_projects = [child for child in all_projects if child.parent_id == project.id]
311322
if len(children_projects) == 0:
312323
return
313324

314325
for child in children_projects:
315-
find_projects_to_update(child, server, all_projects, projects_to_update)
326+
find_projects_to_update(child, all_projects, projects_to_update)
327+
328+
329+
def sanitize_workbook_list(file_name, file_type):
330+
if not os.path.isfile(file_name):
331+
print("Invalid file name '{}'".format(file_name))
332+
return []
333+
file_list = open(file_name, "r")
334+
335+
if file_type == "name":
336+
return [workbook.rstrip() for workbook in file_list if not workbook.isspace()]
337+
if file_type == "path":
338+
return [workbook.rstrip() for workbook in file_list if not workbook.isspace()]
316339

317340

318341
if __name__ == "__main__":

0 commit comments

Comments
 (0)
0