8000 Merge pull request #1 from danteo13/add-filters · danteo13/document-api-python@21ba158 · GitHub
[go: up one dir, main page]

Skip to content

Commit 21ba158

Browse files
authored
Merge pull request #1 from danteo13/add-filters
add worksheet fields and filters
2 parents 593b63e + 9f44b62 commit 21ba158

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ build/
1313
develop-eggs/
1414
dist/
1515
downloads/
16+
examples/
1617
eggs/
1718
.eggs/
1819
lib/

tableaudocumentapi/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from .field import Field
2+
from .filter import Filter
3+
from .worksheet_field import WorksheetField
24
from .connection import Connection
35
from .datasource import Datasource, ConnectionParser
6+
from .worksheet import Worksheet
47
from .workbook import Workbook
58

69
__version__ = '0.0.1'

tableaudocumentapi/filter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Filter:
2+
def __init__(self, xml):
3+
self._xml = xml
4+
self._class = xml.get('class')
5+
self._column = xml.get('column')

tableaudocumentapi/workbook.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import weakref
22

33

4-
from tableaudocumentapi import Datasource, xfile
4+
from tableaudocumentapi import Datasource, Worksheet, xfile
55
from tableaudocumentapi.xfile import xml_open
66

77

@@ -103,7 +103,8 @@ def _prepare_worksheets(xml_root, ds_index):
103103

104104
for worksheet_element in worksheets_element:
105105
worksheet_name = worksheet_element.attrib['name']
106-
worksheets.append(worksheet_name) # TODO: A real worksheet object, for now, only name
106+
worksheet = Worksheet(worksheet_element)
107+
worksheets.append(worksheet)
107108

108109
dependencies = worksheet_element.findall('.//datasource-dependencies')
109110

tableaudocumentapi/worksheet.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import xml.etree.ElementTree as ET
2+
from tableaudocumentapi import Filter, WorksheetField
3+
4+
5+
class Worksheet:
6+
"""An object representing a worksheet in Tableau"""
7+
8+
def __init__(self, xml):
9+
self._worksheet_xml = xml
10+
self._worksheet_tree = ET.ElementTree(self._worksheet_xml)
11+
self._worksheet_root = self._worksheet_tree.getroot()
12+
self._filters = self._prepare_filters(self._worksheet_root)
13+
self._worksheet_fields = self._prepare_fields(self._worksheet_root)
14+
15+
self.name = self._worksheet_xml.get('name')
16+
17+
@property
18+
def filters(self):
19+
return self._filters
20+
21+
@property
22+
def fields(self):
23+
return self._worksheet_fields
24+
25+
@staticmethod
26+
def _prepare_filters(xml_root):
27+
filters = []
28+
element_list = xml_root.findall('.//filter')
29+
if element_list is None:
30+
return []
31+
32+
for elem in element_list:
33+
filter = Filter(elem)
34+
filters.append(filter)
35+
36+
return filters
37+
38+
@staticmethod
39+
def _prepare_fields(xml_root):
40+
worksheet_fields = []
41+
datasources = xml_root.findall('.//datasource-dependencies')
42+
if datasources is None:
43+
return []
44+
45+
for ds in datasources:
46+
datasource_name = ds.get('datasource')
47+
fields = ds.findall('column-instance')
48+
for field in fields:
49+
worksheet_fields.append(WorksheetField(field, datasource_name))
50+
51+
return worksheet_fields

tableaudocumentapi/worksheet_field.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class WorksheetField:
2+
def __init__(self, xml, datasource):
3+
self._xml = xml
4+
self.datasource = datasource
5+
self.name = self._xml.get('name')
6+
self.column = self._xml.get('column')
7+
self.derivation = self._xml.get('derivation')
8+
self.pivot = self._xml.get('pivot')
9+
self.type = self._xml.get('type')

0 commit comments

Comments
 (0)
0