8000 Unify arguments of sample scripts · gentleway/server-client-python@74bec02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74bec02

Browse files
committed
Unify arguments of sample scripts
I am pretty new to TSC, and wanted to run some sample scripts to get an understanding of the library. Doing so, I realized that every sample had a slightly different command line, even for common arguments: * Some expected `site`, some `site-id`, some were lacking site-support completely (and thereby unusable for Tableau Online) * Some had a short option `-i`, some had the short option `-S` for the site name * Some expected password-based authentication, some expected personal access tokens This commit fixes all those inconsistencies, so that users don't have to re-learn the command line options for each individual script.
1 parent 1c802ee commit 74bec02

27 files changed

+270
-297
lines changed

samples/add_default_permission.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,33 @@
1010
####
1111

1212
import argparse
13-
import getpass
1413
import logging
1514

1615
import tableauserverclient as TSC
1716

1817

1918
def main():
2019
parser = argparse.ArgumentParser(description='Add workbook default permissions for a given project.')
21-
parser.add_argument('--server', '-s', required=True, help='Server address')
22-
parser.add_argument('--username', '-u', required=True, help='Username to sign into server')
23-
parser.add_argument('--site', '-S', default=None, help='Site to sign into - default site if not provided')
24-
parser.add_argument('-p', default=None, help='Password to sign into server')
25-
20+
# Common options; please keep those in sync across all samples
21+
parser.add_argument('--server', '-s', required=True, help='server address')
22+
parser.add_argument('--site', '-S', help='site name')
23+
parser.add_argument('--token-name', '-p', required=True,
24+
help='name of the personal access token used to sign into the server')
25+
parser.add_argument('--token-value', '-v', required=True,
26+
help='value of the personal access token used to sign into the server')
2627
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
2728
help='desired logging level (set to error by default)')
29+
# Options specific to this sample
30+
# This sample has no additional options, yet. If you add some, please add them here
2831

2932
args = parser.parse_args()
3033

31-
if args.p is None:
32-
password = getpass.getpass("Password: ")
33-
else:
34-
password = args.p
35-
3634
# Set logging level based on user input, or error by default
3735
logging_level = getattr(logging, args.logging_level.upper())
3836
logging.basicConfig(level=logging_level)
3937

40-
# Sign in
41-
tableau_auth = TSC.TableauAuth(args.username, password, args.site)
38+
# Sign in to server
39+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
4240
server = TSC.Server(args.server, use_server_version=True)
4341
with server.auth.sign_in(tableau_auth):
4442

samples/create_group.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88

99
import argparse
10-
import getpass
1110
import logging
1211

1312
from datetime import time
@@ -18,20 +17,26 @@
1817
def main():
1918

2019
parser = argparse.ArgumentParser(description='Creates a sample user group.')
20+
# Common options; please keep those in sync across all samples
2121
parser.add_argument('--server', '-s', required=True, help='server address')
22-
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
22+
parser.add_argument('--site', '-S', help='site name')
23+
parser.add_argument('--token-name', '-p', required=True,
24+
help='name of the personal access token used to sign into the server')
25+
parser.add_argument('--token-value', '-v', required=True,
26+
help='value of the personal access token used to sign into the server')
2327
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
2428
help='desired logging level (set to error by default)')
25-
args = parser.parse_args()
29+
# Options specific to this sample
30+
# This sample has no additional options, yet. If you add some, please add them here
2631

27-
password = getpass.getpass("Password: ")
32+
args = parser.parse_args()
2833

2934
# Set logging level based on user input, or error by default
3035
logging_level = getattr(logging, args.logging_level.upper())
3136
logging.basicConfig(level=logging_level)
3237

33-
tableau_auth = TSC.TableauAuth(args.username, password)
34-
server = TSC.Server(args.server)
38+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
39+
server = TSC.Server(args.server, use_server_version=True)
3540
with server.auth.sign_in(tableau_auth):
3641
group = TSC.GroupItem('test')
3742
group = server.groups.create(group)

