|
1
|
|
|
# package tto ensure communication with Tableau Server |
|
2
|
|
|
import tableauserverclient as tsc |
|
3
|
|
|
|
|
4
|
|
|
from pathlib import Path |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class TableauServerCommunicator(): |
|
8
|
|
|
req_opts = tsc.RequestOptions(pagesize=1000) |
|
9
|
|
|
tableau_server = None |
|
10
|
|
|
|
|
11
|
|
|
def connect_to_tableau_server(self, local_logger, timmer, connection_details): |
|
12
|
|
|
timmer.start() |
|
13
|
|
|
local_logger.debug('I am about to connect to the Tableau Server with URL: ' |
|
14
|
|
|
+ connection_details['Tableau Server'] + ' and the Site ' |
|
15
|
|
|
+ connection_details['Tableau Site'] + ' using the Username ' |
|
16
|
|
|
+ connection_details['Username']) |
|
17
|
|
|
self.tableau_server = tsc.Server(server_address = connection_details['Tableau Server'], |
|
18
|
|
|
use_server_version = True) |
|
19
|
|
|
tableau_auth = tsc.TableauAuth(username = connection_details['Username'], |
|
20
|
|
|
password = connection_details['Password'], |
|
21
|
|
|
site_id = connection_details['Tableau Site'], |
|
22
|
|
|
user_id_to_impersonate = None) |
|
23
|
|
|
self.tableau_server.auth.sign_in(tableau_auth) |
|
24
|
|
|
local_logger.debug('Connection to the Tableau Server has been established successfully!') |
|
25
|
|
|
timmer.stop() |
|
26
|
|
|
|
|
27
|
|
|
def disconnect_from_tableau_server(self, local_logger, timmer): |
|
28
|
|
|
timmer.start() |
|
29
|
|
|
self.tableau_server.auth.sign_out() |
|
30
|
|
|
local_logger.debug('The connection to the Tableau Server has been terminated!') |
|
31
|
|
|
timmer.stop() |
|
32
|
|
|
|
|
33
|
|
|
@staticmethod |
|
34
|
|
|
def is_publishing_possible(self, local_logger, relevant_project_name, relevant_project_ids): |
|
35
|
|
|
publish_possible = False |
|
36
|
|
|
int_found_projects = len(relevant_project_ids) |
|
37
|
|
|
if int_found_projects == 0: |
|
38
|
|
|
local_logger.error('No project with provided name "' + relevant_project_name |
|
39
|
|
|
+ '" has been found') |
|
40
|
|
|
local_logger.debug('No publishing action will take place!') |
|
41
|
|
|
local_logger.debug('Check your input parameter values for accuracy and try again!') |
|
42
|
|
|
elif int_found_projects > 1: |
|
43
|
|
|
local_logger.error(f'There are {str(int_found_projects)} projects with provided name "' |
|
44
|
|
|
+ relevant_project_name + ' but a unique identifier is expected') |
|
45
|
|
|
local_logger.debug('No publishing action will take place!') |
|
46
|
|
|
local_logger.info('Check your input parameter values for accuracy and try again!') |
|
47
|
|
|
else: |
|
48
|
|
|
publish_possible = True |
|
49
|
|
|
local_logger.info(f'A single project identifier was found "' + relevant_project_ids[0] |
|
50
|
|
|
+ '" so I will proceed with publishing provided file there') |
|
51
|
|
|
local_logger.info('Stay tuned for the confirmation') |
|
52
|
|
|
return publish_possible |
|
53
|
|
|
|
|
54
|
|
|
def load_tableau_project_ids(self, local_logger, timmer, relevant_projects_to_filter, |
|
55
|
|
|
str_filtering_type): |
|
56
|
|
|
timmer.start() |
|
57
|
|
|
project_items, pagination_item = self.tableau_server.projects.get(req_options=self.req_opts) |
|
58
|
|
|
local_logger.info('Reading list of all projects available has been completed') |
|
59
|
|
|
local_logger.info(str(len(project_items)) + ' projects were found') |
|
60
|
|
|
timmer.stop() |
|
61
|
|
|
timmer.start() |
|
62
|
|
|
dictionary_project_ids = [] |
|
63
|
|
|
project_counter = 0 |
|
64
|
|
|
for project_current in project_items: |
|
65
|
|
|
relevant = 0 |
|
66
|
|
|
if str_filtering_type == 'JustOnesMentioned': |
|
67
|
|
|
if project_current.name.replace(chr(8211), chr(45)) in relevant_projects_to_filter: |
|
68
|
|
|
relevant = 1 |
|
69
|
|
|
elif str_filtering_type == 'OnesMentionedMarked': |
|
70
|
|
|
relevant = 2 |
|
71
|
|
|
elif str_filtering_type == 'All': |
|
72
|
|
|
relevant = 99 |
|
73
|
|
|
if relevant >= 1: |
|
74
|
|
|
dictionary_project_ids.append(project_counter) |
|
75
|
|
|
dictionary_project_ids[project_counter] = project_current.id |
|
76
|
|
|
project_counter = project_counter + 1 |
|
77
|
|
|
local_logger.info('Retaining the projects according to filtering type provided ("' |
|
78
|
|
|
+ str_filtering_type + '") has been completed') |
|
79
|
|
|
local_logger.info(str(len(dictionary_project_ids)) + ' projects were retained') |
|
80
|
|
|
timmer.stop() |
|
81
|
|
|
return dictionary_project_ids |
|
82
|
|
|
|
|
83
|
|
|
def publish_data_source_to_tableau_server(self, local_logger, timmer, publish_details): |
|
84
|
|
|
timmer.start() |
|
85
|
|
|
local_logger.info('About to start publishing') |
|
86
|
|
|
data_source_name = Path(publish_details['Tableau Extract File']).name\ |
|
87
|
|
|
.replace('.hyper', '') + " Extract" |
|
88
|
|
|
project_data_source = tsc.DatasourceItem(project_id = publish_details['Project ID'], |
|
89
|
|
|
name = data_source_name) |
|
90
|
|
|
self.tableau_server.datasources.publish(project_data_source, |
|
91
|
|
|
publish_details['Tableau Extract File'], |
|
92
|
|
|
publish_details['Publishing Mode'], |
|
93
|
|
|
) |
|
94
|
|
|
local_logger.info('Publishing has finished with success!') |
|
95
|
|
|
timmer.stop() |
|
96
|
|
|
|