forked from bryanthowell-tableau/tableau_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableau_base.py
447 lines (411 loc) · 15.7 KB
/
tableau_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import random
from tableau_exceptions import *
from logger import Logger
import re
from StringIO import StringIO
import xml.etree.cElementTree as etree
class TableauBase(object):
def __init__(self):
# In reverse order to work down until the acceptable version is found on the server, through login process
self.supported_versions = (u'2018.3', u'2018.2', u'2018.1', u"10.5", u"10.4", u"10.3", u"10.2", u"10.1", u"10.0", u"9.3", u"9.2", u"9.1", u"9.0")
self.logger = None
self.luid_pattern = r"[0-9a-fA-F]*-[0-9a-fA-F]*-[0-9a-fA-F]*-[0-9a-fA-F]*-[0-9a-fA-F]*"
# Defaults, will get updated with each update. Overwritten by set_tableau_server_version
self.version = u"10.5"
self.api_version = u"2.8"
self.tableau_namespace = u'http://tableau.com/api'
self.ns_map = {'t': 'http://tableau.com/api'}
self.ns_prefix = '{' + self.ns_map['t'] + '}'
etree.register_namespace(u't', self.ns_map['t'])
self.site_roles = (
u'Interactor',
u'Publisher',
u'SiteAdministrator',
u'Unlicensed',
u'UnlicensedWithPublish', # This was sunset at some point
u'Viewer',
u'ViewerWithPublish',
u'ServerAdministrator',
u'ReadOnly',
u'Explorer',
u'ExplorerCanPublish',
u'SiteAdministratorExplorer',
u'Creator',
u'SiteAdministratorCreator'
)
server_content_roles_2_0 = {
u"project": (
u'Viewer',
u'Interactor',
u'Editor',
u'Data Source Connector',
u'Data Source Editor',
u'Publisher',
u'Project Leader'
),
u"workbook": (
u'Viewer',
u'Interactor',
u'Editor'
),
u"datasource": (
u'Data Source Connector',
u'Data Source Editor'
)
}
server_content_roles_2_1 = {
u"project": (
u'Viewer',
u'Publisher',
u'Project Leader'
),
u"workbook": (
u'Viewer',
u'Interactor',
u'Editor'
),
u"datasource": (
u'Editor',
u'Connector'
)
}
self.server_content_roles = {
u"2.0": server_content_roles_2_0,
u"2.1": server_content_roles_2_1,
u"2.2": server_content_roles_2_1,
u"2.3": server_content_roles_2_1,
u"2.4": server_content_roles_2_1,
u"2.5": server_content_roles_2_1,
u"2.6": server_content_roles_2_1,
u"2.7": server_content_roles_2_1,
u"2.8": server_content_roles_2_1,
u'3.0': server_content_roles_2_1,
u'3.1': server_content_roles_2_1,
u'3.2': server_content_roles_2_1
}
self.server_to_rest_capability_map = {
u'Add Comment': u'AddComment',
u'Move': u'ChangeHierarchy',
u'Set Permissions': u'ChangePermissions',
u'Connect': u'Connect',
u'Delete': u'Delete',
u'View Summary Data': u'ExportData',
u'Download Summary Data': u'ExportData',
u'Export Image': u'ExportImage',
u'Download Image/PDF': u'ExportImage',
u'Download': u'ExportXml',
u'Download Workbook/Save As': u'ExportXml',
u'Filter': u'Filter',
u'Project Leader': u'ProjectLeader',
u'View': u'Read',
u'Share Customized': u'ShareView',
u'View Comments': u'ViewComments',
u'View Underlying Data': u'ViewUnderlyingData',
u'Download Full Data' : u'ViewUnderlyingData',
u'Web Edit': u'WebAuthoring',
u'Save': u'Write',
u'Inherited Project Leader': u'InheritedProjectLeader',
u'all': u'all' # special command to do everything
}
capabilities_2_0 = {
u"project": (
u'AddComment',
u'ChangeHierarchy',
u'ChangePermissions',
u'Connect',
u'Delete',
u'ExportData',
u'ExportImage',
u'ExportXml',
u'Filter',
u'ProjectLeader',
u'Read',
u'ShareView',
u'ViewComments',
u'ViewUnderlyingData',
u'WebAuthoring',
u'Write'
),
u"workbook": (
u'AddComment',
u'ChangeHierarchy',
u'ChangePermissions',
u'Delete',
u'ExportData',
u'ExportImage',
u'ExportXml',
u'Filter',
u'Read',
u'ShareView',
u'ViewComments',
u'ViewUnderlyingData',
u'WebAuthoring',
u'Write'
),
u"datasource": (
u'ChangePermissions',
u'Connect',
u'Delete',
u'ExportXml',
u'Read',
u'Write'
)
}
capabilities_2_1 = {
u"project": (u"Read", u"Write", u'ProjectLeader'),
u"workbook": (
u'Read',
u'ExportImage',
u'ExportData',
u'ViewComments',
u'AddComment',
u'Filter',
u'ViewUnderlyingData',
u'ShareView',
u'WebAuthoring',
u'Write',
u'ExportXml',
u'ChangeHierarchy',
u'Delete',
u'ChangePermissions',
),
u"datasource": (
u'Read',
u'Connect',
u'Write',
u'ExportXml',
u'Delete',
u'ChangePermissions'
)
}
capabilities_2_8 = {
u"project": (u"Read", u"Write", u'ProjectLeader', u'InheritedProjectLeader'),
u"workbook": (
u'Read',
u'ExportImage',
u'ExportData',
u'ViewComments',
u'AddComment',
u'Filter',
u'ViewUnderlyingData',
u'ShareView',
u'WebAuthoring',
u'Write',
u'ExportXml',
u'ChangeHierarchy',
u'Delete',
u'ChangePermissions',
),
u"datasource": (
u'Read',
u'Connect',
u'Write',
u'ExportXml',
u'Delete',
u'ChangePermissions'
)
}
self.available_capabilities = {
u"2.0": capabilities_2_0,
u"2.1": capabilities_2_1,
u"2.2": capabilities_2_1,
u'2.3': capabilities_2_1,
u'2.4': capabilities_2_1,
u'2.5': capabilities_2_1,
u'2.6': capabilities_2_1,
u'2.7': capabilities_2_1,
u'2.8': capabilities_2_8,
u'3.0': capabilities_2_8,
u'3.1': capabilities_2_8,
u'3.2': capabilities_2_8
}
self.datasource_class_map = {
u"Actian Vectorwise": u"vectorwise",
u"Amazon EMR": u"awshadoophive",
u"Amazon Redshift": u"redshift",
u"Aster Database": u"asterncluster",
u"Cloudera Hadoop": u"hadoophive",
u"DataStax Enterprise": u"datastax",
u"EXASolution": u"exasolution",
u"Firebird": u"firebird",
u"Generic ODBC": u"genericodbc",
u"Google Analytics": u"google-analytics",
u"Google BigQuery": u"bigquery",
u"Hortonworks Hadooop Hive": u"hortonworkshadoophive",
u"HP Vertica": u"vertica",
u"IBM BigInsights": u"bigsql",
u"IBM DB2": u"db2",
u"JavaScript Connector": u"jsconnector",
u"MapR Hadoop Hive": u"maprhadoophive",
u"MarkLogic": u"marklogic",
u"Microsoft Access": u"msaccess",
u"Microsoft Analysis Services": u"msolap",
u"Microsoft Excel": u"excel-direct",
u"Microsoft PowerPivot": u"powerpivot",
u"Microsoft SQL Server": u"sqlserver",
u"MySQL": u"mysql",
u"IBM Netezza": u"netezza",
u"OData": u"odata",
u"Oracle": u"oracle",
u"Oracle Essbase": u"essbase",
u"ParAccel": u"paraccel",
u"Pivotal Greenplum": u"greenplum",
u"PostgreSQL": u"postgres",
u"Progress OpenEdge": u"progressopenedge",
u"SAP HANA": u"saphana",
u"SAP Netweaver Business Warehouse": u"sapbw",
u"SAP Sybase ASE": u"sybasease",
u"SAP Sybase IQ": u"sybaseiq",
u"Salesforce": u"salesforce",
u"Spark SQL": u"spark",
u"Splunk": u"splunk",
u"Statistical File": u"",
u"Tableau Data Extract": u"dataengine",
u"Teradata": u"teradata",
u"Text file": u"textscan",
u"Hyper": u'hyper'
}
self.permissionable_objects = (u'datasource', u'project', u'workbook')
def set_tableau_server_version(self, tableau_server_version):
"""
:type tableau_server_version: unicode
"""
# API Versioning (starting in 9.2)
if unicode(tableau_server_version)in [u"9.2", u"9.3", u"10.0", u"10.1", u"10.2", u"10.3", u"10.4", u"10.5",
u'2018.1', u'2018.2', u'2018.3']:
if unicode(tableau_server_version) == u"9.2":
self.api_version = u"2.1"
elif unicode(tableau_server_version) == u"9.3":
self.api_version = u"2.2"
elif unicode(tableau_server_version) == u'10.0':
self.api_version = u'2.3'
elif unicode(tableau_server_version) == u'10.1':
self.api_version = u'2.4'
elif unicode(tableau_server_version) == u'10.2':
self.api_version = u'2.5'
elif unicode(tableau_server_version) == u'10.3':
self.api_version = u'2.6'
elif unicode(tableau_server_version) == u'10.4':
self.api_version = u'2.7'
elif unicode(tableau_server_version) == u'10.5':
self.api_version = u'2.8'
elif unicode(tableau_server_version) == u'2018.1':
self.api_version = u'3.0'
elif unicode(tableau_server_version) == u'2018.2':
self.api_version = u'3.1'
elif unicode(tableau_server_version) == u'2018.3':
self.api_version = u'3.2'
self.tableau_namespace = u'http://tableau.com/api'
self.ns_map = {'t': 'http://tableau.com/api'}
self.version = tableau_server_version
self.ns_prefix = '{' + self.ns_map['t'] + '}'
return self.api_version
elif unicode(tableau_server_version) in [u"9.0", u"9.1"]:
self.api_version = u"2.0"
self.tableau_namespace = u'http://tableausoftware.com/api'
self.ns_map = {'t': 'http://tableausoftware.com/api'}
self.version = tableau_server_version
self.ns_prefix = '{' + self.ns_map['t'] + '}'
return self.api_version
else:
raise InvalidOptionException(u"Please specify tableau_server_version as a string. '9.0' or '9.2' etc...")
# Logging Methods
def enable_logging(self, logger_obj):
"""
:type logger_obj: Logger
:return:
"""
if isinstance(logger_obj, Logger):
self.logger = logger_obj
def log(self, l):
if self.logger is not None:
self.logger.log(l)
def log_debug(self, l):
if self.logger is not None:
self.logger.log_debug(l)
def start_log_block(self):
if self.logger is not None:
self.logger.start_log_block()
def end_log_block(self):
if self.logger is not None:
self.logger.end_log_block()
def log_uri(self, uri, verb):
if self.logger is not None:
self.logger.log_uri(verb, uri)
def log_xml_request(self, xml, verb):
if self.logger is not None:
self.logger.log_xml_request(verb, xml)
# Method to handle single str or list and return a list
@staticmethod
def to_list(x):
if isinstance(x, (str, unicode)):
l = [x] # Make single into a collection
else:
l = x
return l
# Method to read file in x MB chunks for upload, 10 MB by default (1024 bytes = KB, * 1024 = MB, * 10)
@staticmethod
def read_file_in_chunks(file_object, chunk_size=(1024 * 1024 * 10)):
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
# You must generate a boundary string that is used both in the headers and the generated request that you post.
# This builds a simple 30 hex digit string
@staticmethod
def generate_boundary_string():
"""
:return: unicode
"""
random_digits = [random.SystemRandom().choice('0123456789abcdef') for n in xrange(30)]
s = u"".join(random_digits)
return s
# URI is different form actual URL you need to load a particular view in iframe
@staticmethod
def convert_view_content_url_to_embed_url(content_url):
"""
:type content_url: unicode
:return: unicode
"""
split_url = content_url.split('/')
return u'views/{}/{}'.format(split_url[0], split_url[2])
# Generic method for XML lists for the "query" actions to name -> id dict
@staticmethod
def convert_xml_list_to_name_id_dict(lxml_obj):
"""
:type lxml_obj: etree.Element
:return: dict
"""
d = {}
for element in lxml_obj:
e_id = element.get("id")
# If list is collection, have to run one deeper
if e_id is None:
for list_element in element:
e_id = list_element.get("id")
name = list_element.get("name")
d[name] = e_id
else:
name = element.get("name")
d[name] = e_id
return d
# Convert a permission
def convert_server_permission_name_to_rest_permission(self, permission_name):
if permission_name in self.server_to_rest_capability_map:
return self.server_to_rest_capability_map[permission_name]
else:
raise InvalidOptionException(u'{} is not a permission name on the Tableau Server'.format(permission_name))
# 32 hex characters with 4 dashes
def is_luid(self, val):
"""
:type val: unicode
:return: bool
"""
if len(val) == 36:
if re.match(self.luid_pattern, val) is not None:
return True
else:
return False
else:
return False