8000 #6/7/12 samples: error checking and logging capability (#9) · Kovner/server-client-python@cb179df · GitHub
[go: up one dir, main page]

Skip to content

Commit cb179df

Browse files
authored
tableau#6/7/12 samples: error checking and logging capability (tableau#9)
* Add automated test runs via travis (tableau#8) * samples raise error if project or workbook not found * corrected error message and added check to see if site exists before using * added logging option for all samples * updating based on code review comments
1 parent 63a4e1a commit cb179df

File tree

6 files changed

+67
-24
lines changed

6 files changed

+67
-24
lines changed

samples/explore_datasource.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
parser.add_argument('server', help='server address')
2020
parser.add_argument('username', help='username to sign into server')
2121
parser.add_argument('password', help='password to sign into server')
22-
parser.add_argument('-info', action='store_true', help='set this flag to show logging information on INFO level')
23-
parser.add_argument('-debug', action='store_true', help='set this flag to show logging information on DEBUG level')
2422
parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to datasource to publish')
2523
parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded datasource')
24+
parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error',
25+
help='desired logging level (set to error by default)')
2626
args = parser.parse_args()
2727

28-
if args.debug:
29-
logging.basicConfig(level=logging.DEBUG)
30-
elif args.info:
31-
logging.basicConfig(level=logging.INFO)
28+
# Set logging level based on user input, or error by default
29+
logging_level = getattr(logging, args.logging_level.upper())
30+
logging.basicConfig(level=logging_level)
3231

3332
##### SIGN IN #####
3433
tableau_auth = TSA.TableauAuth(args.username, args.password)

samples/explore_workbook.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@
1919
parser.add_argument('server', help='server address')
2020
parser.add_argument('username', help='username to sign into server')
2121
parser.add_argument('password', help='password to sign into server')
22-
parser.add_argument('-info', action='store_true', help='set this flag to show logging information on INFO level')
23-
parser.add_argument('-debug', action='store_true', help='set this flag to show logging information on DEBUG level')
2422
parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to workbook to publish')
2523
parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded workbook')
2624
parser.add_argument('-preview-image', '-i', metavar='FILEPATH', help='path to save populated preview image')
25+
parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], defualt='error',
26+
help='desired logging level (set to error by default)')
2727
args = parser.parse_args()
2828

29-
if args.debug:
30-
logging.basicConfig(level=logging.DEBUG)
31-
elif args.info:
32-
logging.basicConfig(level=logging.INFO)
29+
# Set logging level based on user input, or error by default
30+
logging_level = getattr(logging, args.logging_level.upper())
31+
logging.basicConfig(level=logging_level)
3332

3433
##### SIGN IN #####
3534
tableau_auth = TSA.TableauAuth(args.username, args.password)

samples/move_workbook_projects.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99

1010
import tableauserverapi as TSA
1111
import argparse
12-
13-
# import logging
14-
# logging.basicConfig(level=logging.DEBUG)
12+
import logging
1513

1614
parser = argparse.ArgumentParser(description='Move one workbook from the default project to another.')
1715
parser.add_argument('server', help='server address')
1816
parser.add_argument('username', help='username to sign into server')
1917
parser.add_argument('password', help='password to sign into server')
2018
parser.add_argument('workbook_name', help='name of workbook to move')
2119
parser.add_argument('destination_project', help='name of project to move workbook into')
20+
parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error',
21+
help='desired logging level (set to error by default)')
2222
args = parser.parse_args()
2323

24+
# Set logging level based on user input, or error by default
25+
logging_level = getattr(logging, args.logging_level.upper())
26+
logging.basicConfig(level=logging_level)
27+
2428
# Step 1: Sign in to server
2529
tableau_auth = TSA.TableauAuth(args.username, args.password)
2630
server = TSA.Server(args.server)
@@ -35,11 +39,16 @@
3539
pagination_info, all_projects = server.projects.get()
3640
dest_project = next((project for project in all_projects if project.name == args.destination_project), None)
3741

38-
# Step 4: Update workbook with new project id
39-
if all_workbooks:
40-
print("Old project: {}".format(all_workbooks[0].project_name))
41-
all_workbooks[0].project_id = dest_project.id
42-
target_workbook = server.workbooks.update(all_workbooks[0])
43-
print("New project: {}".format(target_workbook.project_name))
42+
if dest_project is not None:
43+
# Step 4: Update workbook with new project id
44+
if all_workbooks:
45+
print("Old project: {}".format(all_workbooks[0].project_name))
46+
all_workbooks[0].project_id = dest_project.id
47+
target_workbook = server.workbooks.update(all_workbooks[0])
48+
print("New project: {}".format(target_workbook.project_name))
49+
else:
50+
error = "No workbook named {} found.".format(args.workbook_name)
51+
raise LookupError(error)
4452
else:
45-
print('No workbook named {} found.'.format(args.workbook_name))
53+
error = "No project named {} found.".format(args.destination_project)
54+
raise LookupError(error)

