10000 added check on extract vs .tdsx extension · kgtdbx/document-api-python@b9dc253 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9dc253

Browse files
author
Richard Kooijman
committed
added check on extract vs .tdsx extension
added support for overwriting captions on columns bumped version to 0.7
1 parent a098f3e commit b9dc253

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='tableaudocumentapi',
8-
version='0.6',
8+
version='0.7',
99
author='Tableau',
1010
author_email='github@tableau.com',
1111
url='https://github.com/tableau/document-api-python',

tableaudocumentapi/datasource.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,37 @@ def _get_all_fields(self):
240240
# Some columns are represented by `column` tags and others as `metadata-record` tags
241241
# Find them all and chain them into one dictionary
242242
column_field_objects = self.columns
243-
existing_column_fields = [x.id for x in column_field_objects]
244-
metadata_only_field_objects = (x for x in self._get_metadata_objects() if x.id not in existing_column_fields)
245-
field_objects = itertools.chain(column_field_objects, metadata_only_field_objects)
243+
self._existing_column_fields = [x.id for x in column_field_objects]
244+
self._metadata_only_field_objects = (x for x in self._get_metadata_objects() if x.id not in self._existing_column_fields)
245+
field_objects = itertools.chain(column_field_objects, self._metadata_only_field_objects)
246246

247247
return FieldDictionary({k: v for k, v in field_objects})
248248

249249
def _get_metadata_objects(self):
250-
return (_column_object_from_metadata_xml(x)
251-
for x in self._datasourceTree.findall(".//metadata-record[@class='column']"))
250+
return (_column_object_from_metadata_xml(xml)
251+
for xml in self._datasourceTree.findall(".//metadata-record[@class='column']"))
252252

253253
def _get_column_objects(self):
254254
return [_column_object_from_column_xml(self._datasourceTree, xml)
255255
for xml in self._datasourceTree.findall('.//column')]
256+
257+
def has_extract(self):
258+
return len(self._extract) > 0 and self._extract[0].attrib['enabled'] == 'true'
259+
260+
def process_columns(self):
261+
sub_elems = self._datasourceTree.findall('*')
262+
last_aliases_index = -1
263+
first_column_index = -1
264+
for i in range(len(sub_elems)):
265+
if sub_elems[i].tag == 'column' and first_column_index < 0:
266+
first_column_index = i
267+
if sub_elems[i].tag == 'aliases':
268+
last_aliases_index = i
269+
column_index = max(first_column_index, last_aliases_index + 1)
270+
if column_index <= 0:
271+
raise LookupError("no column nor aliases element found in the data source")
272+
for name, field in self._fields.items():
273+
if field.id not in self._existing_column_fields:
274+
x = Field.create_field_xml(field.id, field.caption, field.datatype, field.role, field.type)
275+
Field.set_description(field.description, x)
276+
self._datasourceXML.insert(column_index, x)

tableaudocumentapi/field.py

Expand all lines: tableaudocumentapi/field.py
Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _initialize_from_metadata_xml(self, xmldata):
6969
self.apply_metadata(xmldata)
7070

7171
@classmethod
72-
def create_field_xml(cls, caption, datatype, role, field_type, name):
72+
def create_field_xml(cls, name, caption, datatype, role='dimension', field_type='ordinal'):
7373
column = ET.Element('column')
7474
column.set('caption', caption)
7575
column.set('datatype', datatype)
@@ -139,8 +139,9 @@ def caption(self):
139139
return self._caption
140140

141141
@caption.setter
142-
def caption(self, new_caption):
143-
self._caption = new_caption
142+
def caption(self, caption):
143+
self._caption = caption
144+
self._xml.set('caption', caption)
144145

145146
@property
146147
def alias(self):
@@ -157,6 +158,18 @@ def role(self):
157158
""" Dimension or Measure """
158159
return self._role
159160

161+
@role.setter
162+
def role(self, role):
163+
self._role = role
164+
165+
@property
166+
def type(self):
167+
return self._type
168+
169+
@type.setter
170+
def type(self, type):
171+
self._type = type
172+
160173
@property
161174
def is_quantitative(self):
162175
""" A dependent value, usually a measure of something
@@ -199,15 +212,20 @@ def description(self, new_description):
199212
Args:
200213
new_description: The new description of the field. String.
201214
"""
202-
desc = self._xml.find('.//desc')
203-
if desc is not None:
204-
self._xml.remove(desc)
205-
desc = ET.SubElement(self._xml, 'desc')
206-
formatted = ET.SubElement(desc, 'formatted-text')
207-
run = ET.SubElement(formatted, 'run')
208-
run.text = new_description
215+
self.set_description(new_description, self._xml)
209216
self._description = self._read_description(self._xml)
210217

218+
@classmethod
219+
def set_description(self, new_description, xml):
220+
desc = xml.find('.//desc')
221+
if desc is not None:
222+
xml.remove(desc)
223+
if new_description is not None:
224+
desc = ET.SubElement(xml, 'desc')
225+
formatted = ET.SubElement(desc, 'formatted-text')
226+
run = ET.SubElement(formatted, 'run')
227+
run.text = new_description
228+
211229
@property
212230
def worksheets(self):
213231
return list(self._worksheets)

0 commit comments

Comments
 (0)
0