samples/create_project.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
####
99

1010
import argparse
11-
import getpass
1211
import logging
1312
import sys
1413

@@ -27,28 +26,26 @@ def create_project(server, project_item):
2726

2827
def main():
2928
parser = argparse.ArgumentParser(description='Create new projects.')
29+
# Common options; please keep those in sync across all samples
3030
parser.add_argument('--server', '-s', required=True, help='server address')
31-
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
32-
parser.add_argument('--site', '-S', default=None)
33-
parser.add_argument('-p', default=None, help='password')
34-
31+
parser.add_argument('--site', '-S', help='site name')
32+
parser.add_argument('--token-name', '-p', required=True,
33+
help='name of the personal access token used to sign into the server')
34+
parser.add_argument('--token-value', '-v', required=True,
35+
help='value of the personal access token used to sign into the server')
3536
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
3637
help='desired logging level (set to error by default)')
38+
# Options specific to this sample
39+
# This sample has no additional options, yet. If you add some, please add them here
3740

3841
args = parser.parse_args()
3942

40-
if args.p is None:
41-
password = getpass.getpass("Password: ")
42-
else:
43-
password = args.p
44-
4543
# Set logging level based on user input, or error by default
4644
logging_level = getattr(logging, args.logging_level.upper())
4745
logging.basicConfig(level=logging_level)
4846

49-
tableau_auth = TSC.TableauAuth(args.username, password)
50-
server = TSC.Server(args.server)
51-
47+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
48+
server = TSC.Server(args.server, use_server_version=True)
5249
with server.auth.sign_in(tableau_auth):
5350
# Use highest Server REST API version available
5451
server.use_server_version()

samples/create_schedules.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88

99
import argparse
10-
import getpass
1110
import logging
1211

1312
from datetime import time
@@ -18,20 +17,26 @@
1817
def main():
1918

2019
parser = argparse.ArgumentParser(description='Creates sample schedules for each type of frequency.')
20+
# Common options; please keep those in sync across all samples
2121
parser.add_argument('--server', '-s', required=True, help='server address')
22-
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
22+
parser.add_argument('--site', '-S', help='site name')
23+
parser.add_argument('--token-name', '-p', required=True,
24+
help='name of the personal access token used to sign into the server')
25+
parser.add_argument('--token-value', '-v', required=True,
26+
help='value of the personal access token used to sign into the server')
2327
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
2428
help='desired logging level (set to error by default)')
25-
args = parser.parse_args()
29+
# Options specific to this sample
30+
# This sample has no additional options, yet. If you add some, please add them here
2631

27-
password = getpass.getpass("Password: ")
32+
args = parser.parse_args()
2833

2934
# Set logging level based on user input, or error by default
3035
logging_level = getattr(logging, args.logging_level.upper())
3136
logging.basicConfig(level=logging_level)
3237

33-
tableau_auth = TSC.TableauAuth(args.username, password)
34-
server = TSC.Server(args.server)
38+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
39+
server = TSC.Server(args.server, use_server_version=True)
3540
with server.auth.sign_in(tableau_auth):
3641
# Hourly Schedule
3742
# This schedule will run every 2 hours between 2:30AM and 11:00PM

samples/download_view_image.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
####
1010

1111
import argparse
12-
import getpass
1312
import logging
1413

1514
import tableauserverclient as TSC
@@ -18,34 +17,30 @@
1817
def main():
1918

