8000 Merge pull request #21 from tableau/development · acpana/server-client-python@dbae807 · GitHub
[go: up one dir, main page]

Skip to content

Commit dbae807

Browse files
authored
Merge pull request tableau#21 from tableau/development
Preparing for renaming
2 parents 444c2ea + 9789f46 commit dbae807

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+747
-619
lines changed

samples/explore_datasource.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,26 @@
1313
import tableauserverapi as TSA
1414
import os.path
1515
import argparse
16+
import getpass
1617
import logging
1718

1819
parser = argparse.ArgumentParser(description='Explore datasource functions supported by the Server API.')
19-
parser.add_argument('server', help='server address')
20-
parser.add_argument('username', help='username to sign into server')
21-
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')
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')
20+
parser.add_argument('--server', '-s', required=True, help='server address')
21+
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
22+
parser.add_argument('--publish', '-p', metavar='FILEPATH', help='path to datasource to publish')
23+
parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded datasource')
24+
parser.add_argument('--logging-level', '-l', 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+
password = getpass.getpass("Password: ")
29+
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)
3233

3334
##### SIGN IN #####
34-
tableau_auth = TSA.TableauAuth(args.username, args.password)
35+
tableau_auth = TSA.TableauAuth(args.username, password)
3536
server = TSA.Server(args.server)
3637
with server.auth.sign_in(tableau_auth):
3738

@@ -59,7 +60,7 @@
5960

6061
# Populate connections
6162
server.datasources.populate_connections(sample_datasource)
62-
print("\nConnections for datasource: ")
63+
print("\nConnections for {}: ".format(sample_datasource.name))
6364
print(["{0}({1})".format(connection.id, connection.datasource_name)
6465
for connection in sample_datasource.connections])
6566

samples/explore_workbook.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@
1313
import os.path
1414
import copy
1515
import argparse
16+
import getpass
1617
import logging
1718

1819
parser = argparse.ArgumentParser(description='Explore workbook functions supported by the Server API.')
19-
parser.add_argument('server', help='server address')
20-
parser.add_argument('username', help='username to sign into server')
21-
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')
24-
parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to workbook to publish')
25-
parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded workbook')
26-
parser.add_argument('-preview-image', '-i', metavar='FILEPATH', help='path to save populated preview image')
20+
parser.add_argument('--server', '-s', required=True, help='server address')
21+
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
22+
parser.add_argument('--publish', '-p', metavar='FILEPATH', help='path to workbook to publish')
23+
parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded workbook')
24+
parser.add_argument('--preview-image', '-i', metavar='FILEPATH', help='path to save populated preview image')
25+
parser.add_argument('--logging-level', '-l', 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+
password = getpass.getpass("Password: ")
30+
31+
# Set logging level based on user input, or error by default
32+
logging_level = getattr(logging, args.logging_level.upper())
33+
logging.basicConfig(level=logging_level)
3334

3435
##### SIGN IN #####
35-
tableau_auth = TSA.TableauAuth(args.username, args.password)
36+
tableau_auth = TSA.TableauAuth(args.username, password)
3637
server = TSA.Server(args.server)
3738
with server.auth.sign_in(tableau_auth):
3839

@@ -59,12 +60,12 @@
5960

6061
# Populate views
6162
server.workbooks.populate_views(sample_workbook)
62-
print("\nName of views in workbook {}: ".format(sample_workbook.name))
63+
print("\nName of views in {}: ".format(sample_workbook.name))
6364
print([view.name for view in sample_workbook.views])
6465

6566
# Populate connections
6667
server.workbooks.populate_connections(sample_workbook)
67-
print("\nConnections for workbook: ")
68+
print("\nConnections for {}: ".format(sample_workbook.name))
6869
print(["{0}({1})".format(connection.id, connection.datasource_name)
6970
for connection in sample_workbook.connections])
7071

samples/move_workbook_projects.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@
99

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

1615
parser = argparse.ArgumentParser(description='Move one workbook from the default project to another.')
17-
parser.add_argument('server', help='server address')
18-
parser.add_argument('username', help='username to sign into server')
19-
parser.add_argument('password', help='password to sign into server')
20-
parser.add_argument('workbook_name', help='name of workbook to move')
21-
parser.add_argument('destination_project', help='name of project to move workbook into')
16+
parser.add_argument('--server', '-s', required=True, help='server address')
17+
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
18+
parser.add_argument('--workbook-name', '-w', required=True, help='name of workbook to move')
19+
parser.add_argument('--destination-project', '-d', required=True, help='name of project to move workbook into')
20+
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
21+
help='desired logging level (set to error by default)')
2222
args = parser.parse_args()
2323

