Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with open(GET_XML_PAGE1, 'rb') as f:
page_1 = f.read().decode('utf-8')
with open(GET_XML_PAGE2, 'rb') as f:
page_2 = f.read().decode('utf-8')
with open(GET_XML_PAGE3, 'rb') as f:
page_3 = f.read().decode('utf-8')
with requests_mock.mock() as m:
# Register Pager with some pages
m.get(self.baseurl + "?pageNumber=1&pageSize=1", text=page_1)
m.get(self.baseurl + "?pageNumber=2&pageSize=1", text=page_2)
m.get(self.baseurl + "?pageNumber=3&pageSize=1", text=page_3)
m.get(self.baseurl + "?pageNumber=1&pageSize=3", text=page_1)
# Starting on page 2 should get 2 out of 3
opts = TSC.RequestOptions(2, 1)
workbooks = list(TSC.Pager(self.server.workbooks, opts))
self.assertTrue(len(workbooks) == 2)
# Check that the workbooks are the 2 we think they should be
wb2, wb3 = workbooks
self.assertEqual(wb2.name, 'Page2Workbook')
self.assertEqual(wb3.name, 'Page3Workbook')
# Starting on 1 with pagesize of 3 should get all 3
opts = TSC.RequestOptions(1, 3)
workbooks = list(TSC.Pager(self.server.workbooks, opts))
self.assertTrue(len(workbooks) == 3)
wb1, wb2, wb3 = workbooks
self.assertEqual(wb1.name, 'Page1Workbook')
self.assertEqual(wb2.name, 'Page2Workbook')
self.assertEqual(wb3.name, 'Page3Workbook')
page_1 = f.read().decode('utf-8')
with open(GET_XML_PAGE2, 'rb') as f:
page_2 = f.read().decode('utf-8')
with open(GET_XML_PAGE3, 'rb') as f:
page_3 = f.read().decode('utf-8')
with requests_mock.mock() as m:
# Register Pager with default request options
m.get(self.baseurl, text=page_1)
# Register Pager with some pages
m.get(self.baseurl + "?pageNumber=1&pageSize=1", text=page_1)
m.get(self.baseurl + "?pageNumber=2&pageSize=1", text=page_2)
m.get(self.baseurl + "?pageNumber=3&pageSize=1", text=page_3)
# No options should get all 3
workbooks = list(TSC.Pager(self.server.workbooks))
self.assertTrue(len(workbooks) == 3)
# Let's check that workbook items aren't duplicates
wb1, wb2, wb3 = workbooks
self.assertEqual(wb1.name, 'Page1Workbook')
self.assertEqual(wb2.name, 'Page2Workbook')
self.assertEqual(wb3.name, 'Page3Workbook')
def deletesite(self):
parser = argparse.ArgumentParser(description='Delete a site')
parser.add_argument('sitename', help='Name of the site to delete') # positional, required argument
# now that we're inside a subcommand, ignore the first TWO argvs, ie the command (tabcmd) and the subcommand (deletesite)
args = parser.parse_args(sys.argv[2:])
# print('Running tabcmd {0}, sitename={1}'.format(sys._getframe().f_code.co_name, args.sitename))
server = self.set_server_from_session_file()
# Iterate through all the sites, deleting any with the name
all_sites = list(TSC.Pager(server.sites))
for s in all_sites:
print(s.name)
if s.name == args.sitename:
dest_server = self.set_server_from_session_file(s.id)
print(server.site_id)
print(dest_server.site_id)
dest_server.sites.delete(s.id)
def removeusers(self):
parser = argparse.ArgumentParser(description='Remove users from a group')
parser.add_argument('groupname', help='Name of the group')
# parser.add_argument('--complete', '-c', required=True, default=True, action='store_true', help='Require all rows to be valid for any change to succeed') # TODO NOTSUPPORTED this isn't currently supported in TSC
parser.add_argument('users', help='File that contains a list of users (one per line) to remove from the group') # TODO change from tabcmd -> making this a positional argument
# now that we're inside a subcommand, ignore the first TWO argvs, ie the command (tabcmd) and the subcommand (removeusers)
args = parser.parse_args(sys.argv[2:])
server = self.set_server_from_session_file()
# Get our groups, iterate to find the one we want # TODO NOTSUPPORTED We should have a filter on groups
for g in TSC.Pager(server.groups):
if g.name == args.groupname:
with open(args.users, newline='') as f:
reader = csv.reader(f)
for row in reader:
# Set-up our filter so we get the user id we need
request_options = TSC.RequestOptions()
request_options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, row[0]))
users = list(TSC.Pager(server.users, request_options))
try:
server.groups.remove_user(g,users[0].id)
except:
pass
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)
# SIGN IN
tableau_auth = TSC.TableauAuth(args.username, password, args.site)
server = TSC.Server(args.server, use_server_version=True)
with server.auth.sign_in(tableau_auth):
endpoint = {
'workbook': server.workbooks,
'datasource': server.datasources,
'view': server.views,
'job': server.jobs,
'project': server.projects,
}.get(args.resource_type)
for resource in TSC.Pager(endpoint.get):
print(resource.id, resource.name)
def deleteusers(self):
parser = argparse.ArgumentParser(description='Delete users The users are read from the given CSV file. The file is a simple list of one user name per line')
parser.add_argument('users', help='File that contains a list of users (one per line) to remove from the site')
parser.add_argument('--username', '-u', required=True, help='Username for server admin')
parser.add_argument('--password', '-p', required=True, help='Password for server admin')
# now that we're inside a subcommand, ignore the first TWO argvs, ie the command (tabcmd) and the subcommand (deletesiteusers)
args = parser.parse_args(sys.argv[2:])
server = self.set_server_from_session_file()
# Keep track of the current site for later so we can get back
current_site = server.sites.get_by_id(server.site_id)
current_server = server
# Get all the sites
all_sites = list(TSC.Pager(server.sites))
# Iterate thru all sites, deleting each user from each StopIteration
for site in all_sites:
# Login to the site
sn = site.name
if site.name == 'Default': # fix up for default site
sn = ""
self.login_and_save_session_file(server._server_address, sn, args.username, args.password)
server = self.set_server_from_session_file()
with open(args.users, newline='') as f:
reader = csv.reader(f)
for row in reader:
# Set-up our filter so we get the user id we need
request_options = TSC.RequestOptions()
# For other users, this only prints the status of the site they belong to
print("Materialized views is enabled on sites:")
# only server admins can get all the sites in the server
# other users can only get the site they are in
for site in TSC.Pager(server.sites):
if site.materialized_views_mode != "disable":
enabled_sites.add(site)
print("Site name: {}".format(site.name))
print('\n')
print("Materialized views is enabled on workbooks:")
# Individual workbooks can be enabled only when the sites they belong to are enabled too
for site in enabled_sites:
site_auth = TSC.TableauAuth(args.username, password, site.content_url)
with server.auth.sign_in(site_auth):
for workbook in TSC.Pager(server.workbooks):
if workbook.materialized_views_config['materialized_views_enabled']:
print("Workbook: {} from site: {}".format(workbook.name, site.name))
def show_materialized_views_status(args, password, site_content_url):
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_content_url)
server = TSC.Server(args.server, use_server_version=True)
enabled_sites = set()
with server.auth.sign_in(tableau_auth):
# For server admin, this will prints all the materialized views enabled sites
# For other users, this only prints the status of the site they belong to
print("Materialized views is enabled on sites:")
# only server admins can get all the sites in the server
# other users can only get the site they are in
for site in TSC.Pager(server.sites):
if site.materialized_views_mode != "disable":
enabled_sites.add(site)
print("Site name: {}".format(site.name))
print('\n')
print("Materialized views is enabled on workbooks:")
# Individual workbooks can be enabled only when the sites they belong to are enabled too
for site in enabled_sites:
site_auth = TSC.TableauAuth(args.username, password, site.content_url)
with server.auth.sign_in(site_auth):
for workbook in TSC.Pager(server.workbooks):
if workbook.materialized_views_config['materialized_views_enabled']:
print("Workbook: {} from site: {}".format(workbook.name, site.name))
def update_workbooks_by_names(name_list, server, materialized_views_config):
workbook_names = sanitize_workbook_list(name_list, "name")
for workbook_name in workbook_names:
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals,
workbook_name.rstrip()))
workbooks = list(TSC.Pager(server.workbooks, req_option))
if len(workbooks) == 0:
print("Cannot find workbook name: {}, each line should only contain one workbook name"
.format(workbook_name))
for workbook in workbooks:
workbook.materialized_views_config = materialized_views_config
server.workbooks.update(workbook)
print("Updated materialized views settings for workbook: {}".format(workbook.name))
print('\n')
def deletegroup(self):
parser = argparse.ArgumentParser(description='Delete a group')
parser.add_argument('groupname', help='Name of the group to delete') # positional, required argument
# now that we're inside a subcommand, ignore the first TWO argvs, ie the command (tabcmd) and the subcommand (deletegroup)
args = parser.parse_args(sys.argv[2:])
# print('Running tabcmd {0}, groupname={1}'.format(sys._getframe().f_code.co_name, args.groupname))
server = self.set_server_from_session_file()
# Iterate through all the groups, deleting any with the name
all_groups = list(TSC.Pager(server.groups))
for g in all_groups:
if g.name == args.groupname:
server.groups.delete(g.id)