2019
parser = argparse.ArgumentParser(description='Download image of a specified view.')
20+
# Common options; please keep those in sync across all samples
2121
parser.add_argument('--server', '-s', required=True, help='server address')
22-
parser.add_argument('--site-id', '-si', required=False,
23-
help='content url for site the view is on')
24-
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
25-
parser.add_argument('--view-name', '-v', required=True,
22+
parser.add_argument('--site', '-S', help='site name')
23+
parser.add_argument('--token-name', '-p', required=True,
24+
help='name of the personal access token used to sign into the server')
25+
parser.add_argument('--token-value', '-v', required=True,
26+
help='value of the personal access token used to sign into the server')
27+
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
28+
help='desired logging level (set to error by default)')
29+
# Options specific to this sample
30+
parser.add_argument('--view-name', '-vn', required=True,
2631
help='name of view to download an image of')
2732
parser.add_argument('--filepath', '-f', required=True, help='filepath to save the image returned')
2833
parser.add_argument('--maxage', '-m', required=False, help='max age of the image in the cache in minutes.')
29-
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
30-
help='desired logging level (set to error by default)')
3134

3235
args = parser.parse_args()
3336

34-
password = getpass.getpass("Password: ")
35-
3637
# Set logging level based on user input, or error by default
3738
logging_level = getattr(logging, args.logging_level.upper())
3839
logging.basicConfig(level=logging_level)
3940

4041
# Step 1: Sign in to server.
41-
site_id = args.site_id
42-
if not site_id:
43-
site_id = ""
44-
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_id)
45-
server = TSC.Server(args.server)
46-
# The new endpoint was introduced in Version 2.5
47-
server.version = "2.5"
48-
42+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
43+
server = TSC.Server(args.server, use_server_version=True)
4944
with server.auth.sign_in(tableau_auth):
5045
# Step 2: Query for the view that we want an image of
5146
req_option = TSC.RequestOptions()

samples/explore_datasource.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
####
1111

1212
import argparse
13-
import getpass
1413
import logging
1514

1615
import tableauserverclient as TSC
@@ -19,25 +18,28 @@
1918
def main():
2019

2120
parser = argparse.ArgumentParser(description='Explore datasource functions supported by the Server API.')
21+
# Common options; please keep those in sync across all samples
2222
parser.add_argument('--server', '-s', required=True, help='server address')
23-
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
24-
parser.add_argument('--publish', '-p', metavar='FILEPATH', help='path to datasource to publish')
25-
parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded datasource')
23+
parser.add_argument('--site', '-S', help='site name')
24+
parser.add_argument('--token-name', '-p', required=True,
25+
help='name of the personal access token used to sign into the server')
26+
parser.add_argument('--token-value', '-v', required=True,
27+
help='value of the personal access token used to sign into the server')
2628
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
2729
help='desired logging level (set to error by default)')
30+
# Options specific to this sample
31+
parser.add_argument('--publish', metavar='FILEPATH', help='path to datasource to publish')
32+
parser.add_argument('--download', metavar='FILEPATH', help='path to save downloaded datasource')
2833

2934
args = parser.parse_args()
3035

31-
password = getpass.getpass("Password: ")
32-
3336
# Set logging level based on user input, or error by default
3437
logging_level = getattr(logging, args.logging_level.upper())
3538
logging.basicConfig(level=logging_level)
3639

3740
# SIGN IN
38-
tableau_auth = TSC.TableauAuth(args.username, password)
39-
server = TSC.Server(args.server)
40-
server.use_highest_version()
41+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
42+
server = TSC.Server(args.server, use_server_version=True)
4143
with server.auth.sign_in(tableau_auth):
4244
# Query projects for use when demonstrating publishing and updating
4345
all_projects, pagination_item = server.projects.get()

samples/explore_webhooks.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
####
1111

1212
import argparse
13-
import getpass
1413
import logging
1514
import os.path
1615

@@ -20,35 +19,28 @@
2019
def main():
2120

