forked from tableau/document-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworksheet.py
More file actions
79 lines (65 loc) · 2.74 KB
/
worksheet.py
File metadata and controls
79 lines (65 loc) · 2.74 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
from tableaudocumentapi.datasource_dependency import DatasourceDependency
from tableaudocumentapi.filter import Filter, _parse_filters
from tableaudocumentapi.utils import _clean_aggregated_column_names
import re
class Worksheet(object):
"""A class representing a Worksheet in a Tableau workbook file """
def __init__(self, worksheet_xml):
"""Initialize the Worksheet from XML element representing it"""
self._xml = worksheet_xml
self._name = worksheet_xml.attrib['name']
self._datasource_dependencies = self._parse_datasource_dependencies()
self._filters = _parse_filters(self._xml, "table/view/filter")
self._rows = self._parse_rows_cols('rows')
self._cols = self._parse_rows_cols('cols')
simple_id = worksheet_xml.find('simple-id')
self._id = simple_id.get('uuid', '').replace("{", "").replace("}", "") if simple_id is not None else ''
@property
def xml(self):
"""Return the xml of the worksheet"""
return self._xml
@property
def name(self):
"""Return the name of the worksheet"""
return self._name
@property
def datasource_dependencies(self):
"""Return the worksheet datsource dependencies"""
return self._datasource_dependencies
@property
def filters(self):
"""Return the worksheet filters"""
return self._filters
@property
def rows(self):
"""Return the worksheet rows"""
return self._rows
@property
def cols(self):
"""Return the worksheet cols"""
return self._cols
@property
def id(self):
"""Return the worksheet id"""
return self._id
def _parse_datasource_dependencies(self):
"""Function that will parse the datsource dependencies under the worksheet"""
datasource_dependencies = []
datasource_dependency_elements = self._xml.findall('table/view/datasource-dependencies')
for dependency in datasource_dependency_elements:
if dependency.get("datasource"):
datasource_dependencies.append(DatasourceDependency(dependency))
return datasource_dependencies
def _parse_rows_cols(self, row_or_col):
"""Function that will parse the rows and columns in the worksheet"""
if row_or_col not in ["rows", "cols"]:
raise ValueError("row_or_col must be 'rows' or 'cols'")
path = f"table/{row_or_col}"
# import pdb; pdb.set_trace()
text = self._xml.findtext(path, default='').strip()
if not text:
return []
list_of_rows_cols = []
for _ in text.split(" / "):
list_of_rows_cols.append((_))
return list_of_rows_cols