8000 Update samples for Python 3.x compatibility (#1479) · tableau/server-client-python@60dfd4d · GitHub
[go: up one dir, main page]

Skip to content

Commit 60dfd4d

Browse files
authored
Update samples for Python 3.x compatibility (#1479)
* Replace obsolete env package with os.environ * Python 2.x to 3.x updates * Fix some comments * Remove workbook data acceleration; feature was removed in 2022 * Remove switch_site() example which is confusing in this context of demonstrating login
1 parent 607fa8b commit 60dfd4d

6 files changed

+32
-137
lines changed

samples/extracts.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
####
2-
# This script demonstrates how to use the Tableau Server Client
3-
# to interact with workbooks. It explores the different
4-
# functions that the Server API supports on workbooks.
5-
#
6-
# With no flags set, this sample will query all workbooks,
7-
# pick one workbook and populate its connections/views, and update
8-
# the workbook. Adding flags will demonstrate the specific feature
9-
# on top of the general operations.
10-
####
2+
# This script demonstrates how to use the Tableau Server Client to interact with extracts.
3+
# It explores the different functions that the REST API supports on extracts.
4+
#####
115

126
import argparse
137
import logging

samples/login.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
import argparse
88
import getpass
99
import logging
10+
import os
1011

1112
import tableauserverclient as TSC
12-
import env
13+
14+
15+
def get_env(key):
16+
if key in os.environ:
17+
return os.environ[key]
18+
return None
1319

1420

1521
# If a sample has additional arguments, then it should copy this code and insert them after the call to
@@ -20,13 +26,13 @@ def set_up_and_log_in():
2026
sample_define_common_options(parser)
2127
args = parser.parse_args()
2228
if not args.server:
23-
args.server = env.server
29+
args.server = get_env("SERVER")
2430
if not args.site:
25-
args.site = env.site
31+
args.site = get_env("SITE")
2632
if not args.token_name:
27-
args.token_name = env.token_name
33+
args.token_name = get_env("TOKEN_NAME")
2834
if not args.token_value:
29-
args.token_value = env.token_value
35+
args.token_value = get_env("TOKEN_VALUE")
3036
args.logging_level = "debug"
3137

3238
server = sample_connect_to_server(args)
@@ -79,10 +85,7 @@ def sample_connect_to_server(args):
7985
# Make sure we use an updated version of the rest apis, and pass in our cert handling choice
8086
server = TSC.Server(args.server, use_server_version=True, http_options={"verify": check_ssl_certificate})
8187
server.auth.sign_in(tableau_auth)
82-
server.version = "2.6"
83-
new_site: TSC.SiteItem = TSC.SiteItem("cdnear", content_url=env.site)
84-
server.auth.switch_site(new_site)
85-
print("Logged in successfully")
88+
server.version = "3.19"
8689

8790
return server
8891

samples/publish_datasource.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
import argparse
2222
import logging
2323

24+
import os
2425
import tableauserverclient as TSC
25-
26-
import env
2726
import tableauserverclient.datetime_helpers
2827

2928

29+
def get_env(key):
30+
if key in os.environ:
31+
return os.environ[key]
32+
return None
33+
34+
3035
def main():
3136
parser = argparse.ArgumentParser(description="Publish a datasource to server.")
3237
# Common options; please keep those in sync across all samples
@@ -52,13 +57,13 @@ def main():
5257

5358
args = parser.parse_args()
5459
if not args.server:
55-
args.server = env.server
60+
args.server = get_env("SERVER")
5661
if not args.site:
57-
args.site = env.site
62+
args.site = get_env("SITE")
5863
if not args.token_name:
59-
args.token_name = env.token_name
64+
args.token_name = get_env("TOKEN_NAME")
6065
if not args.token_value:
61-
args.token_value = env.token_value
66+
args.token_value = get_env("TOKEN_VALUE")
6267
args.logging = "debug"
6368
args.file = "C:/dev/tab-samples/5M.tdsx"
6469
args.async_ = True
@@ -118,8 +123,10 @@ def main():
118123
new_datasource, args.file, publish_mode, connection_credentials=new_conn_creds
119124
)
120125
print(
121-
"{}Datasource published. Datasource ID: {}".format(
122-
new_datasource.id, tableauserverclient.datetime_helpers.timestamp()
126+
(
127+
"{}Datasource published. Datasource ID: {}".format(
128+
new_datasource.id, tableauserverclient.datetime_helpers.timestamp()
129+
)
123130
)
124131
)
125132
print("\t\tClosing connection")

samples/set_refresh_schedule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def usage(args):
3838

3939
def make_filter(**kwargs):
4040
options = TSC.RequestOptions()
41-
for item, value in kwargs.items():
41+
for item, value in list(kwargs.items()):
4242
name = getattr(TSC.RequestOptions.Field, item)
4343
options.filter.add(TSC.Filter(name, TSC.RequestOptions.Operator.Equals, value))
4444
return options

samples/update_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def main():
4545
update_function = endpoint.update_connection
4646
resource = endpoint.get_by_id(args.resource_id)
4747
endpoint.populate_connections(resource)
48-
connections = list(filter(lambda x: x.id == args.connection_id, resource.connections))
48+
connections = list([x for x in resource.connections if x.id == args.connection_id])
4949
assert len(connections) == 1
5050
connection = connections[0]
5151
connection.username = args.datasource_username

samples/update_workbook_data_acceleration.py

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0