2221
parser = argparse.ArgumentParser(description='Explore webhook functions supported by the Server API.')
22+
# Common options; please keep those in sync across all samples
2323
parser.add_argument('--server', '-s', required=True, help='server address')
24-
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
25-
parser.add_argument('--site', '-S', default=None)
26-
parser.add_argument('-p', default=None, help='password')
27-
parser.add_argument('--create', '-c', help='create a webhook')
28-
parser.add_argument('--delete', '-d', help='delete a webhook', action='store_true')
24+
parser.add_argument('--site', '-S', help='site name')
25+
parser.add_argument('--token-name', '-p', required=True,
26+
help='name of the personal access token used to sign into the server')
27+
parser.add_argument('--token-value', '-v', required=True,
28+
help='value of the personal access token used to sign into the server')
2929
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
3030
help='desired logging level (set to error by default)')
31+
# Options specific to this sample
32+
parser.add_argument('--create', help='create a webhook')
33+
parser.add_argument('--delete', help='delete a webhook', action='store_true')
3134

3235
args = parser.parse_args()
33-
if args.p is None:
34-
password = getpass.getpass("Password: ")
35-
else:
36-
password = args.p
3736

3837
# Set logging level based on user input, or error by default
3938
logging_level = getattr(logging, args.logging_level.upper())
4039
logging.basicConfig(level=logging_level)
4140

4241
# SIGN IN
43-
tableau_auth = TSC.TableauAuth(args.username, password, args.site)
44-
print("Signing in to " + args.server + " [" + args.site + "] as " + args.username)
45-
server = TSC.Server(args.server)
46-
47-
# Set http options to disable verifying SSL
48-
server.add_http_options({'verify': False})
49-
50-
server.use_server_version()
51-
42+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
43+
server = TSC.Server(args.server, use_server_version=True)
5244
with server.auth.sign_in(tableau_auth):
5345

5446
# Create webhook if create flag is set (-create, -c)

samples/explore_workbook.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
####
1111

1212
import argparse
13-
import getpass
1413
import logging
1514
import os.path
1615

@@ -20,33 +19,34 @@
2019
def main():
2120

2221
parser = argparse.ArgumentParser(description='Explore workbook functions supported by the Server API.')
22+
# Common options; please keep those in sync across all samples
2323
parser.add_argument('--server', '-s', required=True, help='server address')
24-
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
25-
parser.add_argument('--publish', '-p', metavar='FILEPATH', help='path to workbook to publish')
26-
parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded workbook')
27-
parser.add_argument('--preview-image', '-i', metavar='FILENAME',
28-
help='filename (a .png file) to save the preview image')
24+
parser.add_argument('--site', '-S', help='site name')
25+
parser.add_argument('--token-name', '-p', required=True,
26+
help='name of the personal access token used to sign into the server')
27+
parser.add_argument('--token-value', '-v', required=True,
28+
help='value of the personal access token used to sign into the server')
2929
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
3030
help='desired logging level (set to error by default)')
31+
# Options specific to this sample
32+
parser.add_argument('--publish', metavar='FILEPATH', help='path to workbook to publish')
33+
parser.add_argument('--download', metavar='FILEPATH', help='path to save downloaded workbook')
34+
parser.add_argument('--preview-image', '-i', metavar='FILENAME',
35+
help='filename (a .png file) to save the preview image')
3136

3237
args = parser.parse_args()
3338

34-
password = getpass.getpass("Password: ")
35-
3639
# Set logging level based on user input, or error by default
3740
logging_level = getattr(logging, args.logging_level.upper())
3841
logging.basicConfig(level=logging_level)
3942

4043
# SIGN IN
41-
tableau_auth = TSC.TableauAuth(args.username, password)
42-
server = TSC.Server(args.server)
43-
server.use_highest_version()
44-
45-
overwrite_true = TSC.Server.PublishMode.Overwrite
46-
44+
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
45+
server = TSC.Server(args.server, use_server_version=True)
4746
with server.auth.sign_in(tableau_auth):
4847

4948
# Publish workbook if publish flag is set (-publish, -p)
49+
overwrite_true = TSC.Server.PublishMode.Overwrite
5050
if args.publish:
5151
all_projects, pagination_item = server.projects.get()
5252
default_project = next((project for project in all_projects if project.is_default()), None)

0 commit comments

Comments
 (0)
0