diff --git a/Examples/GetFields/show_fields.py b/Examples/GetFields/show_fields.py index ee45f87..40d87fe 100644 --- a/Examples/GetFields/show_fields.py +++ b/Examples/GetFields/show_fields.py @@ -23,6 +23,8 @@ if field.default_aggregation: print(' the default aggregation is {}'.format(field.default_aggregation)) blank_line = True + if field.description: + print(' the description is {}'.format(field.description)) if blank_line: print('') diff --git a/tableaudocumentapi/field.py b/tableaudocumentapi/field.py index 711e772..63cc72c 100644 --- a/tableaudocumentapi/field.py +++ b/tableaudocumentapi/field.py @@ -1,4 +1,6 @@ import functools +import xml.etree.ElementTree as ET + _ATTRIBUTES = [ 'id', # Name of the field as specified in the file, usually surrounded by [ ] @@ -8,6 +10,7 @@ 'type', # three possible values: quantitative, ordinal, or nominal 'alias', # Name of the field as displayed in Tableau if the default name isn't wanted 'calculation', # If this field is a calculated field, this will be the formula + 'description', # If this field has a description, this will be the description (including formatting tags) ] _METADATA_ATTRIBUTES = [ @@ -164,6 +167,11 @@ def default_aggregation(self): """ The default type of aggregation on the field (e.g Sum, Avg)""" return self._aggregation + @property + def description(self): + """ The contents of the tag on a field """ + return self._description + @property def worksheets(self): return list(self._worksheets) @@ -184,3 +192,11 @@ def _read_calculation(xmldata): return None return calc.attrib.get('formula', None) + + @staticmethod + def _read_description(xmldata): + description = xmldata.find('.//desc') + if description is None: + return None + + return u'{}'.format(ET.tostring(description, encoding='utf-8')) # This is necessary for py3 support diff --git a/test/assets/datasource_test.tds b/test/assets/datasource_test.tds index a1e78a8..bfab77b 100644 --- a/test/assets/datasource_test.tds +++ b/test/assets/datasource_test.tds @@ -75,7 +75,14 @@ - + + + + A thing + Something will go here too, in a muted gray + + + diff --git a/test/test_datasource.py b/test/test_datasource.py index 6ea03ea..66b3f79 100644 --- a/test/test_datasource.py +++ b/test/test_datasource.py @@ -58,6 +58,11 @@ def test_datasource_field_datatype(self): def test_datasource_field_role(self): self.assertEqual(self.ds.fields['[x]'].role, 'measure') + def test_datasource_field_description(self): + actual = self.ds.fields['[a]'].description + self.assertIsNotNone(actual) + self.assertTrue(u'muted gray' in actual) + class DataSourceFieldsTWB(unittest.TestCase):