forked from tableau/document-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
81 lines (68 loc) · 2.81 KB
/
filter.py
File metadata and controls
81 lines (68 loc) · 2.81 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
import re
from tableaudocumentapi.utils import _clean_aggregated_column_names
class Filter(object):
"""A class representing a Filter in a worksheet or datasource"""
def __init__(self, filter_xml):
"""Initialize Filter from XML Element
Args:
filter_xml: XML element representing the filter
"""
self._xml = filter_xml
self._filter_class = filter_xml.get('class')
self._column = _clean_aggregated_column_names(filter_xml.get('column'))[1]
self._datasource = _clean_aggregated_column_names(filter_xml.get('column'))[0]
self._groupfilters = self._parse_groupfilters()
@property
def xml(self):
"""Return xml of the filter """
return self._xml
@property
def groupfilters(self):
"""Return groupfilters of the filter"""
return self._groupfilters
@property
def filter_class(self):
"""Return class attribute of the filter"""
return self._filter_class
@property
def column(self):
"""Return columns of the filter """
return self._column
@property
def datasource(self):
"""Return datasource of the columns of the filter """
return self._datasource
def _parse_groupfilters(self):
"""Function that will parse the groupfilters under the filter"""
groupfilters = []
groupfilter_elements = self._xml.findall('groupfilter')
for groupfilter in groupfilter_elements:
parsed_groupfilter = self._parse_single_groupfilter(groupfilter)
if parsed_groupfilter:
groupfilters.append(parsed_groupfilter)
return groupfilters
def _parse_single_groupfilter(self, groupfilter_xml):
"""Parse a single groupfilter element recursively"""
if groupfilter_xml is None:
return None
groupfilter_data = {
'function': groupfilter_xml.get('function'),
'level': groupfilter_xml.get('level'),
'member': groupfilter_xml.get('member'),
'attributes': dict(groupfilter_xml.attrib),
'children': []
}
# Parse nested groupfilters recursively
child_groupfilters = groupfilter_xml.findall('groupfilter')
for child in child_groupfilters:
child_data = self._parse_single_groupfilter(child)
if child_data:
groupfilter_data['children'].append(child_data)
return groupfilter_data
def _parse_filters(root_node, path):
"""Function that will parse the filters under the worksheet or datasource"""
filters = []
filter_elements = root_node.findall(path)
for filter_element in filter_elements:
filters.append(Filter(filter_element))
return filters