24+
password = getpass.getpass("Password: ")
25+
26+
# Set logging level based on user input, or error by default
27+
logging_level = getattr(logging, args.logging_level.upper())
28+
logging.basicConfig(level=logging_level)
29+
2430
# Step 1: Sign in to server
25-
tableau_auth = TSA.TableauAuth(args.username, args.password)
31+
tableau_auth = TSA.TableauAuth(args.username, password)
2632
server = TSA.Server(args.server)
2733
with server.auth.sign_in(tableau_auth):
2834
# Step 2: Query workbook to move
@@ -35,11 +41,16 @@
3541
pagination_info, all_projects = server.projects.get()
3642
dest_project = next((project for project in all_projects if project.name == args.destination_project), None)
3743

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))
44+
if dest_project is not None:
45+
# Step 4: Update workbook with new project id
46+
if all_workbooks:
47+
print("Old project: {}".format(all_workbooks[0].project_name))
48+
all_workbooks[0].project_id = dest_project.id
49+
target_workbook = server.workbooks.update(all_workbooks[0])
50+
print("New project: {}".format(target_workbook.project_name))
51+
else:
52+
error = "No workbook named {} found.".format(args.workbook_name)
53+
raise LookupError(error)
4454
else:
45-
print('No workbook named {} found.'.format(args.workbook_name))
55+
error = "No project named {} found.".format(args.destination_project)
56+
raise LookupError(error)

samples/move_workbook_sites.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,28 @@
1111
import shutil
1212
import argparse
1313
import tempfile
14+
import getpass
15+
import logging
1416

1517
parser = argparse.ArgumentParser(description="Move one workbook from the"
1618
"default project of the default site to"
1719
"the default project of another site.")
18-
parser.add_argument('server', help='server address')
19-
parser.add_argument('username', help='username to sign into server')
20-
parser.add_argument('password', help='password to sign into server')
21-
parser.add_argument('workbook_name', help='name of workbook to move')
22-
parser.add_argument('destination_site', help='name of site to move workbook into')
20+
parser.add_argument('--server', '-s', required=True, help='server address')
21+
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
22+
parser.add_argument('--workbook-name', '-w', required=True, help='name of workbook to move')
23+
parser.add_argument('--destination-site', '-d', required=True, help='name of site to move workbook into')
24+
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
25+
help='desired logging level (set to error by default)')
2326
args = parser.parse_args()
2427

28+
password = getpass.getpass("Password: ")
29+
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+
2534
# Step 1: Sign in to both sites on server
26-
tableau_auth = TSA.TableauAuth(args.username, args.password)
35+
tableau_auth = TSA.TableauAuth(args.username, password)
2736

2837
source_server = TSA.Server(args.server)
2938
dest_server = TSA.Server(args.server)
@@ -43,8 +52,18 @@
4352
try:
4453
workbook_path = source_server.workbooks.download(all_workbooks[0].id, tmpdir)
4554

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

5069
# Step 5: Find destination site's default project
@@ -56,6 +75,10 @@
5675
new_workbook = TSA.WorkbookItem(name=args.workbook_name, project_id=target_project.id)
5776
new_workbook = dest_server.workbooks.publish(new_workbook, workbook_path,
5877
mode=dest_server.PublishMode.Overwrite)
78+
print("Successfully moved {0} ({1})".format(new_workbook.name, new_workbook.id))
79+
else:
80+
error = "The default project could not be found."
81+
raise LookupError(error)
5982

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