samples/move_workbook_sites.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import shutil
1212
import argparse
1313
import tempfile
14+
import logging
1415

1516
parser = argparse.ArgumentParser(description="Move one workbook from the"
1617
"default project of the default site to"
@@ -20,8 +21,14 @@
2021
parser.add_argument('password', help='password to sign into server')
2122
parser.add_argument('workbook_name', help='name of workbook to move')
2223
parser.add_argument('destination_site', help='name of site to move workbook into')
24+
parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error',
25+
help='desired logging level (set to error by default)')
2326
args = parser.parse_args()
2427

28+
# Set logging level based on user input, or error by default
29+
logging_level = getattr(logging, args.logging_level.upper())
30+
logging.basicConfig(level=logging_level)
31+
2532
# Step 1: Sign in to both sites on server
2633
tableau_auth = TSA.TableauAuth(args.username, args.password)
2734

@@ -43,8 +50,18 @@
4350
try:
4451
workbook_path = source_server.workbooks.download(all_workbooks[0].id, tmpdir)
4552

46-
# Step 4: Sign in to destination site
53+
# Step 4: Check if destination site exists, then sign in to the site
54+
pagination_info, all_sites = source_server.sites.get()
55+
found_destination_site = any((True for site in all_sites if
56+
args.destination_site.lower() == site.content_url.lower()))
57+
if not found_destination_site:
58+
error = "No site named {} found.".format(args.destination_site)
59+
raise LookupError(error)
60+
4761
tableau_auth.site = args.destination_site
62+
63+
# Signing into another site requires another server object
64+
# because of the different auth token and site ID.
4865
with dest_server.auth.sign_in(tableau_auth):
4966

5067
# Step 5: Find destination site's default project
@@ -56,6 +73,10 @@
5673
new_workbook = TSA.WorkbookItem(name=args.workbook_name, project_id=target_project.id)
5774
new_workbook = dest_server.workbooks.publish(new_workbook, workbook_path,
5875
mode=dest_server.PublishMode.Overwrite)
76+
print("Successfully moved {0} ({1})".format(new_workbook.name, new_workbook.id))
77+
else:
78+
error = "The default project could not be found."
79+
raise LookupError(error)
5980

6081
# Step 7: Delete workbook from source site and delete temp directory
6182
source_server.workbooks.delete(all_workbooks[0].id)

samples/publish_workbook.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616

1717
import tableauserverapi as TSA
1818
import argparse
19+
import logging
1920

2021
parser = argparse.ArgumentParser(description='Publish a workbook to server.')
2122
parser.add_argument('server', help='server address')
2223
parser.add_argument('username', help='username to sign into server')
2324
parser.add_argument('password', help='password to sign into server')
2425
parser.add_argument('filepath', help='filepath to the workbook to publish')
26+
parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error',
27+
help='desired logging level (set to error by default)')
2528
args = parser.parse_args()
2629

30+
# Set logging level based on user input, or error by default
31+
logging_level = getattr(logging, args.logging_level.upper())
32+
logging.basicConfig(level=logging_level)
33+
2734
# Step 1: Sign in to server.
2835
tableau_auth = TSA.TableauAuth(args.username, args.password)
2936
server = TSA.Server(args.server)
@@ -39,4 +46,5 @@
3946
new_workbook = server.workbooks.publish(new_workbook, args.filepath, server.PublishMode.Overwrite)
4047
print("Workbook published. ID: {0}".format(new_workbook.id))
4148
else:
42-
print("The default project could not be found.")
49+
error = "The default project could not be found."
50+
raise LookupError(error)

samples/set_http_options.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@
1111
import tableauserverapi as TSA
1212
import argparse
1313
import getpass
14+
import logging
1415

1516
parser = argparse.ArgumentParser(description='List workbooks on site.')
1617
parser.add_argument('server', help='server address')
1718
parser.add_argument('username', help='username to sign into server')
19+
parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error',
20+
help='desired logging level (set to error by default)')
1821
args = parser.parse_args()
1922

2023
password = getpass.getpass("Password: ")
2124

25+
# Set logging level based on user input, or error by default
26+
logging_level = getattr(logging, args.logging_level.upper())
27+
logging.basicConfig(level=logging_level)
28+
2229
# Step 1: Create required objects for sign in
2330
tableau_auth = TSA.TableauAuth(args.username, password)
2431
server = TSA.Server(args.server)

0 commit comments

Comments
 (0)
0