From 8e1040b51addeee50769997b4b737ff8afe4c258 Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Fri, 2 Sep 2016 15:47:46 -0700 Subject: [PATCH 1/5] Add automated test runs via travis (#8) --- .travis.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..656e169f7 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: python +python: + - "2.7" + - "3.3" + - "3.4" + - "3.5" + - "pypy" +# command to install dependencies +install: + - "pip install -e ." + # - "pip install pep8" +# command to run tests +script: + # Tests + - python setup.py test + # pep8 - disabled for now until we can scrub the files to make sure we pass before turning it on + # - pep8 . From 963ac719abb09d8f1f6b73aa20275a7a35ef1533 Mon Sep 17 00:00:00 2001 From: shinchris Date: Fri, 2 Sep 2016 16:24:55 -0700 Subject: [PATCH 2/5] samples raise error if project or workbook not found --- samples/move_workbook_projects.py | 22 ++++++++++++---------- samples/move_workbook_sites.py | 24 ++++++++++++++---------- samples/publish_workbook.py | 3 ++- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/samples/move_workbook_projects.py b/samples/move_workbook_projects.py index 19722630e..7a1fa20fd 100644 --- a/samples/move_workbook_projects.py +++ b/samples/move_workbook_projects.py @@ -10,9 +10,6 @@ import tableauserverapi as TSA import argparse -# import logging -# logging.basicConfig(level=logging.DEBUG) - parser = argparse.ArgumentParser(description='Move one workbook from the default project to another.') parser.add_argument('server', help='server address') parser.add_argument('username', help='username to sign into server') @@ -35,11 +32,16 @@ pagination_info, all_projects = server.projects.get() dest_project = next((project for project in all_projects if project.name == args.destination_project), None) - # Step 4: Update workbook with new project id - if all_workbooks: - print("Old project: {}".format(all_workbooks[0].project_name)) - all_workbooks[0].project_id = dest_project.id - target_workbook = server.workbooks.update(all_workbooks[0]) - print("New project: {}".format(target_workbook.project_name)) + if dest_project is not None: + # Step 4: Update workbook with new project id + if all_workbooks: + print("Old project: {}".format(all_workbooks[0].project_name)) + all_workbooks[0].project_id = dest_project.id + target_workbook = server.workbooks.update(all_workbooks[0]) + print("New project: {}".format(target_workbook.project_name)) + else: + error = "No workbook named {} found.".format(args.workbook_name) + raise LookupError(error) else: - print('No workbook named {} found.'.format(args.workbook_name)) + error = "The default project could not be found." + raise LookupError(error) diff --git a/samples/move_workbook_sites.py b/samples/move_workbook_sites.py index 43ccd33cf..b8e86ee5c 100644 --- a/samples/move_workbook_sites.py +++ b/samples/move_workbook_sites.py @@ -25,15 +25,15 @@ # Step 1: Sign in to both sites on server tableau_auth = TSA.TableauAuth(args.username, args.password) -source_server = TSA.Server(args.server) -dest_server = TSA.Server(args.server) +source_site_server = TSA.Server(args.server) +dest_site_server = TSA.Server(args.server) -with source_server.auth.sign_in(tableau_auth): +with source_site_server.auth.sign_in(tableau_auth): # Step 2: Query workbook to move req_option = TSA.RequestOptions() req_option.filter.add(TSA.Filter(TSA.RequestOptions.Field.Name, TSA.RequestOptions.Operator.Equals, args.workbook_name)) - pagination_info, all_workbooks = source_server.workbooks.get(req_option) + pagination_info, all_workbooks = source_site_server.workbooks.get(req_option) # Step 3: Download workbook to a temp directory if len(all_workbooks) == 0: @@ -41,23 +41,27 @@ else: tmpdir = tempfile.mkdtemp() try: - workbook_path = source_server.workbooks.download(all_workbooks[0].id, tmpdir) + workbook_path = source_site_server.workbooks.download(all_workbooks[0].id, tmpdir) # Step 4: Sign in to destination site tableau_auth.site = args.destination_site - with dest_server.auth.sign_in(tableau_auth): + with dest_site_server.auth.sign_in(tableau_auth): # Step 5: Find destination site's default project - pagination_info, dest_projects = dest_server.projects.get() + pagination_info, dest_projects = dest_site_server.projects.get() target_project = next((project for project in dest_projects if project.is_default()), None) # Step 6: If default project is found, form a new workbook item and publish. if target_project is not None: new_workbook = TSA.WorkbookItem(name=args.workbook_name, project_id=target_project.id) - new_workbook = dest_server.workbooks.publish(new_workbook, workbook_path, - mode=dest_server.PublishMode.Overwrite) + new_workbook = dest_site_server.workbooks.publish(new_workbook, workbook_path, + mode=dest_site_server.PublishMode.Overwrite) + print("Successfully moved {0} ({1})".format(new_workbook.name, new_workbook.id)) + else: + error = "The default project could not be found." + raise LookupError(error) # Step 7: Delete workbook from source site and delete temp directory - source_server.workbooks.delete(all_workbooks[0].id) + source_site_server.workbooks.delete(all_workbooks[0].id) finally: shutil.rmtree(tmpdir) diff --git a/samples/publish_workbook.py b/samples/publish_workbook.py index b3836d389..7d94799d8 100644 --- a/samples/publish_workbook.py +++ b/samples/publish_workbook.py @@ -39,4 +39,5 @@ new_workbook = server.workbooks.publish(new_workbook, args.filepath, server.PublishMode.Overwrite) print("Workbook published. ID: {0}".format(new_workbook.id)) else: - print("The default project could not be found.") + error = "The default project could not be found." + raise LookupError(error) From fb56e0b79a39c425149cb0b91ee0655598146ad3 Mon Sep 17 00:00:00 2001 From: shinchris Date: Tue, 6 Sep 2016 14:54:00 -0700 Subject: [PATCH 3/5] corrected error message and added check to see if site exists before using --- samples/move_workbook_projects.py | 2 +- samples/move_workbook_sites.py | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/samples/move_workbook_projects.py b/samples/move_workbook_projects.py index 7a1fa20fd..173cae505 100644 --- a/samples/move_workbook_projects.py +++ b/samples/move_workbook_projects.py @@ -43,5 +43,5 @@ error = "No workbook named {} found.".format(args.workbook_name) raise LookupError(error) else: - error = "The default project could not be found." + error = "No project named {} found.".format(args.destination_project) raise LookupError(error) diff --git a/samples/move_workbook_sites.py b/samples/move_workbook_sites.py index b8e86ee5c..68b8795c1 100644 --- a/samples/move_workbook_sites.py +++ b/samples/move_workbook_sites.py @@ -25,15 +25,15 @@ # Step 1: Sign in to both sites on server tableau_auth = TSA.TableauAuth(args.username, args.password) -source_site_server = TSA.Server(args.server) -dest_site_server = TSA.Server(args.server) +source_server = TSA.Server(args.server) +dest_server = TSA.Server(args.server) -with source_site_server.auth.sign_in(tableau_auth): +with source_server.auth.sign_in(tableau_auth): # Step 2: Query workbook to move req_option = TSA.RequestOptions() req_option.filter.add(TSA.Filter(TSA.RequestOptions.Field.Name, TSA.RequestOptions.Operator.Equals, args.workbook_name)) - pagination_info, all_workbooks = source_site_server.workbooks.get(req_option) + pagination_info, all_workbooks = source_server.workbooks.get(req_option) # Step 3: Download workbook to a temp directory if len(all_workbooks) == 0: @@ -41,27 +41,35 @@ else: tmpdir = tempfile.mkdtemp() try: - workbook_path = source_site_server.workbooks.download(all_workbooks[0].id, tmpdir) + workbook_path = source_server.workbooks.download(all_workbooks[0].id, tmpdir) + + # Step 4: Check if destination site exists and sign in to the site + pagination_info, all_sites = source_server.sites.get() + if args.destination_site.lower() not in [site.content_url.lower() for site in all_sites]: + error = "No site named {} found.".format(args.destination_site) + raise LookupError(error) - # Step 4: Sign in to destination site tableau_auth.site = args.destination_site - with dest_site_server.auth.sign_in(tableau_auth): + + # Signing into another site requires another server object + # because of the different auth token and site ID. + with dest_server.auth.sign_in(tableau_auth): # Step 5: Find destination site's default project - pagination_info, dest_projects = dest_site_server.projects.get() + pagination_info, dest_projects = dest_server.projects.get() target_project = next((project for project in dest_projects if project.is_default()), None) # Step 6: If default project is found, form a new workbook item and publish. if target_project is not None: new_workbook = TSA.WorkbookItem(name=args.workbook_name, project_id=target_project.id) - new_workbook = dest_site_server.workbooks.publish(new_workbook, workbook_path, - mode=dest_site_server.PublishMode.Overwrite) + new_workbook = dest_server.workbooks.publish(new_workbook, workbook_path, + mode=dest_server.PublishMode.Overwrite) print("Successfully moved {0} ({1})".format(new_workbook.name, new_workbook.id)) else: error = "The default project could not be found." raise LookupError(error) # Step 7: Delete workbook from source site and delete temp directory - source_site_server.workbooks.delete(all_workbooks[0].id) + source_server.workbooks.delete(all_workbooks[0].id) finally: shutil.rmtree(tmpdir) From 3057b3d7e90bc021bc010a14d5462e9f00eb98c6 Mon Sep 17 00:00:00 2001 From: shinchris Date: Tue, 6 Sep 2016 15:05:11 -0700 Subject: [PATCH 4/5] added logging option for all samples --- samples/explore_datasource.py | 11 +++++++---- samples/explore_workbook.py | 11 +++++++---- samples/move_workbook_projects.py | 11 +++++++++++ samples/move_workbook_sites.py | 11 +++++++++++ samples/publish_workbook.py | 11 +++++++++++ samples/set_http_options.py | 11 +++++++++++ 6 files changed, 58 insertions(+), 8 deletions(-) diff --git a/samples/explore_datasource.py b/samples/explore_datasource.py index a9ec5fc71..1918cee17 100644 --- a/samples/explore_datasource.py +++ b/samples/explore_datasource.py @@ -19,16 +19,19 @@ parser.add_argument('server', help='server address') parser.add_argument('username', help='username to sign into server') parser.add_argument('password', help='password to sign into server') -parser.add_argument('-info', action='store_true', help='set this flag to show logging information on INFO level') -parser.add_argument('-debug', action='store_true', help='set this flag to show logging information on DEBUG level') parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to datasource to publish') parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded datasource') +parser.add_argument('--logging-level', choices=['debug', 'info'], + help='desired logging level (set to error by default)') args = parser.parse_args() -if args.debug: +# Set logging level based on user input, or error by default +if args.logging_level == 'debug': logging.basicConfig(level=logging.DEBUG) -elif args.info: +elif args.logging_level == 'info': logging.basicConfig(level=logging.INFO) +else: + logging.basicConfig(level=logging.ERROR) ##### SIGN IN ##### tableau_auth = TSA.TableauAuth(args.username, args.password) diff --git a/samples/explore_workbook.py b/samples/explore_workbook.py index da5e79169..f708676c9 100644 --- a/samples/explore_workbook.py +++ b/samples/explore_workbook.py @@ -19,17 +19,20 @@ parser.add_argument('server', help='server address') parser.add_argument('username', help='username to sign into server') parser.add_argument('password', help='password to sign into server') -parser.add_argument('-info', action='store_true', help='set this flag to show logging information on INFO level') -parser.add_argument('-debug', action='store_true', help='set this flag to show logging information on DEBUG level') parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to workbook to publish') parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded workbook') parser.add_argument('-preview-image', '-i', metavar='FILEPATH', help='path to save populated preview image') +parser.add_argument('--logging-level', choices=['debug', 'info'], + help='desired logging level (set to error by default)') args = parser.parse_args() -if args.debug: +# Set logging level based on user input, or error by default +if args.logging_level == 'debug': logging.basicConfig(level=logging.DEBUG) -elif args.info: +elif args.logging_level == 'info': logging.basicConfig(level=logging.INFO) +else: + logging.basicConfig(level=logging.ERROR) ##### SIGN IN ##### tableau_auth = TSA.TableauAuth(args.username, args.password) diff --git a/samples/move_workbook_projects.py b/samples/move_workbook_projects.py index 173cae505..54af0f917 100644 --- a/samples/move_workbook_projects.py +++ b/samples/move_workbook_projects.py @@ -9,6 +9,7 @@ import tableauserverapi as TSA import argparse +import logging parser = argparse.ArgumentParser(description='Move one workbook from the default project to another.') parser.add_argument('server', help='server address') @@ -16,8 +17,18 @@ parser.add_argument('password', help='password to sign into server') parser.add_argument('workbook_name', help='name of workbook to move') parser.add_argument('destination_project', help='name of project to move workbook into') +parser.add_argument('--logging-level', choices=['debug', 'info'], + help='desired logging level (set to error by default)') args = parser.parse_args() +# Set logging level based on user input, or error by default +if args.logging_level == 'debug': + logging.basicConfig(level=logging.DEBUG) +elif args.logging_level == 'info': + logging.basicConfig(level=logging.INFO) +else: + logging.basicConfig(level=logging.ERROR) + # Step 1: Sign in to server tableau_auth = TSA.TableauAuth(args.username, args.password) server = TSA.Server(args.server) diff --git a/samples/move_workbook_sites.py b/samples/move_workbook_sites.py index 68b8795c1..c9728ab94 100644 --- a/samples/move_workbook_sites.py +++ b/samples/move_workbook_sites.py @@ -11,6 +11,7 @@ import shutil import argparse import tempfile +import logging parser = argparse.ArgumentParser(description="Move one workbook from the" "default project of the default site to" @@ -20,8 +21,18 @@ parser.add_argument('password', help='password to sign into server') parser.add_argument('workbook_name', help='name of workbook to move') parser.add_argument('destination_site', help='name of site to move workbook into') +parser.add_argument('--logging-level', choices=['debug', 'info'], + help='desired logging level (set to error by default)') args = parser.parse_args() +# Set logging level based on user input, or error by default +if args.logging_level == 'debug': + logging.basicConfig(level=logging.DEBUG) +elif args.logging_level == 'info': + logging.basicConfig(level=logging.INFO) +else: + logging.basicConfig(level=logging.ERROR) + # Step 1: Sign in to both sites on server tableau_auth = TSA.TableauAuth(args.username, args.password) diff --git a/samples/publish_workbook.py b/samples/publish_workbook.py index 7d94799d8..ca7d09a13 100644 --- a/samples/publish_workbook.py +++ b/samples/publish_workbook.py @@ -16,14 +16,25 @@ import tableauserverapi as TSA import argparse +import logging parser = argparse.ArgumentParser(description='Publish a workbook to server.') parser.add_argument('server', help='server address') parser.add_argument('username', help='username to sign into server') parser.add_argument('password', help='password to sign into server') parser.add_argument('filepath', help='filepath to the workbook to publish') +parser.add_argument('--logging-level', choices=['debug', 'info'], + help='desired logging level (set to error by default)') args = parser.parse_args() +# Set logging level based on user input, or error by default +if args.logging_level == 'debug': + logging.basicConfig(level=logging.DEBUG) +elif args.logging_level == 'info': + logging.basicConfig(level=logging.INFO) +else: + logging.basicConfig(level=logging.ERROR) + # Step 1: Sign in to server. tableau_auth = TSA.TableauAuth(args.username, args.password) server = TSA.Server(args.server) diff --git a/samples/set_http_options.py b/samples/set_http_options.py index 487be46f0..5bbf31097 100644 --- a/samples/set_http_options.py +++ b/samples/set_http_options.py @@ -11,14 +11,25 @@ import tableauserverapi as TSA import argparse import getpass +import logging parser = argparse.ArgumentParser(description='List workbooks on site.') parser.add_argument('server', help='server address') parser.add_argument('username', help='username to sign into server') +parser.add_argument('--logging-level', choices=['debug', 'info'], + help='desired logging level (set to error by default)') args = parser.parse_args() password = getpass.getpass("Password: ") +# Set logging level based on user input, or error by default +if args.logging_level == 'debug': + logging.basicConfig(level=logging.DEBUG) +elif args.logging_level == 'info': + logging.basicConfig(level=logging.INFO) +else: + logging.basicConfig(level=logging.ERROR) + # Step 1: Create required objects for sign in tableau_auth = TSA.TableauAuth(args.username, password) server = TSA.Server(args.server) From 9440536f650ec14209eee142405789dec574f8f6 Mon Sep 17 00:00:00 2001 From: shinchris Date: Wed, 7 Sep 2016 17:46:08 -0700 Subject: [PATCH 5/5] updating based on code review comments --- samples/explore_datasource.py | 10 +++------- samples/explore_workbook.py | 10 +++------- samples/move_workbook_projects.py | 10 +++------- samples/move_workbook_sites.py | 16 +++++++--------- samples/publish_workbook.py | 10 +++------- samples/set_http_options.py | 10 +++------- 6 files changed, 22 insertions(+), 44 deletions(-) diff --git a/samples/explore_datasource.py b/samples/explore_datasource.py index 1918cee17..4c2fb5a6d 100644 --- a/samples/explore_datasource.py +++ b/samples/explore_datasource.py @@ -21,17 +21,13 @@ parser.add_argument('password', help='password to sign into server') parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to datasource to publish') parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded datasource') -parser.add_argument('--logging-level', choices=['debug', 'info'], +parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') args = parser.parse_args() # Set logging level based on user input, or error by default -if args.logging_level == 'debug': - logging.basicConfig(level=logging.DEBUG) -elif args.logging_level == 'info': - logging.basicConfig(level=logging.INFO) -else: - logging.basicConfig(level=logging.ERROR) +logging_level = getattr(logging, args.logging_level.upper()) +logging.basicConfig(level=logging_level) ##### SIGN IN ##### tableau_auth = TSA.TableauAuth(args.username, args.password) diff --git a/samples/explore_workbook.py b/samples/explore_workbook.py index f708676c9..d371a1b38 100644 --- a/samples/explore_workbook.py +++ b/samples/explore_workbook.py @@ -22,17 +22,13 @@ parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to workbook to publish') parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded workbook') parser.add_argument('-preview-image', '-i', metavar='FILEPATH', help='path to save populated preview image') -parser.add_argument('--logging-level', choices=['debug', 'info'], +parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], defualt='error', help='desired logging level (set to error by default)') args = parser.parse_args() # Set logging level based on user input, or error by default -if args.logging_level == 'debug': - logging.basicConfig(level=logging.DEBUG) -elif args.logging_level == 'info': - logging.basicConfig(level=logging.INFO) -else: - logging.basicConfig(level=logging.ERROR) +logging_level = getattr(logging, args.logging_level.upper()) +logging.basicConfig(level=logging_level) ##### SIGN IN ##### tableau_auth = TSA.TableauAuth(args.username, args.password) diff --git a/samples/move_workbook_projects.py b/samples/move_workbook_projects.py index 54af0f917..e47d4f322 100644 --- a/samples/move_workbook_projects.py +++ b/samples/move_workbook_projects.py @@ -17,17 +17,13 @@ parser.add_argument('password', help='password to sign into server') parser.add_argument('workbook_name', help='name of workbook to move') parser.add_argument('destination_project', help='name of project to move workbook into') -parser.add_argument('--logging-level', choices=['debug', 'info'], +parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') args = parser.parse_args() # Set logging level based on user input, or error by default -if args.logging_level == 'debug': - logging.basicConfig(level=logging.DEBUG) -elif args.logging_level == 'info': - logging.basicConfig(level=logging.INFO) -else: - logging.basicConfig(level=logging.ERROR) +logging_level = getattr(logging, args.logging_level.upper()) +logging.basicConfig(level=logging_level) # Step 1: Sign in to server tableau_auth = TSA.TableauAuth(args.username, args.password) diff --git a/samples/move_workbook_sites.py b/samples/move_workbook_sites.py index c9728ab94..968f23aae 100644 --- a/samples/move_workbook_sites.py +++ b/samples/move_workbook_sites.py @@ -21,17 +21,13 @@ parser.add_argument('password', help='password to sign into server') parser.add_argument('workbook_name', help='name of workbook to move') parser.add_argument('destination_site', help='name of site to move workbook into') -parser.add_argument('--logging-level', choices=['debug', 'info'], +parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') args = parser.parse_args() # Set logging level based on user input, or error by default -if args.logging_level == 'debug': - logging.basicConfig(level=logging.DEBUG) -elif args.logging_level == 'info': - logging.basicConfig(level=logging.INFO) -else: - logging.basicConfig(level=logging.ERROR) +logging_level = getattr(logging, args.logging_level.upper()) +logging.basicConfig(level=logging_level) # Step 1: Sign in to both sites on server tableau_auth = TSA.TableauAuth(args.username, args.password) @@ -54,9 +50,11 @@ try: workbook_path = source_server.workbooks.download(all_workbooks[0].id, tmpdir) - # Step 4: Check if destination site exists and sign in to the site + # Step 4: Check if destination site exists, then sign in to the site pagination_info, all_sites = source_server.sites.get() - if args.destination_site.lower() not in [site.content_url.lower() for site in all_sites]: + found_destination_site = any((True for site in all_sites if + args.destination_site.lower() == site.content_url.lower())) + if not found_destination_site: error = "No site named {} found.".format(args.destination_site) raise LookupError(error) diff --git a/samples/publish_workbook.py b/samples/publish_workbook.py index ca7d09a13..662b38323 100644 --- a/samples/publish_workbook.py +++ b/samples/publish_workbook.py @@ -23,17 +23,13 @@ parser.add_argument('username', help='username to sign into server') parser.add_argument('password', help='password to sign into server') parser.add_argument('filepath', help='filepath to the workbook to publish') -parser.add_argument('--logging-level', choices=['debug', 'info'], +parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') args = parser.parse_args() # Set logging level based on user input, or error by default -if args.logging_level == 'debug': - logging.basicConfig(level=logging.DEBUG) -elif args.logging_level == 'info': - logging.basicConfig(level=logging.INFO) -else: - logging.basicConfig(level=logging.ERROR) +logging_level = getattr(logging, args.logging_level.upper()) +logging.basicConfig(level=logging_level) # Step 1: Sign in to server. tableau_auth = TSA.TableauAuth(args.username, args.password) diff --git a/samples/set_http_options.py b/samples/set_http_options.py index 5bbf31097..f5ef02db0 100644 --- a/samples/set_http_options.py +++ b/samples/set_http_options.py @@ -16,19 +16,15 @@ parser = argparse.ArgumentParser(description='List workbooks on site.') parser.add_argument('server', help='server address') parser.add_argument('username', help='username to sign into server') -parser.add_argument('--logging-level', choices=['debug', 'info'], +parser.add_argument('--logging-level', choices=['debug', 'info', 'error'], default='error', help='desired logging level (set to error by default)') args = parser.parse_args() password = getpass.getpass("Password: ") # Set logging level based on user input, or error by default -if args.logging_level == 'debug': - logging.basicConfig(level=logging.DEBUG) -elif args.logging_level == 'info': - logging.basicConfig(level=logging.INFO) -else: - logging.basicConfig(level=logging.ERROR) +logging_level = getattr(logging, args.logging_level.upper()) +logging.basicConfig(level=logging_level) # Step 1: Create required objects for sign in tableau_auth = TSA.TableauAuth(args.username, password)