samples/publish_workbook.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,25 @@
1616

1717
import tableauserverapi as TSA
1818
import argparse
19+
import getpass
20+
import logging
1921

2022
parser = argparse.ArgumentParser(description='Publish a workbook to server.')
21-
parser.add_argument('server', help='server address')
22-
parser.add_argument('username', help='username to sign into server')
23-
parser.add_argument('password', help='password to sign into server')
24-
parser.add_argument('filepath', help='filepath to the workbook to publish')
23+
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('--filepath', '-f', required=True, help='filepath to the workbook to publish')
26+
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
27+
help='desired logging level (set to error by default)')
2528
args = parser.parse_args()
2629

30+
password = getpass.getpass("Password: ")
31+
32+
# Set logging level based on user input, or error by default
33+
logging_level = getattr(logging, args.logging_level.upper())
34+
logging.basicConfig(level=logging_level)
35+
2736
# Step 1: Sign in to server.
28-
tableau_auth = TSA.TableauAuth(args.username, args.password)
37+
tableau_auth = TSA.TableauAuth(args.username, password)
2938
server = TSA.Server(args.server)
3039
with server.auth.sign_in(tableau_auth):
3140

@@ -39,4 +48,5 @@
3948
new_workbook = server.workbooks.publish(new_workbook, args.filepath, server.PublishMode.Overwrite)
4049
print("Workbook published. ID: {0}".format(new_workbook.id))
4150
else:
42-
print("The default project could not be found.")
51+
error = "The default project could not be found."
52+
raise LookupError(error)

samples/set_http_options.py

Lines changed: 10 additions & 3 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

15-
parser = argparse.ArgumentParser(description='List workbooks on site.')
16-
parser.add_argument('server', help='server address')
17-
parser.add_argument('username', help='username to sign into server')
16+
parser = argparse.ArgumentParser(description='List workbooks on site, with option set to ignore SSL verification.')
17+
parser.add_argument('--server', '-s', required=True, help='server address')
18+
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
19+
parser.add_argument('--logging-level', '-l', 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)

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
description='A Python module for working with the Tableau Server REST API.',
1616
test_suite='test',
1717
install_requires=[
18-
'requests>=2.11,<2.12.0a0',
18+
'requests>=2.11,<2.12.0a0'
19+
],
20+
tests_require=[
1921
'requests-mock>=1.0,<1.1a0'
2022
]
2123
)

tableauserverapi/models/connection_item.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,15 @@
44

55
class ConnectionItem(object):
66
def __init__(self):
7-
self._id = None
8-
self._type = None
97
self._datasource_id = None
108
self._datasource_name = None
9+
self._id = None
10+
self._connection_type = None
11+
self.embed_password = None
12+
self.password = None
1113
self.server_address = None
1214
self.server_port = None
1315
self.username = None
14-
self.password = None
15-
self.embed_password = None
16-
17-
@property
18-
def id(self):
19-
return self._id
20-
21-
@property
22-
def type(self):
23-
return self._type
2416

2517
@property
2618
def datasource_id(self):
@@ -30,6 +22,14 @@ def datasource_id(self):
3022
def datasource_name(self):
3123
return self._datasource_name
3224

25+
@property
26+
def id(self):
27+
return self._id
28+
29+
@property
30+
def connection_type(self):
31+
return self._connection_type
32+
3333
@classmethod
3434
def from_response(cls, resp):
3535
all_connection_items = list()
@@ -38,7 +38,7 @@ def from_response(cls, resp):
3838
for connection_xml in all_connection_xml:
3939
connection_item = cls()
4040
connection_item._id = connection_xml.get('id', None)
41-
connection_item._type = connection_xml.get('type', None)
41+
connection_item._connection_type = connection_xml.get('type', None)
4242
connection_item.server_address = connection_xml.get('serverAddress', None)
4343
connection_item.server_port = connection_xml.get('serverPort', None)
4444
connection_item.username = connection_xml.get('userName', None)

0 commit comments

Comments
 (0)
0