forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasource_item.py
More file actions
474 lines (402 loc) · 15.5 KB
/
datasource_item.py
File metadata and controls
474 lines (402 loc) · 15.5 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import copy
import datetime
import xml.etree.ElementTree as ET
from typing import Optional
from defusedxml.ElementTree import fromstring
from tableauserverclient.datetime_helpers import parse_datetime
from tableauserverclient.models.connection_item import ConnectionItem
from tableauserverclient.models.exceptions import UnpopulatedPropertyError
from tableauserverclient.models.permissions_item import PermissionsRule
from tableauserverclient.models.property_decorators import (
property_not_nullable,
property_is_boolean,
property_is_enum,
)
from tableauserverclient.models.revision_item import RevisionItem
from tableauserverclient.models.tag_item import TagItem
class DatasourceItem:
"""
Represents a Tableau datasource item.
Parameters
----------
project_id : Optional[str]
The project ID that the datasource belongs to.
name : Optional[str]
The name of the datasource.
Attributes
----------
ask_data_enablement : Optional[str]
Determines if a data source allows use of Ask Data. The value can be
TSC.DatasourceItem.AskDataEnablement.Enabled,
TSC.DatasourceItem.AskDataEnablement.Disabled, or
TSC.DatasourceItem.AskDataEnablement.SiteDefault. If no setting is
specified, it will default to SiteDefault. See REST API Publish
Datasource for more information about ask_data_enablement.
connections : list[ConnectionItem]
The list of data connections (ConnectionItem) for the specified data
source. You must first call the populate_connections method to access
this data. See the ConnectionItem class.
content_url : Optional[str]
The name of the data source as it would appear in a URL.
created_at : Optional[datetime.datetime]
The time the data source was created.
certified : Optional[bool]
A Boolean value that indicates whether the data source is certified.
certification_note : Optional[str]
The optional note that describes the certified data source.
datasource_type : Optional[str]
The type of data source, for example, sqlserver or excel-direct.
description : Optional[str]
The description for the data source.
encrypt_extracts : Optional[bool]
A Boolean value to determine if a datasource should be encrypted or not.
See Extract and Encryption Methods for more information.
has_extracts : Optional[bool]
A Boolean value that indicates whether the datasource has extracts.
id : Optional[str]
The identifier for the data source. You need this value to query a
specific data source or to delete a data source with the get_by_id and
delete methods.
name : Optional[str]
The name of the data source. If not specified, the name of the published
data source file is used.
owner_id : Optional[str]
The identifier of the owner of the data source.
project_id : Optional[str]
The identifier of the project associated with the data source. You must
provide this identifier when you create an instance of a DatasourceItem.
project_name : Optional[str]
The name of the project associated with the data source.
tags : Optional[set[str]]
The tags (list of strings) that have been added to the data source.
updated_at : Optional[datetime.datetime]
The date and time when the data source was last updated.
use_remote_query_agent : Optional[bool]
A Boolean value that indicates whether to allow or disallow your Tableau
Cloud site to use Tableau Bridge clients. Bridge allows you to maintain
data sources with live connections to supported on-premises data
sources. See Configure and Manage the Bridge Client Pool for more
information.
webpage_url : Optional[str]
The url of the datasource as displayed in browsers.
"""
class AskDataEnablement:
Enabled = "Enabled"
Disabled = "Disabled"
SiteDefault = "SiteDefault"
def __repr__(self):
return "<Datasource {} '{}' ({} parent={} >".format(
self._id,
self.name,
self.description or "No Description",
self.project_id,
)
def __init__(self, project_id: Optional[str] = None, name: Optional[str] = None) -> None:
self._ask_data_enablement: Optional[str] = None
self._certified: Optional[bool] = None
self._certification_note: Optional[str] = None
self._connections: Optional[list[ConnectionItem]] = None
self._content_url: Optional[str] = None
self._created_at: Optional[datetime.datetime] = None
self._datasource_type: Optional[str] = None
self._description: Optional[str] = None
self._encrypt_extracts: Optional[bool] = None
self._has_extracts: Optional[bool] = None
self._id: Optional[str] = None
self._initial_tags: set = set()
self._project_name: Optional[str] = None
self._revisions = None
self._size: Optional[int] = None
self._updated_at: Optional[datetime.datetime] = None
self._use_remote_query_agent: Optional[bool] = None
self._webpage_url: Optional[str] = None
self.description: Optional[str] = None
self.name: Optional[str] = name
self.owner_id: Optional[str] = None
self.project_id: Optional[str] = project_id
self.tags: set[str] = set()
self._permissions = None
self._data_quality_warnings = None
return None
@property
def ask_data_enablement(self) -> Optional[str]:
return self._ask_data_enablement
@ask_data_enablement.setter
@property_is_enum(AskDataEnablement)
def ask_data_enablement(self, value: Optional[str]):
self._ask_data_enablement = value
@property
def connections(self):
if self._connections is None:
error = "Datasource item must be populated with connections first."
raise UnpopulatedPropertyError(error)
return self._connections()
@property
def permissions(self) -> Optional[list[PermissionsRule]]:
if self._permissions is None:
error = "Project item must be populated with permissions first."
raise UnpopulatedPropertyError(error)
return self._permissions()
@property
def content_url(self) -> Optional[str]:
return self._content_url
@property
def created_at(self) -> Optional[datetime.datetime]:
return self._created_at
@property
def certified(self) -> Optional[bool]:
return self._certified
@certified.setter
@property_not_nullable
@property_is_boolean
def certified(self, value: Optional[bool]):
self._certified = value
@property
def certification_note(self) -> Optional[str]:
return self._certification_note
@certification_note.setter
def certification_note(self, value: Optional[str]):
self._certification_note = value
@property
def encrypt_extracts(self) -> Optional[bool]:
return self._encrypt_extracts
@encrypt_extracts.setter
@property_is_boolean
def encrypt_extracts(self, value: Optional[bool]):
self._encrypt_extracts = value
@property
def dqws(self):
if self._data_quality_warnings is None:
error = "Project item must be populated with dqws first."
raise UnpopulatedPropertyError(error)
return self._data_quality_warnings()
@property
def has_extracts(self) -> Optional[bool]:
return self._has_extracts
@property
def id(self) -> Optional[str]:
return self._id
@property
def project_id(self) -> Optional[str]:
return self._project_id
@project_id.setter
def project_id(self, value: Optional[str]):
self._project_id = value
@property
def project_name(self) -> Optional[str]:
return self._project_name
@property
def datasource_type(self) -> Optional[str]:
return self._datasource_type
@property
def description(self) -> Optional[str]:
return self._description
@description.setter
def description(self, value: Optional[str]):
self._description = value
@property
def updated_at(self) -> Optional[datetime.datetime]:
return self._updated_at
@property
def use_remote_query_agent(self) -> Optional[bool]:
return self._use_remote_query_agent
@use_remote_query_agent.setter
@property_is_boolean
def use_remote_query_agent(self, value: bool):
self._use_remote_query_agent = value
@property
def webpage_url(self) -> Optional[str]:
return self._webpage_url
@property
def revisions(self) -> list[RevisionItem]:
if self._revisions is None:
error = "Datasource item must be populated with revisions first."
raise UnpopulatedPropertyError(error)
return self._revisions()
@property
def size(self) -> Optional[int]:
return self._size
def _set_connections(self, connections) -> None:
self._connections = connections
def _set_permissions(self, permissions):
self._permissions = permissions
def _set_data_quality_warnings(self, dqws):
self._data_quality_warnings = dqws
def _set_revisions(self, revisions):
self._revisions = revisions
def _parse_common_elements(self, datasource_xml, ns):
if not isinstance(datasource_xml, ET.Element):
datasource_xml = fromstring(datasource_xml).find(".//t:datasource", namespaces=ns)
if datasource_xml is not None:
(
ask_data_enablement,
certified,
certification_note,
_,
_,
_,
_,
encrypt_extracts,
has_extracts,
_,
_,
owner_id,
project_id,
project_name,
_,
updated_at,
use_remote_query_agent,
webpage_url,
size,
) = self._parse_element(datasource_xml, ns)
self._set_values(
ask_data_enablement,
certified,
certification_note,
None,
None,
None,
None,
encrypt_extracts,
has_extracts,
None,
None,
owner_id,
project_id,
project_name,
None,
updated_at,
use_remote_query_agent,
webpage_url,
size,
)
return self
def _set_values(
self,
ask_data_enablement,
certified,
certification_note,
content_url,
created_at,
datasource_type,
description,
encrypt_extracts,
has_extracts,
id_,
name,
owner_id,
project_id,
project_name,
tags,
updated_at,
use_remote_query_agent,
webpage_url,
size,
):
if ask_data_enablement is not None:
self._ask_data_enablement = ask_data_enablement
if certification_note:
self.certification_note = certification_note
self.certified = certified # Always True/False, not conditional
if content_url:
self._content_url = content_url
if created_at:
self._created_at = created_at
if datasource_type:
self._datasource_type = datasource_type
if description:
self.description = description
if encrypt_extracts is not None:
self.encrypt_extracts = str(encrypt_extracts).lower() == "true"
if has_extracts is not None:
self._has_extracts = str(has_extracts).lower() == "true"
if id_ is not None:
self._id = id_
if name:
self.name = name
if owner_id:
self.owner_id = owner_id
if project_id:
self.project_id = project_id
if project_name:
self._project_name = project_name
if tags:
self.tags = tags
self._initial_tags = copy.copy(tags)
if updated_at:
self._updated_at = updated_at
if use_remote_query_agent is not None:
self._use_remote_query_agent = str(use_remote_query_agent).lower() == "true"
if webpage_url:
self._webpage_url = webpage_url
if size is not None:
self._size = int(size)
@classmethod
def from_response(cls, resp: str, ns: dict) -> list["DatasourceItem"]:
all_datasource_items = list()
parsed_response = fromstring(resp)
all_datasource_xml = parsed_response.findall(".//t:datasource", namespaces=ns)
for datasource_xml in all_datasource_xml:
datasource_item = cls.from_xml(datasource_xml, ns)
all_datasource_items.append(datasource_item)
return all_datasource_items
@classmethod
def from_xml(cls, datasource_xml, ns):
datasource_item = cls()
datasource_item._set_values(*cls._parse_element(datasource_xml, ns))
return datasource_item
@staticmethod
def _parse_element(datasource_xml: ET.Element, ns: dict) -> tuple:
id_ = datasource_xml.get("id", None)
name = datasource_xml.get("name", None)
datasource_type = datasource_xml.get("type", None)
description = datasource_xml.get("description", None)
content_url = datasource_xml.get("contentUrl", None)
created_at = parse_datetime(datasource_xml.get("createdAt", None))
updated_at = parse_datetime(datasource_xml.get("updatedAt", None))
certification_note = datasource_xml.get("certificationNote", None)
certified = str(datasource_xml.get("isCertified", None)).lower() == "true"
encrypt_extracts = datasource_xml.get("encryptExtracts", None)
has_extracts = datasource_xml.get("hasExtracts", None)
use_remote_query_agent = datasource_xml.get("useRemoteQueryAgent", None)
webpage_url = datasource_xml.get("webpageUrl", None)
size = datasource_xml.get("size", None)
tags = None
tags_elem = datasource_xml.find(".//t:tags", namespaces=ns)
if tags_elem is not None:
tags = TagItem.from_xml_element(tags_elem, ns)
project_id = None
project_name = None
project_elem = datasource_xml.find(".//t:project", namespaces=ns)
if project_elem is not None:
project_id = project_elem.get("id", None)
project_name = project_elem.get("name", None)
owner_id = None
owner_elem = datasource_xml.find(".//t:owner", namespaces=ns)
if owner_elem is not None:
owner_id = owner_elem.get("id", None)
ask_data_enablement = None
ask_data_elem = datasource_xml.find(".//t:askData", namespaces=ns)
if ask_data_elem is not None:
ask_data_enablement = ask_data_elem.get("enablement", None)
return (
ask_data_enablement,
certified,
certification_note,
content_url,
created_at,
datasource_type,
description,
encrypt_extracts,
has_extracts,
id_,
name,
owner_id,
project_id,
project_name,
tags,
updated_at,
use_remote_query_agent,
webpage_url,
size,
)