-
Notifications
You must be signed in to change notification settings - Fork 421
/
datasource_item.py
387 lines (337 loc) · 12.1 KB
/
datasource_item.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
import copy
import datetime
import xml.etree.ElementTree as ET
from typing import Dict, List, Optional, Set, Tuple
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(object):
class AskDataEnablement:
Enabled = "Enabled"
Disabled = "Disabled"
SiteDefault = "SiteDefault"
def __repr__(self):
return "<Datasource {0} '{1}' ({2} parent={3} >".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 = None
self._certified = None
self._certification_note = None
self._connections = None
self._content_url: Optional[str] = None
self._created_at = None
self._datasource_type = None
self._description = None
self._encrypt_extracts = None
self._has_extracts = 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 = None
self._use_remote_query_agent = None
self._webpage_url = None
self.description = None
self.name = name
self.owner_id: Optional[str] = None
self.project_id = project_id
self.tags: Set[str] = set()
self._permissions = None
self._data_quality_warnings = None
return None
@property
def ask_data_enablement(self) -> Optional[AskDataEnablement]:
return self._ask_data_enablement
@ask_data_enablement.setter
@property_is_enum(AskDataEnablement)
def ask_data_enablement(self, value: Optional[AskDataEnablement]):
self._ask_data_enablement = value
@property
def connections(self) -> Optional[List[ConnectionItem]]:
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):
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: 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):
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,
)